Skip to content

Commit 2bfd5da

Browse files
Add sample data script and update the annotator examples
1 parent 9809835 commit 2bfd5da

File tree

4 files changed

+164
-72
lines changed

4 files changed

+164
-72
lines changed

examples/sam_annotator_2d.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
import imageio.v3 as imageio
22
from micro_sam.sam_annotator import annotator_2d
3+
from micro_sam.sample_data import fetch_hela_2d_example_data, fetch_livecell_example_data, fetch_wholeslide_example_data
34

45

5-
# TODO describe how to get the data and don't use hard-coded path
66
def livecell_annotator():
7-
im = imageio.imread(
8-
"/home/pape/Work/data/incu_cyte/livecell/images/livecell_test_images/A172_Phase_C7_1_01d04h00m_4.tif"
9-
)
10-
embedding_path = "./embeddings/embeddings-livecell_cropped.zarr"
11-
annotator_2d(im, embedding_path, show_embeddings=False)
7+
"""Run the 2d annotator for an example image from the LiveCELL dataset.
8+
9+
See https://doi.org/10.1038/s41592-021-01249-6 for details on the data.
10+
"""
11+
example_data = fetch_livecell_example_data("./data")
12+
image = imageio.imread(example_data)
13+
embedding_path = "./embeddings/embeddings-livecell.zarr"
14+
annotator_2d(image, embedding_path, show_embeddings=False)
1215

1316

14-
# This runs interactive 2d annotation for data from the cell tracking challenge:
15-
# It uses the training data for the HeLA dataset. You can download the data from
16-
# http://data.celltrackingchallenge.net/training-datasets/DIC-C2DH-HeLa.zip
1717
def hela_2d_annotator():
18-
im = imageio.imread("./data/DIC-C2DH-HeLa/train/01/t011.tif")
18+
"""Run the 2d annotator for an example image form the cell tracking challenge HeLa 2d dataset.
19+
"""
20+
example_data = fetch_hela_2d_example_data("./data")
21+
image = imageio.imread(example_data)
1922
embedding_path = "./embeddings/embeddings-hela2d.zarr"
20-
annotator_2d(im, embedding_path, show_embeddings=False)
23+
annotator_2d(image, embedding_path, show_embeddings=False)
24+
25+
26+
def wholeslide_annotator():
27+
"""Run the 2d annotator with tiling for an example whole-slide image from the
28+
NeuRIPS cell segmentation challenge.
29+
30+
See https://neurips22-cellseg.grand-challenge.org/ for details on the data.
31+
"""
32+
example_data = fetch_wholeslide_example_data("./data")
33+
image = imageio.imread(example_data)
34+
embedding_path = "./embeddings/whole-slide-embeddings.zarr"
35+
annotator_2d(image, embedding_path, tile_shape=(1024, 1024), halo=(256, 256))
2136

2237

2338
def main():
@@ -27,6 +42,9 @@ def main():
2742
# 2d annotator for cell tracking challenge hela data
2843
hela_2d_annotator()
2944

45+
# 2d annotator for a whole slide image
46+
# wholeslide_annotator()
47+
3048

3149
if __name__ == "__main__":
3250
main()

examples/sam_annotator_3d.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,22 @@
1-
import os
2-
from pathlib import Path
3-
41
from elf.io import open_file
5-
import pooch
62
from micro_sam.sam_annotator import annotator_3d
3+
from micro_sam.sample_data import fetch_3d_example_data
74

85

9-
def main():
10-
example_data_directory = "./data"
11-
with open_file(str(fetch_example_data(example_data_directory))) as f:
6+
def em_3d_annotator():
7+
"""Run the 3d annotator for an example EM volume."""
8+
# download the example data
9+
example_data = fetch_3d_example_data("./data")
10+
# load the example data (load the sequence of tif files as 3d volume)
11+
with open_file(example_data) as f:
1212
raw = f["*.png"][:]
13+
# start the annotator, cache the embeddings
1314
embedding_path = "./embeddings/embeddings-lucchi.zarr"
1415
annotator_3d(raw, embedding_path, show_embeddings=False)
1516

1617

