Skip to content

Commit 5bb67c8

Browse files
authored
Merge pull request #22 from fractal-analytics-platform/2d_to_3d_chunking
2d to 3d chunking
2 parents 19060da + 188b7fb commit 5bb67c8

File tree

6 files changed

+31
-9
lines changed

6 files changed

+31
-9
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ authors = [
2727
# Required Python version and dependencies
2828
requires-python = ">=3.10"
2929
dependencies = [
30-
"fractal-tasks-core==1.4.0","ngio==0.1.4",
30+
"fractal-tasks-core==1.4.2","ngio==0.1.6",
3131
]
3232

3333
# Optional dependencies (e.g. for `pip install -e ".[dev]"`, see

src/fractal_helper_tasks/__FRACTAL_MANIFEST__.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
"input_types": {
5050
"is_3D": false
5151
},
52+
"output_types": {
53+
"is_3D": true
54+
},
5255
"tags": [
5356
"Mixed modality",
5457
"2D to 3D workflows"
@@ -113,6 +116,11 @@
113116
"type": "string",
114117
"description": "If the image name between 2D & 3D don't match, this is the suffix that should be added to the 3D image. If the 2D image is in \"/path/to/my_plate_mip.zarr/B/03/0\" and the 3D image is in \"/path/to/my_plate.zarr/B/03/0_illum_corr\", the value should be \"_illum_corr\"."
115118
},
119+
"z_chunks": {
120+
"title": "Z Chunks",
121+
"type": "integer",
122+
"description": "Chunking for the Z dimension. Set this parameter if you want the label image to be chunked differently from the 3D image in the Z dimension."
123+
},
116124
"overwrite": {
117125
"default": false,
118126
"title": "Overwrite",

src/fractal_helper_tasks/convert_2D_segmentation_to_3D.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def convert_2D_segmentation_to_3D(
9999
plate_suffix: str = "_mip",
100100
image_suffix_2D_to_remove: Optional[str] = None,
101101
image_suffix_3D_to_add: Optional[str] = None,
102+
z_chunks: Optional[int] = None,
102103
overwrite: bool = False,
103104
) -> None:
104105
"""Convert 2D segmentation to 3D segmentation.
@@ -141,6 +142,9 @@ def convert_2D_segmentation_to_3D(
141142
If the 2D image is in "/path/to/my_plate_mip.zarr/B/03/0" and the
142143
3D image is in "/path/to/my_plate.zarr/B/03/0_illum_corr", the
143144
value should be "_illum_corr".
145+
z_chunks: Chunking for the Z dimension. Set this parameter if you want
146+
the label image to be chunked differently from the 3D image in
147+
the Z dimension.
144148
overwrite: If `True`, overwrite existing label and ROI tables in the
145149
3D OME-Zarr
146150
"""
@@ -173,12 +177,20 @@ def convert_2D_segmentation_to_3D(
173177

174178
# 1a) Load a 2D label image
175179
label_img = da.from_zarr(f"{zarr_url}/labels/{label_name}/{level}")
176-
chunks = label_img.chunksize
180+
chunks = list(label_img.chunksize)
177181

178182
# 1b) Get number z planes & Z spacing from 3D OME-Zarr file
179183
with zarr.open(zarr_3D_url, mode="rw+") as zarr_img:
180184
zarr_3D = da.from_zarr(zarr_img[0])
181185
new_z_planes = zarr_3D.shape[-3]
186+
z_chunk_3d = zarr_3D.chunksize[-3]
187+
188+
# TODO: Improve axis detection in ngio refactor?
189+
if z_chunks:
190+
chunks[-3] = z_chunks
191+
else:
192+
chunks[-3] = z_chunk_3d
193+
chunks = tuple(chunks)
182194

183195
image_meta = load_NgffImageMeta(zarr_3D_url)
184196
z_pixel_size = image_meta.get_pixel_sizes_zyx(level=0)[0]
@@ -249,12 +261,15 @@ def convert_2D_segmentation_to_3D(
249261

250262
logger.info("Finished 2D to 3D conversion")
251263

252-
output_dict = dict(
253-
filters=dict(
254-
types=dict(is_3D=True),
255-
)
264+
# Give the 3D image as an output so that filters are applied correctly
265+
image_list_updates = dict(
266+
image_list_updates=[
267+
dict(
268+
zarr_url=zarr_3D_url,
269+
)
270+
]
256271
)
257-
return output_dict
272+
return image_list_updates
258273

259274

260275
if __name__ == "__main__":

src/fractal_helper_tasks/dev/task_list.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
),
1414
ParallelTask(
1515
input_types=dict(is_3D=False),
16+
output_types=dict(is_3D=True),
1617
name="Convert 2D segmentation to 3D",
1718
executable="convert_2D_segmentation_to_3D.py",
1819
meta={"cpus_per_task": 2, "mem": 8000},

src/fractal_helper_tasks/rechunk_zarr.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def rechunk_zarr(
131131
types=dict(rechunked=True),
132132
)
133133
],
134-
filters=dict(types=dict(rechunked=True)),
135134
)
136135
return output
137136

tests/test_rechunk_zarr.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ def test_rechunk_no_overwrite_input(tmp_zenodo_zarr: list[str]):
119119
types=dict(rechunked=True),
120120
)
121121
],
122-
filters=dict(types=dict(rechunked=True)),
123122
)
124123
assert expected_output == output
125124

0 commit comments

Comments
 (0)