Skip to content

Commit f47e1a8

Browse files
expand testing and reduce overheads
1 parent 01d4170 commit f47e1a8

File tree

3 files changed

+108
-60
lines changed

3 files changed

+108
-60
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ dependencies = []
5252
[project.optional-dependencies]
5353
dev = [
5454
"devtools",
55+
"notebook",
5556
"pooch",
5657
"hatch",
5758
"pytest",
@@ -118,4 +119,4 @@ docstring_parser = "==0.15"
118119
ilastik-core = ">=1.4.2a1,<2"
119120
vigra = ">=1.12.1,<2"
120121
fractal-tasks-core = "==1.5.3"
121-
fractal-task-tools = "==0.0.12"
122+
fractal-task-tools = "==0.0.12"

tests/conftest.py

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import os
2-
import shutil
31
from pathlib import Path
42

5-
import pooch
63
import pytest
74

85

@@ -12,43 +9,45 @@ def testdata_path() -> Path:
129
return TEST_DIR / "data/"
1310

1411

15-
@pytest.fixture(scope="session")
16-
def zenodo_zarr_3d(testdata_path: Path) -> 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.14883998"
27-
DOI_slug = DOI.replace("/", "_").replace(".", "_")
28-
rootfolder = testdata_path / DOI_slug
29-
folder = rootfolder / "AssayPlate_Greiner_CELLSTAR655090.zarr"
30-
31-
registry = {
32-
"AssayPlate_Greiner_CELLSTAR655090.zarr.zip": None,
33-
}
34-
base_url = f"doi:{DOI}"
35-
POOCH = pooch.create(
36-
pooch.os_cache("pooch") / DOI_slug,
37-
base_url,
38-
registry=registry,
39-
retry_if_failed=10,
40-
allow_updates=False,
41-
)
42-
43-
file_name = "AssayPlate_Greiner_CELLSTAR655090.zarr"
44-
# 2) Download/unzip a single Zarr from Zenodo
45-
file_paths = POOCH.fetch(
46-
f"{file_name}.zip", processor=pooch.Unzip(extract_dir=file_name)
47-
)
48-
zarr_full_path = file_paths[0].split(file_name)[0] + file_name
49-
50-
# 3) Copy the downloaded Zarr into tests/data
51-
if os.path.isdir(str(folder)):
52-
shutil.rmtree(str(folder))
53-
shutil.copytree(Path(zarr_full_path) / file_name, folder)
54-
return Path(folder)
12+
# This fixture is not used in the current context, but it can be uncommented if needed.
13+
#
14+
# @pytest.fixture(scope="session")
15+
# def zenodo_zarr_3d(testdata_path: Path) -> str:
16+
# """
17+
# This takes care of multiple steps:
18+
#
19+
# 1. Download/unzip two Zarr containers (3D and MIP) from Zenodo, via pooch
20+
# 2. Copy the two Zarr containers into tests/data
21+
# 3. Modify the Zarrs in tests/data, to add whatever is not in Zenodo
22+
# """
23+
#
24+
# # 1) Download Zarrs from Zenodo
25+
# DOI = "10.5281/zenodo.14883998"
26+
# DOI_slug = DOI.replace("/", "_").replace(".", "_")
27+
# rootfolder = testdata_path / DOI_slug
28+
# folder = rootfolder / "AssayPlate_Greiner_CELLSTAR655090.zarr"
29+
#
30+
# registry = {
31+
# "AssayPlate_Greiner_CELLSTAR655090.zarr.zip": None,
32+
# }
33+
# base_url = f"doi:{DOI}"
34+
# POOCH = pooch.create(
35+
# pooch.os_cache("pooch") / DOI_slug,
36+
# base_url,
37+
# registry=registry,
38+
# retry_if_failed=10,
39+
# allow_updates=False,
40+
# )
41+
#
42+
# file_name = "AssayPlate_Greiner_CELLSTAR655090.zarr"
43+
# # 2) Download/unzip a single Zarr from Zenodo
44+
# file_paths = POOCH.fetch(
45+
# f"{file_name}.zip", processor=pooch.Unzip(extract_dir=file_name)
46+
# )
47+
# zarr_full_path = file_paths[0].split(file_name)[0] + file_name
48+
#
49+
# # 3) Copy the downloaded Zarr into tests/data
50+
# if os.path.isdir(str(folder)):
51+
# shutil.rmtree(str(folder))
52+
# shutil.copytree(Path(zarr_full_path) / file_name, folder)
53+
# return Path(folder)

tests/test_ilastik_pixel_classification_segmentation.py

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@
1616

1717

1818
@pytest.fixture(scope="function")
19-
def test_data_dir_3d(tmp_path: Path, zenodo_zarr_3d: list) -> str:
19+
def ome_zarr_3d_url(tmp_path: Path, testdata_path: Path) -> str:
2020
"""
2121
Copy a test-data folder into a temporary folder.
2222
"""
23+
source_dir = testdata_path / "AssayPlate_Greiner_CELLSTAR655090_B03_0.zarr"
2324
dest_dir = (tmp_path / "ilastik_data_3d").as_posix()
2425
if Path(dest_dir).exists():
2526
shutil.rmtree(dest_dir)
26-
debug(zenodo_zarr_3d, dest_dir)
27-
shutil.copytree(zenodo_zarr_3d, dest_dir)
27+
debug(f"Copying {source_dir} to {dest_dir}")
28+
shutil.copytree(source_dir, dest_dir)
2829
return dest_dir
2930

3031

3132
def test_ilastik_pixel_classification_segmentation_task_3D_dual_channel(
32-
test_data_dir_3d,
33+
ome_zarr_3d_url,
3334
):
3435
"""
3536
Test the 3D ilastik_pixel_classification_segmentation task with dual channel input.
3637
"""
3738
ilastik_model = (
38-
Path(__file__).parent / "data/pixel_classifier_3D_dual_channel.ilp"
39+
Path(__file__).parent / "data" / "pixel_classifier_3D_dual_channel.ilp"
3940
).as_posix()
40-
zarr_url = f"{test_data_dir_3d}/B/03/0"
4141

4242
ilastik_pixel_classification_segmentation(
43-
zarr_url=zarr_url,
44-
level=4,
43+
zarr_url=ome_zarr_3d_url,
44+
level=0,
4545
channel=IlastikChannel1InputModel(label="DAPI_2"),
4646
channel2=IlastikChannel2InputModel(label="ECadherin_2"),
4747
ilastik_model=str(ilastik_model),
@@ -53,8 +53,8 @@ def test_ilastik_pixel_classification_segmentation_task_3D_dual_channel(
5353
# but only one is provided
5454
with pytest.raises(ValueError):
5555
ilastik_pixel_classification_segmentation(
56-
zarr_url=zarr_url,
57-
level=4,
56+
zarr_url=ome_zarr_3d_url,
57+
level=0,
5858
channel=IlastikChannel1InputModel(label="DAPI_2"),
5959
channel2=IlastikChannel2InputModel(label=None),
6060
ilastik_model=str(ilastik_model),
@@ -64,7 +64,7 @@ def test_ilastik_pixel_classification_segmentation_task_3D_dual_channel(
6464

6565

6666
def test_ilastik_pixel_classification_segmentation_task_3D_single_channel(
67-
test_data_dir_3d,
67+
ome_zarr_3d_url,
6868
):
6969
"""
7070
Test the 3D ilastik_pixel_classification_segmentation task
@@ -73,11 +73,10 @@ def test_ilastik_pixel_classification_segmentation_task_3D_single_channel(
7373
ilastik_model = (
7474
Path(__file__).parent / "data/pixel_classifier_3D_single_channel.ilp"
7575
).as_posix()
76-
zarr_url = f"{test_data_dir_3d}/B/03/0"
7776

7877
ilastik_pixel_classification_segmentation(
79-
zarr_url=zarr_url,
80-
level=4,
78+
zarr_url=ome_zarr_3d_url,
79+
level=0,
8180
channel=IlastikChannel1InputModel(label="DAPI_2"),
8281
channel2=IlastikChannel2InputModel(label=None),
8382
ilastik_model=str(ilastik_model),
@@ -89,8 +88,57 @@ def test_ilastik_pixel_classification_segmentation_task_3D_single_channel(
8988
# but two are provided
9089
with pytest.raises(ValueError):
9190
ilastik_pixel_classification_segmentation(
92-
zarr_url=zarr_url,
93-
level=4,
91+
zarr_url=ome_zarr_3d_url,
92+
level=0,
93+
channel=IlastikChannel1InputModel(label="DAPI_2"),
94+
channel2=IlastikChannel2InputModel(label="ECadherin_2"),
95+
ilastik_model=str(ilastik_model),
96+
output_label_name="test_label",
97+
relabeling=True,
98+
)
99+
100+
101+
@pytest.fixture(scope="function")
102+
def ome_zarr_2d_url(tmp_path: Path, testdata_path: Path) -> str:
103+
"""
104+
Copy a test-data folder into a temporary folder for 2D tests.
105+
"""
106+
source_dir = testdata_path / "test_mip_ome.zarr"
107+
dest_dir = (tmp_path / "ilastik_data_2d").as_posix()
108+
if Path(dest_dir).exists():
109+
shutil.rmtree(dest_dir)
110+
debug(f"Copying {source_dir} to {dest_dir}")
111+
shutil.copytree(source_dir, dest_dir)
112+
return dest_dir
113+
114+
115+
def test_ilastik_pixel_classification_segmentation_task_2D_single_channel(
116+
ome_zarr_2d_url,
117+
):
118+
"""
119+
Test the 2D ilastik_pixel_classification_segmentation task
120+
with single channel input.
121+
"""
122+
ilastik_model = (
123+
Path(__file__).parent / "data/pixel_classifier_2D_single_channel.ilp"
124+
).as_posix()
125+
126+
ilastik_pixel_classification_segmentation(
127+
zarr_url=ome_zarr_2d_url,
128+
level=1,
129+
channel=IlastikChannel1InputModel(label="DAPI_2"),
130+
channel2=IlastikChannel2InputModel(label=None),
131+
ilastik_model=str(ilastik_model),
132+
output_label_name="test_label",
133+
relabeling=True,
134+
)
135+
136+
# Test failing of task if model was trained with one channel
137+
# but two are provided
138+
with pytest.raises(ValueError):
139+
ilastik_pixel_classification_segmentation(
140+
zarr_url=ome_zarr_2d_url,
141+
level=1,
94142
channel=IlastikChannel1InputModel(label="DAPI_2"),
95143
channel2=IlastikChannel2InputModel(label="ECadherin_2"),
96144
ilastik_model=str(ilastik_model),

0 commit comments

Comments
 (0)