Skip to content

Commit f6edb08

Browse files
committed
Add basic tests for convert_2D_segmentation_to_3D
1 parent ff9538d commit f6edb08

6 files changed

+110
-2
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies = [
1818
# Optional dependencies (e.g. for `pip install -e ".[dev]"`, see
1919
# https://peps.python.org/pep-0621/#dependencies-optional-dependencies)
2020
[project.optional-dependencies]
21-
dev = ["devtools", "pytest", "requests", "build", "jsonschema"]
21+
dev = ["devtools", "pytest", "requests", "build", "jsonschema", "pooch"]
2222
test = ["pytest", "pytest-cov"]
2323

2424
# Build options (see https://peps.python.org/pep-0517)

src/fractal_helper_tasks/__FRACTAL_MANIFEST__.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
],
119119
"additionalProperties": false
120120
},
121-
"docs_info": "## convert_2D_segmentation_to_3D\nConvert 2D segmentation to 3D segmentation.\n\nThis task loads the 2D segmentation, replicates it along the Z slice and\nstores it back into the 3D OME-Zarr image.\n\nThis is a temporary workaround task, as long as we store 2D data in\na separate OME-Zarr file from the 3D data. If the 2D & 3D OME-Zarr images\nhave different suffixes in their name, use `image_suffix_2D_to_remove` &\n`image_suffix_3D_to_add`. If their base names are different, this task\ndoes not support processing them at the moment.\n"
121+
"docs_info": "## convert_2D_segmentation_to_3D\nConvert 2D segmentation to 3D segmentation.\n\nThis task loads the 2D segmentation, replicates it along the Z slice and\nstores it back into the 3D OME-Zarr image.\n\nThis is a temporary workaround task, as long as we store 2D data in\na separate OME-Zarr file from the 3D data. If the 2D & 3D OME-Zarr images\nhave different suffixes in their name, use `image_suffix_2D_to_remove` &\n`image_suffix_3D_to_add`. If their base names are different, this task\ndoes not support processing them at the moment.\n\nIt makes the assumption that the 3D OME-Zarrs are stored in the same place \nas the 2D OME-Zarrs (same based folder).\n"
122122
}
123123
],
124124
"has_args_schemas": true,

src/fractal_helper_tasks/convert_2D_segmentation_to_3D.py

+3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ def convert_2D_segmentation_to_3D(
113113
`image_suffix_3D_to_add`. If their base names are different, this task
114114
does not support processing them at the moment.
115115
116+
It makes the assumption that the 3D OME-Zarrs are stored in the same place
117+
as the 2D OME-Zarrs (same based folder).
118+
116119
Args:
117120
zarr_url: Path or url to the individual OME-Zarr image to be processed.
118121
(standard argument for Fractal tasks, managed by Fractal server).

tests/conftest.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import os
2+
import shutil
3+
from pathlib import Path
4+
5+
import pooch
6+
import pytest
7+
8+
9+
@pytest.fixture(scope="session")
10+
def testdata_path() -> Path:
11+
TEST_DIR = Path(__file__).parent
12+
return TEST_DIR / "data/"
13+
14+
15+
@pytest.fixture(scope="session")
16+
def zenodo_zarr(testdata_path: Path) -> list[str]:
17+
"""
18+
This takes care of multiple steps:
19+
20+
1. Download/unzip two Zarr containers (3D and MIP) from Zenodo, via pooch
21+
2. Copy the two Zarr containers into tests/data
22+
3. Modify the Zarrs in tests/data, to add whatever is not in Zenodo
23+
"""
24+
25+
# 1 Download Zarrs from Zenodo
26+
DOI = "10.5281/zenodo.10257149"
27+
DOI_slug = DOI.replace("/", "_").replace(".", "_")
28+
platenames = ["plate.zarr", "plate_mip.zarr"]
29+
rootfolder = testdata_path / DOI_slug
30+
folders = [rootfolder / plate for plate in platenames]
31+
32+
registry = {
33+
"20200812-CardiomyocyteDifferentiation14-Cycle1.zarr.zip": None,
34+
"20200812-CardiomyocyteDifferentiation14-Cycle1_mip.zarr.zip": None,
35+
}
36+
base_url = f"doi:{DOI}"
37+
POOCH = pooch.create(
38+
pooch.os_cache("pooch") / DOI_slug,
39+
base_url,
40+
registry=registry,
41+
retry_if_failed=10,
42+
allow_updates=False,
43+
)
44+
45+
for ind, file_name in enumerate(
46+
[
47+
"20200812-CardiomyocyteDifferentiation14-Cycle1.zarr",
48+
"20200812-CardiomyocyteDifferentiation14-Cycle1_mip.zarr",
49+
]
50+
):
51+
# 1) Download/unzip a single Zarr from Zenodo
52+
file_paths = POOCH.fetch(
53+
f"{file_name}.zip", processor=pooch.Unzip(extract_dir=file_name)
54+
)
55+
zarr_full_path = file_paths[0].split(file_name)[0] + file_name
56+
print(zarr_full_path)
57+
folder = folders[ind]
58+
59+
# 2) Copy the downloaded Zarr into tests/data
60+
if os.path.isdir(str(folder)):
61+
shutil.rmtree(str(folder))
62+
shutil.copytree(Path(zarr_full_path) / file_name, folder)
63+
return [str(f) for f in folders]
64+
65+
66+
@pytest.fixture(scope="function")
67+
def tmp_zenodo_zarr(zenodo_zarr: list[str], tmpdir: Path) -> list[str]:
68+
"""Generates a copy of the zenodo zarrs in a tmpdir"""
69+
zenodo_mip_path = str(tmpdir / Path(zenodo_zarr[1]).name)
70+
zenodo_path = str(tmpdir / Path(zenodo_zarr[0]).name)
71+
shutil.copytree(zenodo_zarr[0], zenodo_path)
72+
shutil.copytree(zenodo_zarr[1], zenodo_mip_path)
73+
return [zenodo_path, zenodo_mip_path]

tests/data/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10_5281_zenodo_10257149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Test copy 2D to 3D segmentation."""
2+
3+
import dask.array as da
4+
import zarr
5+
6+
from fractal_helper_tasks.convert_2D_segmentation_to_3D import (
7+
convert_2D_segmentation_to_3D,
8+
)
9+
10+
11+
def test_2d_to_3d(tmp_zenodo_zarr: list[str]):
12+
zarr_url = f"{tmp_zenodo_zarr[1]}/B/03/0"
13+
label_name = "nuclei"
14+
15+
convert_2D_segmentation_to_3D(
16+
zarr_url=zarr_url,
17+
label_name=label_name,
18+
)
19+
zarr_3D_label_url = f"{tmp_zenodo_zarr[0]}/B/03/0/labels/{label_name}"
20+
# Check that the label has been copied correctly
21+
with zarr.open(zarr_3D_label_url, mode="rw+") as zarr_img:
22+
zarr_3D = da.from_zarr(zarr_img[0])
23+
assert zarr_3D.shape == (2, 540, 1280)
24+
25+
26+
# TODO: Add custom ROI tables to be copied to 3D
27+
28+
# TODO: Add test with new label name, new table names
29+
30+
# TODO: Create a version of the test data where image suffixes need to be
31+
# changed, run tests on those

0 commit comments

Comments
 (0)