ngio 0.4.8__py3-none-any.whl → 0.5.0__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.
Files changed (56) hide show
  1. ngio/__init__.py +5 -2
  2. ngio/common/__init__.py +11 -6
  3. ngio/common/_masking_roi.py +34 -54
  4. ngio/common/_pyramid.py +322 -75
  5. ngio/common/_roi.py +258 -330
  6. ngio/experimental/iterators/_feature.py +3 -3
  7. ngio/experimental/iterators/_rois_utils.py +10 -11
  8. ngio/hcs/_plate.py +192 -136
  9. ngio/images/_abstract_image.py +539 -35
  10. ngio/images/_create_synt_container.py +45 -47
  11. ngio/images/_create_utils.py +406 -0
  12. ngio/images/_image.py +524 -248
  13. ngio/images/_label.py +257 -180
  14. ngio/images/_masked_image.py +2 -2
  15. ngio/images/_ome_zarr_container.py +658 -255
  16. ngio/io_pipes/_io_pipes.py +9 -9
  17. ngio/io_pipes/_io_pipes_masked.py +7 -7
  18. ngio/io_pipes/_io_pipes_roi.py +6 -6
  19. ngio/io_pipes/_io_pipes_types.py +3 -3
  20. ngio/io_pipes/_match_shape.py +6 -8
  21. ngio/io_pipes/_ops_slices_utils.py +8 -5
  22. ngio/ome_zarr_meta/__init__.py +29 -18
  23. ngio/ome_zarr_meta/_meta_handlers.py +402 -689
  24. ngio/ome_zarr_meta/ngio_specs/__init__.py +4 -0
  25. ngio/ome_zarr_meta/ngio_specs/_axes.py +152 -51
  26. ngio/ome_zarr_meta/ngio_specs/_dataset.py +13 -22
  27. ngio/ome_zarr_meta/ngio_specs/_ngio_hcs.py +129 -91
  28. ngio/ome_zarr_meta/ngio_specs/_ngio_image.py +69 -69
  29. ngio/ome_zarr_meta/v04/__init__.py +5 -1
  30. ngio/ome_zarr_meta/v04/{_v04_spec_utils.py → _v04_spec.py} +55 -86
  31. ngio/ome_zarr_meta/v05/__init__.py +27 -0
  32. ngio/ome_zarr_meta/v05/_custom_models.py +18 -0
  33. ngio/ome_zarr_meta/v05/_v05_spec.py +495 -0
  34. ngio/resources/__init__.py +1 -1
  35. ngio/resources/resource_model.py +1 -1
  36. ngio/tables/_tables_container.py +82 -24
  37. ngio/tables/backends/_abstract_backend.py +7 -0
  38. ngio/tables/backends/_anndata.py +60 -7
  39. ngio/tables/backends/_anndata_utils.py +2 -4
  40. ngio/tables/backends/_csv.py +3 -19
  41. ngio/tables/backends/_json.py +10 -13
  42. ngio/tables/backends/_parquet.py +3 -31
  43. ngio/tables/backends/_py_arrow_backends.py +222 -0
  44. ngio/tables/backends/_utils.py +1 -1
  45. ngio/tables/v1/_roi_table.py +41 -24
  46. ngio/utils/__init__.py +8 -12
  47. ngio/utils/_cache.py +48 -0
  48. ngio/utils/_zarr_utils.py +354 -236
  49. {ngio-0.4.8.dist-info → ngio-0.5.0.dist-info}/METADATA +12 -5
  50. ngio-0.5.0.dist-info/RECORD +88 -0
  51. ngio/images/_create.py +0 -276
  52. ngio/tables/backends/_non_zarr_backends.py +0 -196
  53. ngio/utils/_logger.py +0 -50
  54. ngio-0.4.8.dist-info/RECORD +0 -85
  55. {ngio-0.4.8.dist-info → ngio-0.5.0.dist-info}/WHEEL +0 -0
  56. {ngio-0.4.8.dist-info → ngio-0.5.0.dist-info}/licenses/LICENSE +0 -0
@@ -1,12 +1,14 @@
1
1
  """Utility to read/write OME-Zarr metadata v0.4."""
2
2
 
