Skip to content

Commit a366371

Browse files
Merge pull request #79 from computational-cell-analytics/sample-data
Add sample data script and update the annotator examples
2 parents 9809835 + 2c72508 commit a366371

File tree

7 files changed

+204
-133
lines changed

7 files changed

+204
-133
lines changed

development/annotator_2d_tiled.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

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: 11 additions & 34 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:
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:
1213
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()
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
from micro_sam.sam_annotator import image_folder_annotator
2+
from micro_sam.sample_data import fetch_image_series_example_data
23

34

4-
def main():
5+
def series_annotation():
6+
"""Annotate a series of images. Example runs for three different example images.
7+
"""
8+
example_data = fetch_image_series_example_data("./data")
59
image_folder_annotator(
6-
"./data/series", "./segmented-series",
7-
embedding_path="./embeddings/series-embeddings",
10+
example_data, "./data/series-segmentation-result", embedding_path="./embeddings/series-embeddings",
811
pattern="*.tif", model_type="vit_b"
912
)
1013

1114

15+
def main():
16+
series_annotation()
17+
18+
1219
if __name__ == "__main__":
1320
main()

micro_sam/sample_data.py

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

0 commit comments

Comments
 (0)