Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VisiumHD: detect full-res image when path is relative #230

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning][].
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
[semantic versioning]: https://semver.org/spec/v2.0.0.html

## incoming release

- (Visium/Visium HD) lowres and hires images now mapped also to the 'global' coordinate system #230

## [0.1.6] - 2024-11-26

- (MERSCOPE) added `feature_key` attribute for points (i.e., the `'gene'` column) #210
Expand Down
4 changes: 2 additions & 2 deletions src/spatialdata_io/readers/visium.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ def visium(
image_hires = imread(path / VisiumKeys.IMAGE_HIRES_FILE, **imread_kwargs).squeeze().transpose(2, 0, 1)
image_hires = DataArray(image_hires, dims=("c", "y", "x"))
images[dataset_id + "_hires_image"] = Image2DModel.parse(
image_hires, transformations={"downscaled_hires": Identity()}, rgb=None
image_hires, transformations={"downscaled_hires": Identity(), "global": transform_hires.inverse()}, rgb=None
)
if (path / VisiumKeys.IMAGE_LOWRES_FILE).exists():
image_lowres = imread(path / VisiumKeys.IMAGE_LOWRES_FILE, **imread_kwargs).squeeze().transpose(2, 0, 1)
image_lowres = DataArray(image_lowres, dims=("c", "y", "x"))
images[dataset_id + "_lowres_image"] = Image2DModel.parse(
image_lowres,
transformations={"downscaled_lowres": Identity()},
transformations={"downscaled_lowres": Identity(), "global": transform_lowres.inverse()},
rgb=None,
)

Expand Down
22 changes: 9 additions & 13 deletions src/spatialdata_io/readers/visium_hd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import json
import os
import re
import warnings
from collections.abc import Mapping
Expand Down Expand Up @@ -116,12 +115,12 @@ def load_image(path: Path, suffix: str, scale_factors: list[int] | None = None)
stacklevel=2,
)

def _get_bins(path: Path) -> list[str]:
def _get_bins(path_bins: Path) -> list[str]:
return sorted(
[
bin_size
for bin_size in os.listdir(path)
if os.path.isdir(os.path.join(path, bin_size)) and bin_size.startswith(VisiumHDKeys.BIN_PREFIX)
bin_size.name
for bin_size in path_bins.iterdir()
if bin_size.is_dir() and bin_size.name.startswith(VisiumHDKeys.BIN_PREFIX)
]
)

Expand Down Expand Up @@ -265,10 +264,7 @@ def _get_bins(path: Path) -> list[str]:
else:
path_fullres = path / VisiumHDKeys.MICROSCOPE_IMAGE
if path_fullres.exists():
fullres_image_filenames = [
f for f in os.listdir(path_fullres) if os.path.isfile(os.path.join(path_fullres, f))
]
fullres_image_paths = [path_fullres / image_filename for image_filename in fullres_image_filenames]
fullres_image_paths = [file for file in path_fullres.iterdir() if file.is_file()]
elif list((path_fullres := (path / f"{filename_prefix}tissue_image")).parent.glob(f"{path_fullres.name}.*")):
fullres_image_paths = list(path_fullres.parent.glob(f"{path_fullres.name}.*"))
else:
Expand All @@ -291,7 +287,7 @@ def _get_bins(path: Path) -> list[str]:

if fullres_image_file is not None:
load_image(
path=path / fullres_image_file,
path=fullres_image_file,
suffix="_full_image",
scale_factors=[2, 2, 2, 2],
)
Expand All @@ -310,7 +306,7 @@ def _get_bins(path: Path) -> list[str]:
)
set_transformation(
images[dataset_id + "_hires_image"],
{"downscaled_hires": Identity()},
{"downscaled_hires": Identity(), "global": transform_hires.inverse()},
set_all=True,
)

Expand All @@ -328,7 +324,7 @@ def _get_bins(path: Path) -> list[str]:
)
set_transformation(
images[dataset_id + "_lowres_image"],
{"downscaled_lowres": Identity()},
{"downscaled_lowres": Identity(), "global": transform_lowres.inverse()},
set_all=True,
)

Expand Down Expand Up @@ -356,7 +352,7 @@ def _get_bins(path: Path) -> list[str]:

def _infer_dataset_id(path: Path) -> str:
suffix = f"_{VisiumHDKeys.FEATURE_SLICE_FILE.value}"
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) and f.endswith(suffix)]
files = [file.name for file in path.iterdir() if file.is_file() and file.name.endswith(suffix)]
if len(files) == 0 or len(files) > 1:
raise ValueError(
f"Cannot infer `dataset_id` from the feature slice file in {path}, please pass `dataset_id` as an argument."
Expand Down
Loading