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

add support for the get_transform analog to set_transform #264

Merged
merged 13 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 37 additions & 0 deletions iohub/ngff/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,43 @@ def get_axis_index(self, axis_name: str) -> int:
"""
return self.axis_names.index(axis_name.lower())

def get_transforms(
ziw-liu marked this conversation as resolved.
Show resolved Hide resolved
self,
image: str | Literal["*"],
) -> tuple[TransformationMeta, TransformationMeta]:
"""Get the total coordinate scale and translation metadata
for one image array or the whole FOV.

Parameters
----------
image : str | Literal["*"]
Name of one image array (e.g. "0") to transform,
or "*" for the whole FOV
"""
ziw-liu marked this conversation as resolved.
Show resolved Hide resolved
transforms: list[TransformationMeta] = self.metadata.multiscales[0].coordinate_transformations
if image != "*" and image in self:
for i, dataset_meta in enumerate(self.metadata.multiscales[0].datasets):
if dataset_meta.path == image:
transforms.extend(
self.metadata.multiscales[0]
.datasets[i]
.coordinate_transformations
)
elif image != "*":
raise ValueError(f"Key {image} not recognized.")

full_scale = np.array([1]*len(self.axes), dtype=float)
full_translation = np.array([0]*len(self.axes), dtype=float)
ziw-liu marked this conversation as resolved.
Show resolved Hide resolved
for transform in transforms:
if transform.type == "scale":
full_scale *= np.array(transform.scale)
elif transform.type == "translation":
full_translation += full_scale * np.array(transform.translation)
ziw-liu marked this conversation as resolved.
Show resolved Hide resolved

return TransformationMeta(
type="scale", scale=tuple(full_scale)
), TransformationMeta(type="translation", translation=tuple(full_translation))

def set_transform(
self,
image: str | Literal["*"],
Expand Down
51 changes: 51 additions & 0 deletions tests/ngff/test_ngff.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,57 @@ def test_set_transform_image(ch_shape_dtype, arr_name):
]


@pytest.mark.parametrize(
"transforms",
[
(
[TransformationMeta(type="identity")],
TransformationMeta(type="scale", scale=(1.0, 1.0, 1.0, 1.0, 1.0)),
TransformationMeta(type="translation", translation=(0.0, 0.0, 0.0, 0.0, 0.0)),
),
(
[TransformationMeta(type="scale", scale=(1.0, 2.0, 3.0, 4.0, 5.0))],
TransformationMeta(type="scale", scale=(1.0, 2.0, 3.0, 4.0, 5.0)),
TransformationMeta(type="translation", translation=(0.0, 0.0, 0.0, 0.0, 0.0)),
),
(
[TransformationMeta(type="translation", translation=(1.0, 2.0, 3.0, 4.0, 5.0))],
TransformationMeta(type="scale", scale=(1.0, 1.0, 1.0, 1.0, 1.0)),
TransformationMeta(type="translation", translation=(1.0, 2.0, 3.0, 4.0, 5.0)),
),
(
[
TransformationMeta(type="scale", scale=(2.0, 2.0, 2.0, 2.0, 2.0)),
TransformationMeta(type="translation", translation=(1.0, 1.0, 1.0, 1.0, 1.0)),
],
TransformationMeta(type="scale", scale=(2.0, 2.0, 2.0, 2.0, 2.0)),
TransformationMeta(type="translation", translation=(2.0, 2.0, 2.0, 2.0, 2.0)),
),
],
)
@given(
ch_shape_dtype=_channels_and_random_5d_shape_and_dtype(),
arr_name=short_alpha_numeric,
)
def test_get_transform_image(transforms, ch_shape_dtype, arr_name):
"""Test `iohub.ngff.Position.set_transform()`"""
ziw-liu marked this conversation as resolved.
Show resolved Hide resolved
ziw-liu marked this conversation as resolved.
Show resolved Hide resolved
transform, expected_scale, expected_translate = transforms
channel_names, shape, dtype = ch_shape_dtype
with TemporaryDirectory() as temp_dir:
store_path = os.path.join(temp_dir, "ome.zarr")
with open_ome_zarr(
store_path, layout="fov", mode="w-", channel_names=channel_names
) as dataset:
dataset.create_zeros(name=arr_name, shape=shape, dtype=dtype)
assert dataset.metadata.multiscales[0].datasets[
0
].coordinate_transformations == [TransformationMeta(type="identity")]
dataset.set_transform(image=arr_name, transform=transform)
scale, translate = dataset.get_transforms(image=arr_name)
assert scale == expected_scale
assert translate == expected_translate


@given(
ch_shape_dtype=_channels_and_random_5d_shape_and_dtype(),
arr_name=short_alpha_numeric,
Expand Down
Loading