synapse_net.sample_data

 1import os
 2import tempfile
 3import pooch
 4
 5from .file_utils import read_mrc, get_cache_dir
 6
 7
 8def get_sample_data(name: str) -> str:
 9    """Get the filepath to SynapseNet sample data, stored as mrc file.
10
11    Args:
12        name: The name of the sample data. Currently, we only provide 'tem_2d' and 'tem_tomo'.
13
14    Returns:
15        The filepath to the downloaded sample data.
16    """
17    registry = {
18        "tem_2d.mrc": "3c6f9ff6d7673d9bf2fd46c09750c3c7dbb8fa1aa59dcdb3363b65cc774dcf28",
19        "tem_tomo.mrc": "fe862ce7c22000d4440e3aa717ca9920b42260f691e5b2ab64cd61c928693c99",
20    }
21    urls = {
22        "tem_2d.mrc": "https://owncloud.gwdg.de/index.php/s/5sAQ0U4puAspcHg/download",
23        "tem_tomo.mrc": "https://owncloud.gwdg.de/index.php/s/FJDhDfbT4UxhtOn/download",
24    }
25    key = f"{name}.mrc"
26
27    if key not in registry:
28        valid_names = [k[:-4] for k in registry.keys()]
29        raise ValueError(f"Invalid sample name {name}, please choose one of {valid_names}.")
30
31    cache_dir = get_cache_dir()
32    data_registry = pooch.create(
33        path=os.path.join(cache_dir, "sample_data"),
34        base_url="",
35        registry=registry,
36        urls=urls,
37    )
38    file_path = data_registry.fetch(key)
39    return file_path
40
41
42def _sample_data(name):
43    file_path = get_sample_data(name)
44    data, voxel_size = read_mrc(file_path)
45    metadata = {"file_path": file_path, "voxel_size": voxel_size}
46    add_image_kwargs = {"name": name, "metadata": metadata, "colormap": "gray"}
47    return [(data, add_image_kwargs)]
48
49
50def sample_data_tem_2d():
51    return _sample_data("tem_2d")
52
53
54def sample_data_tem_tomo():
55    return _sample_data("tem_tomo")
56
57
58def download_data_from_zenodo(path: str, name: str):
59    """Download data uploaded for the SynapseNet manuscript from zenodo.
60
61    Args:
62        path: The path where the downloaded data will be saved.
63        name: The name of the zenodi dataset.
64    """
65    from torch_em.data.datasets.util import download_source, unzip
66
67    urls = {
68        "2d_tem": "https://zenodo.org/records`/14236382/files/tem_2d.zip?download=1",
69        "inner_ear_ribbon_synapse": "https://zenodo.org/records/14232607/files/inner-ear-ribbon-synapse-tomgrams.zip?download=1",  # noqa
70        "training_data": "https://zenodo.org/records/14330011/files/synapse-net.zip?download=1"
71    }
72    assert name in urls
73    url = urls[name]
74
75    # May need to adapt this for other datasets.
76    # Check if the download already exists.
77    dl_path = path
78    if os.path.exists(dl_path):
79        return
80
81    with tempfile.TemporaryDirectory() as tmp:
82        tmp_path = os.path.join(tmp, f"{name}.zip")
83        download_source(tmp_path, url, download=True, checksum=None)
84        unzip(tmp_path, path, remove=False)
def get_sample_data(name: str) -> str:
 9def get_sample_data(name: str) -> str:
10    """Get the filepath to SynapseNet sample data, stored as mrc file.
11
12    Args:
13        name: The name of the sample data. Currently, we only provide 'tem_2d' and 'tem_tomo'.
14
15    Returns:
16        The filepath to the downloaded sample data.
17    """
18    registry = {
19        "tem_2d.mrc": "3c6f9ff6d7673d9bf2fd46c09750c3c7dbb8fa1aa59dcdb3363b65cc774dcf28",
20        "tem_tomo.mrc": "fe862ce7c22000d4440e3aa717ca9920b42260f691e5b2ab64cd61c928693c99",
21    }
22    urls = {
23        "tem_2d.mrc": "https://owncloud.gwdg.de/index.php/s/5sAQ0U4puAspcHg/download",
24        "tem_tomo.mrc": "https://owncloud.gwdg.de/index.php/s/FJDhDfbT4UxhtOn/download",
25    }
26    key = f"{name}.mrc"
27
28    if key not in registry:
29        valid_names = [k[:-4] for k in registry.keys()]
30        raise ValueError(f"Invalid sample name {name}, please choose one of {valid_names}.")
31
32    cache_dir = get_cache_dir()
33    data_registry = pooch.create(
34        path=os.path.join(cache_dir, "sample_data"),
35        base_url="",
36        registry=registry,
37        urls=urls,
38    )
39    file_path = data_registry.fetch(key)
40    return file_path

Get the filepath to SynapseNet sample data, stored as mrc file.

Arguments:
  • name: The name of the sample data. Currently, we only provide 'tem_2d' and 'tem_tomo'.
Returns:

The filepath to the downloaded sample data.

def sample_data_tem_2d():
51def sample_data_tem_2d():
52    return _sample_data("tem_2d")
def sample_data_tem_tomo():
55def sample_data_tem_tomo():
56    return _sample_data("tem_tomo")
def download_data_from_zenodo(path: str, name: str):
59def download_data_from_zenodo(path: str, name: str):
60    """Download data uploaded for the SynapseNet manuscript from zenodo.
61
62    Args:
63        path: The path where the downloaded data will be saved.
64        name: The name of the zenodi dataset.
65    """
66    from torch_em.data.datasets.util import download_source, unzip
67
68    urls = {
69        "2d_tem": "https://zenodo.org/records`/14236382/files/tem_2d.zip?download=1",
70        "inner_ear_ribbon_synapse": "https://zenodo.org/records/14232607/files/inner-ear-ribbon-synapse-tomgrams.zip?download=1",  # noqa
71        "training_data": "https://zenodo.org/records/14330011/files/synapse-net.zip?download=1"
72    }
73    assert name in urls
74    url = urls[name]
75
76    # May need to adapt this for other datasets.
77    # Check if the download already exists.
78    dl_path = path
79    if os.path.exists(dl_path):
80        return
81
82    with tempfile.TemporaryDirectory() as tmp:
83        tmp_path = os.path.join(tmp, f"{name}.zip")
84        download_source(tmp_path, url, download=True, checksum=None)
85        unzip(tmp_path, path, remove=False)

Download data uploaded for the SynapseNet manuscript from zenodo.

Arguments:
  • path: The path where the downloaded data will be saved.
  • name: The name of the zenodi dataset.