micro_sam.sample_data

Sample microscopy data.

You can change the download location for sample data and model weights by setting the environment variable: MICROSAM_CACHEDIR

By default sample data is downloaded to a folder named 'micro_sam/sample_data' inside your default cache directory, eg: * Mac: ~/Library/Caches/ * Unix: ~/.cache/ or the value of the XDG_CACHE_HOME environment variable, if defined. * Windows: C:\Users<user>\AppData\Local<AppAuthor><AppName>\Cache

  1r"""Sample microscopy data.
  2
  3You can change the download location for sample data and model weights
  4by setting the environment variable: MICROSAM_CACHEDIR
  5
  6By default sample data is downloaded to a folder named 'micro_sam/sample_data'
  7inside your default cache directory, eg:
  8    * Mac: ~/Library/Caches/<AppName>
  9    * Unix: ~/.cache/<AppName> or the value of the XDG_CACHE_HOME environment variable, if defined.
 10    * Windows: C:\Users\<user>\AppData\Local\<AppAuthor>\<AppName>\Cache
 11"""
 12
 13import os
 14import pooch
 15from pathlib import Path
 16from typing import Union
 17
 18import numpy as np
 19import imageio.v3 as imageio
 20
 21from skimage.transform import resize
 22from skimage.data import binary_blobs
 23
 24from bioimage_cpp.segmentation import label
 25
 26from .util import get_cache_directory
 27
 28
 29def fetch_image_series_example_data(save_directory: Union[str, os.PathLike]) -> str:
 30    """Download the sample images for the image series annotator.
 31
 32    Args:
 33        save_directory: Root folder to save the downloaded data.
 34
 35    Returns:
 36        The folder that contains the downloaded data.
 37    """
 38    # This sample dataset is currently not provided to napari by the micro-sam
 39    # plugin, because images are not all the same shape and cannot be combined
 40    # into a single layer
 41    save_directory = Path(save_directory)
 42    os.makedirs(save_directory, exist_ok=True)
 43    print("Example data directory is:", save_directory.resolve())
 44    fname = "image-series.zip"
 45    unpack_filenames = [os.path.join("series", f"im{i}.tif") for i in range(3)]
 46    unpack = pooch.Unzip(members=unpack_filenames)
 47    pooch.retrieve(
 48        url="https://owncloud.gwdg.de/index.php/s/M1zGnfkulWoAhUG/download",
 49        known_hash="92346ca9770bcaf55248efee590718d54c7135b6ebca15d669f3b77b6afc8706",
 50        fname=fname,
 51        path=save_directory,
 52        progressbar=True,
 53        processor=unpack,
 54    )
 55    data_folder = os.path.join(save_directory, f"{fname}.unzip", "series")
 56    assert os.path.exists(data_folder)
 57    return data_folder
 58
 59
 60def sample_data_image_series():
 61    """Provides image series example image to napari.
 62
 63    Opens as three separate image layers in napari (one per image in series).
 64    The third image in the series has a different size and modality.
 65    """
 66    # Return list of tuples
 67    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
 68    # Check the documentation for more information about the
 69    # add_image_kwargs
 70    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
 71    base_data_directory = os.path.join(get_cache_directory(), "sample_data")
 72    data_directory = fetch_image_series_example_data(base_data_directory)
 73    fnames = os.listdir(data_directory)
 74    full_filenames = [os.path.join(data_directory, f) for f in fnames]
 75    full_filenames.sort()
 76    data_and_image_kwargs = [(imageio.imread(f), {"name": f"img-{i}"}) for i, f in enumerate(full_filenames)]
 77    return data_and_image_kwargs
 78
 79
 80def fetch_wholeslide_example_data(save_directory: Union[str, os.PathLike]) -> str:
 81    """Download the sample data for the 2d annotator.
 82
 83    This downloads part of a whole-slide image from the NeurIPS Cell Segmentation Challenge.
 84    See https://neurips22-cellseg.grand-challenge.org/ for details on the data.
 85
 86    Args:
 87        save_directory: Root folder to save the downloaded data.
 88
 89    Returns:
 90        The path of the downloaded image.
 91    """
 92    save_directory = Path(save_directory)
 93    os.makedirs(save_directory, exist_ok=True)
 94    print("Example data directory is:", save_directory.resolve())
 95    fname = "whole-slide-example-image.tif"
 96    pooch.retrieve(
 97        url="https://owncloud.gwdg.de/index.php/s/6ozPtgBmAAJC1di/download",
 98        known_hash="3ddb9c9dcc844429932ab951eb0743d5a1af83ee9b0ab54f06ceb2090a606d36",
 99        fname=fname,
100        path=save_directory,
101        progressbar=True,
102    )
103    return os.path.join(save_directory, fname)
104
105
106def sample_data_wholeslide():
107    """Provides wholeslide 2d example image to napari."""
108    # Return list of tuples
109    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
110    # Check the documentation for more information about the
111    # add_image_kwargs
112    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
113    base_data_directory = os.path.join(get_cache_directory(), "sample_data")
114    filename = fetch_wholeslide_example_data(base_data_directory)
115    data = imageio.imread(filename)
116    add_image_kwargs = {"name": "wholeslide"}
117    return [(data, add_image_kwargs)]
118
119
120def fetch_livecell_example_data(save_directory: Union[str, os.PathLike]) -> str:
121    """Download the sample data for the 2d annotator.
122
123    This downloads a single image from the LIVECell dataset.
124    See https://doi.org/10.1038/s41592-021-01249-6 for details on the data.
125
126    Args:
127        save_directory: Root folder to save the downloaded data.
128
129    Returns:
130        The path of the downloaded image.
131    """
132    save_directory = Path(save_directory)
133    os.makedirs(save_directory, exist_ok=True)
134    fname = "livecell-2d-image.png"
135    pooch.retrieve(
136        url="https://owncloud.gwdg.de/index.php/s/fSaOJIOYjmFBjPM/download",
137        known_hash="4f190983ea672fc333ac26d735d9625d5abb6e4a02bd4d32523127977a31e8fe",
138        fname=fname,
139        path=save_directory,
140        progressbar=True,
141    )
142    return os.path.join(save_directory, fname)
143
144
145def sample_data_livecell():
146    """Provides LIVECell 2d example image to napari."""
147    # Return list of tuples
148    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
149    # Check the documentation for more information about the
150    # add_image_kwargs
151    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
152    base_data_directory = os.path.join(get_cache_directory(), 'sample_data')
153    filename = fetch_livecell_example_data(base_data_directory)
154    data = imageio.imread(filename)
155    add_image_kwargs = {"name": "livecell"}
156    return [(data, add_image_kwargs)]
157
158
159def fetch_hela_2d_example_data(save_directory: Union[str, os.PathLike]) -> Union[str, os.PathLike]:
160    """Download the sample data for the 2d annotator.
161
162    This downloads a single image from the HeLa CTC dataset.
163
164    Args:
165        save_directory: Root folder to save the downloaded data.
166
167    Returns:
168        The path of the downloaded image.
169    """
170    save_directory = Path(save_directory)
171    os.makedirs(save_directory, exist_ok=True)
172    print("Example data directory is:", save_directory.resolve())
173    fname = "hela-2d-image.png"
174    pooch.retrieve(
175        url="https://owncloud.gwdg.de/index.php/s/2sr1DHQ34tV7WEb/download",
176        known_hash="908fa00e4b273610aa4e0a9c0f22dfa64a524970852f387908f3fa65238259c7",
177        fname=fname,
178        path=save_directory,
179        progressbar=True,
180    )
181    return os.path.join(save_directory, fname)
182
183
184def sample_data_hela_2d():
185    """Provides HeLa 2d example image to napari."""
186    # Return list of tuples
187    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
188    # Check the documentation for more information about the
189    # add_image_kwargs
190    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
191    base_data_directory = os.path.join(get_cache_directory(), "sample_data")
192    filename = fetch_hela_2d_example_data(base_data_directory)
193    data = imageio.imread(filename)
194    add_image_kwargs = {"name": "hela_2d"}
195    return [(data, add_image_kwargs)]
196
197
198def fetch_3d_example_data(save_directory: Union[str, os.PathLike]) -> str:
199    """Download the sample data for the 3d annotator.
200
201    This downloads the test volume of the Lucchi dataset for mitochondria segmentation in EM,
202    from https://www.epfl.ch/labs/cvlab/data/data-em/.
203
204    Args:
205        save_directory: Root folder to save the downloaded data.
206
207    Returns:
208        The path of the downloaded volume, a tif file.
209    """
210    save_directory = Path(save_directory)
211    os.makedirs(save_directory, exist_ok=True)
212    print("Example data directory is:", save_directory.resolve())
213    fname = "lucchi-test-volume.tif"
214    pooch.retrieve(
215        url="https://documents.epfl.ch/groups/c/cv/cvlab-unit/www/data/%20ElectronMicroscopy_Hippocampus/testing.tif",
216        known_hash="2450a904a804164f241144b7d7170eeb7e5299b0b56de1e24d5359a27d2aa35b",
217        fname=fname,
218        path=save_directory,
219        progressbar=True,
220    )
221    return str(save_directory.joinpath(fname))
222
223
224def sample_data_3d():
225    """Provides the Lucchi 3d example volume to napari."""
226    # Return list of tuples
227    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
228    # Check the documentation for more information about the
229    # add_image_kwargs
230    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
231    base_data_directory = os.path.join(get_cache_directory(), "sample_data")
232    data = imageio.imread(fetch_3d_example_data(base_data_directory))
233    add_image_kwargs = {"name": "lucchi"}
234    return [(data, add_image_kwargs)]
235
236
237def fetch_tracking_example_data(save_directory: Union[str, os.PathLike]) -> str:
238    """Download the sample data for the tracking annotator.
239
240    This data is the Cell Tracking Challenge dataset DIC-C2DH-HeLa.
241    Cell Tracking Challenge webpage: http://data.celltrackingchallenge.net
242    HeLa cells on a flat glass
243    Dr. G. van Cappellen. Erasmus Medical Center, Rotterdam, The Netherlands
244    Training dataset: http://data.celltrackingchallenge.net/training-datasets/DIC-C2DH-HeLa.zip (37 MB)
245    Challenge dataset: http://data.celltrackingchallenge.net/challenge-datasets/DIC-C2DH-HeLa.zip (41 MB)
246
247    Args:
248        save_directory: Root folder to save the downloaded data.
249
250    Returns:
251        The folder that contains the downloaded data.
252    """
253    save_directory = Path(save_directory)
254    os.makedirs(save_directory, exist_ok=True)
255    unpack_filenames = [os.path.join("DIC-C2DH-HeLa", "01", f"t{str(i).zfill(3)}.tif") for i in range(84)]
256    unpack = pooch.Unzip(members=unpack_filenames)
257    fname = "DIC-C2DH-HeLa.zip"
258    pooch.retrieve(
259        url="http://data.celltrackingchallenge.net/training-datasets/DIC-C2DH-HeLa.zip",  # 37 MB
260        known_hash="832fed2d05bb7488cf9c51a2994b75f8f3f53b3c3098856211f2d39023c34e1a",
261        fname=fname,
262        path=save_directory,
263        progressbar=True,
264        processor=unpack,
265    )
266    cell_tracking_dir = save_directory.joinpath(f"{fname}.unzip", "DIC-C2DH-HeLa", "01")
267    assert os.path.exists(cell_tracking_dir)
268    return str(cell_tracking_dir)
269
270
271def sample_data_tracking():
272    """Provides tracking example dataset to napari."""
273    # Return list of tuples
274    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
275    # Check the documentation for more information about the
276    # add_image_kwargs
277    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
278    base_data_directory = os.path.join(get_cache_directory(), 'sample_data')
279    data_directory = fetch_tracking_example_data(base_data_directory)
280    fnames = os.listdir(data_directory)
281    full_filenames = [os.path.join(data_directory, f) for f in fnames]
282    full_filenames.sort()
283    data = np.stack([imageio.imread(f) for f in full_filenames], axis=0)
284    add_image_kwargs = {"name": "tracking"}
285    return [(data, add_image_kwargs)]
286
287
288def fetch_tracking_segmentation_data(save_directory: Union[str, os.PathLike]) -> str:
289    """Download groundtruth segmentation for the tracking example data.
290
291    This downloads the groundtruth segmentation for the image data from `fetch_tracking_example_data`.
292
293    Args:
294        save_directory: Root folder to save the downloaded data.
295
296    Returns:
297        The folder that contains the downloaded data.
298    """
299    save_directory = Path(save_directory)
300    os.makedirs(save_directory, exist_ok=True)
301    print("Example data directory is:", save_directory.resolve())
302    unpack_filenames = [os.path.join("masks", f"mask_{str(i).zfill(4)}.tif") for i in range(84)]
303    unpack = pooch.Unzip(members=unpack_filenames)
304    fname = "hela-ctc-01-gt.zip"
305    pooch.retrieve(
306        url="https://owncloud.gwdg.de/index.php/s/AWxQMblxwR99OjC/download",
307        known_hash="c0644d8ebe1390fb60125560ba15aa2342caf44f50ff0667a0318ea0ac6c958b",
308        fname=fname,
309        path=save_directory,
310        progressbar=True,
311        processor=unpack,
312    )
313    cell_tracking_dir = save_directory.joinpath(f"{fname}.unzip", "masks")
314    assert os.path.exists(cell_tracking_dir)
315    return str(cell_tracking_dir)
316
317
318def sample_data_segmentation():
319    """Provides segmentation example dataset to napari."""
320    # Return list of tuples
321    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
322    # Check the documentation for more information about the
323    # add_image_kwargs
324    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
325    base_data_directory = os.path.join(get_cache_directory(), 'sample_data')
326    data_directory = fetch_tracking_segmentation_data(base_data_directory)
327    fnames = os.listdir(data_directory)
328    full_filenames = [os.path.join(data_directory, f) for f in fnames]
329    full_filenames.sort()
330    data = np.stack([imageio.imread(f) for f in full_filenames], axis=0)
331    add_image_kwargs = {"name": "segmentation"}
332    return [(data, add_image_kwargs)]
333
334
335def synthetic_data(shape, seed=None):
336    """Create synthetic image data and segmentation for training."""
337    ndim = len(shape)
338    assert ndim in (2, 3)
339    image_shape = shape if ndim == 2 else shape[1:]
340    image = binary_blobs(length=image_shape[0], blob_size_fraction=0.05, volume_fraction=0.15, rng=seed)
341
342    if image_shape[1] != image_shape[0]:
343        image = resize(image, image_shape, order=0, anti_aliasing=False, preserve_range=True).astype(image.dtype)
344    if ndim == 3:
345        nz = shape[0]
346        image = np.stack([image] * nz)
347
348    segmentation = label(image)
349    image = image.astype("uint8") * 255
350    return image, segmentation
351
352
353def fetch_nucleus_3d_example_data(save_directory: Union[str, os.PathLike]) -> str:
354    """Download the sample data for 3d segmentation of nuclei.
355
356    This data contains a small crop from a volume from the publication
357    "Efficient automatic 3D segmentation of cell nuclei for high-content screening"
358    https://doi.org/10.1186/s12859-022-04737-4
359
360    Args:
361        save_directory: Root folder to save the downloaded data.
362
363    Returns:
364        The path of the downloaded image.
365    """
366    save_directory = Path(save_directory)
367    os.makedirs(save_directory, exist_ok=True)
368    print("Example data directory is:", save_directory.resolve())
369    fname = "3d-nucleus-data.tif"
370    pooch.retrieve(
371        url="https://owncloud.gwdg.de/index.php/s/eW0uNCo8gedzWU4/download",
372        known_hash="4946896f747dc1c3fc82fb2e1320226d92f99d22be88ea5f9c37e3ba4e281205",
373        fname=fname,
374        path=save_directory,
375        progressbar=True,
376    )
377    return os.path.join(save_directory, fname)
378
379
380def fetch_wholeslide_histopathology_example_data(save_directory: Union[str, os.PathLike]) -> str:
381    """Download the sample histpathology data for the 2d annotator.
382
383    This downloads a WSI image from https://openslide.cs.cmu.edu/download/openslide-testdata/,
384    under the "CC0 1.0 Universal" license.
385
386    Args:
387        save_directory: Root folder to save the downloaded data.
388
389    Returns:
390        The path of the downloaded image.
391    """
392    save_directory = Path(save_directory)
393    os.makedirs(save_directory, exist_ok=True)
394    print("Example data directory is:", save_directory.resolve())
395    fname = "whole-slide-histopathology-example-image.svs"
396    pooch.retrieve(
397        url="https://openslide.cs.cmu.edu/download/openslide-testdata/Aperio/CMU-1.svs",
398        known_hash="00a3d54482cd707abf254fe69dccc8d06b8ff757a1663f1290c23418c480eb30",
399        fname=fname,
400        path=save_directory,
401        progressbar=True,
402    )
403    return os.path.join(save_directory, fname)
def fetch_image_series_example_data(save_directory: Union[str, os.PathLike]) -> str:
30def fetch_image_series_example_data(save_directory: Union[str, os.PathLike]) -> str:
31    """Download the sample images for the image series annotator.
32
33    Args:
34        save_directory: Root folder to save the downloaded data.
35
36    Returns:
37        The folder that contains the downloaded data.
38    """
39    # This sample dataset is currently not provided to napari by the micro-sam
40    # plugin, because images are not all the same shape and cannot be combined
41    # into a single layer
42    save_directory = Path(save_directory)
43    os.makedirs(save_directory, exist_ok=True)
44    print("Example data directory is:", save_directory.resolve())
45    fname = "image-series.zip"
46    unpack_filenames = [os.path.join("series", f"im{i}.tif") for i in range(3)]
47    unpack = pooch.Unzip(members=unpack_filenames)
48    pooch.retrieve(
49        url="https://owncloud.gwdg.de/index.php/s/M1zGnfkulWoAhUG/download",
50        known_hash="92346ca9770bcaf55248efee590718d54c7135b6ebca15d669f3b77b6afc8706",
51        fname=fname,
52        path=save_directory,
53        progressbar=True,
54        processor=unpack,
55    )
56    data_folder = os.path.join(save_directory, f"{fname}.unzip", "series")
57    assert os.path.exists(data_folder)
58    return data_folder