17-
def fetch_example_data(save_directory):
18-
# Lucchi++ Data from: https://casser.io/connectomics/
19-
save_directory = Path(save_directory)
20-
if not save_directory.exists():
21-
os.makedirs(save_directory)
22-
print("Created new folder for example data downloads.")
23-
print("Example data directory is:", save_directory.resolve())
24-
unpack_filenames = [os.path.join("Lucchi++", "Test_In", f"mask{str(i).zfill(4)}.png") for i in range(165)]
25-
unpack = pooch.Unzip(members=unpack_filenames)
26-
fnames = pooch.retrieve(
27-
url="http://www.casser.io/files/lucchi_pp.zip",
28-
known_hash="770ce9e98fc6f29c1b1a250c637e6c5125f2b5f1260e5a7687b55a79e2e8844d",
29-
fname="lucchi_pp.zip",
30-
path=save_directory,
31-
progressbar=True,
32-
processor=unpack,
33-
)
34-
lucchi_testin_dir = save_directory.joinpath("lucchi_pp.zip.unzip", "Lucchi++", "Test_In")
35-
return lucchi_testin_dir
18+
def main():
19+
em_3d_annotator()
3620

3721

3822
if __name__ == "__main__":

examples/sam_annotator_tracking.py

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,23 @@
1-
import os
2-
from pathlib import Path
3-
41
from elf.io import open_file
5-
import pooch
62
from micro_sam.sam_annotator import annotator_tracking
3+
from micro_sam.sample_data import fetch_tracking_example_data
74

85

96
def track_ctc_data():
10-
example_data_directory = "./data"
11-
with open_file(str(fetch_example_data(example_data_directory)), mode="r") as f:
12-
timeseries = f["*.tif"]
7+
"""Run interactive tracking for data from the cell tracking challenge.
8+
"""
9+
# download the example data
10+
example_data = fetch_tracking_example_data("./data")
11+
# load the example data (load the sequence of tif files as timeseries)
12+
with open_file(example_data, mode="r") as f:
13+
timeseries = f["*.tif"][:]
14+
# start the annotator with cached embeddings
1315
annotator_tracking(timeseries, embedding_path="./embeddings/embeddings-ctc.zarr", show_embeddings=False)
1416

1517

16-
def fetch_example_data(save_directory):
17-
"""Cell tracking challenge dataset DIC-C2DH-HeLa.
18-
19-
Cell tracking challenge webpage: http://data.celltrackingchallenge.net
20-
HeLa cells on a flat glass
21-
Dr. G. van Cappellen. Erasmus Medical Center, Rotterdam, The Netherlands
22-
Training dataset: http://data.celltrackingchallenge.net/training-datasets/DIC-C2DH-HeLa.zip (37 MB)
23-
Challenge dataset: http://data.celltrackingchallenge.net/challenge-datasets/DIC-C2DH-HeLa.zip (41 MB)
24-
"""
25-
save_directory = Path(save_directory)
26-
if not save_directory.exists():
27-
os.makedirs(save_directory)
28-
print("Created new folder for example data downloads.")
29-
print("Example data directory is:", save_directory.resolve())
30-
unpack_filenames = [os.path.join("DIC-C2DH-HeLa", "01", f"t{str(i).zfill(3)}.tif") for i in range(84)]
31-
unpack = pooch.Unzip(members=unpack_filenames)
32-
fnames = pooch.retrieve(
33-
url="http://data.celltrackingchallenge.net/training-datasets/DIC-C2DH-HeLa.zip", # 37 MB
34-
known_hash="fac24746fa0ad5ddf6f27044c785edef36bfa39f7917da4ad79730a7748787af",
35-
fname="DIC-C2DH-HeLa.zip",
36-
path=save_directory,
37-
progressbar=True,
38-
processor=unpack,
39-
)
40-
cell_tracking_directory = save_directory.joinpath("DIC-C2DH-HeLa", "train", "01")
41-
return cell_tracking_directory
18+
def main():
19+
track_ctc_data()
4220

4321

4422
if __name__ == "__main__":
45-
# run interactive tracking for data from the cell tracking challenge
46-
track_ctc_data()
23+
main()

