Skip to content

Commit 43cf2c5

Browse files
Merge pull request #165 from GenevieveBuckley/napari-plugin
micro-sam napari plugin
2 parents 4e4df74 + e3e5d10 commit 43cf2c5

File tree

6 files changed

+282
-27
lines changed

6 files changed

+282
-27
lines changed

MANIFEST.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include LICENSE
2+
include README.md
3+
4+
recursive-exclude * __pycache__
5+
recursive-exclude * *.py[co]

micro_sam/napari.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: micro-sam
2+
display_name: SegmentAnything for Microscopy
3+
contributions:
4+
commands:
5+
- id: micro-sam.sample_data_image_series
6+
python_name: micro_sam.sample_data:sample_data_image_series
7+
title: Load image series sample data from micro-sam plugin
8+
- id: micro-sam.sample_data_wholeslide
9+
python_name: micro_sam.sample_data:sample_data_wholeslide
10+
title: Load WholeSlide sample data from micro-sam plugin
11+
- id: micro-sam.sample_data_livecell
12+
python_name: micro_sam.sample_data:sample_data_livecell
13+
title: Load LiveCell sample data from micro-sam plugin
14+
- id: micro-sam.sample_data_hela_2d
15+
python_name: micro_sam.sample_data:sample_data_hela_2d
16+
title: Load 2D Hela sample data from micro-sam plugin
17+
- id: micro-sam.sample_data_3d
18+
python_name: micro_sam.sample_data:sample_data_3d
19+
title: Load Lucchi++ 3D sample data from micro-sam plugin
20+
- id: micro-sam.sample_data_tracking
21+
python_name: micro_sam.sample_data:sample_data_tracking
22+
title: Load tracking sample data from micro-sam plugin
23+
- id: micro-sam.sample_data_segmentation
24+
python_name: micro_sam.sample_data:sample_data_segmentation
25+
title: Load segmentation sample data from micro-sam plugin
26+
sample_data:
27+
- command: micro-sam.sample_data_image_series
28+
display_name: Image series example data
29+
key: micro-sam-image-series
30+
- command: micro-sam.sample_data_wholeslide
31+
display_name: WholeSlide example data
32+
key: micro-sam-wholeslide
33+
- command: micro-sam.sample_data_livecell
34+
display_name: LiveCell example data
35+
key: micro-sam-livecell
36+
- command: micro-sam.sample_data_hela_2d
37+
display_name: HeLa 2d example data
38+
key: micro-sam-hela2d
39+
- command: micro-sam.sample_data_3d
40+
display_name: Lucchi++ 3d dataset
41+
key: micro-sam-lucchi
42+
- command: micro-sam.sample_data_tracking
43+
display_name: Tracking sample dataset
44+
key: micro-sam-tracking
45+
- command: micro-sam.sample_data_segmentation
46+
display_name: Segmentation sample dataset
47+
key: micro-sam-segmentation

micro_sam/sample_data.py

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from pathlib import Path
77
from typing import Union
88

9+
import imageio.v3 as imageio
10+
import numpy as np
911
import pooch
1012

1113

@@ -17,6 +19,9 @@ def fetch_image_series_example_data(save_directory: Union[str, os.PathLike]) ->
1719
Returns:
1820
The folder that contains the downloaded data.
1921
"""
22+
# This sample dataset is currently not provided to napari by the micro-sam
23+
# plugin, because images are not all the same shape and cannot be combined
24+
# into a single layer
2025
save_directory = Path(save_directory)
2126
os.makedirs(save_directory, exist_ok=True)
2227
print("Example data directory is:", save_directory.resolve())
@@ -36,6 +41,26 @@ def fetch_image_series_example_data(save_directory: Union[str, os.PathLike]) ->
3641
return data_folder
3742

3843

44+
def sample_data_image_series():
45+
"""Provides image series example image to napari.
46+
47+
Opens as three separate image layers in napari (one per image in series).
48+
The third image in the series has a different size and modality.
49+
"""
50+
# Return list of tuples
51+
# [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
52+
# Check the documentation for more information about the
53+
# add_image_kwargs
54+
# https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
55+
default_base_data_dir = pooch.os_cache('micro-sam')
56+
data_directory = fetch_image_series_example_data(default_base_data_dir)
57+
fnames = os.listdir(data_directory)
58+
full_filenames = [os.path.join(data_directory, f) for f in fnames]
59+
full_filenames.sort()
60+
data_and_image_kwargs = [(imageio.imread(f), {"name": f"img-{i}"}) for i, f in enumerate(full_filenames)]
61+
return data_and_image_kwargs
62+
63+
3964
def fetch_wholeslide_example_data(save_directory: Union[str, os.PathLike]) -> str:
4065
"""Download the sample data for the 2d annotator.
4166
@@ -61,6 +86,20 @@ def fetch_wholeslide_example_data(save_directory: Union[str, os.PathLike]) -> st
6186
return os.path.join(save_directory, fname)
6287