Download the sample images for the image series annotator.

Arguments:
  • save_directory: Root folder to save the downloaded data.
Returns:

The folder that contains the downloaded data.

def sample_data_image_series():
61def sample_data_image_series():
62    """Provides image series example image to napari.
63
64    Opens as three separate image layers in napari (one per image in series).
65    The third image in the series has a different size and modality.
66    """
67    # Return list of tuples
68    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
69    # Check the documentation for more information about the
70    # add_image_kwargs
71    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
72    base_data_directory = os.path.join(get_cache_directory(), "sample_data")
73    data_directory = fetch_image_series_example_data(base_data_directory)
74    fnames = os.listdir(data_directory)
75    full_filenames = [os.path.join(data_directory, f) for f in fnames]
76    full_filenames.sort()
77    data_and_image_kwargs = [(imageio.imread(f), {"name": f"img-{i}"}) for i, f in enumerate(full_filenames)]
78    return data_and_image_kwargs

Provides image series example image to napari.

Opens as three separate image layers in napari (one per image in series). The third image in the series has a different size and modality.

def fetch_wholeslide_example_data(save_directory: Union[str, os.PathLike]) -> str:
 81def fetch_wholeslide_example_data(save_directory: Union[str, os.PathLike]) -> str:
 82    """Download the sample data for the 2d annotator.
 83
 84    This downloads part of a whole-slide image from the NeurIPS Cell Segmentation Challenge.
 85    See https://neurips22-cellseg.grand-challenge.org/ for details on the data.
 86
 87    Args:
 88        save_directory: Root folder to save the downloaded data.
 89
 90    Returns:
 91        The path of the downloaded image.
 92    """
 93    save_directory = Path(save_directory)
 94    os.makedirs(save_directory, exist_ok=True)
 95    print("Example data directory is:", save_directory.resolve())
 96    fname = "whole-slide-example-image.tif"
 97    pooch.retrieve(
 98        url="https://owncloud.gwdg.de/index.php/s/6ozPtgBmAAJC1di/download",
 99        known_hash="3ddb9c9dcc844429932ab951eb0743d5a1af83ee9b0ab54f06ceb2090a606d36",
100        fname=fname,
101        path=save_directory,
102        progressbar=True,
103    )
104    return os.path.join(save_directory, fname)

