Skip to content

Commit 0276e43

Browse files
authored
Merge pull request #38 from fractal-analytics-platform/ngio_04
Refactor to ngio 0.4
2 parents 50dadc7 + 3128920 commit 0276e43

File tree

9 files changed

+41
-44
lines changed

9 files changed

+41
-44
lines changed

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ authors = [
2727
# Required Python version and dependencies
2828
requires-python = ">=3.11"
2929
dependencies = [
30-
"ngio>=0.2.8, <0.3.0",
30+
"ngio>=0.4.1, <0.5.0",
3131
"fractal-task-tools==0.0.12",
32+
"pyarrow<21.0.0",
3233
]
3334

3435
# Optional dependencies (e.g. for `pip install -e ".[dev]"`, see
3536
# https://peps.python.org/pep-0621/#dependencies-optional-dependencies)
3637
[project.optional-dependencies]
37-
dev = ["devtools", "hatch", "pytest", "requests", "jsonschema", "ruff", "pre-commit", "pooch"]
38+
dev = ["devtools", "hatch", "pytest", "requests", "jsonschema", "ruff", "pre-commit", "pooch", "pillow"]
3839
test = ["pytest", "pytest-cov"]
3940

4041
# https://docs.astral.sh/ruff

src/fractal_helper_tasks/add_z_singleton.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def add_z_singleton(
5151
"Thus, the add Z singleton dimension task can't be applied to it."
5252
)
5353
image = old_ome_zarr_img.get_array(mode="dask")
54-
axes_names = old_ome_zarr_img.meta.axes_mapper.on_disk_axes_names
54+
axes_names = list(old_ome_zarr_img.meta.axes_handler.axes_names)
5555
ndim = image.ndim
5656
insert_index = ndim - 2
5757
if insert_index < 0:

src/fractal_helper_tasks/convert_2D_segmentation_to_3D.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def convert_2D_segmentation_to_3D(
124124
raise ValueError(
125125
f"Label image {label_name} is not 2D. It has a shape of "
126126
f"{label_img.shape} and the axes "
127-
f"{label_img.axes_mapper.on_disk_axes_names}."
127+
f"{label_img.axes}."
128128
)
129129

130130
chunks = list(label_img.chunks)
@@ -135,10 +135,10 @@ def convert_2D_segmentation_to_3D(
135135
pixel_size=label_img.pixel_size,
136136
)
137137

138-
z_index = label_img.axes_mapper.get_index("z")
139-
y_index = label_img.axes_mapper.get_index("y")
140-
x_index = label_img.axes_mapper.get_index("x")
141-
z_index_3d_reference = ref_image_3d.axes_mapper.get_index("z")
138+
z_index = label_img.axes_handler.get_index("z")
139+
y_index = label_img.axes_handler.get_index("y")
140+
x_index = label_img.axes_handler.get_index("x")
141+
z_index_3d_reference = ref_image_3d.axes_handler.get_index("z")
142142
if z_chunks:
143143
chunks[z_index] = z_chunks
144144
else:
@@ -151,7 +151,7 @@ def convert_2D_segmentation_to_3D(
151151

152152
pixel_size = label_img.pixel_size
153153
pixel_size.z = ref_image_3d.pixel_size.z
154-
axes_names = label_img.axes_mapper.on_disk_axes_names
154+
axes_names = label_img.meta.axes_handler.axes_names
155155

156156
z_extent = nb_z_planes * pixel_size.z
157157

@@ -191,8 +191,10 @@ def convert_2D_segmentation_to_3D(
191191
f"Table {table_name} not found in 2D OME-Zarr {zarr_url}."
192192
)
193193
table = ome_zarr_container_2d.get_table(table_name)
194-
print(table.type())
195-
if table.type() == "roi_table" or table.type() == "masking_roi_table":
194+
if (
195+
table.table_type() == "roi_table"
196+
or table.table_type() == "masking_roi_table"
197+
):
196198
for roi in table.rois():
197199
roi.z_length = z_extent
198200
ome_zarr_container_3d.add_table(

src/fractal_helper_tasks/drop_t_dimension.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ def drop_t_dimension(
5252
)
5353
# TODO: Check if T dimension not singleton
5454
image = old_ome_zarr_img.get_array(mode="dask")
55-
t_index = old_ome_zarr_img.meta.axes_mapper.get_index("t")
55+
t_index = old_ome_zarr_img.axes_handler.get_index("t")
5656
new_img = da.squeeze(image, axis=t_index)
5757
pixel_size = old_ome_zarr_img.pixel_size
5858
new_pixel_size = ngio.PixelSize(x=pixel_size.x, y=pixel_size.y, z=pixel_size.z)
59-
axes_names = old_ome_zarr_img.meta.axes_mapper.on_disk_axes_names
59+
axes_names = list(old_ome_zarr_img.axes)
6060
del axes_names[t_index]
6161
chunk_sizes = old_ome_zarr_img.chunks
6262
new_chunk_sizes = chunk_sizes[:t_index] + chunk_sizes[t_index + 1 :]

src/fractal_helper_tasks/rechunk_zarr.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
from typing import Any, Optional
1111

1212
import ngio
13-
import ngio.images
14-
import ngio.images.label
15-
from ngio.ome_zarr_meta import AxesMapper
1613
from pydantic import validate_call
1714

1815
from fractal_helper_tasks.utils import normalize_chunk_size_dict
@@ -22,7 +19,7 @@
2219

2320
def change_chunks(
2421
initial_chunks: list[int],
25-
axes_mapper: AxesMapper,
22+
multiscale: ngio.Image | ngio.Label,
2623
chunk_sizes: dict[str, Optional[int]],
2724
) -> list[int]:
2825
"""Create a new chunk_size list with rechunking.
@@ -33,12 +30,12 @@ def change_chunks(
3330
"""
3431
for axes_name, chunk_value in chunk_sizes.items():
3532
if chunk_value is not None:
36-
axes_index = axes_mapper.get_index(axes_name)
33+
axes_index = multiscale.axes_handler.get_index(axes_name)
3734
if axes_index is None:
3835
raise ValueError(
3936
f"Rechunking with {axes_name=} is specified, but the "
4037
"OME-Zarr only has the following axes: "
41-
f"{axes_mapper.on_disk_axes_names}"
38+
f"{multiscale.axes}"
4239
)
4340
initial_chunks[axes_index] = chunk_value
4441
return initial_chunks
@@ -89,7 +86,7 @@ def rechunk_zarr(
8986
chunks = highest_res_img.chunks
9087
new_chunksize = change_chunks(
9188
initial_chunks=list(chunks),
92-
axes_mapper=highest_res_img.meta.axes_mapper,
89+
multiscale=highest_res_img,
9390
chunk_sizes=chunk_sizes,
9491
)
9592

@@ -123,7 +120,7 @@ def rechunk_zarr(
123120
old_label = ome_zarr_container.get_label(name=label)
124121
new_chunksize = change_chunks(
125122
initial_chunks=list(old_label.chunks),
126-
axes_mapper=old_label.meta.axes_mapper,
123+
multiscale=old_label,
127124
chunk_sizes=chunk_sizes,
128125
)
129126
new_label = new_ome_zarr_container.derive_label(
@@ -139,9 +136,7 @@ def rechunk_zarr(
139136
label_pyramid_paths = old_label.meta.paths
140137
for path in label_pyramid_paths:
141138
old_label = ome_zarr_container.get_label(name=label, path=path)
142-
new_label.set_array(
143-
old_label.get_array(mode="dask")
144-
)
139+
new_label.set_array(old_label.get_array(mode="dask"))
145140

146141
if overwrite_input:
147142
os.rename(zarr_url, f"{zarr_url}_tmp")

tests/test_add_z_singleton.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_add_singleton(
6060

6161
new_ome_zarr_container = ngio.open_ome_zarr_container(new_zarr_url)
6262
assert (
63-
new_ome_zarr_container.image_meta.axes_mapper.on_disk_axes_names
63+
list(new_ome_zarr_container.image_meta.axes_handler.axes_names)
6464
== target_axes_names
6565
)
6666
assert new_ome_zarr_container.get_image().pixel_size.z == 1.0

tests/test_convert_2d_to_3d_segmentation.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import numpy as np
77
import pandas as pd
88
import pytest
9-
from ngio.images.label import build_masking_roi_table
109
from ngio.tables import GenericTable
1110

1211
from fractal_helper_tasks.convert_2D_segmentation_to_3D import (
@@ -134,10 +133,9 @@ def test_2d_to_3d_table_copying(tmp_path: Path):
134133

135134
create_synthetic_data(zarr_url, zarr_url_3d, label_name, z_spacing=1.0)
136135
ome_zarr_2d = ngio.open_ome_zarr_container(zarr_url)
137-
rois = ome_zarr_2d.get_table(
138-
"masking_ROI_table", check_type="masking_roi_table"
139-
).rois()
140-
assert rois[0].z_length == 1.0
136+
roi_table = ome_zarr_2d.get_table("masking_ROI_table")
137+
assert roi_table.table_type() == "masking_roi_table"
138+
assert roi_table.rois()[0].z_length == 1.0
141139

142140
convert_2D_segmentation_to_3D(
143141
zarr_url=zarr_url,
@@ -147,16 +145,18 @@ def test_2d_to_3d_table_copying(tmp_path: Path):
147145
ome_zarr_3d = ngio.open_ome_zarr_container(zarr_url_3d)
148146
# Validate correctness of tables
149147
assert ome_zarr_3d.list_tables() == ["masking_ROI_table", "image_ROI_table"]
150-
rois = ome_zarr_3d.get_table(
151-
"masking_ROI_table", check_type="masking_roi_table"
152-
).rois()
148+
roi_table = ome_zarr_3d.get_table("masking_ROI_table")
149+
assert roi_table.table_type() == "masking_roi_table"
150+
rois = roi_table.rois()
153151
assert len(rois) == 1
154152
assert rois[0].name == "1"
155153
assert rois[0].x_length == 10.0
156154
# z_length goes from 1 to 10 because we have 10 z planes
157155
assert rois[0].z_length == 10.0
158156

159-
rois = ome_zarr_3d.get_table("image_ROI_table", check_type="roi_table").rois()
157+
roi_table = ome_zarr_3d.get_table("image_ROI_table")
158+
assert roi_table.table_type() == "roi_table"
159+
rois = roi_table.rois()
160160
assert len(rois) == 1
161161
assert rois[0].name == "image_ROI_table"
162162
assert rois[0].x_length == 50.0
@@ -193,7 +193,7 @@ def test_2d_to_3d_real_data(tmp_zenodo_zarr: list[str]):
193193

194194
# Create a masking roi table in the 2D image
195195
ome_zarr_2d = ngio.open_ome_zarr_container(zarr_url)
196-
masking_roi_table = build_masking_roi_table(ome_zarr_2d.get_label(name=label_name))
196+
masking_roi_table = ome_zarr_2d.build_masking_roi_table(label_name)
197197

198198
ome_zarr_2d.add_table(
199199
name=tables_to_copy[0],
@@ -227,7 +227,7 @@ def test_2d_to_3d_real_data_no_label_copy(tmp_zenodo_zarr: list[str]):
227227

228228
# Add a generic table to be copied over
229229
generic_table = GenericTable(
230-
dataframe=pd.DataFrame({"col1": [1, 2], "col2": [3, 4]})
230+
table_data=pd.DataFrame({"col1": [1, 2], "col2": [3, 4]})
231231
)
232232

233233
ome_zarr_2d.add_table(

tests/test_drop_t_dimension.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ def test_drop_t_dimension(
3030
)
3131
new_zarr_url = f"{zarr_url}_no_T"
3232
new_ome_zarr_container = ngio.open_ome_zarr_container(new_zarr_url)
33-
assert new_ome_zarr_container.image_meta.axes_mapper.on_disk_axes_names == [
33+
34+
assert new_ome_zarr_container.image_meta.axes_handler.axes_names == (
3435
"c",
3536
"z",
3637
"y",
3738
"x",
38-
]
39+
)

tests/test_rechunk_zarr.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88
)
99

1010

11-
# FIXME: Reactive these tests with ngio 0.2.3
1211
@pytest.mark.parametrize(
1312
"chunk_sizes, output_chunk_sizes",
1413
[
1514
({"x": 1000, "y": 1000}, (1, 1, 1000, 1000)),
1615
({"X": 1000, "Y": 1000}, (1, 1, 1000, 1000)),
17-
# ({"x": 6000, "y": 6000}, (1, 1, 2160, 5120)),
16+
({"x": 6000, "y": 6000}, (1, 1, 2160, 5120)),
1817
({}, (1, 1, 2160, 2560)),
1918
({"x": None, "y": None}, (1, 1, 2160, 2560)),
20-
# ({"z": 10}, (1, 1, 2160, 2560)),
21-
# ({"Z": 10}, (1, 1, 2160, 2560)),
19+
({"z": 10}, (1, 1, 2160, 2560)),
20+
({"Z": 10}, (1, 1, 2160, 2560)),
2221
],
2322
)
2423
def test_rechunk_2d(tmp_zenodo_zarr: list[str], chunk_sizes, output_chunk_sizes):
@@ -33,12 +32,11 @@ def test_rechunk_2d(tmp_zenodo_zarr: list[str], chunk_sizes, output_chunk_sizes)
3332
assert chunk_sizes == output_chunk_sizes
3433

3534

36-
# FIXME: Reactive these tests with ngio 0.2.3
3735
@pytest.mark.parametrize(
3836
"chunk_sizes, output_chunk_sizes",
3937
[
4038
({"x": None, "y": None}, (1, 1, 2160, 2560)),
41-
# ({"z": 10}, (1, 2, 2160, 2560)),
39+
({"z": 10}, (1, 2, 2160, 2560)),
4240
],
4341
)
4442
def test_rechunk_3d(tmp_zenodo_zarr: list[str], chunk_sizes, output_chunk_sizes):

0 commit comments

Comments
 (0)