From 771de729272ee4f1eec543e04874605aa3a009a8 Mon Sep 17 00:00:00 2001 From: C-Achard Date: Thu, 19 Nov 2026 12:12:17 +0100 Subject: [PATCH 1/2] Add example to sample images --- docs/welcome.rst | 4 ++ examples/README.md | 2 +- napari_cellseg3d/_sample_data.py | 52 +++++++++++++++++++++ napari_cellseg3d/_tests/test_sample_data.py | 19 ++++++++ napari_cellseg3d/napari.yaml | 9 ++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 napari_cellseg3d/_sample_data.py create mode 100644 napari_cellseg3d/_tests/test_sample_data.py diff --git a/docs/welcome.rst b/docs/welcome.rst index 652d880b..64d2c0e5 100644 --- a/docs/welcome.rst +++ b/docs/welcome.rst @@ -100,6 +100,10 @@ and choose the correct tool to use: Many buttons have tooltips to help you understand what they do. Simply hover over them to see the tooltip. +.. tip:: + To try the plugin without your own data, download & open the small demo volume from + **File > Open Sample > CellSeg3D > Demo volume (c5image)**. + Documentation contents -------------------------------------------- diff --git a/examples/README.md b/examples/README.md index ab67956e..890a332c 100644 --- a/examples/README.md +++ b/examples/README.md @@ -2,7 +2,7 @@ Here is a very small volume (from [IDR project 853](https://idr.openmicroscopy.org/webclient/?show=project-853)) to test on. All credits to the original authors of the data. -You can install, launch `napari`, activate the CellSeg3D plugin app, and drag & drop this volume into the canvas. +You can install, launch `napari`, activate the CellSeg3D plugin app, and either drag & drop this volume into the canvas or open it from `File > Open Sample > CellSeg3D > Demo volume (c5image)`. Then, for example, run the `Inference` module with one of our models. See [CellSeg3D documentation](https://adaptivemotorcontrollab.github.io/CellSeg3D/welcome.html) for more details. diff --git a/napari_cellseg3d/_sample_data.py b/napari_cellseg3d/_sample_data.py new file mode 100644 index 00000000..cd70a298 --- /dev/null +++ b/napari_cellseg3d/_sample_data.py @@ -0,0 +1,52 @@ +"""Sample data contributed to napari's File > Open Sample menu.""" + +import hashlib +import urllib.request +from pathlib import Path + +from napari_cellseg3d.utils import LOGGER as logger + +C5IMAGE_NAME = "c5image.tif" +C5IMAGE_REPO_FILE = Path(__file__).parents[1] / "examples" / C5IMAGE_NAME +C5IMAGE_CACHE_FILE = Path(__file__).parent / "res" / "sample_data" / C5IMAGE_NAME +C5IMAGE_URL = ( + "https://raw.githubusercontent.com/AdaptiveMotorControlLab/" + f"CellSeg3D/main/examples/{C5IMAGE_NAME}" +) +C5IMAGE_SHA256 = "9bb5a3da2ade6f66899da7247ca0b18c6c7adb43b9bd6b2c3869a32945a7d9ff" + + +def _sha256(path: Path) -> str: + return hashlib.sha256(path.read_bytes()).hexdigest() + + +def _download(url: str, dest: Path, sha256: str) -> None: + dest.parent.mkdir(parents=True, exist_ok=True) + tmp = dest.with_suffix(dest.suffix + ".part") + logger.info(f"Downloading sample data from {url}") + try: + with urllib.request.urlopen(url) as response: # noqa: S310 + tmp.write_bytes(response.read()) + digest = _sha256(tmp) + if digest != sha256: + raise ValueError( + f"Checksum mismatch for {url}: expected {sha256}, got {digest}" + ) + tmp.replace(dest) + finally: + tmp.unlink(missing_ok=True) + + +def _c5image_path() -> Path: + if C5IMAGE_REPO_FILE.is_file(): + return C5IMAGE_REPO_FILE + if not C5IMAGE_CACHE_FILE.is_file(): + _download(C5IMAGE_URL, C5IMAGE_CACHE_FILE, C5IMAGE_SHA256) + return C5IMAGE_CACHE_FILE + + +def c5image(): + """Small demo volume from IDR project 853 (see examples/README.md).""" + from tifffile import imread + + return [(imread(_c5image_path()), {"name": "c5image"}, "image")] diff --git a/napari_cellseg3d/_tests/test_sample_data.py b/napari_cellseg3d/_tests/test_sample_data.py new file mode 100644 index 00000000..e738b597 --- /dev/null +++ b/napari_cellseg3d/_tests/test_sample_data.py @@ -0,0 +1,19 @@ +from napari_cellseg3d import _sample_data + + +def test_c5image_from_repo(): + assert _sample_data.C5IMAGE_REPO_FILE.is_file() + layers = _sample_data.c5image() + assert len(layers) == 1 + data, meta, layer_type = layers[0] + assert data.shape == (124, 86, 94) + assert meta["name"] == "c5image" + assert layer_type == "image" + + +def test_c5image_open_sample(make_napari_viewer_proxy): + assert _sample_data.C5IMAGE_REPO_FILE.is_file() + viewer = make_napari_viewer_proxy() + viewer.open_sample("napari-cellseg3d", "c5image") + assert viewer.layers[-1].name == "c5image" + assert viewer.layers[-1].data.shape == (124, 86, 94) diff --git a/napari_cellseg3d/napari.yaml b/napari_cellseg3d/napari.yaml index f6f57978..2d22ea0a 100644 --- a/napari_cellseg3d/napari.yaml +++ b/napari_cellseg3d/napari.yaml @@ -24,6 +24,10 @@ contributions: title: Create Trainer widget python_name: napari_cellseg3d.plugins:Trainer + - id: napari-cellseg3d.sample_c5image + title: Load demo volume (c5image) + python_name: napari_cellseg3d._sample_data:c5image + widgets: - command: napari-cellseg3d.load @@ -40,3 +44,8 @@ contributions: - command: napari-cellseg3d.help display_name: Help/About... + + sample_data: + - key: c5image + display_name: Demo volume (c5image) + command: napari-cellseg3d.sample_c5image From d22d638301f813eaeef673bdb73a7504efe38368 Mon Sep 17 00:00:00 2001 From: C-Achard Date: Thu, 19 Nov 2026 12:16:10 +0100 Subject: [PATCH 2/2] Use inferno cmap --- napari_cellseg3d/_sample_data.py | 3 ++- napari_cellseg3d/_tests/test_sample_data.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/napari_cellseg3d/_sample_data.py b/napari_cellseg3d/_sample_data.py index cd70a298..94acf7b9 100644 --- a/napari_cellseg3d/_sample_data.py +++ b/napari_cellseg3d/_sample_data.py @@ -49,4 +49,5 @@ def c5image(): """Small demo volume from IDR project 853 (see examples/README.md).""" from tifffile import imread - return [(imread(_c5image_path()), {"name": "c5image"}, "image")] + metadata = {"name": "c5image", "colormap": "inferno"} + return [(imread(_c5image_path()), metadata, "image")] diff --git a/napari_cellseg3d/_tests/test_sample_data.py b/napari_cellseg3d/_tests/test_sample_data.py index e738b597..56cfaff6 100644 --- a/napari_cellseg3d/_tests/test_sample_data.py +++ b/napari_cellseg3d/_tests/test_sample_data.py @@ -17,3 +17,4 @@ def test_c5image_open_sample(make_napari_viewer_proxy): viewer.open_sample("napari-cellseg3d", "c5image") assert viewer.layers[-1].name == "c5image" assert viewer.layers[-1].data.shape == (124, 86, 94) + assert viewer.layers[-1].colormap.name == "inferno"