Download the sample data for the 2d annotator.

This downloads part of a whole-slide image from the NeurIPS Cell Segmentation Challenge. See https://neurips22-cellseg.grand-challenge.org/ for details on the data.

Arguments:
  • save_directory: Root folder to save the downloaded data.
Returns:

The path of the downloaded image.

def sample_data_wholeslide():
107def sample_data_wholeslide():
108    """Provides wholeslide 2d example image to napari."""
109    # Return list of tuples
110    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
111    # Check the documentation for more information about the
112    # add_image_kwargs
113    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
114    base_data_directory = os.path.join(get_cache_directory(), "sample_data")
115    filename = fetch_wholeslide_example_data(base_data_directory)
116    data = imageio.imread(filename)
117    add_image_kwargs = {"name": "wholeslide"}
118    return [(data, add_image_kwargs)]

Provides wholeslide 2d example image to napari.

def fetch_livecell_example_data(save_directory: Union[str, os.PathLike]) -> str:
121def fetch_livecell_example_data(save_directory: Union[str, os.PathLike]) -> str:
122    """Download the sample data for the 2d annotator.
123
124    This downloads a single image from the LIVECell dataset.
125    See https://doi.org/10.1038/s41592-021-01249-6 for details on the data.
126
127    Args:
128        save_directory: Root folder to save the downloaded data.
129
130    Returns:
131        The path of the downloaded image.
132    """
133    save_directory = Path(save_directory)
134    os.makedirs(save_directory, exist_ok=True)
135    fname = "livecell-2d-image.png"
136    pooch.retrieve(
137        url="https://owncloud.gwdg.de/index.php/s/fSaOJIOYjmFBjPM/download",
138        known_hash="4f190983ea672fc333ac26d735d9625d5abb6e4a02bd4d32523127977a31e8fe",
139        fname=fname,
140        path=save_directory,
141        progressbar=True,
142    )
143    return os.path.join(save_directory, fname)