micro_sam/sample_data.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import os
2+
from pathlib import Path
3+
4+
import pooch
5+
6+
7+
def fetch_wholeslide_example_data(save_directory):
8+
"""Download the sample data for the 2d annotator.
9+
10+
This downloads part of a whole-slide image from the NeurIPS Cell Segmentation Challenge.
11+
See https://neurips22-cellseg.grand-challenge.org/ for details on the data.
12+
"""
13+
save_directory = Path(save_directory)
14+
os.makedirs(save_directory, exist_ok=True)
15+
print("Example data directory is:", save_directory.resolve())
16+
fname = "whole-slide-example-image.tif"
17+
pooch.retrieve(
18+
url="https://owncloud.gwdg.de/index.php/s/6ozPtgBmAAJC1di/download",
19+
known_hash="3ddb9c9dcc844429932ab951eb0743d5a1af83ee9b0ab54f06ceb2090a606d36",
20+
fname=fname,
21+
path=save_directory,
22+
progressbar=True,
23+
)
24+
return os.path.join(save_directory, fname)
25+
26+
27+
def fetch_livecell_example_data(save_directory):
28+
"""Download the sample data for the 2d annotator.
29+
30+
This downloads a single image from the LiveCELL dataset.
31+
See https://doi.org/10.1038/s41592-021-01249-6 for details on the data.
32+
"""
33+
save_directory = Path(save_directory)
34+
os.makedirs(save_directory, exist_ok=True)
35+
print("Example data directory is:", save_directory.resolve())
36+
fname = "livecell-2d-image.png"
37+
pooch.retrieve(
38+
url="https://owncloud.gwdg.de/index.php/s/fSaOJIOYjmFBjPM/download",
39+
known_hash="4f190983ea672fc333ac26d735d9625d5abb6e4a02bd4d32523127977a31e8fe",
40+
fname=fname,
41+
path=save_directory,
42+
progressbar=True,
43+
)
44+
return os.path.join(save_directory, fname)
45+
46+
47+
def fetch_hela_2d_example_data(save_directory):
48+
"""Download the sample data for the 2d annotator.
49+
50+
This downloads a single image from the HeLa CTC dataset.
51+
"""
52+
save_directory = Path(save_directory)
53+
os.makedirs(save_directory, exist_ok=True)
54+
print("Example data directory is:", save_directory.resolve())
55+
fname = "hela-2d-image.png"
56+
pooch.retrieve(
57+
url="https://owncloud.gwdg.de/index.php/s/2sr1DHQ34tV7WEb/download",
58+
known_hash="908fa00e4b273610aa4e0a9c0f22dfa64a524970852f387908f3fa65238259c7",
59+
fname=fname,
60+
path=save_directory,
61+
progressbar=True,
62+
)
63+
return os.path.join(save_directory, fname)
64+
65+
66+
def fetch_3d_example_data(save_directory):
67+
"""Download the sample data for the 3d annotator.
68+
69+
This downloads the Lucchi++ datasets from https://casser.io/connectomics/.
70+
It is a dataset for mitochondria segmentation in EM.
71+
"""
72+
save_directory = Path(save_directory)
73+
os.makedirs(save_directory, exist_ok=True)
74+
print("Example data directory is:", save_directory.resolve())
75+
unpack_filenames = [os.path.join("Lucchi++", "Test_In", f"mask{str(i).zfill(4)}.png") for i in range(165)]
76+
unpack = pooch.Unzip(members=unpack_filenames)
77+
pooch.retrieve(
78+
url="http://www.casser.io/files/lucchi_pp.zip",
79+
known_hash="770ce9e98fc6f29c1b1a250c637e6c5125f2b5f1260e5a7687b55a79e2e8844d",
80+
fname="lucchi_pp.zip",
81+
path=save_directory,
82+
progressbar=True,
83+
processor=unpack,
84+
)
85+
lucchi_dir = save_directory.joinpath("lucchi_pp.zip.unzip", "Lucchi++", "Test_In")
86+
return str(lucchi_dir)
87+
88+
89+
def fetch_tracking_example_data(save_directory):
90+
"""Download the sample data for the tracking annotator.
91+
92+
This data is the cell tracking challenge dataset DIC-C2DH-HeLa.
93+
Cell tracking challenge webpage: http://data.celltrackingchallenge.net
94+
HeLa cells on a flat glass
95+
Dr. G. van Cappellen. Erasmus Medical Center, Rotterdam, The Netherlands
96+
Training dataset: http://data.celltrackingchallenge.net/training-datasets/DIC-C2DH-HeLa.zip (37 MB)
97+
Challenge dataset: http://data.celltrackingchallenge.net/challenge-datasets/DIC-C2DH-HeLa.zip (41 MB)
98+
"""
99+
save_directory = Path(save_directory)
100+
os.makedirs(save_directory, exist_ok=True)
101+
print("Example data directory is:", save_directory.resolve())
102+
unpack_filenames = [os.path.join("DIC-C2DH-HeLa", "01", f"t{str(i).zfill(3)}.tif") for i in range(84)]
103+
unpack = pooch.Unzip(members=unpack_filenames)
104+
pooch.retrieve(
105+
url="http://data.celltrackingchallenge.net/training-datasets/DIC-C2DH-HeLa.zip", # 37 MB
106+
known_hash="fac24746fa0ad5ddf6f27044c785edef36bfa39f7917da4ad79730a7748787af",
107+
fname="DIC-C2DH-HeLa.zip",
108+
path=save_directory,
109+
progressbar=True,
110+
processor=unpack,
111+
)
112+
cell_tracking_directory = save_directory.joinpath("DIC-C2DH-HeLa", "train", "01")
113+
return str(cell_tracking_directory)

0 commit comments

Comments
 (0)