Skip to content

Commit

Permalink
[FIX] addresses #169, also adds short-term fix for #150
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamaedler committed Feb 10, 2025
1 parent 99db0d3 commit 5e765ac
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions src/scportrait/pipeline/_utils/sdata_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,49 +187,47 @@ def _write_image_sdata(
# check if the image is already a multi-scale image
if isinstance(image, xarray.DataTree):
# if so only validate the model since this means we are getting the image from a spatialdata object already
# image = Image2DModel.validate(image)
# this appraoch is currently not functional but an issue was opened at https://github.com/scverse/spatialdata/issues/865
# fix until #https://github.com/scverse/spatialdata/issues/528 is resolved
Image2DModel().validate(image)
if scale_factors is not None:
Warning("Scale factors are ignored when passing a multi-scale image.")
image = image.scale0.image

if scale_factors is None:
scale_factors = [2, 4, 8]
if scale_factors is None:
scale_factors = [2, 4, 8]

if isinstance(image, xarray.DataArray):
# if so first validate the model since this means we are getting the image from a spatialdata object already
# then apply the scales transform
# image = Image2DModel.validate(image)
# this appraoch is currently not functional but an issue was opened at https://github.com/scverse/spatialdata/issues/865

if channel_names is not None:
Warning(
"Channel names are ignored when passing a single scale image in the DataArray format. Channel names are read directly from the DataArray."
else:
if scale_factors is None:
scale_factors = [2, 4, 8]
if scale_factors is None:
scale_factors = [2, 4, 8]

if isinstance(image, xarray.DataArray):
# if so first validate the model since this means we are getting the image from a spatialdata object already
# fix until #https://github.com/scverse/spatialdata/issues/528 is resolved
Image2DModel().validate(image)

if channel_names is not None:
Warning(
"Channel names are ignored when passing a single scale image in the DataArray format. Channel names are read directly from the DataArray."
)

image = Image2DModel.parse(
image,
scale_factors=scale_factors,
rgb=False,
)

image = Image2DModel.parse(
image,
scale_factors=scale_factors,
rgb=False,
)

else:
if channel_names is None:
channel_names = [f"channel_{i}" for i in range(image.shape[0])]

# transform to spatialdata image model
transform_original = Identity()
image = Image2DModel.parse(
image,
dims=["c", "y", "x"],
chunks=chunks,
c_coords=channel_names,
scale_factors=scale_factors,
transformations={"global": transform_original},
rgb=False,
)
else:
if channel_names is None:
channel_names = [f"channel_{i}" for i in range(image.shape[0])]

# transform to spatialdata image model
transform_original = Identity()
image = Image2DModel.parse(
image,
dims=["c", "y", "x"],
chunks=chunks,
c_coords=channel_names,
scale_factors=scale_factors,
transformations={"global": transform_original},
rgb=False,
)

if overwrite:
self._force_delete_object(_sdata, image_name, "images")
Expand Down

0 comments on commit 5e765ac

Please sign in to comment.