6388

89+
def sample_data_wholeslide():
90+
"""Provides wholeslide 2d example image to napari."""
91+
# Return list of tuples
92+
# [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
93+
# Check the documentation for more information about the
94+
# add_image_kwargs
95+
# https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
96+
default_base_data_dir = pooch.os_cache('micro-sam')
97+
filename = fetch_wholeslide_example_data(default_base_data_dir)
98+
data = imageio.imread(filename)
99+
add_image_kwargs = {"name": "wholeslide"}
100+
return [(data, add_image_kwargs)]
101+
102+
64103
def fetch_livecell_example_data(save_directory: Union[str, os.PathLike]) -> str:
65104
"""Download the sample data for the 2d annotator.
66105
@@ -74,7 +113,6 @@ def fetch_livecell_example_data(save_directory: Union[str, os.PathLike]) -> str:
74113
"""
75114
save_directory = Path(save_directory)
76115
os.makedirs(save_directory, exist_ok=True)
77-
print("Example data directory is:", save_directory.resolve())
78116
fname = "livecell-2d-image.png"
79117
pooch.retrieve(
80118
url="https://owncloud.gwdg.de/index.php/s/fSaOJIOYjmFBjPM/download",
@@ -86,7 +124,21 @@ def fetch_livecell_example_data(save_directory: Union[str, os.PathLike]) -> str:
86124
return os.path.join(save_directory, fname)
87125

88126

89-
def fetch_hela_2d_example_data(save_directory: Union[str, os.PathLike]) -> str:
127+
def sample_data_livecell():
128+
"""Provides livecell 2d example image to napari."""
129+
# Return list of tuples
130+
# [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
131+
# Check the documentation for more information about the
132+
# add_image_kwargs
133+
# https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
134+
default_base_data_dir = pooch.os_cache('micro-sam')
135+
filename = fetch_livecell_example_data(default_base_data_dir)
136+
data = imageio.imread(filename)
137+
add_image_kwargs = {"name": "livecell"}
138+
return [(data, add_image_kwargs)]
139+
140+
141+
def fetch_hela_2d_example_data(save_directory: Union[str, os.PathLike]) -> Union[str, os.PathLike]:
90142
"""Download the sample data for the 2d annotator.
91143
92144
This downloads a single image from the HeLa CTC dataset.
@@ -110,6 +162,20 @@ def fetch_hela_2d_example_data(save_directory: Union[str, os.PathLike]) -> str:
110162
return os.path.join(save_directory, fname)
111163

112164

165+
def sample_data_hela_2d():
166+
"""Provides HeLa 2d example image to napari."""
167+
# Return list of tuples
168+
# [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
169+
# Check the documentation for more information about the
170+
# add_image_kwargs
171+
# https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
172+
default_base_data_dir = pooch.os_cache("micro-sam")
173+
filename = fetch_hela_2d_example_data(default_base_data_dir)
174+
data = imageio.imread(filename)
175+
add_image_kwargs = {"name": "hela_2d"}
176+
return [(data, add_image_kwargs)]
177+
178+
113179
def fetch_3d_example_data(save_directory: Union[str, os.PathLike]) -> str:
114180
"""Download the sample data for the 3d annotator.
115181
@@ -139,6 +205,23 @@ def fetch_3d_example_data(save_directory: Union[str, os.PathLike]) -> str:
139205
return str(lucchi_dir)
140206

141207

208+
def sample_data_3d():
209+
"""Provides Lucchi++ 3d example image to napari."""
210+
# Return list of tuples
211+
# [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
212+
# Check the documentation for more information about the
213+
# add_image_kwargs
214+
# https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
215+
default_base_data_dir = pooch.os_cache("micro-sam")
216+
data_directory = fetch_3d_example_data(default_base_data_dir)
217+
fnames = os.listdir(data_directory)
218+
full_filenames = [os.path.join(data_directory, f) for f in fnames]
219+
full_filenames.sort()
220+
data = np.stack([imageio.imread(f) for f in full_filenames], axis=0)
221+
add_image_kwargs = {"name": "lucchi++"}
222+
return [(data, add_image_kwargs)]
223+
224+
142225
def fetch_tracking_example_data(save_directory: Union[str, os.PathLike]) -> str:
143226
"""Download the sample data for the tracking annotator.
144227
@@ -156,7 +239,6 @@ def fetch_tracking_example_data(save_directory: Union[str, os.PathLike]) -> str:
156239
"""
157240
save_directory = Path(save_directory)
158241
os.makedirs(save_directory, exist_ok=True)
159-
print("Example data directory is:", save_directory.resolve())
160242
unpack_filenames = [os.path.join("DIC-C2DH-HeLa", "01", f"t{str(i).zfill(3)}.tif") for i in range(84)]
161243
unpack = pooch.Unzip(members=unpack_filenames)
162244
fname = "DIC-C2DH-HeLa.zip"
@@ -173,6 +255,23 @@ def fetch_tracking_example_data(save_directory: Union[str, os.PathLike]) -> str:
173255
return str(cell_tracking_dir)
174256

175257

258+
def sample_data_tracking():
259+
"""Provides tracking example dataset to napari."""
260+
# Return list of tuples
261+
# [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
262+
# Check the documentation for more information about the
263+
# add_image_kwargs
264+
# https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
265+
default_base_data_dir = pooch.os_cache("micro-sam")
266+
data_directory = fetch_tracking_example_data(default_base_data_dir)
267+
fnames = os.listdir(data_directory)
268+
full_filenames = [os.path.join(data_directory, f) for f in fnames]
269+
full_filenames.sort()
270+
data = np.stack([imageio.imread(f) for f in full_filenames], axis=0)
271+
add_image_kwargs = {"name": "tracking"}
272+
return [(data, add_image_kwargs)]
273+
274+
176275
def fetch_tracking_segmentation_data(save_directory: Union[str, os.PathLike]) -> str:
177276
"""Download groundtruth segmentation for the tracking example data.
178277
@@ -200,3 +299,20 @@ def fetch_tracking_segmentation_data(save_directory: Union[str, os.PathLike]) ->
200299
cell_tracking_dir = save_directory.joinpath(f"{fname}.unzip", "masks")
201300
assert os.path.exists(cell_tracking_dir)
202301
return str(cell_tracking_dir)
302+
303+
304+
def sample_data_segmentation():
305+
"""Provides segmentation example dataset to napari."""
306+
# Return list of tuples
307+
# [(data1, add_image_kwargs1), (data2, add_image_kwargs2)]
308+
# Check the documentation for more information about the
309+
# add_image_kwargs
310+
# https://napari.org/stable/api/napari.Viewer.html#napari.Viewer.add_image
311+
default_base_data_dir = pooch.os_cache("micro-sam")
312+
data_directory = fetch_tracking_segmentation_data(default_base_data_dir)
313+
fnames = os.listdir(data_directory)
314+
full_filenames = [os.path.join(data_directory, f) for f in fnames]
315+
full_filenames.sort()
316+
data = np.stack([imageio.imread(f) for f in full_filenames], axis=0)
317+
add_image_kwargs = {"name": "segmentation"}
318+
return [(data, add_image_kwargs)]

pyproject.toml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[build-system]
2+
requires = ["setuptools>=42.0.0", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
6+
[tool.black]
7+
line-length = 79
8+
target-version = ['py38', 'py39', 'py310']
9+
10+
11+
[tool.ruff]
12+
line-length = 79
13+
select = [
14+
"E", "F", "W", #flake8
15+
"UP", # pyupgrade
16+
"I", # isort
17+
"BLE", # flake8-blind-exception
18+
"B", # flake8-bugbear
19+
"A", # flake8-builtins
20+
"C4", # flake8-comprehensions
21+
"ISC", # flake8-implicit-str-concat
22+
"G", # flake8-logging-format
23+
"PIE", # flake8-pie
24+
"SIM", # flake8-simplify
25+
]
26+
ignore = [
27+
"E501", # line too long. let black handle this
28+
"UP006", "UP007", # type annotation. As using magicgui require runtime type annotation then we disable this.
29+
"SIM117", # flake8-simplify - some of merged with statements are not looking great with black, reanble after drop python 3.9
30+
]
31+
32+
exclude = [
33+
".bzr",
34+
".direnv",
35+
".eggs",
36+
".git",
37+
".mypy_cache",
38+
".pants.d",
39+
".ruff_cache",
40+
".svn",
41+
".tox",
42+
".venv",
43+
"__pypackages__",
44+
"_build",
45+
"buck-out",
46+
"build",
47+
"dist",
48+
"node_modules",
49+
"venv",
50+
"*vendored*",
51+
"*_vendor*",
52+
]
53+
54+
target-version = "py38"
55+
fix = true

setup.cfg

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[metadata]
2+
name = micro-sam
3+
version = attr: micro_sam.__version__
4+
description = SegmentAnything For Microscopy
5+
long_description = file: README.md
6+
long_description_content_type = text/markdown
7+
url = https://github.com/computational-cell-analytics/micro-sam
8+
author = Anwai Archit, Constantin Pape
9+
author_email = [email protected]
10+
license = MIT
11+
license_files = LICENSE
12+
classifiers =
13+
Development Status :: 2 - Pre-Alpha
14+
Framework :: napari
15+
Intended Audience :: Developers
16+
License :: OSI Approved :: BSD License
17+
Operating System :: OS Independent
18+
Programming Language :: Python
19+
Programming Language :: Python :: 3
20+
Programming Language :: Python :: 3 :: Only
21+
Programming Language :: Python :: 3.8
22+
Programming Language :: Python :: 3.9
23+
Programming Language :: Python :: 3.10
24+
Topic :: Scientific/Engineering :: Image Processing
25+
project_urls =
26+
Bug Tracker = https://github.com/computational-cell-analytics/micro-sam/issues
27+
Documentation = https://computational-cell-analytics.github.io/micro-sam/micro_sam.html
28+
Source Code = https://github.com/computational-cell-analytics/micro-sam
29+
User Support = https://github.com/computational-cell-analytics/micro-sam/issues
30+
31+
[options]
32+
packages = find:
33+
python_requires = >=3.8
34+
include_package_data = True
35+
package_dir =
36+
= .
37+
38+
[options.packages.find]
39+
where = .
40+
41+
# tell napari where to find to your manifest
42+
[options.entry_points]
43+
napari.manifest =
44+
micro-sam = micro_sam:napari.yaml
45+
46+
# make sure it gets included in your package
47+
[options.package_data]
48+
* = *.yaml
49+
50+
[project.scripts]
51+
micro_sam.annotator = "micro_sam.sam_annotator.annotator:main"
52+
micro_sam.annotator_2d = "micro_sam.sam_annotator.annotator_2d:main"
53+
micro_sam.annotator_3d = "micro_sam.sam_annotator.annotator_3d:main"
54+
micro_sam.annotator_tracking = "micro_sam.sam_annotator.annotator_tracking:main"
55+
micro_sam.image_series_annotator = "micro_sam.sam_annotator.image_series_annotator:main"
56+
micro_sam.precompute_embeddings = "micro_sam.util:main"

setup.py

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

0 commit comments

Comments
 (0)