Skip to content

Commit

Permalink
Add basic tests for convert_2D_segmentation_to_3D
Browse files Browse the repository at this point in the history
  • Loading branch information
jluethi committed May 1, 2024
1 parent ff9538d commit f6edb08
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = [
# Optional dependencies (e.g. for `pip install -e ".[dev]"`, see
# https://peps.python.org/pep-0621/#dependencies-optional-dependencies)
[project.optional-dependencies]
dev = ["devtools", "pytest", "requests", "build", "jsonschema"]
dev = ["devtools", "pytest", "requests", "build", "jsonschema", "pooch"]
test = ["pytest", "pytest-cov"]

# Build options (see https://peps.python.org/pep-0517)
Expand Down
2 changes: 1 addition & 1 deletion src/fractal_helper_tasks/__FRACTAL_MANIFEST__.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
],
"additionalProperties": false
},
"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"
"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"
}
],
"has_args_schemas": true,
Expand Down
3 changes: 3 additions & 0 deletions src/fractal_helper_tasks/convert_2D_segmentation_to_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ def convert_2D_segmentation_to_3D(
`image_suffix_3D_to_add`. If their base names are different, this task
does not support processing them at the moment.
It makes the assumption that the 3D OME-Zarrs are stored in the same place
as the 2D OME-Zarrs (same based folder).
Args:
zarr_url: Path or url to the individual OME-Zarr image to be processed.
(standard argument for Fractal tasks, managed by Fractal server).
Expand Down
73 changes: 73 additions & 0 deletions tests/conftest.py
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]
1 change: 1 addition & 0 deletions tests/data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10_5281_zenodo_10257149
31 changes: 31 additions & 0 deletions tests/test_convert_2d_to_3d_segmentation.py
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

0 comments on commit f6edb08

Please sign in to comment.