Skip to content

Commit

Permalink
Merge pull request #78 from aeisenbarth/rgb-argument
Browse files Browse the repository at this point in the history
[WIP] Set rgb argument to auto-detect whether image channels are RGB
  • Loading branch information
LucaMarconato authored Sep 8, 2024
2 parents c15dca9 + a76de1d commit 1d031c2
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning][].

- (Xenium) added `dims` parameter for more control in `xenium_aligned_image()`

### Fixed

- Passing `rgb=None` to image model parser, leading to 3-4 channel images being interpreted as RGB(A)

## [0.1.4] - 2024-08-07

### Changed
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies = [
"anndata",
"numpy",
"scanpy",
"spatialdata>=0.2.1",
"spatialdata>=0.2.2",
"scikit-image",
"h5py",
"joblib",
Expand Down
4 changes: 2 additions & 2 deletions src/spatialdata_io/readers/_utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

NDArrayA = NDArray[Any]
except (ImportError, TypeError):
NDArray = np.ndarray # type: ignore[misc]
NDArrayA = np.ndarray # type: ignore[misc]
NDArray = np.ndarray # type: ignore[misc,unused-ignore]
NDArrayA = np.ndarray # type: ignore[misc,unused-ignore]


def _read_counts(
Expand Down
1 change: 1 addition & 0 deletions src/spatialdata_io/readers/codex.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def codex(
"images": Image2DModel.parse(
image,
scale_factors=[2, 2],
rgb=None,
)
}
sdata = SpatialData(images=images, shapes={str(region): shapes}, table=table)
Expand Down
1 change: 1 addition & 0 deletions src/spatialdata_io/readers/cosmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def cosmx(
"global_only_image": aff,
},
dims=("y", "x", "c"),
rgb=None,
**image_models_kwargs,
)
images[f"{fov}_image"] = parsed_im
Expand Down
10 changes: 6 additions & 4 deletions src/spatialdata_io/readers/mcmicro.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def mcmicro(
assert len(samples) == 1
data = imread(samples[0], **imread_kwargs)
# , scale_factors=[2, 2]
data = Image2DModel.parse(data, transformations=_get_transformation(), **image_models_kwargs)
data = Image2DModel.parse(data, transformations=_get_transformation(), rgb=None, **image_models_kwargs)
images["tma_map"] = data

image_dir = path / McmicroKeys.IMAGES_DIR_TMA
Expand All @@ -116,7 +116,7 @@ def mcmicro(
image_id = core_id

data = imread(sample, **imread_kwargs)
data = Image2DModel.parse(data, c_coords=marker_names, **image_models_kwargs)
data = Image2DModel.parse(data, c_coords=marker_names, rgb=None, **image_models_kwargs)
transformations = _get_transformation(
tma=int(core_id) if tma else None, tma_centroids=tma_centroids, raster_data=data
)
Expand All @@ -131,7 +131,9 @@ def mcmicro(
raw_name = raw_image.with_name(raw_image.stem).with_suffix("").stem

data = imread(raw_image, **imread_kwargs)
images[raw_name] = Image2DModel.parse(data, transformations={raw_name: Identity()}, **image_models_kwargs)
images[raw_name] = Image2DModel.parse(
data, transformations={raw_name: Identity()}, rgb=None, **image_models_kwargs
)

illumination_dir = path / McmicroKeys.ILLUMINATION_DIR
if illumination_dir.exists():
Expand All @@ -143,7 +145,7 @@ def mcmicro(

data = imread(illumination_image, **imread_kwargs)
images[illumination_name] = Image2DModel.parse(
data, transformations={raw_name: Identity()}, **image_models_kwargs
data, transformations={raw_name: Identity()}, rgb=None, **image_models_kwargs
)

samples_labels = list((path / McmicroKeys.LABELS_DIR).glob("*/*" + McmicroKeys.IMAGE_SUFFIX))
Expand Down
3 changes: 2 additions & 1 deletion src/spatialdata_io/readers/merscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def _rioxarray_load_merscope(
dim="c",
)

return Image2DModel.parse(im, c_coords=stainings, **image_models_kwargs)
return Image2DModel.parse(im, c_coords=stainings, rgb=None, **image_models_kwargs)


def _dask_image_load_merscope(
Expand All @@ -292,6 +292,7 @@ def _dask_image_load_merscope(
im,
dims=("c", "y", "x"),
c_coords=stainings,
rgb=None,
**image_models_kwargs,
)

Expand Down
2 changes: 1 addition & 1 deletion src/spatialdata_io/readers/steinbock.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _get_images(
image_models_kwargs: Mapping[str, Any] = MappingProxyType({}),
) -> Union[SpatialImage, MultiscaleSpatialImage]:
image = imread(path / SteinbockKeys.IMAGES_DIR / f"{sample}{SteinbockKeys.IMAGE_SUFFIX}", **imread_kwargs)
return Image2DModel.parse(data=image, transformations={sample: Identity()}, **image_models_kwargs)
return Image2DModel.parse(data=image, transformations={sample: Identity()}, rgb=None, **image_models_kwargs)


def _get_labels(
Expand Down
7 changes: 5 additions & 2 deletions src/spatialdata_io/readers/visium.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def visium(
full_image,
scale_factors=[2, 2, 2, 2],
transformations={"global": transform_original},
rgb=None,
**image_models_kwargs,
)
else:
Expand All @@ -230,13 +231,15 @@ 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()}
image_hires, transformations={"downscaled_hires": Identity()}, 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()}
image_lowres,
transformations={"downscaled_lowres": Identity()},
rgb=None,
)

return SpatialData(images=images, shapes=shapes, table=table)
Expand Down
2 changes: 1 addition & 1 deletion src/spatialdata_io/readers/xenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def _get_images(
# let's add a dummy channel as a temporary workaround.
image = da.concatenate([image, da.zeros_like(image[0:1])], axis=0)
return Image2DModel.parse(
image, transformations={"global": Identity()}, dims=("c", "y", "x"), **image_models_kwargs
image, transformations={"global": Identity()}, dims=("c", "y", "x"), rgb=None, **image_models_kwargs
)


Expand Down

0 comments on commit 1d031c2

Please sign in to comment.