Download the sample data for the 2d annotator.

This downloads a single image from the LIVECell dataset. See https://doi.org/10.1038/s41592-021-01249-6 for details on the data.

Arguments:
  • save_directory: Root folder to save the downloaded data.
Returns:

The path of the downloaded image.

def sample_data_livecell():
146def sample_data_livecell():
147    """Provides LIVECell 2d example image to napari."""
148    # Return list of tuples
149    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
150    # Check the documentation for more information about the
151    # add_image_kwargs
152    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
153    base_data_directory = os.path.join(get_cache_directory(), 'sample_data')
154    filename = fetch_livecell_example_data(base_data_directory)
155    data = imageio.imread(filename)
156    add_image_kwargs = {"name": "livecell"}
157    return [(data, add_image_kwargs)]

Provides LIVECell 2d example image to napari.

def fetch_hela_2d_example_data(save_directory: Union[str, os.PathLike]) -> Union[str, os.PathLike]:
160def fetch_hela_2d_example_data(save_directory: Union[str, os.PathLike]) -> Union[str, os.PathLike]:
161    """Download the sample data for the 2d annotator.
162
163    This downloads a single image from the HeLa CTC dataset.
164
165    Args:
166        save_directory: Root folder to save the downloaded data.
167
168    Returns:
169        The path of the downloaded image.
170    """
171    save_directory = Path(save_directory)
172    os.makedirs(save_directory, exist_ok=True)
173    print("Example data directory is:", save_directory.resolve())
174    fname = "hela-2d-image.png"
175    pooch.retrieve(
176        url="https://owncloud.gwdg.de/index.php/s/2sr1DHQ34tV7WEb/download",
177        known_hash="908fa00e4b273610aa4e0a9c0f22dfa64a524970852f387908f3fa65238259c7",
178        fname=fname,
179        path=save_directory,
180        progressbar=True,
181    )
182    return os.path.join(save_directory, fname)

