ngio 0.1.6__py3-none-any.whl → 0.2.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ngio/__init__.py +31 -5
- ngio/common/__init__.py +44 -0
- ngio/common/_array_pipe.py +160 -0
- ngio/common/_axes_transforms.py +63 -0
- ngio/common/_common_types.py +5 -0
- ngio/common/_dimensions.py +113 -0
- ngio/common/_pyramid.py +222 -0
- ngio/{core/roi.py → common/_roi.py} +22 -23
- ngio/common/_slicer.py +97 -0
- ngio/{pipes/_zoom_utils.py → common/_zoom.py} +2 -78
- ngio/hcs/__init__.py +60 -0
- ngio/images/__init__.py +23 -0
- ngio/images/abstract_image.py +240 -0
- ngio/images/create.py +251 -0
- ngio/images/image.py +383 -0
- ngio/images/label.py +96 -0
- ngio/images/omezarr_container.py +512 -0
- ngio/ome_zarr_meta/__init__.py +35 -0
- ngio/ome_zarr_meta/_generic_handlers.py +320 -0
- ngio/ome_zarr_meta/_meta_handlers.py +142 -0
- ngio/ome_zarr_meta/ngio_specs/__init__.py +63 -0
- ngio/ome_zarr_meta/ngio_specs/_axes.py +481 -0
- ngio/ome_zarr_meta/ngio_specs/_channels.py +378 -0
- ngio/ome_zarr_meta/ngio_specs/_dataset.py +134 -0
- ngio/ome_zarr_meta/ngio_specs/_ngio_hcs.py +5 -0
- ngio/ome_zarr_meta/ngio_specs/_ngio_image.py +434 -0
- ngio/ome_zarr_meta/ngio_specs/_pixel_size.py +84 -0
- ngio/ome_zarr_meta/v04/__init__.py +11 -0
- ngio/ome_zarr_meta/v04/_meta_handlers.py +54 -0
- ngio/ome_zarr_meta/v04/_v04_spec_utils.py +412 -0
- ngio/tables/__init__.py +21 -5
- ngio/tables/_validators.py +192 -0
- ngio/tables/backends/__init__.py +8 -0
- ngio/tables/backends/_abstract_backend.py +71 -0
- ngio/tables/backends/_anndata_utils.py +194 -0
- ngio/tables/backends/_anndata_v1.py +75 -0
- ngio/tables/backends/_json_v1.py +56 -0
- ngio/tables/backends/_table_backends.py +102 -0
- ngio/tables/tables_container.py +300 -0
- ngio/tables/v1/__init__.py +6 -5
- ngio/tables/v1/_feature_table.py +161 -0
- ngio/tables/v1/_generic_table.py +99 -182
- ngio/tables/v1/_masking_roi_table.py +175 -0
- ngio/tables/v1/_roi_table.py +226 -0
- ngio/utils/__init__.py +23 -10
- ngio/utils/_datasets.py +51 -0
- ngio/utils/_errors.py +10 -4
- ngio/utils/_zarr_utils.py +378 -0
- {ngio-0.1.6.dist-info → ngio-0.2.0a1.dist-info}/METADATA +18 -39
- ngio-0.2.0a1.dist-info/RECORD +53 -0
- ngio/core/__init__.py +0 -7
- ngio/core/dimensions.py +0 -122
- ngio/core/image_handler.py +0 -228
- ngio/core/image_like_handler.py +0 -549
- ngio/core/label_handler.py +0 -410
- ngio/core/ngff_image.py +0 -387
- ngio/core/utils.py +0 -287
- ngio/io/__init__.py +0 -19
- ngio/io/_zarr.py +0 -88
- ngio/io/_zarr_array_utils.py +0 -0
- ngio/io/_zarr_group_utils.py +0 -60
- ngio/iterators/__init__.py +0 -1
- ngio/ngff_meta/__init__.py +0 -27
- ngio/ngff_meta/fractal_image_meta.py +0 -1267
- ngio/ngff_meta/meta_handler.py +0 -92
- ngio/ngff_meta/utils.py +0 -235
- ngio/ngff_meta/v04/__init__.py +0 -6
- ngio/ngff_meta/v04/specs.py +0 -158
- ngio/ngff_meta/v04/zarr_utils.py +0 -376
- ngio/pipes/__init__.py +0 -7
- ngio/pipes/_slicer_transforms.py +0 -176
- ngio/pipes/_transforms.py +0 -33
- ngio/pipes/data_pipe.py +0 -52
- ngio/tables/_ad_reader.py +0 -80
- ngio/tables/_utils.py +0 -301
- ngio/tables/tables_group.py +0 -252
- ngio/tables/v1/feature_tables.py +0 -182
- ngio/tables/v1/masking_roi_tables.py +0 -243
- ngio/tables/v1/roi_tables.py +0 -285
- ngio/utils/_common_types.py +0 -5
- ngio/utils/_pydantic_utils.py +0 -52
- ngio-0.1.6.dist-info/RECORD +0 -44
- {ngio-0.1.6.dist-info → ngio-0.2.0a1.dist-info}/WHEEL +0 -0
- {ngio-0.1.6.dist-info → ngio-0.2.0a1.dist-info}/licenses/LICENSE +0 -0
ngio/images/label.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""A module for handling label images in OME-NGFF files."""
|
|
2
|
+
|
|
3
|
+
from typing import Literal
|
|
4
|
+
|
|
5
|
+
from ngio.images.abstract_image import AbstractImage, consolidate_image
|
|
6
|
+
from ngio.ome_zarr_meta import (
|
|
7
|
+
ImplementedLabelMetaHandlers,
|
|
8
|
+
LabelMetaHandler,
|
|
9
|
+
NgioLabelMeta,
|
|
10
|
+
)
|
|
11
|
+
from ngio.utils import (
|
|
12
|
+
NgioValidationError,
|
|
13
|
+
ZarrGroupHandler,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Label(AbstractImage[LabelMetaHandler]):
|
|
18
|
+
"""Placeholder class for a label."""
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
group_handler: ZarrGroupHandler,
|
|
23
|
+
path: str,
|
|
24
|
+
meta_handler: LabelMetaHandler | None,
|
|
25
|
+
) -> None:
|
|
26
|
+
"""Initialize the Image at a single level.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
group_handler: The Zarr group handler.
|
|
30
|
+
path: The path to the image in the omezarr file.
|
|
31
|
+
meta_handler: The image metadata handler.
|
|
32
|
+
|
|
33
|
+
"""
|
|
34
|
+
if meta_handler is None:
|
|
35
|
+
meta_handler = ImplementedLabelMetaHandlers().find_meta_handler(
|
|
36
|
+
group_handler
|
|
37
|
+
)
|
|
38
|
+
super().__init__(
|
|
39
|
+
group_handler=group_handler, path=path, meta_handler=meta_handler
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def meta(self) -> NgioLabelMeta:
|
|
44
|
+
"""Return the metadata."""
|
|
45
|
+
return self._meta_handler.meta
|
|
46
|
+
|
|
47
|
+
def consolidate(
|
|
48
|
+
self,
|
|
49
|
+
mode: Literal["dask", "numpy", "coarsen"] = "dask",
|
|
50
|
+
) -> None:
|
|
51
|
+
"""Consolidate the label on disk."""
|
|
52
|
+
consolidate_image(self, mode=mode, order=0)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class LabelsContainer:
|
|
56
|
+
"""A class to handle the /labels group in an OME-NGFF file."""
|
|
57
|
+
|
|
58
|
+
def __init__(self, group_handler: ZarrGroupHandler) -> None:
|
|
59
|
+
"""Initialize the LabelGroupHandler."""
|
|
60
|
+
self._group_handler = group_handler
|
|
61
|
+
|
|
62
|
+
# Validate the group
|
|
63
|
+
# Either contains a labels attribute or is empty
|
|
64
|
+
attrs = self._group_handler.load_attrs()
|
|
65
|
+
if len(attrs) == 0:
|
|
66
|
+
# It's an empty group
|
|
67
|
+
pass
|
|
68
|
+
elif "labels" in attrs and isinstance(attrs["labels"], list):
|
|
69
|
+
# It's a valid group
|
|
70
|
+
pass
|
|
71
|
+
else:
|
|
72
|
+
raise NgioValidationError(
|
|
73
|
+
f"Invalid /labels group. "
|
|
74
|
+
f"Expected a single labels attribute with a list of label names. "
|
|
75
|
+
f"Found: {attrs}"
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
def list(self) -> list[str]:
|
|
79
|
+
"""Create the /labels group if it doesn't exist."""
|
|
80
|
+
attrs = self._group_handler.load_attrs()
|
|
81
|
+
return attrs.get("labels", [])
|
|
82
|
+
|
|
83
|
+
def get(self, name: str, path: str) -> Label:
|
|
84
|
+
"""Get a label from the group."""
|
|
85
|
+
group_handler = self._group_handler.derive_handler(name)
|
|
86
|
+
return Label(group_handler, path, None)
|
|
87
|
+
|
|
88
|
+
def derive(
|
|
89
|
+
self,
|
|
90
|
+
name: str,
|
|
91
|
+
reference_image: AbstractImage,
|
|
92
|
+
overwrite: bool = False,
|
|
93
|
+
**kwargs,
|
|
94
|
+
) -> Label:
|
|
95
|
+
"""Derive a label from an image."""
|
|
96
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
"""Abstract class for handling OME-NGFF images."""
|
|
2
|
+
|
|
3
|
+
# %%
|
|
4
|
+
from collections.abc import Collection
|
|
5
|
+
from typing import Literal, overload
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
from ngio.images.create import _create_empty_image
|
|
10
|
+
from ngio.images.image import Image, ImagesContainer
|
|
11
|
+
from ngio.images.label import Label, LabelsContainer
|
|
12
|
+
from ngio.ome_zarr_meta import (
|
|
13
|
+
NgioImageMeta,
|
|
14
|
+
PixelSize,
|
|
15
|
+
)
|
|
16
|
+
from ngio.ome_zarr_meta.ngio_specs import (
|
|
17
|
+
SpaceUnits,
|
|
18
|
+
TimeUnits,
|
|
19
|
+
)
|
|
20
|
+
from ngio.tables import (
|
|
21
|
+
FeatureTable,
|
|
22
|
+
MaskingROITable,
|
|
23
|
+
RoiTable,
|
|
24
|
+
Table,
|
|
25
|
+
TablesContainer,
|
|
26
|
+
TypedTable,
|
|
27
|
+
)
|
|
28
|
+
from ngio.utils import (
|
|
29
|
+
AccessModeLiteral,
|
|
30
|
+
NgioValidationError,
|
|
31
|
+
NgioValueError,
|
|
32
|
+
StoreOrGroup,
|
|
33
|
+
ZarrGroupHandler,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _default_table_container(handler: ZarrGroupHandler) -> TablesContainer | None:
|
|
38
|
+
"""Return a default table container."""
|
|
39
|
+
success, table_handler = handler.safe_derive_handler("tables")
|
|
40
|
+
if success and isinstance(table_handler, ZarrGroupHandler):
|
|
41
|
+
return TablesContainer(table_handler)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _default_label_container(handler: ZarrGroupHandler) -> LabelsContainer | None:
|
|
45
|
+
"""Return a default label container."""
|
|
46
|
+
success, label_handler = handler.safe_derive_handler("labels")
|
|
47
|
+
if success and isinstance(label_handler, ZarrGroupHandler):
|
|
48
|
+
return LabelsContainer(label_handler)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class OmeZarrContainer:
|
|
52
|
+
"""This class contains an OME-Zarr image and its associated tables and labels."""
|
|
53
|
+
|
|
54
|
+
_images_container: ImagesContainer
|
|
55
|
+
_labels_container: LabelsContainer | None
|
|
56
|
+
_tables_container: TablesContainer | None
|
|
57
|
+
|
|
58
|
+
def __init__(
|
|
59
|
+
self,
|
|
60
|
+
store: StoreOrGroup,
|
|
61
|
+
cache: bool = False,
|
|
62
|
+
mode: AccessModeLiteral = "r+",
|
|
63
|
+
table_container: TablesContainer | None = None,
|
|
64
|
+
label_container: LabelsContainer | None = None,
|
|
65
|
+
validate_arrays: bool = True,
|
|
66
|
+
) -> None:
|
|
67
|
+
"""Initialize the OmeZarrContainer."""
|
|
68
|
+
self._group_handler = ZarrGroupHandler(store, cache, mode)
|
|
69
|
+
self._images_container = ImagesContainer(self._group_handler)
|
|
70
|
+
|
|
71
|
+
if label_container is None:
|
|
72
|
+
label_container = _default_label_container(self._group_handler)
|
|
73
|
+
self._labels_container = label_container
|
|
74
|
+
|
|
75
|
+
if table_container is None:
|
|
76
|
+
table_container = _default_table_container(self._group_handler)
|
|
77
|
+
self._tables_container = table_container
|
|
78
|
+
|
|
79
|
+
def __repr__(self) -> str:
|
|
80
|
+
"""Return a string representation of the image."""
|
|
81
|
+
num_labels = len(self.list_labels())
|
|
82
|
+
num_tables = len(self.list_tables())
|
|
83
|
+
|
|
84
|
+
base_str = f"OmeZarrContainer(levels={self.levels}"
|
|
85
|
+
if num_labels > 0 and num_labels < 3:
|
|
86
|
+
base_str += f", labels={self.list_labels()}"
|
|
87
|
+
elif num_labels >= 3:
|
|
88
|
+
base_str += f", #labels={num_labels}"
|
|
89
|
+
if num_tables > 0 and num_tables < 3:
|
|
90
|
+
base_str += f", tables={self.list_tables()}"
|
|
91
|
+
elif num_tables >= 3:
|
|
92
|
+
base_str += f", #tables={num_tables}"
|
|
93
|
+
base_str += ")"
|
|
94
|
+
return base_str
|
|
95
|
+
|
|
96
|
+
@property
|
|
97
|
+
def images_container(self) -> ImagesContainer:
|
|
98
|
+
"""Return the image container."""
|
|
99
|
+
return self._images_container
|
|
100
|
+
|
|
101
|
+
@property
|
|
102
|
+
def labels_container(self) -> LabelsContainer:
|
|
103
|
+
"""Return the labels container."""
|
|
104
|
+
if self._labels_container is None:
|
|
105
|
+
raise NgioValidationError("No labels found in the image.")
|
|
106
|
+
return self._labels_container
|
|
107
|
+
|
|
108
|
+
@property
|
|
109
|
+
def tables_container(self) -> TablesContainer:
|
|
110
|
+
"""Return the tables container."""
|
|
111
|
+
if self._tables_container is None:
|
|
112
|
+
raise NgioValidationError("No tables found in the image.")
|
|
113
|
+
return self._tables_container
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def image_meta(self) -> NgioImageMeta:
|
|
117
|
+
"""Return the image metadata."""
|
|
118
|
+
return self._images_container.meta
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def levels(self) -> int:
|
|
122
|
+
"""Return the number of levels in the image."""
|
|
123
|
+
return self._images_container.levels
|
|
124
|
+
|
|
125
|
+
@property
|
|
126
|
+
def levels_paths(self) -> list[str]:
|
|
127
|
+
"""Return the paths of the levels in the image."""
|
|
128
|
+
return self._images_container.levels_paths
|
|
129
|
+
|
|
130
|
+
def initialize_channel_meta(
|
|
131
|
+
self,
|
|
132
|
+
labels: Collection[str] | int | None = None,
|
|
133
|
+
wavelength_id: Collection[str] | None = None,
|
|
134
|
+
percentiles: tuple[float, float] | None = None,
|
|
135
|
+
colors: Collection[str] | None = None,
|
|
136
|
+
active: Collection[bool] | None = None,
|
|
137
|
+
**omero_kwargs: dict,
|
|
138
|
+
) -> None:
|
|
139
|
+
"""Create a ChannelsMeta object with the default unit."""
|
|
140
|
+
self._images_container.initialize_channel_meta(
|
|
141
|
+
labels=labels,
|
|
142
|
+
wavelength_id=wavelength_id,
|
|
143
|
+
percentiles=percentiles,
|
|
144
|
+
colors=colors,
|
|
145
|
+
active=active,
|
|
146
|
+
**omero_kwargs,
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
def update_percentiles(
|
|
150
|
+
self,
|
|
151
|
+
start_percentile: float = 0.1,
|
|
152
|
+
end_percentile: float = 99.9,
|
|
153
|
+
) -> None:
|
|
154
|
+
"""Update the percentiles of the image."""
|
|
155
|
+
self._images_container.update_percentiles(
|
|
156
|
+
start_percentile=start_percentile, end_percentile=end_percentile
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
def get_image(
|
|
160
|
+
self,
|
|
161
|
+
path: str | None = None,
|
|
162
|
+
pixel_size: PixelSize | None = None,
|
|
163
|
+
highest_resolution: bool = True,
|
|
164
|
+
) -> Image:
|
|
165
|
+
"""Get an image at a specific level."""
|
|
166
|
+
return self._images_container.get(
|
|
167
|
+
path=path,
|
|
168
|
+
pixel_size=pixel_size,
|
|
169
|
+
highest_resolution=highest_resolution,
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
def derive_image(
|
|
173
|
+
self,
|
|
174
|
+
store: StoreOrGroup,
|
|
175
|
+
ref_path: str | None = None,
|
|
176
|
+
shape: Collection[int] | None = None,
|
|
177
|
+
chunks: Collection[int] | None = None,
|
|
178
|
+
copy_tables: bool = False,
|
|
179
|
+
copy_labels: bool = False,
|
|
180
|
+
overwrite: bool = False,
|
|
181
|
+
) -> "OmeZarrContainer":
|
|
182
|
+
"""Derive a new image from the current image."""
|
|
183
|
+
if copy_labels:
|
|
184
|
+
raise NotImplementedError("Copying labels is not yet implemented.")
|
|
185
|
+
|
|
186
|
+
if copy_tables:
|
|
187
|
+
raise NotImplementedError("Copying tables is not yet implemented.")
|
|
188
|
+
|
|
189
|
+
_ = self._images_container.derive(
|
|
190
|
+
store=store,
|
|
191
|
+
ref_path=ref_path,
|
|
192
|
+
shape=shape,
|
|
193
|
+
chunks=chunks,
|
|
194
|
+
overwrite=overwrite,
|
|
195
|
+
)
|
|
196
|
+
return OmeZarrContainer(
|
|
197
|
+
store=store,
|
|
198
|
+
cache=False,
|
|
199
|
+
mode="r+",
|
|
200
|
+
table_container=None,
|
|
201
|
+
label_container=None,
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
def list_tables(self) -> list[str]:
|
|
205
|
+
"""List all tables in the image."""
|
|
206
|
+
if self._tables_container is None:
|
|
207
|
+
return []
|
|
208
|
+
return self._tables_container.list()
|
|
209
|
+
|
|
210
|
+
@overload
|
|
211
|
+
def get_table(self, name: str, check_type: None) -> Table: ...
|
|
212
|
+
|
|
213
|
+
@overload
|
|
214
|
+
def get_table(self, name: str, check_type: Literal["roi_table"]) -> RoiTable: ...
|
|
215
|
+
|
|
216
|
+
@overload
|
|
217
|
+
def get_table(
|
|
218
|
+
self, name: str, check_type: Literal["masking_roi_table"]
|
|
219
|
+
) -> MaskingROITable: ...
|
|
220
|
+
|
|
221
|
+
@overload
|
|
222
|
+
def get_table(
|
|
223
|
+
self, name: str, check_type: Literal["feature_table"]
|
|
224
|
+
) -> FeatureTable: ...
|
|
225
|
+
|
|
226
|
+
def get_table(self, name: str, check_type: TypedTable | None = None) -> Table:
|
|
227
|
+
"""Get a table from the image."""
|
|
228
|
+
if self._tables_container is None:
|
|
229
|
+
raise NgioValidationError("No tables found in the image.")
|
|
230
|
+
|
|
231
|
+
table = self._tables_container.get(name)
|
|
232
|
+
match check_type:
|
|
233
|
+
case "roi_table":
|
|
234
|
+
if not isinstance(table, RoiTable):
|
|
235
|
+
raise NgioValueError(
|
|
236
|
+
f"Table '{name}' is not a ROI table. Found type: {table.type()}"
|
|
237
|
+
)
|
|
238
|
+
return table
|
|
239
|
+
case "masking_roi_table":
|
|
240
|
+
if not isinstance(table, MaskingROITable):
|
|
241
|
+
raise NgioValueError(
|
|
242
|
+
f"Table '{name}' is not a masking ROI table. "
|
|
243
|
+
f"Found type: {table.type()}"
|
|
244
|
+
)
|
|
245
|
+
return table
|
|
246
|
+
case "feature_table":
|
|
247
|
+
if not isinstance(table, FeatureTable):
|
|
248
|
+
raise NgioValueError(
|
|
249
|
+
f"Table '{name}' is not a feature table. "
|
|
250
|
+
f"Found type: {table.type()}"
|
|
251
|
+
)
|
|
252
|
+
return table
|
|
253
|
+
case None:
|
|
254
|
+
return table
|
|
255
|
+
case _:
|
|
256
|
+
raise NgioValueError(f"Unknown check_type: {check_type}")
|
|
257
|
+
|
|
258
|
+
def add_table(
|
|
259
|
+
self,
|
|
260
|
+
name: str,
|
|
261
|
+
table: Table,
|
|
262
|
+
backend: str | None = None,
|
|
263
|
+
overwrite: bool = False,
|
|
264
|
+
) -> None:
|
|
265
|
+
"""Add a table to the image."""
|
|
266
|
+
if self._tables_container is None:
|
|
267
|
+
raise NgioValidationError("No tables found in the image.")
|
|
268
|
+
self._tables_container.add(
|
|
269
|
+
name=name, table=table, backend=backend, overwrite=overwrite
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
def list_labels(self) -> list[str]:
|
|
273
|
+
"""List all labels in the image."""
|
|
274
|
+
if self._labels_container is None:
|
|
275
|
+
return []
|
|
276
|
+
return self._labels_container.list()
|
|
277
|
+
|
|
278
|
+
def get_label(self, name: str, path: str) -> Label:
|
|
279
|
+
"""Get a label from the image."""
|
|
280
|
+
if self._labels_container is None:
|
|
281
|
+
raise NgioValidationError("No labels found in the image.")
|
|
282
|
+
return self._labels_container.get(name=name, path=path)
|
|
283
|
+
|
|
284
|
+
def derive_label(self, name: str, **kwargs) -> Label:
|
|
285
|
+
"""Derive a label from an image."""
|
|
286
|
+
if self._labels_container is None:
|
|
287
|
+
raise NgioValidationError("No labels found in the image.")
|
|
288
|
+
|
|
289
|
+
ref_image = self.get_image()
|
|
290
|
+
return self._labels_container.derive(
|
|
291
|
+
name=name, reference_image=ref_image, **kwargs
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def open_omezarr_container(
|
|
296
|
+
store: StoreOrGroup,
|
|
297
|
+
cache: bool = False,
|
|
298
|
+
mode: AccessModeLiteral = "r+",
|
|
299
|
+
validate_arrays: bool = True,
|
|
300
|
+
) -> OmeZarrContainer:
|
|
301
|
+
"""Open an OME-Zarr image."""
|
|
302
|
+
return OmeZarrContainer(
|
|
303
|
+
store=store,
|
|
304
|
+
cache=cache,
|
|
305
|
+
mode=mode,
|
|
306
|
+
validate_arrays=validate_arrays,
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def open_image(
|
|
311
|
+
store: StoreOrGroup,
|
|
312
|
+
path: str | None = None,
|
|
313
|
+
pixel_size: PixelSize | None = None,
|
|
314
|
+
highest_resolution: bool = False,
|
|
315
|
+
cache: bool = False,
|
|
316
|
+
mode: AccessModeLiteral = "r+",
|
|
317
|
+
) -> Image:
|
|
318
|
+
"""Open a single level image from an OME-Zarr image."""
|
|
319
|
+
group_handler = ZarrGroupHandler(store, cache, mode)
|
|
320
|
+
images_container = ImagesContainer(group_handler)
|
|
321
|
+
return images_container.get(
|
|
322
|
+
path=path,
|
|
323
|
+
pixel_size=pixel_size,
|
|
324
|
+
highest_resolution=highest_resolution,
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def create_empty_omezarr(
|
|
329
|
+
store: StoreOrGroup,
|
|
330
|
+
shape: Collection[int],
|
|
331
|
+
xy_pixelsize: float,
|
|
332
|
+
z_spacing: float = 1.0,
|
|
333
|
+
time_spacing: float = 1.0,
|
|
334
|
+
levels: int | list[str] = 5,
|
|
335
|
+
xy_scaling_factor: float = 2,
|
|
336
|
+
z_scaling_factor: float = 1.0,
|
|
337
|
+
space_unit: SpaceUnits | str | None = None,
|
|
338
|
+
time_unit: TimeUnits | str | None = None,
|
|
339
|
+
axes_names: Collection[str] | None = None,
|
|
340
|
+
name: str | None = None,
|
|
341
|
+
chunks: Collection[int] | None = None,
|
|
342
|
+
dtype: str = "uint16",
|
|
343
|
+
channel_labels: list[str] | None = None,
|
|
344
|
+
channel_wavelengths: list[str] | None = None,
|
|
345
|
+
percentiles: tuple[float, float] | None = None,
|
|
346
|
+
channel_colors: Collection[str] | None = None,
|
|
347
|
+
channel_active: Collection[bool] | None = None,
|
|
348
|
+
overwrite: bool = False,
|
|
349
|
+
version: str = "0.4",
|
|
350
|
+
) -> OmeZarrContainer:
|
|
351
|
+
"""Create an empty OME-Zarr image with the given shape and metadata.
|
|
352
|
+
|
|
353
|
+
Args:
|
|
354
|
+
store (StoreOrGroup): The Zarr store or group to create the image in.
|
|
355
|
+
shape (Collection[int]): The shape of the image.
|
|
356
|
+
xy_pixelsize (float): The pixel size in x and y dimensions.
|
|
357
|
+
z_spacing (float, optional): The spacing between z slices. Defaults to 1.0.
|
|
358
|
+
time_spacing (float, optional): The spacing between time points.
|
|
359
|
+
Defaults to 1.0.
|
|
360
|
+
levels (int | list[str], optional): The number of levels in the pyramid or a
|
|
361
|
+
list of level names. Defaults to 5.
|
|
362
|
+
xy_scaling_factor (float, optional): The down-scaling factor in x and y
|
|
363
|
+
dimensions. Defaults to 2.0.
|
|
364
|
+
z_scaling_factor (float, optional): The down-scaling factor in z dimension.
|
|
365
|
+
Defaults to 1.0.
|
|
366
|
+
space_unit (SpaceUnits | str | None, optional): The unit of space. Defaults to
|
|
367
|
+
None.
|
|
368
|
+
time_unit (TimeUnits | str | None, optional): The unit of time. Defaults to
|
|
369
|
+
None.
|
|
370
|
+
axes_names (Collection[str] | None, optional): The names of the axes.
|
|
371
|
+
If None the canonical names are used. Defaults to None.
|
|
372
|
+
name (str | None, optional): The name of the image. Defaults to None.
|
|
373
|
+
chunks (Collection[int] | None, optional): The chunk shape. If None the shape
|
|
374
|
+
is used. Defaults to None.
|
|
375
|
+
dtype (str, optional): The data type of the image. Defaults to "uint16".
|
|
376
|
+
channel_labels (list[str] | None, optional): The labels of the channels.
|
|
377
|
+
Defaults to None.
|
|
378
|
+
channel_wavelengths (list[str] | None, optional): The wavelengths of the
|
|
379
|
+
channels. Defaults to None.
|
|
380
|
+
percentiles (tuple[float, float] | None, optional): The percentiles of the
|
|
381
|
+
channels. Defaults to None.
|
|
382
|
+
channel_colors (Collection[str] | None, optional): The colors of the channels.
|
|
383
|
+
Defaults to None.
|
|
384
|
+
channel_active (Collection[bool] | None, optional): Whether the channels are
|
|
385
|
+
active. Defaults to None.
|
|
386
|
+
overwrite (bool, optional): Whether to overwrite an existing image.
|
|
387
|
+
Defaults to True.
|
|
388
|
+
version (str, optional): The version of the OME-Zarr specification.
|
|
389
|
+
Defaults to "0.4".
|
|
390
|
+
"""
|
|
391
|
+
handler = _create_empty_image(
|
|
392
|
+
store=store,
|
|
393
|
+
shape=shape,
|
|
394
|
+
xy_pixelsize=xy_pixelsize,
|
|
395
|
+
z_spacing=z_spacing,
|
|
396
|
+
time_spacing=time_spacing,
|
|
397
|
+
levels=levels,
|
|
398
|
+
xy_scaling_factor=xy_scaling_factor,
|
|
399
|
+
z_scaling_factor=z_scaling_factor,
|
|
400
|
+
space_unit=space_unit,
|
|
401
|
+
time_unit=time_unit,
|
|
402
|
+
axes_names=axes_names,
|
|
403
|
+
name=name,
|
|
404
|
+
chunks=chunks,
|
|
405
|
+
dtype=dtype,
|
|
406
|
+
overwrite=overwrite,
|
|
407
|
+
version=version,
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
omezarr = OmeZarrContainer(store=handler.store, mode="r+")
|
|
411
|
+
omezarr.initialize_channel_meta(
|
|
412
|
+
labels=channel_labels,
|
|
413
|
+
wavelength_id=channel_wavelengths,
|
|
414
|
+
percentiles=percentiles,
|
|
415
|
+
colors=channel_colors,
|
|
416
|
+
active=channel_active,
|
|
417
|
+
)
|
|
418
|
+
return omezarr
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def create_omezarr_from_array(
|
|
422
|
+
store: StoreOrGroup,
|
|
423
|
+
array: np.ndarray,
|
|
424
|
+
xy_pixelsize: float,
|
|
425
|
+
z_spacing: float = 1.0,
|
|
426
|
+
time_spacing: float = 1.0,
|
|
427
|
+
levels: int | list[str] = 5,
|
|
428
|
+
xy_scaling_factor: float = 2.0,
|
|
429
|
+
z_scaling_factor: float = 1.0,
|
|
430
|
+
space_unit: SpaceUnits | str | None = None,
|
|
431
|
+
time_unit: TimeUnits | str | None = None,
|
|
432
|
+
axes_names: Collection[str] | None = None,
|
|
433
|
+
channel_labels: list[str] | None = None,
|
|
434
|
+
channel_wavelengths: list[str] | None = None,
|
|
435
|
+
percentiles: tuple[float, float] | None = (0.1, 99.9),
|
|
436
|
+
channel_colors: Collection[str] | None = None,
|
|
437
|
+
channel_active: Collection[bool] | None = None,
|
|
438
|
+
name: str | None = None,
|
|
439
|
+
chunks: Collection[int] | None = None,
|
|
440
|
+
overwrite: bool = False,
|
|
441
|
+
version: str = "0.4",
|
|
442
|
+
) -> OmeZarrContainer:
|
|
443
|
+
"""Create an OME-Zarr image from a numpy array.
|
|
444
|
+
|
|
445
|
+
Args:
|
|
446
|
+
store (StoreOrGroup): The Zarr store or group to create the image in.
|
|
447
|
+
array (np.ndarray): The image data.
|
|
448
|
+
xy_pixelsize (float): The pixel size in x and y dimensions.
|
|
449
|
+
z_spacing (float, optional): The spacing between z slices. Defaults to 1.0.
|
|
450
|
+
time_spacing (float, optional): The spacing between time points.
|
|
451
|
+
Defaults to 1.0.
|
|
452
|
+
levels (int | list[str], optional): The number of levels in the pyramid or a
|
|
453
|
+
list of level names. Defaults to 5.
|
|
454
|
+
xy_scaling_factor (float, optional): The down-scaling factor in x and y
|
|
455
|
+
dimensions. Defaults to 2.0.
|
|
456
|
+
z_scaling_factor (float, optional): The down-scaling factor in z dimension.
|
|
457
|
+
Defaults to 1.0.
|
|
458
|
+
space_unit (SpaceUnits | str | None, optional): The unit of space. Defaults to
|
|
459
|
+
None.
|
|
460
|
+
time_unit (TimeUnits | str | None, optional): The unit of time. Defaults to
|
|
461
|
+
None.
|
|
462
|
+
axes_names (Collection[str] | None, optional): The names of the axes.
|
|
463
|
+
If None the canonical names are used. Defaults to None.
|
|
464
|
+
name (str | None, optional): The name of the image. Defaults to None.
|
|
465
|
+
chunks (Collection[int] | None, optional): The chunk shape. If None the shape
|
|
466
|
+
is used. Defaults to None.
|
|
467
|
+
channel_labels (list[str] | None, optional): The labels of the channels.
|
|
468
|
+
Defaults to None.
|
|
469
|
+
channel_wavelengths (list[str] | None, optional): The wavelengths of the
|
|
470
|
+
channels. Defaults to None.
|
|
471
|
+
percentiles (tuple[float, float] | None, optional): The percentiles of the
|
|
472
|
+
channels. Defaults to None.
|
|
473
|
+
channel_colors (Collection[str] | None, optional): The colors of the channels.
|
|
474
|
+
Defaults to None.
|
|
475
|
+
channel_active (Collection[bool] | None, optional): Whether the channels are
|
|
476
|
+
active. Defaults to None.
|
|
477
|
+
overwrite (bool, optional): Whether to overwrite an existing image.
|
|
478
|
+
Defaults to True.
|
|
479
|
+
version (str, optional): The version of the OME-Zarr specification.
|
|
480
|
+
Defaults to "0.4".
|
|
481
|
+
"""
|
|
482
|
+
handler = _create_empty_image(
|
|
483
|
+
store=store,
|
|
484
|
+
shape=array.shape,
|
|
485
|
+
xy_pixelsize=xy_pixelsize,
|
|
486
|
+
z_spacing=z_spacing,
|
|
487
|
+
time_spacing=time_spacing,
|
|
488
|
+
levels=levels,
|
|
489
|
+
xy_scaling_factor=xy_scaling_factor,
|
|
490
|
+
z_scaling_factor=z_scaling_factor,
|
|
491
|
+
space_unit=space_unit,
|
|
492
|
+
time_unit=time_unit,
|
|
493
|
+
axes_names=axes_names,
|
|
494
|
+
name=name,
|
|
495
|
+
chunks=chunks,
|
|
496
|
+
dtype=array.dtype,
|
|
497
|
+
overwrite=overwrite,
|
|
498
|
+
version=version,
|
|
499
|
+
)
|
|
500
|
+
|
|
501
|
+
omezarr = OmeZarrContainer(store=handler.store, mode="r+")
|
|
502
|
+
image = omezarr.get_image()
|
|
503
|
+
image.set_array(array)
|
|
504
|
+
image.consolidate()
|
|
505
|
+
omezarr.initialize_channel_meta(
|
|
506
|
+
labels=channel_labels,
|
|
507
|
+
wavelength_id=channel_wavelengths,
|
|
508
|
+
percentiles=percentiles,
|
|
509
|
+
colors=channel_colors,
|
|
510
|
+
active=channel_active,
|
|
511
|
+
)
|
|
512
|
+
return omezarr
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Utilities for reading and writing OME-Zarr metadata."""
|
|
2
|
+
|
|
3
|
+
from ngio.ome_zarr_meta._generic_handlers import (
|
|
4
|
+
BaseImageMetaHandler,
|
|
5
|
+
BaseLabelMetaHandler,
|
|
6
|
+
ImageMetaHandler,
|
|
7
|
+
LabelMetaHandler,
|
|
8
|
+
)
|
|
9
|
+
from ngio.ome_zarr_meta._meta_handlers import (
|
|
10
|
+
ImplementedImageMetaHandlers,
|
|
11
|
+
ImplementedLabelMetaHandlers,
|
|
12
|
+
open_image_meta_handler,
|
|
13
|
+
)
|
|
14
|
+
from ngio.ome_zarr_meta.ngio_specs import (
|
|
15
|
+
AxesMapper,
|
|
16
|
+
Dataset,
|
|
17
|
+
NgioImageMeta,
|
|
18
|
+
NgioLabelMeta,
|
|
19
|
+
PixelSize,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"AxesMapper",
|
|
24
|
+
"BaseImageMetaHandler",
|
|
25
|
+
"BaseLabelMetaHandler",
|
|
26
|
+
"Dataset",
|
|
27
|
+
"ImageMetaHandler",
|
|
28
|
+
"ImplementedImageMetaHandlers",
|
|
29
|
+
"ImplementedLabelMetaHandlers",
|
|
30
|
+
"LabelMetaHandler",
|
|
31
|
+
"NgioImageMeta",
|
|
32
|
+
"NgioLabelMeta",
|
|
33
|
+
"PixelSize",
|
|
34
|
+
"open_image_meta_handler",
|
|
35
|
+
]
|