Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
melonora committed Nov 13, 2024
1 parent 92b25c6 commit 0cdab15
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/spatialdata/_core/spatialdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,8 @@ def write_channel_names(self, element_name: str) -> None:
Parameters
----------
element_name
The name of the element to write. If None, write the channel names of all image elements.
The name of the element to write the channel names of. If None, write the channel names of all image
elements.
"""
from spatialdata._core._elements import Elements

Expand All @@ -1514,15 +1515,17 @@ def write_channel_names(self, element_name: str) -> None:

element_type, element = validation_result

# Mypy does not understand that path is not None so we have a conditional
if self.path is not None:
# Mypy does not understand that path is not None so we have the check in the conditional
if element_type == "images" and self.path is not None:
_, _, element_group = self._get_groups_for_element(
zarr_path=Path(self.path), element_type=element_type, element_name=element_name
)

from spatialdata._io._utils import overwrite_channel_names

overwrite_channel_names(element_group, element)
else:
raise ValueError(f"Can't set channel names for element of type '{element_type}'.")

def write_transformations(self, element_name: str | None = None) -> None:
"""
Expand Down
19 changes: 15 additions & 4 deletions tests/io/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,22 @@ def test_save_transformations_incremental(element_name, full_sdata, caplog):


# test io for channel names
@pytest.mark.skip(reason="Not implemented yet")
def test_save_channel_names_incremental(images: SpatialData) -> None:
# note: the non-incremental IO for channel names is already covered in TestReadWrite.test_images(), so here we
# only test the incremental IO
pass
with tempfile.TemporaryDirectory() as tmp_dir:
f0 = os.path.join(tmp_dir, "sdata.zarr")
images.write(f0)

new_channels = ["first", "second", "third"]
images.set_sdata_image_channel_names("image2d", new_channels)
images.set_sdata_image_channel_names("image2d_multiscale", new_channels)
images.set_sdata_image_channel_names("image3d_numpy", new_channels)
images.set_sdata_image_channel_names("image3d_multiscale_numpy", new_channels)

images = SpatialData.read(f0)
assert images["image2d"].coords["c"].data.tolist() == new_channels
assert images["image2d_multiscale"]["scale0"]["image"].coords["c"].data.tolist() == new_channels
assert images["image3d_numpy"].coords["c"].data.tolist() == new_channels
assert images["image3d_multiscale_numpy"]["scale0"]["image"].coords["c"].data.tolist() == new_channels


# test io for consolidated metadata
Expand Down

0 comments on commit 0cdab15

Please sign in to comment.