Download the sample data for the 2d annotator.

This downloads a single image from the HeLa CTC dataset.

Arguments:
  • save_directory: Root folder to save the downloaded data.
Returns:

The path of the downloaded image.

def sample_data_hela_2d():
185def sample_data_hela_2d():
186    """Provides HeLa 2d example image to napari."""
187    # Return list of tuples
188    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
189    # Check the documentation for more information about the
190    # add_image_kwargs
191    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
192    base_data_directory = os.path.join(get_cache_directory(), "sample_data")
193    filename = fetch_hela_2d_example_data(base_data_directory)
194    data = imageio.imread(filename)
195    add_image_kwargs = {"name": "hela_2d"}
196    return [(data, add_image_kwargs)]

Provides HeLa 2d example image to napari.

def fetch_3d_example_data(save_directory: Union[str, os.PathLike]) -> str:
199def fetch_3d_example_data(save_directory: Union[str, os.PathLike]) -> str:
200    """Download the sample data for the 3d annotator.
201
202    This downloads the test volume of the Lucchi dataset for mitochondria segmentation in EM,
203    from https://www.epfl.ch/labs/cvlab/data/data-em/.
204
205    Args:
206        save_directory: Root folder to save the downloaded data.
207
208    Returns:
209        The path of the downloaded volume, a tif file.
210    """
211    save_directory = Path(save_directory)
212    os.makedirs(save_directory, exist_ok=True)
213    print("Example data directory is:", save_directory.resolve())
214    fname = "lucchi-test-volume.tif"
215    pooch.retrieve(
216        url="https://documents.epfl.ch/groups/c/cv/cvlab-unit/www/data/%20ElectronMicroscopy_Hippocampus/testing.tif",
217        known_hash="2450a904a804164f241144b7d7170eeb7e5299b0b56de1e24d5359a27d2aa35b",
218        fname=fname,
219        path=save_directory,
220        progressbar=True,
221    )
222    return str(save_directory.joinpath(fname))

