-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic tests for convert_2D_segmentation_to_3D
- Loading branch information
Showing
6 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
|
||
import pooch | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def testdata_path() -> Path: | ||
TEST_DIR = Path(__file__).parent | ||
return TEST_DIR / "data/" | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def zenodo_zarr(testdata_path: Path) -> list[str]: | ||
""" | ||
This takes care of multiple steps: | ||
1. Download/unzip two Zarr containers (3D and MIP) from Zenodo, via pooch | ||
2. Copy the two Zarr containers into tests/data | ||
3. Modify the Zarrs in tests/data, to add whatever is not in Zenodo | ||
""" | ||
|
||
# 1 Download Zarrs from Zenodo | ||
DOI = "10.5281/zenodo.10257149" | ||
DOI_slug = DOI.replace("/", "_").replace(".", "_") | ||
platenames = ["plate.zarr", "plate_mip.zarr"] | ||
rootfolder = testdata_path / DOI_slug | ||
folders = [rootfolder / plate for plate in platenames] | ||
|
||
registry = { | ||
"20200812-CardiomyocyteDifferentiation14-Cycle1.zarr.zip": None, | ||
"20200812-CardiomyocyteDifferentiation14-Cycle1_mip.zarr.zip": None, | ||
} | ||
base_url = f"doi:{DOI}" | ||
POOCH = pooch.create( | ||
pooch.os_cache("pooch") / DOI_slug, | ||
base_url, | ||
registry=registry, | ||
retry_if_failed=10, | ||
allow_updates=False, | ||
) | ||
|
||
for ind, file_name in enumerate( | ||
[ | ||
"20200812-CardiomyocyteDifferentiation14-Cycle1.zarr", | ||
"20200812-CardiomyocyteDifferentiation14-Cycle1_mip.zarr", | ||
] | ||
): | ||
# 1) Download/unzip a single Zarr from Zenodo | ||
file_paths = POOCH.fetch( | ||
f"{file_name}.zip", processor=pooch.Unzip(extract_dir=file_name) | ||
) | ||
zarr_full_path = file_paths[0].split(file_name)[0] + file_name | ||
print(zarr_full_path) | ||
folder = folders[ind] | ||
|
||
# 2) Copy the downloaded Zarr into tests/data | ||
if os.path.isdir(str(folder)): | ||
shutil.rmtree(str(folder)) | ||
shutil.copytree(Path(zarr_full_path) / file_name, folder) | ||
return [str(f) for f in folders] | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def tmp_zenodo_zarr(zenodo_zarr: list[str], tmpdir: Path) -> list[str]: | ||
"""Generates a copy of the zenodo zarrs in a tmpdir""" | ||
zenodo_mip_path = str(tmpdir / Path(zenodo_zarr[1]).name) | ||
zenodo_path = str(tmpdir / Path(zenodo_zarr[0]).name) | ||
shutil.copytree(zenodo_zarr[0], zenodo_path) | ||
shutil.copytree(zenodo_zarr[1], zenodo_mip_path) | ||
return [zenodo_path, zenodo_mip_path] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
10_5281_zenodo_10257149 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Test copy 2D to 3D segmentation.""" | ||
|
||
import dask.array as da | ||
import zarr | ||
|
||
from fractal_helper_tasks.convert_2D_segmentation_to_3D import ( | ||
convert_2D_segmentation_to_3D, | ||
) | ||
|
||
|
||
def test_2d_to_3d(tmp_zenodo_zarr: list[str]): | ||
zarr_url = f"{tmp_zenodo_zarr[1]}/B/03/0" | ||
label_name = "nuclei" | ||
|
||
convert_2D_segmentation_to_3D( | ||
zarr_url=zarr_url, | ||
label_name=label_name, | ||
) | ||
zarr_3D_label_url = f"{tmp_zenodo_zarr[0]}/B/03/0/labels/{label_name}" | ||
# Check that the label has been copied correctly | ||
with zarr.open(zarr_3D_label_url, mode="rw+") as zarr_img: | ||
zarr_3D = da.from_zarr(zarr_img[0]) | ||
assert zarr_3D.shape == (2, 540, 1280) | ||
|
||
|
||
# TODO: Add custom ROI tables to be copied to 3D | ||
|
||
# TODO: Add test with new label name, new table names | ||
|
||
# TODO: Create a version of the test data where image suffixes need to be | ||
# changed, run tests on those |