3
- from ngio.ome_zarr_meta.v04._v04_spec_utils import (
3
+ from ngio.ome_zarr_meta.v04._v04_spec import (
4
4
  ngio_to_v04_image_meta,
5
5
  ngio_to_v04_label_meta,
6
+ ngio_to_v04_labels_group_meta,
6
7
  ngio_to_v04_plate_meta,
7
8
  ngio_to_v04_well_meta,
8
9
  v04_to_ngio_image_meta,
9
10
  v04_to_ngio_label_meta,
11
+ v04_to_ngio_labels_group_meta,
10
12
  v04_to_ngio_plate_meta,
11
13
  v04_to_ngio_well_meta,
12
14
  )
@@ -14,10 +16,12 @@ from ngio.ome_zarr_meta.v04._v04_spec_utils import (
14
16
  __all__ = [
15
17
  "ngio_to_v04_image_meta",
16
18
  "ngio_to_v04_label_meta",
19
+ "ngio_to_v04_labels_group_meta",
17
20
  "ngio_to_v04_plate_meta",
18
21
  "ngio_to_v04_well_meta",
19
22
  "v04_to_ngio_image_meta",
20
23
  "v04_to_ngio_label_meta",
24
+ "v04_to_ngio_labels_group_meta",
21
25
  "v04_to_ngio_plate_meta",
22
26
  "v04_to_ngio_well_meta",
23
27
  ]
@@ -9,7 +9,9 @@ For Images and Labels implements the following functionalities:
9
9
  - A function to convert a ngio image metadata to a v04 image metadata.
10
10
  """
11
11
 
12
- from ome_zarr_models.common.multiscales import ValidTransform as ValidTransformV04
12
+ from ome_zarr_models.common.coordinate_transformations import (
13
+ ValidTransform as ValidTransformV04,
14
+ )
13
15
  from ome_zarr_models.v04.axes import Axis as AxisV04
14
16
  from ome_zarr_models.v04.coordinate_transformations import VectorScale as VectorScaleV04
15
17
  from ome_zarr_models.v04.coordinate_transformations import (
@@ -17,13 +19,13 @@ from ome_zarr_models.v04.coordinate_transformations import (
17
19
  )
18
20
  from ome_zarr_models.v04.hcs import HCSAttrs as HCSAttrsV04
19
21
  from ome_zarr_models.v04.image import ImageAttrs as ImageAttrsV04
20
- from ome_zarr_models.v04.image_label import ImageLabelAttrs as LabelAttrsV04
22
+ from ome_zarr_models.v04.image_label import ImageLabelAttrs as ImageLabelAttrsV04
23
+ from ome_zarr_models.v04.labels import LabelsAttrs as LabelsAttrsV04
21
24
  from ome_zarr_models.v04.multiscales import Dataset as DatasetV04
22
25
  from ome_zarr_models.v04.multiscales import Multiscale as MultiscaleV04
23
26
  from ome_zarr_models.v04.omero import Channel as ChannelV04
24
27
  from ome_zarr_models.v04.omero import Omero as OmeroV04
25
28
  from ome_zarr_models.v04.omero import Window as WindowV04
26
- from pydantic import ValidationError
27
29
 
28
30
  from ngio.ome_zarr_meta.ngio_specs import (
29
31
  AxesHandler,
@@ -37,6 +39,7 @@ from ngio.ome_zarr_meta.ngio_specs import (
37
39
  ImageLabelSource,
38
40
  NgioImageMeta,
39
41
  NgioLabelMeta,
42
+ NgioLabelsGroupMeta,
40
43
  NgioPlateMeta,
41
44
  NgioWellMeta,
42
45
  default_channel_name,
@@ -44,37 +47,6 @@ from ngio.ome_zarr_meta.ngio_specs import (
44
47
  from ngio.ome_zarr_meta.v04._custom_models import CustomWellAttrs as WellAttrsV04
45
48
 
46
49
 
47
- def _is_v04_image_meta(metadata: dict) -> ImageAttrsV04 | ValidationError:
48
- """Check if the metadata is a valid OME-Zarr v04 metadata.
49
-
50
- Args:
51
- metadata (dict): The metadata to check.
52
-
53
- Returns:
54
- bool: True if the metadata is a valid OME-Zarr v04 metadata, False otherwise.
55
- """
56
- try:
57
- return ImageAttrsV04(**metadata)
58
- except ValidationError as e:
59
- return e
60
-
61
-
62
- def _is_v04_label_meta(metadata: dict) -> LabelAttrsV04 | ValidationError:
63
- """Check if the metadata is a valid OME-Zarr v04 metadata.
64
-
65
- Args:
66
- metadata (dict): The metadata to check.
67
-
68
- Returns:
69
- bool: True if the metadata is a valid OME-Zarr v04 metadata, False otherwise.
70
- """
71
- try:
72
- return LabelAttrsV04(**metadata)
73
- except ValidationError as e:
74
- return e
75
- raise RuntimeError("Unreachable code")
76
-
77
-
78
50
  def _v04_omero_to_channels(v04_omero: OmeroV04 | None) -> ChannelsMeta | None:
79
51
  if v04_omero is None:
80
52
  return None
@@ -147,8 +119,6 @@ def _compute_scale_translation(
147
119
  def _v04_to_ngio_datasets(
148
120
  v04_multiscale: MultiscaleV04,
149
121
  axes_setup: AxesSetup,
150
- allow_non_canonical_axes: bool = False,
151
- strict_canonical_order: bool = True,
152
122
  ) -> list[Dataset]:
153
123
  """Convert a v04 multiscale to a list of ngio datasets."""
154
124
  datasets = []
@@ -169,7 +139,7 @@ def _v04_to_ngio_datasets(
169
139
  unit = str(unit)
170
140
  axes.append(
171
141
  Axis(
172
- name=v04_axis.name,
142
+ name=str(v04_axis.name),
173
143
  axis_type=AxisType(v04_axis.type),
174
144
  # (for some reason the type is a generic JsonValue,
175
145
  # but it should be a string or None)
@@ -179,8 +149,6 @@ def _v04_to_ngio_datasets(
179
149
  axes_handler = AxesHandler(
180
150
  axes=axes,
181
151
  axes_setup=axes_setup,
182
- allow_non_canonical_axes=allow_non_canonical_axes,
183
- strict_canonical_order=strict_canonical_order,
184
152
  )
185
153
 
186
154
  for v04_dataset in v04_multiscale.datasets:
@@ -200,25 +168,19 @@ def _v04_to_ngio_datasets(
200
168
 
201
169
  def v04_to_ngio_image_meta(
202
170
  metadata: dict,
203
- axes_setup: AxesSetup | None = None,
204
- allow_non_canonical_axes: bool = False,
205
- strict_canonical_order: bool = True,
206
- ) -> tuple[bool, NgioImageMeta | ValidationError]:
171
+ axes_setup: AxesSetup,
172
+ ) -> NgioImageMeta:
207
173
  """Convert a v04 image metadata to a ngio image metadata.
208
174
 
209
175
  Args:
210
176
  metadata (dict): The v04 image metadata.
211
177
  axes_setup (AxesSetup, optional): The axes setup. This is
212
178
  required to convert image with non-canonical axes names.
213
- allow_non_canonical_axes (bool, optional): Allow non-canonical axes.
214
- strict_canonical_order (bool, optional): Strict canonical order.
215
179
 
216
180
  Returns:
217
181
  NgioImageMeta: The ngio image metadata.
218
182
  """
219
- v04_image = _is_v04_image_meta(metadata)
220
- if isinstance(v04_image, ValidationError):
221
- return False, v04_image
183
+ v04_image = ImageAttrsV04(**metadata)
222
184
 
223
185
  if len(v04_image.multiscales) > 1:
224
186
  raise NotImplementedError(
@@ -228,18 +190,15 @@ def v04_to_ngio_image_meta(
228
190
  v04_muliscale = v04_image.multiscales[0]
229
191
 
230
192
  channels_meta = _v04_omero_to_channels(v04_image.omero)
231
- axes_setup = axes_setup if axes_setup is not None else AxesSetup()
232
193
  datasets = _v04_to_ngio_datasets(
233
194
  v04_muliscale,
234
195
  axes_setup=axes_setup,
235
- allow_non_canonical_axes=allow_non_canonical_axes,
236
- strict_canonical_order=strict_canonical_order,
237
196
  )
238
197
 
239
198
  name = v04_muliscale.name
240
199
  if name is not None and not isinstance(name, str):
241
200
  name = str(name)
242
- return True, NgioImageMeta(
201
+ return NgioImageMeta(
243
202
  version="0.4",
244
203
  name=name,
245
204
  datasets=datasets,
@@ -249,25 +208,19 @@ def v04_to_ngio_image_meta(
249
208
 
250
209
  def v04_to_ngio_label_meta(
251
210
  metadata: dict,
252
- axes_setup: AxesSetup | None = None,
253
- allow_non_canonical_axes: bool = False,
254
- strict_canonical_order: bool = True,
255
- ) -> tuple[bool, NgioLabelMeta | ValidationError]:
211
+ axes_setup: AxesSetup,
212
+ ) -> NgioLabelMeta:
256
213
  """Convert a v04 image metadata to a ngio image metadata.
257
214
 
258
215
  Args:
259
216
  metadata (dict): The v04 image metadata.
260
217
  axes_setup (AxesSetup, optional): The axes setup. This is
261
218
  required to convert image with non-canonical axes names.
262
- allow_non_canonical_axes (bool, optional): Allow non-canonical axes.
263
- strict_canonical_order (bool, optional): Strict canonical order.
264
219
 
265
220
  Returns:
266
221
  NgioImageMeta: The ngio image metadata.
267
222
  """
268
- v04_label = _is_v04_label_meta(metadata)
269
- if isinstance(v04_label, ValidationError):
270
- return False, v04_label
223
+ v04_label = ImageLabelAttrsV04(**metadata)
271
224
 
272
225
  if len(v04_label.multiscales) > 1:
273
226
  raise NotImplementedError(
@@ -275,13 +228,9 @@ def v04_to_ngio_label_meta(
275
228
  )
276
229
 
277
230
  v04_muliscale = v04_label.multiscales[0]
278
-
279
- axes_setup = axes_setup if axes_setup is not None else AxesSetup()
280
231
  datasets = _v04_to_ngio_datasets(
281
232
  v04_muliscale,
282
233
  axes_setup=axes_setup,
283
- allow_non_canonical_axes=allow_non_canonical_axes,
284
- strict_canonical_order=strict_canonical_order,
285
234
  )
286
235
 
287
236
  source = v04_label.image_label.source
@@ -301,7 +250,7 @@ def v04_to_ngio_label_meta(
301
250
  if name is not None and not isinstance(name, str):
302
251
  name = str(name)
303
252
 
304
- return True, NgioLabelMeta(
253
+ return NgioLabelMeta(
305
254
  version="0.4",
306
255
  name=name,
307
256
  datasets=datasets,
@@ -417,48 +366,55 @@ def ngio_to_v04_label_meta(metadata: NgioLabelMeta) -> dict:
417
366
  "multiscales": [v04_muliscale],
418
367
  "image-label": metadata.image_label.model_dump(),
419
368
  }
420
- v04_label = LabelAttrsV04(**labels_meta)
369
+ v04_label = ImageLabelAttrsV04(**labels_meta)
421
370
  return v04_label.model_dump(exclude_none=True, by_alias=True)
422
371
 
423
372
 
373
+ def v04_to_ngio_labels_group_meta(
374
+ metadata: dict,
375
+ ) -> NgioLabelsGroupMeta:
376
+ """Convert a v04 label group metadata to a ngio label group metadata.
377
+
378
+ Args:
379
+ metadata (dict): The v04 label group metadata.
380
+
381
+ Returns:
382
+ NgioLabelGroupMeta: The ngio label group metadata.
383
+ """
384
+ v04_label_group = LabelsAttrsV04(**metadata).model_dump()
385
+ labels = v04_label_group.get("labels", [])
386
+ return NgioLabelsGroupMeta(labels=labels, version="0.4")
387
+
388
+
424
389
  def v04_to_ngio_well_meta(
425
390
  metadata: dict,
426
- ) -> tuple[bool, NgioWellMeta | ValidationError]:
391
+ ) -> NgioWellMeta:
427
392
  """Convert a v04 well metadata to a ngio well metadata.
428
393
 
429
394
  Args:
430
395
  metadata (dict): The v04 well metadata.
431
396
 
432
397
  Returns:
433
- result (bool): True if the conversion was successful, False otherwise.
434
- ngio_well_meta (NgioWellMeta): The ngio well metadata.
398
+ NgioWellMeta: The ngio well metadata.
435
399
  """
436
- try:
437
- v04_well = WellAttrsV04(**metadata)
438
- except ValidationError as e:
439
- return False, e
440
-
441
- return True, NgioWellMeta(**v04_well.model_dump())
400
+ v04_well = WellAttrsV04(**metadata).well.model_dump()
401
+ images = v04_well.get("images", [])
402
+ return NgioWellMeta(images=images, version="0.4")
442
403
 
443
404
 
444
405
  def v04_to_ngio_plate_meta(
445
406
  metadata: dict,
446
- ) -> tuple[bool, NgioPlateMeta | ValidationError]:
407
+ ) -> NgioPlateMeta:
447
408
  """Convert a v04 plate metadata to a ngio plate metadata.
448
409
 
449
410
  Args:
450
411
  metadata (dict): The v04 plate metadata.
451
412
 
452
413
  Returns:
453
- result (bool): True if the conversion was successful, False otherwise.
454
- ngio_plate_meta (NgioPlateMeta): The ngio plate metadata.
414
+ NgioPlateMeta: The ngio plate metadata.
455
415
  """
456
- try:
457
- v04_plate = HCSAttrsV04(**metadata)
458
- except ValidationError as e:
459
- return False, e
460
-
461
- return True, NgioPlateMeta(**v04_plate.model_dump())
416
+ v04_plate = HCSAttrsV04(**metadata).plate.model_dump()
417
+ return NgioPlateMeta(plate=v04_plate, version="0.4") # type: ignore
462
418
 
463
419
 
464
420
  def ngio_to_v04_well_meta(metadata: NgioWellMeta) -> dict:
@@ -470,7 +426,7 @@ def ngio_to_v04_well_meta(metadata: NgioWellMeta) -> dict:
470
426
  Returns:
471
427
  dict: The v04 well metadata.
472
428
  """
473
- v04_well = WellAttrsV04(**metadata.model_dump())
429
+ v04_well = WellAttrsV04(well=metadata.model_dump()) # type: ignore
474
430
  return v04_well.model_dump(exclude_none=True, by_alias=True)
475
431
 
476
432
 
@@ -485,3 +441,16 @@ def ngio_to_v04_plate_meta(metadata: NgioPlateMeta) -> dict:
485
441
  """
486
442
  v04_plate = HCSAttrsV04(**metadata.model_dump())
487
443
  return v04_plate.model_dump(exclude_none=True, by_alias=True)
444
+
445
+
446
+ def ngio_to_v04_labels_group_meta(metadata: NgioLabelsGroupMeta) -> dict:
447
+ """Convert a ngio label group metadata to a v04 label group metadata.
448
+
449
+ Args:
450
+ metadata (NgioLabelsGroupMeta): The ngio label group metadata.
451
+
452
+ Returns:
453
+ dict: The v04 label group metadata.
454
+ """
455
+ v04_label_group = LabelsAttrsV04(labels=metadata.labels)
456
+ return v04_label_group.model_dump(exclude_none=True, by_alias=True)
@@ -0,0 +1,27 @@
1
+ """Utility to read/write OME-Zarr metadata v0.4."""
2
+
3
+ from ngio.ome_zarr_meta.v05._v05_spec import (
4
+ ngio_to_v05_image_meta,
5
+ ngio_to_v05_label_meta,
6
+ ngio_to_v05_labels_group_meta,
7
+ ngio_to_v05_plate_meta,
8
+ ngio_to_v05_well_meta,
9
+ v05_to_ngio_image_meta,
10
+ v05_to_ngio_label_meta,
11
+ v05_to_ngio_labels_group_meta,
12
+ v05_to_ngio_plate_meta,
13
+ v05_to_ngio_well_meta,
14
+ )
15
+
16
+ __all__ = [
17
+ "ngio_to_v05_image_meta",
18
+ "ngio_to_v05_label_meta",
19
+ "ngio_to_v05_labels_group_meta",
20
+ "ngio_to_v05_plate_meta",
21
+ "ngio_to_v05_well_meta",
22
+ "v05_to_ngio_image_meta",
23
+ "v05_to_ngio_label_meta",
24
+ "v05_to_ngio_labels_group_meta",
25
+ "v05_to_ngio_plate_meta",
26
+ "v05_to_ngio_well_meta",
27
+ ]
@@ -0,0 +1,18 @@
1
+ from typing import Annotated
2
+
3
+ from ome_zarr_models.v05.well import WellAttrs as WellAttrs05
4
+ from ome_zarr_models.v05.well_types import WellImage as WellImage05
5
+ from ome_zarr_models.v05.well_types import WellMeta as WellMeta05
6
+ from pydantic import SkipValidation
7
+
8
+
9
+ class CustomWellImage(WellImage05):
10
+ path: Annotated[str, SkipValidation]
11
+
12
+
13
+ class CustomWellMeta(WellMeta05):
14
+ images: list[CustomWellImage] # type: ignore[valid-type]
15
+
16
+
17
+ class CustomWellAttrs(WellAttrs05):
18
+ well: CustomWellMeta # type: ignore[valid-type]