Download the sample data for the 3d annotator.

This downloads the test volume of the Lucchi dataset for mitochondria segmentation in EM, from https://www.epfl.ch/labs/cvlab/data/data-em/.

Arguments:
  • save_directory: Root folder to save the downloaded data.
Returns:

The path of the downloaded volume, a tif file.

def sample_data_3d():
225def sample_data_3d():
226    """Provides the Lucchi 3d example volume to napari."""
227    # Return list of tuples
228    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
229    # Check the documentation for more information about the
230    # add_image_kwargs
231    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
232    base_data_directory = os.path.join(get_cache_directory(), "sample_data")
233    data = imageio.imread(fetch_3d_example_data(base_data_directory))
234    add_image_kwargs = {"name": "lucchi"}
235    return [(data, add_image_kwargs)]

Provides the Lucchi 3d example volume to napari.

def fetch_tracking_example_data(save_directory: Union[str, os.PathLike]) -> str:
238def fetch_tracking_example_data(save_directory: Union[str, os.PathLike]) -> str:
239    """Download the sample data for the tracking annotator.
240
241    This data is the Cell Tracking Challenge dataset DIC-C2DH-HeLa.
242    Cell Tracking Challenge webpage: http://data.celltrackingchallenge.net
243    HeLa cells on a flat glass
244    Dr. G. van Cappellen. Erasmus Medical Center, Rotterdam, The Netherlands
245    Training dataset: http://data.celltrackingchallenge.net/training-datasets/DIC-C2DH-HeLa.zip (37 MB)
246    Challenge dataset: http://data.celltrackingchallenge.net/challenge-datasets/DIC-C2DH-HeLa.zip (41 MB)
247
248    Args:
249        save_directory: Root folder to save the downloaded data.
250
251    Returns:
252        The folder that contains the downloaded data.
253    """
254    save_directory = Path(save_directory)
255    os.makedirs(save_directory, exist_ok=True)
256    unpack_filenames = [os.path.join("DIC-C2DH-HeLa", "01", f"t{str(i).zfill(3)}.tif") for i in range(84)]
257    unpack = pooch.Unzip(members=unpack_filenames)
258    fname = "DIC-C2DH-HeLa.zip"
259    pooch.retrieve(
260        url="http://data.celltrackingchallenge.net/training-datasets/DIC-C2DH-HeLa.zip",  # 37 MB
261        known_hash="832fed2d05bb7488cf9c51a2994b75f8f3f53b3c3098856211f2d39023c34e1a",
262        fname=fname,
263        path=save_directory,
264        progressbar=True,
265        processor=unpack,
266    )
267    cell_tracking_dir = save_directory.joinpath(f"{fname}.unzip", "DIC-C2DH-HeLa", "01")
268    assert os.path.exists(cell_tracking_dir)
269    return str(cell_tracking_dir)

