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
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
"""Base class for handling OME-NGFF metadata in Zarr groups."""
|
|
2
|
+
|
|
3
|
+
from typing import Generic, Protocol, TypeVar
|
|
4
|
+
|
|
5
|
+
from pydantic import ValidationError
|
|
6
|
+
|
|
7
|
+
from ngio.ome_zarr_meta.ngio_specs import AxesSetup, NgioImageMeta, NgioLabelMeta
|
|
8
|
+
from ngio.utils import (
|
|
9
|
+
NgioValueError,
|
|
10
|
+
ZarrGroupHandler,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
ConverterError = ValidationError | Exception | None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ImageMetaHandler(Protocol):
|
|
17
|
+
"""Protocol for OME-Zarr image handlers."""
|
|
18
|
+
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
group_handler: ZarrGroupHandler,
|
|
22
|
+
axes_setup: AxesSetup | None = None,
|
|
23
|
+
allow_non_canonical_axes: bool = False,
|
|
24
|
+
strict_canonical_order: bool = True,
|
|
25
|
+
):
|
|
26
|
+
"""Initialize the handler."""
|
|
27
|
+
...
|
|
28
|
+
|
|
29
|
+
def safe_load_meta(self) -> NgioImageMeta | ConverterError:
|
|
30
|
+
"""Load the metadata from the store."""
|
|
31
|
+
...
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def meta(self) -> NgioImageMeta:
|
|
35
|
+
"""Return the metadata."""
|
|
36
|
+
...
|
|
37
|
+
|
|
38
|
+
def write_meta(self, meta: NgioImageMeta) -> None:
|
|
39
|
+
"""Write the metadata to the store."""
|
|
40
|
+
...
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class LabelMetaHandler(Protocol):
|
|
44
|
+
"""Protocol for OME-Zarr label handlers."""
|
|
45
|
+
|
|
46
|
+
def __init__(
|
|
47
|
+
self,
|
|
48
|
+
group_handler: ZarrGroupHandler,
|
|
49
|
+
axes_setup: AxesSetup | None = None,
|
|
50
|
+
allow_non_canonical_axes: bool = False,
|
|
51
|
+
strict_canonical_order: bool = True,
|
|
52
|
+
):
|
|
53
|
+
"""Initialize the handler."""
|
|
54
|
+
...
|
|
55
|
+
|
|
56
|
+
def safe_load_meta(self) -> NgioLabelMeta | ConverterError:
|
|
57
|
+
"""Load the metadata from the store."""
|
|
58
|
+
...
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def meta(self) -> NgioLabelMeta:
|
|
62
|
+
"""Return the metadata."""
|
|
63
|
+
...
|
|
64
|
+
|
|
65
|
+
def write_meta(self, meta: NgioLabelMeta) -> None:
|
|
66
|
+
"""Write the metadata to the store."""
|
|
67
|
+
...
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
###########################################################################
|
|
71
|
+
#
|
|
72
|
+
# The code below implements a generic class for handling OME-Zarr metadata
|
|
73
|
+
# in Zarr groups.
|
|
74
|
+
#
|
|
75
|
+
###########################################################################
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class ImageMetaImporter(Protocol):
|
|
79
|
+
@staticmethod
|
|
80
|
+
def __call__(
|
|
81
|
+
metadata: dict,
|
|
82
|
+
axes_setup: AxesSetup | None = None,
|
|
83
|
+
allow_non_canonical_axes: bool = False,
|
|
84
|
+
strict_canonical_order: bool = True,
|
|
85
|
+
) -> tuple[bool, NgioImageMeta | ConverterError]:
|
|
86
|
+
"""Convert the metadata to a NgioImageMeta object.
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
metadata (dict): The metadata (typically from a Zarr group .attrs).
|
|
90
|
+
axes_setup (AxesSetup, optional): The axes setup.
|
|
91
|
+
This is used to map axes with non-canonical names.
|
|
92
|
+
allow_non_canonical_axes (bool, optional): Whether to allow non-canonical
|
|
93
|
+
axes.
|
|
94
|
+
strict_canonical_order (bool, optional): Whether to enforce a strict
|
|
95
|
+
canonical order.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
tuple[bool, NgioImageMeta | ConverterError]: A tuple with a boolean
|
|
99
|
+
indicating whether the conversion was successful and the
|
|
100
|
+
NgioImageMeta object or an error.
|
|
101
|
+
|
|
102
|
+
"""
|
|
103
|
+
...
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class ImageMetaExporter(Protocol):
|
|
107
|
+
def __call__(self, metadata: NgioImageMeta) -> dict: ...
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class LabelMetaImporter(Protocol):
|
|
111
|
+
@staticmethod
|
|
112
|
+
def __call__(
|
|
113
|
+
metadata: dict,
|
|
114
|
+
axes_setup: AxesSetup | None = None,
|
|
115
|
+
allow_non_canonical_axes: bool = False,
|
|
116
|
+
strict_canonical_order: bool = True,
|
|
117
|
+
) -> tuple[bool, NgioLabelMeta | ConverterError]:
|
|
118
|
+
"""Convert the metadata to a NgioLabelMeta object.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
metadata (dict): The metadata (typically from a Zarr group .attrs).
|
|
122
|
+
axes_setup (AxesSetup, optional): The axes setup.
|
|
123
|
+
This is used to map axes with non-canonical names.
|
|
124
|
+
allow_non_canonical_axes (bool, optional): Whether to allow non-canonical
|
|
125
|
+
axes.
|
|
126
|
+
strict_canonical_order (bool, optional): Whether to enforce a strict
|
|
127
|
+
canonical order.
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
tuple[bool, NgioLabelMeta | ConverterError]: A tuple with a boolean
|
|
131
|
+
indicating whether the conversion was successful and the
|
|
132
|
+
NgioLabelMeta object or an error.
|
|
133
|
+
|
|
134
|
+
"""
|
|
135
|
+
...
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class LabelMetaExporter(Protocol):
|
|
139
|
+
def __call__(self, metadata: NgioLabelMeta) -> dict: ...
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
_meta = TypeVar("_meta", NgioImageMeta, NgioLabelMeta)
|
|
143
|
+
_meta_importer = TypeVar("_meta_importer", ImageMetaImporter, LabelMetaImporter)
|
|
144
|
+
_meta_exporter = TypeVar("_meta_exporter", ImageMetaExporter, LabelMetaExporter)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class GenericMetaHandler(Generic[_meta, _meta_importer, _meta_exporter]):
|
|
148
|
+
"""Generic class for handling OME-Zarr metadata in Zarr groups."""
|
|
149
|
+
|
|
150
|
+
def __init__(
|
|
151
|
+
self,
|
|
152
|
+
meta_importer: _meta_importer,
|
|
153
|
+
meta_exporter: _meta_exporter,
|
|
154
|
+
group_handler: ZarrGroupHandler,
|
|
155
|
+
axes_setup: AxesSetup | None = None,
|
|
156
|
+
allow_non_canonical_axes: bool = False,
|
|
157
|
+
strict_canonical_order: bool = True,
|
|
158
|
+
):
|
|
159
|
+
"""Initialize the handler.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
meta_importer (MetaImporter): The metadata importer.
|
|
163
|
+
meta_exporter (MetaExporter): The metadata exporter.
|
|
164
|
+
group_handler (ZarrGroupHandler): The Zarr group handler.
|
|
165
|
+
axes_setup (AxesSetup, optional): The axes setup.
|
|
166
|
+
This is used to map axes with non-canonical names.
|
|
167
|
+
allow_non_canonical_axes (bool, optional): Whether to allow non-canonical
|
|
168
|
+
axes.
|
|
169
|
+
strict_canonical_order (bool, optional): Whether to enforce a strict
|
|
170
|
+
canonical order.
|
|
171
|
+
"""
|
|
172
|
+
self._group_handler = group_handler
|
|
173
|
+
self._meta_importer = meta_importer
|
|
174
|
+
self._meta_exporter = meta_exporter
|
|
175
|
+
self._axes_setup = axes_setup
|
|
176
|
+
self._allow_non_canonical_axes = allow_non_canonical_axes
|
|
177
|
+
self._strict_canonical_order = strict_canonical_order
|
|
178
|
+
|
|
179
|
+
def _load_meta(self, return_error: bool = False):
|
|
180
|
+
"""Load the metadata from the store."""
|
|
181
|
+
attrs = self._group_handler.load_attrs()
|
|
182
|
+
is_valid, meta_or_error = self._meta_importer(
|
|
183
|
+
metadata=attrs,
|
|
184
|
+
axes_setup=self._axes_setup,
|
|
185
|
+
allow_non_canonical_axes=self._allow_non_canonical_axes,
|
|
186
|
+
strict_canonical_order=self._strict_canonical_order,
|
|
187
|
+
)
|
|
188
|
+
if is_valid:
|
|
189
|
+
return meta_or_error
|
|
190
|
+
|
|
191
|
+
if return_error:
|
|
192
|
+
return meta_or_error
|
|
193
|
+
|
|
194
|
+
raise NgioValueError(f"Could not load metadata: {meta_or_error}")
|
|
195
|
+
|
|
196
|
+
def safe_load_meta(self) -> _meta | ConverterError:
|
|
197
|
+
"""Load the metadata from the store."""
|
|
198
|
+
return self._load_meta(return_error=True)
|
|
199
|
+
|
|
200
|
+
def _write_meta(self, meta) -> None:
|
|
201
|
+
"""Write the metadata to the store."""
|
|
202
|
+
v04_meta = self._meta_exporter(metadata=meta)
|
|
203
|
+
self._group_handler.write_attrs(v04_meta)
|
|
204
|
+
|
|
205
|
+
def write_meta(self, meta: _meta) -> None:
|
|
206
|
+
"""Write the metadata to the store."""
|
|
207
|
+
raise NotImplementedError
|
|
208
|
+
|
|
209
|
+
@property
|
|
210
|
+
def meta(self) -> _meta:
|
|
211
|
+
"""Return the metadata."""
|
|
212
|
+
raise NotImplementedError
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
class BaseImageMetaHandler(
|
|
216
|
+
GenericMetaHandler[NgioImageMeta, ImageMetaImporter, ImageMetaExporter]
|
|
217
|
+
):
|
|
218
|
+
"""Generic class for handling OME-Zarr metadata in Zarr groups."""
|
|
219
|
+
|
|
220
|
+
def __init__(
|
|
221
|
+
self,
|
|
222
|
+
meta_importer: ImageMetaImporter,
|
|
223
|
+
meta_exporter: ImageMetaExporter,
|
|
224
|
+
group_handler: ZarrGroupHandler,
|
|
225
|
+
axes_setup: AxesSetup | None = None,
|
|
226
|
+
allow_non_canonical_axes: bool = False,
|
|
227
|
+
strict_canonical_order: bool = True,
|
|
228
|
+
):
|
|
229
|
+
"""Initialize the handler.
|
|
230
|
+
|
|
231
|
+
Args:
|
|
232
|
+
meta_importer (ImageMetaImporter): The metadata importer.
|
|
233
|
+
meta_exporter (ImageMetaExporter): The metadata exporter.
|
|
234
|
+
group_handler (ZarrGroupHandler): The Zarr group handler.
|
|
235
|
+
axes_setup (AxesSetup, optional): The axes setup.
|
|
236
|
+
This is used to map axes with non-canonical names.
|
|
237
|
+
allow_non_canonical_axes (bool, optional): Whether to allow non-canonical
|
|
238
|
+
axes.
|
|
239
|
+
strict_canonical_order (bool, optional): Whether to enforce a strict
|
|
240
|
+
canonical order.
|
|
241
|
+
"""
|
|
242
|
+
super().__init__(
|
|
243
|
+
meta_importer=meta_importer,
|
|
244
|
+
meta_exporter=meta_exporter,
|
|
245
|
+
group_handler=group_handler,
|
|
246
|
+
axes_setup=axes_setup,
|
|
247
|
+
allow_non_canonical_axes=allow_non_canonical_axes,
|
|
248
|
+
strict_canonical_order=strict_canonical_order,
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
def safe_load_meta(
|
|
252
|
+
self, return_error: bool = False
|
|
253
|
+
) -> NgioImageMeta | ConverterError:
|
|
254
|
+
"""Load the metadata from the store."""
|
|
255
|
+
return self._load_meta(return_error)
|
|
256
|
+
|
|
257
|
+
@property
|
|
258
|
+
def meta(self) -> NgioImageMeta:
|
|
259
|
+
"""Load the metadata from the store."""
|
|
260
|
+
meta = self._load_meta()
|
|
261
|
+
if isinstance(meta, NgioImageMeta):
|
|
262
|
+
return meta
|
|
263
|
+
raise NgioValueError(f"Could not load metadata: {meta}")
|
|
264
|
+
|
|
265
|
+
def write_meta(self, meta: NgioImageMeta) -> None:
|
|
266
|
+
self._write_meta(meta)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
class BaseLabelMetaHandler(
|
|
270
|
+
GenericMetaHandler[NgioLabelMeta, LabelMetaImporter, LabelMetaExporter]
|
|
271
|
+
):
|
|
272
|
+
"""Generic class for handling OME-Zarr metadata in Zarr groups."""
|
|
273
|
+
|
|
274
|
+
def __init__(
|
|
275
|
+
self,
|
|
276
|
+
meta_importer: LabelMetaImporter,
|
|
277
|
+
meta_exporter: LabelMetaExporter,
|
|
278
|
+
group_handler: ZarrGroupHandler,
|
|
279
|
+
axes_setup: AxesSetup | None = None,
|
|
280
|
+
allow_non_canonical_axes: bool = False,
|
|
281
|
+
strict_canonical_order: bool = True,
|
|
282
|
+
):
|
|
283
|
+
"""Initialize the handler.
|
|
284
|
+
|
|
285
|
+
Args:
|
|
286
|
+
meta_importer (LabelMetaImporter): The metadata importer.
|
|
287
|
+
meta_exporter (LabelMetaExporter): The metadata exporter.
|
|
288
|
+
group_handler (ZarrGroupHandler): The Zarr group handler.
|
|
289
|
+
axes_setup (AxesSetup, optional): The axes setup.
|
|
290
|
+
This is used to map axes with non-canonical names.
|
|
291
|
+
allow_non_canonical_axes (bool, optional): Whether to allow non-canonical
|
|
292
|
+
axes.
|
|
293
|
+
strict_canonical_order (bool, optional): Whether to enforce a strict
|
|
294
|
+
canonical order.
|
|
295
|
+
"""
|
|
296
|
+
super().__init__(
|
|
297
|
+
meta_importer=meta_importer,
|
|
298
|
+
meta_exporter=meta_exporter,
|
|
299
|
+
group_handler=group_handler,
|
|
300
|
+
axes_setup=axes_setup,
|
|
301
|
+
allow_non_canonical_axes=allow_non_canonical_axes,
|
|
302
|
+
strict_canonical_order=strict_canonical_order,
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
def safe_load_meta(
|
|
306
|
+
self, return_error: bool = False
|
|
307
|
+
) -> NgioLabelMeta | ConverterError:
|
|
308
|
+
"""Load the metadata from the store."""
|
|
309
|
+
return self._load_meta(return_error)
|
|
310
|
+
|
|
311
|
+
@property
|
|
312
|
+
def meta(self) -> NgioLabelMeta:
|
|
313
|
+
"""Load the metadata from the store."""
|
|
314
|
+
meta = self._load_meta()
|
|
315
|
+
if isinstance(meta, NgioLabelMeta):
|
|
316
|
+
return meta
|
|
317
|
+
raise NgioValueError(f"Could not load metadata: {meta}")
|
|
318
|
+
|
|
319
|
+
def write_meta(self, meta: NgioLabelMeta) -> None:
|
|
320
|
+
self._write_meta(meta)
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
from typing import Any, Generic, TypeVar
|
|
2
|
+
|
|
3
|
+
from pydantic import ValidationError
|
|
4
|
+
|
|
5
|
+
from ngio.ome_zarr_meta._generic_handlers import (
|
|
6
|
+
ImageMetaHandler,
|
|
7
|
+
LabelMetaHandler,
|
|
8
|
+
)
|
|
9
|
+
from ngio.ome_zarr_meta.ngio_specs import AxesSetup
|
|
10
|
+
from ngio.ome_zarr_meta.v04 import V04ImageMetaHandler, V04LabelMetaHandler
|
|
11
|
+
from ngio.utils import (
|
|
12
|
+
AccessModeLiteral,
|
|
13
|
+
NgioValidationError,
|
|
14
|
+
NgioValueError,
|
|
15
|
+
StoreOrGroup,
|
|
16
|
+
ZarrGroupHandler,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
_Image_or_Label_Plugin = TypeVar(
|
|
20
|
+
"_Image_or_Label_Plugin", ImageMetaHandler, LabelMetaHandler
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _ImplementedMetaHandlers(Generic[_Image_or_Label_Plugin]):
|
|
25
|
+
"""This class is a singleton that manages the available image handler plugins."""
|
|
26
|
+
|
|
27
|
+
_instance = None
|
|
28
|
+
_implemented_handlers: dict[str, Any]
|
|
29
|
+
|
|
30
|
+
def __new__(cls):
|
|
31
|
+
"""Create a new instance of the class if it does not exist."""
|
|
32
|
+
if cls._instance is None:
|
|
33
|
+
cls._instance = super().__new__(cls)
|
|
34
|
+
cls._instance._implemented_handlers = {}
|
|
35
|
+
return cls._instance
|
|
36
|
+
|
|
37
|
+
def available_handlers(self) -> list[str]:
|
|
38
|
+
"""Get the available image handler versions.
|
|
39
|
+
|
|
40
|
+
The versions are returned in descending order.
|
|
41
|
+
such that the latest version is the first in the list and the fist to be
|
|
42
|
+
checked.
|
|
43
|
+
"""
|
|
44
|
+
return list(reversed(self._implemented_handlers.keys()))
|
|
45
|
+
|
|
46
|
+
def find_meta_handler(
|
|
47
|
+
self,
|
|
48
|
+
group_handler: ZarrGroupHandler,
|
|
49
|
+
axes_setup: AxesSetup | None = None,
|
|
50
|
+
allow_non_canonical_axes: bool = False,
|
|
51
|
+
strict_canonical_order: bool = True,
|
|
52
|
+
) -> _Image_or_Label_Plugin:
|
|
53
|
+
"""Try to get a handler for the given store based on the metadata version."""
|
|
54
|
+
_errors = {}
|
|
55
|
+
|
|
56
|
+
for version, handler in reversed(self._implemented_handlers.items()):
|
|
57
|
+
handler = handler(
|
|
58
|
+
group_handler=group_handler,
|
|
59
|
+
axes_setup=axes_setup,
|
|
60
|
+
allow_non_canonical_axes=allow_non_canonical_axes,
|
|
61
|
+
strict_canonical_order=strict_canonical_order,
|
|
62
|
+
)
|
|
63
|
+
meta = handler.safe_load_meta()
|
|
64
|
+
if isinstance(meta, ValidationError):
|
|
65
|
+
_errors[version] = meta
|
|
66
|
+
continue
|
|
67
|
+
return handler
|
|
68
|
+
|
|
69
|
+
raise NgioValidationError(
|
|
70
|
+
f"Could not load OME-Zarr metadata from any known version. "
|
|
71
|
+
f"Errors: {_errors}"
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
def get_handler(
|
|
75
|
+
self,
|
|
76
|
+
version: str,
|
|
77
|
+
group_handler: ZarrGroupHandler,
|
|
78
|
+
axes_setup: AxesSetup | None = None,
|
|
79
|
+
allow_non_canonical_axes: bool = False,
|
|
80
|
+
strict_canonical_order: bool = True,
|
|
81
|
+
) -> _Image_or_Label_Plugin:
|
|
82
|
+
"""Get a handler for a specific version."""
|
|
83
|
+
if version not in self._implemented_handlers:
|
|
84
|
+
raise NgioValueError(f"Image handler for version {version} does not exist.")
|
|
85
|
+
return self._implemented_handlers[version](
|
|
86
|
+
group_handler=group_handler,
|
|
87
|
+
axes_setup=axes_setup,
|
|
88
|
+
allow_non_canonical_axes=allow_non_canonical_axes,
|
|
89
|
+
strict_canonical_order=strict_canonical_order,
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
def add_handler(
|
|
93
|
+
self, key: str, handler: type[_Image_or_Label_Plugin], overwrite: bool = False
|
|
94
|
+
):
|
|
95
|
+
"""Register a new handler."""
|
|
96
|
+
if key in self._implemented_handlers and not overwrite:
|
|
97
|
+
raise NgioValueError(f"Image handler for version {key} already exists.")
|
|
98
|
+
self._implemented_handlers[key] = handler
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class ImplementedImageMetaHandlers(_ImplementedMetaHandlers[ImageMetaHandler]):
|
|
102
|
+
def __init__(self):
|
|
103
|
+
super().__init__()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
ImplementedImageMetaHandlers().add_handler("0.4", V04ImageMetaHandler)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class ImplementedLabelMetaHandlers(_ImplementedMetaHandlers[LabelMetaHandler]):
|
|
110
|
+
def __init__(self):
|
|
111
|
+
super().__init__()
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
ImplementedLabelMetaHandlers().add_handler("0.4", V04LabelMetaHandler)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def open_image_meta_handler(
|
|
118
|
+
store: StoreOrGroup,
|
|
119
|
+
cache: bool = False,
|
|
120
|
+
mode: AccessModeLiteral = "a",
|
|
121
|
+
axes_setup: AxesSetup | None = None,
|
|
122
|
+
allow_non_canonical_axes: bool = False,
|
|
123
|
+
strict_canonical_order: bool = True,
|
|
124
|
+
) -> ImageMetaHandler:
|
|
125
|
+
"""Open the metadata of an OME-Zarr image.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
store: The Zarr store or group where the model is
|
|
129
|
+
cache: Whether to cache the metadata.
|
|
130
|
+
mode: The store access mode.
|
|
131
|
+
axes_setup: The axes setup. This is used to map axes with
|
|
132
|
+
a non-canonical name to a canonical name.
|
|
133
|
+
allow_non_canonical_axes: Whether to allow non-canonical axes.
|
|
134
|
+
strict_canonical_order: Whether to enforce strict canonical order.
|
|
135
|
+
"""
|
|
136
|
+
zarr_group_handler = ZarrGroupHandler(store, cache, mode)
|
|
137
|
+
return ImplementedImageMetaHandlers().find_meta_handler(
|
|
138
|
+
zarr_group_handler,
|
|
139
|
+
axes_setup=axes_setup,
|
|
140
|
+
allow_non_canonical_axes=allow_non_canonical_axes,
|
|
141
|
+
strict_canonical_order=strict_canonical_order,
|
|
142
|
+
)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""ngio internal specs module.
|
|
2
|
+
|
|
3
|
+
Since the OME-Zarr specification are still evolving, this module provides a
|
|
4
|
+
set of classes to internally handle the metadata.
|
|
5
|
+
|
|
6
|
+
This models can be tr
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from ngio.ome_zarr_meta.ngio_specs._axes import (
|
|
10
|
+
AxesExpand,
|
|
11
|
+
AxesMapper,
|
|
12
|
+
AxesSetup,
|
|
13
|
+
AxesSqueeze,
|
|
14
|
+
AxesTransformation,
|
|
15
|
+
AxesTranspose,
|
|
16
|
+
Axis,
|
|
17
|
+
AxisType,
|
|
18
|
+
SpaceUnits,
|
|
19
|
+
TimeUnits,
|
|
20
|
+
canonical_axes_order,
|
|
21
|
+
canonical_label_axes_order,
|
|
22
|
+
)
|
|
23
|
+
from ngio.ome_zarr_meta.ngio_specs._channels import (
|
|
24
|
+
Channel,
|
|
25
|
+
ChannelsMeta,
|
|
26
|
+
ChannelVisualisation,
|
|
27
|
+
NgioColors,
|
|
28
|
+
default_channel_name,
|
|
29
|
+
)
|
|
30
|
+
from ngio.ome_zarr_meta.ngio_specs._dataset import Dataset
|
|
31
|
+
from ngio.ome_zarr_meta.ngio_specs._ngio_image import (
|
|
32
|
+
ImageLabelSource,
|
|
33
|
+
NgioImageLabelMeta,
|
|
34
|
+
NgioImageMeta,
|
|
35
|
+
NgioLabelMeta,
|
|
36
|
+
)
|
|
37
|
+
from ngio.ome_zarr_meta.ngio_specs._pixel_size import PixelSize
|
|
38
|
+
|
|
39
|
+
__all__ = [
|
|
40
|
+
"AxesExpand",
|
|
41
|
+
"AxesMapper",
|
|
42
|
+
"AxesSetup",
|
|
43
|
+
"AxesSqueeze",
|
|
44
|
+
"AxesTransformation",
|
|
45
|
+
"AxesTranspose",
|
|
46
|
+
"Axis",
|
|
47
|
+
"AxisType",
|
|
48
|
+
"Channel",
|
|
49
|
+
"ChannelVisualisation",
|
|
50
|
+
"ChannelsMeta",
|
|
51
|
+
"Dataset",
|
|
52
|
+
"ImageLabelSource",
|
|
53
|
+
"NgioColors",
|
|
54
|
+
"NgioImageLabelMeta",
|
|
55
|
+
"NgioImageMeta",
|
|
56
|
+
"NgioLabelMeta",
|
|
57
|
+
"PixelSize",
|
|
58
|
+
"SpaceUnits",
|
|
59
|
+
"TimeUnits",
|
|
60
|
+
"canonical_axes_order",
|
|
61
|
+
"canonical_label_axes_order",
|
|
62
|
+
"default_channel_name",
|
|
63
|
+
]
|