Download the sample data for the tracking annotator.

This data is the Cell Tracking Challenge dataset DIC-C2DH-HeLa. Cell Tracking Challenge webpage: http://data.celltrackingchallenge.net HeLa cells on a flat glass Dr. G. van Cappellen. Erasmus Medical Center, Rotterdam, The Netherlands Training dataset: http://data.celltrackingchallenge.net/training-datasets/DIC-C2DH-HeLa.zip (37 MB) Challenge dataset: http://data.celltrackingchallenge.net/challenge-datasets/DIC-C2DH-HeLa.zip (41 MB)

Arguments:
  • save_directory: Root folder to save the downloaded data.
Returns:

The folder that contains the downloaded data.

def sample_data_tracking():
272def sample_data_tracking():
273    """Provides tracking example dataset to napari."""
274    # Return list of tuples
275    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
276    # Check the documentation for more information about the
277    # add_image_kwargs
278    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
279    base_data_directory = os.path.join(get_cache_directory(), 'sample_data')
280    data_directory = fetch_tracking_example_data(base_data_directory)
281    fnames = os.listdir(data_directory)
282    full_filenames = [os.path.join(data_directory, f) for f in fnames]
283    full_filenames.sort()
284    data = np.stack([imageio.imread(f) for f in full_filenames], axis=0)
285    add_image_kwargs = {"name": "tracking"}
286    return [(data, add_image_kwargs)]

Provides tracking example dataset to napari.

def fetch_tracking_segmentation_data(save_directory: Union[str, os.PathLike]) -> str:
289def fetch_tracking_segmentation_data(save_directory: Union[str, os.PathLike]) -> str:
290    """Download groundtruth segmentation for the tracking example data.
291
292    This downloads the groundtruth segmentation for the image data from `fetch_tracking_example_data`.
293
294    Args:
295        save_directory: Root folder to save the downloaded data.
296
297    Returns:
298        The folder that contains the downloaded data.
299    """
300    save_directory = Path(save_directory)
301    os.makedirs(save_directory, exist_ok=True)
302    print("Example data directory is:", save_directory.resolve())
303    unpack_filenames = [os.path.join("masks", f"mask_{str(i).zfill(4)}.tif") for i in range(84)]
304    unpack = pooch.Unzip(members=unpack_filenames)
305    fname = "hela-ctc-01-gt.zip"
306    pooch.retrieve(
307        url="https://owncloud.gwdg.de/index.php/s/AWxQMblxwR99OjC/download",
308        known_hash="c0644d8ebe1390fb60125560ba15aa2342caf44f50ff0667a0318ea0ac6c958b",
309        fname=fname,
310        path=save_directory,
311        progressbar=True,
312        processor=unpack,
313    )
314    cell_tracking_dir = save_directory.joinpath(f"{fname}.unzip", "masks")
315    assert os.path.exists(cell_tracking_dir)
316    return str(cell_tracking_dir)

Download groundtruth segmentation for the tracking example data.

This downloads the groundtruth segmentation for the image data from fetch_tracking_example_data.

Arguments:
  • save_directory: Root folder to save the downloaded data.
Returns:

The folder that contains the downloaded data.

def sample_data_segmentation():
319def sample_data_segmentation():
320    """Provides segmentation example dataset to napari."""
321    # Return list of tuples
322    # [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
323    # Check the documentation for more information about the
324    # add_image_kwargs
325    # https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
326    base_data_directory = os.path.join(get_cache_directory(), 'sample_data')
327    data_directory = fetch_tracking_segmentation_data(base_data_directory)
328    fnames = os.listdir(data_directory)
329    full_filenames = [os.path.join(data_directory, f) for f in fnames]
330    full_filenames.sort()
331    data = np.stack([imageio.imread(f) for f in full_filenames], axis=0)
332    add_image_kwargs = {"name": "segmentation"}
333    return [(data, add_image_kwargs)]

Provides segmentation example dataset to napari.

def synthetic_data(shape, seed=None):
336def synthetic_data(shape, seed=None):
337    """Create synthetic image data and segmentation for training."""
338    ndim = len(shape)
339    assert ndim in (2, 3)
340    image_shape = shape if ndim == 2 else shape[1:]
341    image = binary_blobs(length=image_shape[0], blob_size_fraction=0.05, volume_fraction=0.15, rng=seed)
342
343    if image_shape[1] != image_shape[0]:
344        image = resize(image, image_shape, order=0, anti_aliasing=False, preserve_range=True).astype(image.dtype)
345    if ndim == 3:
346        nz = shape[0]
347        image = np.stack([image] * nz)
348
349    segmentation = label(image)
350    image = image.astype("uint8") * 255
351    return image, segmentation

Create synthetic image data and segmentation for training.

def fetch_nucleus_3d_example_data(save_directory: Union[str, os.PathLike]) -> str:
354def fetch_nucleus_3d_example_data(save_directory: Union[str, os.PathLike]) -> str:
355    """Download the sample data for 3d segmentation of nuclei.
356
357    This data contains a small crop from a volume from the publication
358    "Efficient automatic 3D segmentation of cell nuclei for high-content screening"
359    https://doi.org/10.1186/s12859-022-04737-4
360
361    Args:
362        save_directory: Root folder to save the downloaded data.
363
364    Returns:
365        The path of the downloaded image.
366    """
367    save_directory = Path(save_directory)
368    os.makedirs(save_directory, exist_ok=True)
369    print("Example data directory is:", save_directory.resolve())
370    fname = "3d-nucleus-data.tif"
371    pooch.retrieve(
372        url="https://owncloud.gwdg.de/index.php/s/eW0uNCo8gedzWU4/download",
373        known_hash="4946896f747dc1c3fc82fb2e1320226d92f99d22be88ea5f9c37e3ba4e281205",
374        fname=fname,
375        path=save_directory,
376        progressbar=True,
377    )
378    return os.path.join(save_directory, fname)

Download the sample data for 3d segmentation of nuclei.

This data contains a small crop from a volume from the publication "Efficient automatic 3D segmentation of cell nuclei for high-content screening" https://doi.org/10.1186/s12859-022-04737-4

Arguments:
  • save_directory: Root folder to save the downloaded data.
Returns:

The path of the downloaded image.

def fetch_wholeslide_histopathology_example_data(save_directory: Union[str, os.PathLike]) -> str:
381def fetch_wholeslide_histopathology_example_data(save_directory: Union[str, os.PathLike]) -> str:
382    """Download the sample histpathology data for the 2d annotator.
383
384    This downloads a WSI image from https://openslide.cs.cmu.edu/download/openslide-testdata/,
385    under the "CC0 1.0 Universal" license.
386
387    Args:
388        save_directory: Root folder to save the downloaded data.
389
390    Returns:
391        The path of the downloaded image.
392    """
393    save_directory = Path(save_directory)
394    os.makedirs(save_directory, exist_ok=True)
395    print("Example data directory is:", save_directory.resolve())
396    fname = "whole-slide-histopathology-example-image.svs"
397    pooch.retrieve(
398        url="https://openslide.cs.cmu.edu/download/openslide-testdata/Aperio/CMU-1.svs",
399        known_hash="00a3d54482cd707abf254fe69dccc8d06b8ff757a1663f1290c23418c480eb30",
400        fname=fname,
401        path=save_directory,
402        progressbar=True,
403    )
404    return os.path.join(save_directory, fname)

Download the sample histpathology data for the 2d annotator.

This downloads a WSI image from https://openslide.cs.cmu.edu/download/openslide-testdata/, under the "CC0 1.0 Universal" license.

Arguments:
  • save_directory: Root folder to save the downloaded data.
Returns:

The path of the downloaded image.