ngio 0.5.0a2__py3-none-any.whl → 0.5.0a3__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 (47) hide show
  1. ngio/__init__.py +2 -2
  2. ngio/common/__init__.py +11 -6
  3. ngio/common/_masking_roi.py +12 -41
  4. ngio/common/_pyramid.py +206 -76
  5. ngio/common/_roi.py +257 -329
  6. ngio/experimental/iterators/_feature.py +3 -3
  7. ngio/experimental/iterators/_rois_utils.py +10 -11
  8. ngio/hcs/_plate.py +50 -43
  9. ngio/images/_abstract_image.py +417 -35
  10. ngio/images/_create_synt_container.py +35 -42
  11. ngio/images/_create_utils.py +423 -0
  12. ngio/images/_image.py +154 -176
  13. ngio/images/_label.py +144 -119
  14. ngio/images/_ome_zarr_container.py +361 -196
  15. ngio/io_pipes/_io_pipes.py +9 -9
  16. ngio/io_pipes/_io_pipes_masked.py +7 -7
  17. ngio/io_pipes/_io_pipes_roi.py +6 -6
  18. ngio/io_pipes/_io_pipes_types.py +3 -3
  19. ngio/io_pipes/_match_shape.py +5 -4
  20. ngio/io_pipes/_ops_slices_utils.py +8 -5
  21. ngio/ome_zarr_meta/__init__.py +15 -18
  22. ngio/ome_zarr_meta/_meta_handlers.py +334 -713
  23. ngio/ome_zarr_meta/ngio_specs/_axes.py +1 -0
  24. ngio/ome_zarr_meta/ngio_specs/_dataset.py +13 -22
  25. ngio/ome_zarr_meta/ngio_specs/_ngio_hcs.py +54 -61
  26. ngio/ome_zarr_meta/ngio_specs/_ngio_image.py +14 -68
  27. ngio/ome_zarr_meta/v04/__init__.py +1 -1
  28. ngio/ome_zarr_meta/v04/{_v04_spec_utils.py → _v04_spec.py} +16 -61
  29. ngio/ome_zarr_meta/v05/__init__.py +1 -1
  30. ngio/ome_zarr_meta/v05/{_v05_spec_utils.py → _v05_spec.py} +18 -61
  31. ngio/tables/_tables_container.py +2 -4
  32. ngio/tables/backends/_anndata.py +57 -8
  33. ngio/tables/backends/_anndata_utils.py +1 -6
  34. ngio/tables/backends/_csv.py +3 -19
  35. ngio/tables/backends/_json.py +10 -13
  36. ngio/tables/backends/_parquet.py +3 -31
  37. ngio/tables/backends/_py_arrow_backends.py +222 -0
  38. ngio/tables/v1/_roi_table.py +41 -24
  39. ngio/utils/__init__.py +4 -12
  40. ngio/utils/_zarr_utils.py +160 -53
  41. {ngio-0.5.0a2.dist-info → ngio-0.5.0a3.dist-info}/METADATA +6 -2
  42. {ngio-0.5.0a2.dist-info → ngio-0.5.0a3.dist-info}/RECORD +44 -45
  43. {ngio-0.5.0a2.dist-info → ngio-0.5.0a3.dist-info}/WHEEL +1 -1
  44. ngio/images/_create.py +0 -287
  45. ngio/tables/backends/_non_zarr_backends.py +0 -196
  46. ngio/utils/_logger.py +0 -50
  47. {ngio-0.5.0a2.dist-info → ngio-0.5.0a3.dist-info}/licenses/LICENSE +0 -0
ngio/images/_label.py CHANGED
@@ -1,23 +1,23 @@
1
1
  """A module for handling label images in OME-NGFF files."""
2
2
 
3
- from collections.abc import Sequence
4
- from typing import Literal
3
+ from collections.abc import Mapping, Sequence
4
+ from typing import Any, Literal
5
5
 
6
6
  from zarr.core.array import CompressorLike
7
7
 
8
8
  from ngio.common import compute_masking_roi
9
- from ngio.images._abstract_image import AbstractImage
10
- from ngio.images._create import create_empty_label_container
9
+ from ngio.common._pyramid import ChunksLike, ShardsLike
10
+ from ngio.images._abstract_image import AbstractImage, abstract_derive
11
11
  from ngio.images._image import Image
12
12
  from ngio.ome_zarr_meta import (
13
13
  LabelMetaHandler,
14
14
  NgioLabelMeta,
15
15
  PixelSize,
16
- find_label_meta_handler,
17
16
  )
18
17
  from ngio.ome_zarr_meta.ngio_specs import (
19
18
  DefaultSpaceUnit,
20
19
  DefaultTimeUnit,
20
+ NgffVersions,
21
21
  SpaceUnits,
22
22
  TimeUnits,
23
23
  )
@@ -28,10 +28,9 @@ from ngio.utils import (
28
28
  StoreOrGroup,
29
29
  ZarrGroupHandler,
30
30
  )
31
- from ngio.utils._zarr_utils import find_dimension_separator
32
31
 
33
32
 
34
- class Label(AbstractImage[LabelMetaHandler]):
33
+ class Label(AbstractImage):
35
34
  """Placeholder class for a label."""
36
35
 
37
36
  get_as_numpy = AbstractImage._get_as_numpy
@@ -58,7 +57,7 @@ class Label(AbstractImage[LabelMetaHandler]):
58
57
 
59
58
  """
60
59
  if meta_handler is None:
61
- meta_handler = find_label_meta_handler(group_handler)
60
+ meta_handler = LabelMetaHandler(group_handler)
62
61
  super().__init__(
63
62
  group_handler=group_handler, path=path, meta_handler=meta_handler
64
63
  )
@@ -67,10 +66,18 @@ class Label(AbstractImage[LabelMetaHandler]):
67
66
  """Return the string representation of the label."""
68
67
  return f"Label(path={self.path}, {self.dimensions})"
69
68
 
69
+ @property
70
+ def meta_handler(self) -> LabelMetaHandler:
71
+ """Return the metadata handler."""
72
+ assert isinstance(self._meta_handler, LabelMetaHandler)
73
+ return self._meta_handler
74
+
70
75
  @property
71
76
  def meta(self) -> NgioLabelMeta:
72
77
  """Return the metadata."""
73
- return self._meta_handler.meta
78
+ meta = self.meta_handler.get_meta()
79
+ assert isinstance(meta, NgioLabelMeta)
80
+ return meta
74
81
 
75
82
  def set_axes_unit(
76
83
  self,
@@ -85,7 +92,7 @@ class Label(AbstractImage[LabelMetaHandler]):
85
92
  """
86
93
  meta = self.meta
87
94
  meta = meta.to_units(space_unit=space_unit, time_unit=time_unit)
88
- self._meta_handler.write_meta(meta)
95
+ self.meta_handler.update_meta(meta)
89
96
 
90
97
  def build_masking_roi_table(self) -> MaskingRoiTable:
91
98
  """Compute the masking ROI table."""
@@ -153,49 +160,79 @@ class LabelsContainer:
153
160
  f"Available labels: {self.list()}"
154
161
  )
155
162
 
156
- group_handler = self._group_handler.derive_handler(name)
157
- label_meta_handler = find_label_meta_handler(group_handler)
158
- path = label_meta_handler.meta.get_dataset(
159
- path=path, pixel_size=pixel_size, strict=strict
160
- ).path
163
+ group_handler = self._group_handler.get_handler(name)
164
+ label_meta_handler = LabelMetaHandler(group_handler)
165
+ path = (
166
+ label_meta_handler.get_meta()
167
+ .get_dataset(path=path, pixel_size=pixel_size, strict=strict)
168
+ .path
169
+ )
161
170
  return Label(group_handler, path, label_meta_handler)
162
171
 
163
172
  def derive(
164
173
  self,
165
174
  name: str,
166
175
  ref_image: Image | Label,
176
+ # Metadata parameters
167
177
  shape: Sequence[int] | None = None,
168
- pixel_size: PixelSize | None = None,
169
- axes_names: Sequence[str] | None = None,
170
- chunks: Sequence[int] | None = None,
171
- dtype: str = "uint32",
178
+ pixelsize: float | tuple[float, float] | None = None,
179
+ z_spacing: float | None = None,
180
+ time_spacing: float | None = None,
181
+ channels_policy: Literal["same", "squeeze"] | int = "squeeze",
182
+ ngff_version: NgffVersions | None = None,
183
+ # Zarr Array parameters
184
+ chunks: ChunksLike = "auto",
185
+ shards: ShardsLike | None = None,
186
+ dtype: str | None = None,
172
187
  dimension_separator: Literal[".", "/"] | None = None,
173
188
  compressors: CompressorLike | None = None,
189
+ extra_array_kwargs: Mapping[str, Any] | None = None,
174
190
  overwrite: bool = False,
191
+ # Deprecated arguments
192
+ labels: Sequence[str] | None = None,
193
+ pixel_size: PixelSize | None = None,
175
194
  ) -> "Label":
176
- """Create an empty OME-Zarr label from a reference image.
195
+ """Create an empty OME-Zarr image from an existing image.
177
196
 
178
- And add the label to the /labels group.
197
+ If a kwarg is not provided, the value from the reference image will be used.
179
198
 
180
199
  Args:
181
200
  store (StoreOrGroup): The Zarr store or group to create the image in.
182
- ref_image (Image | Label): A reference image that will be used to create
183
- the new image.
184
- name (str): The name of the new image.
201
+ ref_image (Image | Label): The reference image to derive the new image from.
185
202
  shape (Sequence[int] | None): The shape of the new image.
186
- pixel_size (PixelSize | None): The pixel size of the new image.
203
+ pixelsize (float | tuple[float, float] | None): The pixel size of the new
204
+ image.
205
+ z_spacing (float | None): The z spacing of the new image.
206
+ time_spacing (float | None): The time spacing of the new image.
207
+ scaling_factors (Sequence[float] | Literal["auto"] | None): The scaling
208
+ factors of the new image.
187
209
  axes_names (Sequence[str] | None): The axes names of the new image.
188
- For labels, the channel axis is not allowed.
210
+ name (str | None): The name of the new image.
211
+ channels_meta (Sequence[str | Channel] | None): The channels metadata
212
+ of the new image.
213
+ channels_policy (Literal["squeeze", "same"] | int): Possible policies:
214
+ - If "squeeze", the channels axis will be removed (no matter its size).
215
+ - If "same", the channels axis will be kept as is (if it exists).
216
+ - If an integer is provided, the channels axis will be changed to have
217
+ that size.
218
+ ngff_version (NgffVersions | None): The NGFF version to use.
189
219
  chunks (Sequence[int] | None): The chunk shape of the new image.
190
- dtype (str): The data type of the new label.
220
+ shards (ShardsLike | None): The shard shape of the new image.
221
+ dtype (str | None): The data type of the new image.
191
222
  dimension_separator (DIMENSION_SEPARATOR | None): The separator to use for
192
- dimensions. If None it will use the same as the reference image.
193
- compressors (CompressorLike | None): The compressors to use. If None it will
194
- use the same as the reference image.
223
+ dimensions.
224
+ compressors (CompressorLike | None): The compressors to use.
225
+ extra_array_kwargs (Mapping[str, Any] | None): Extra arguments to pass to
226
+ the zarr array creation.
195
227
  overwrite (bool): Whether to overwrite an existing image.
228
+ labels (Sequence[str] | None): The labels of the new image.
229
+ This argument is deprecated please use channels_meta instead.
230
+ pixel_size (PixelSize | None): The pixel size of the new image.
231
+ This argument is deprecated please use pixelsize, z_spacing,
232
+ and time_spacing instead.
196
233
 
197
234
  Returns:
198
- Label: The new label.
235
+ Label: The new derived label.
199
236
 
200
237
  """
201
238
  existing_labels = self.list()
@@ -208,17 +245,24 @@ class LabelsContainer:
208
245
  label_group = self._group_handler.get_group(name, create_mode=True)
209
246
 
210
247
  derive_label(
211
- store=label_group,
212
248
  ref_image=ref_image,
213
- name=name,
249
+ store=label_group,
214
250
  shape=shape,
215
- pixel_size=pixel_size,
216
- axes_names=axes_names,
251
+ pixelsize=pixelsize,
252
+ z_spacing=z_spacing,
253
+ time_spacing=time_spacing,
254
+ name=name,
255
+ channels_policy=channels_policy,
256
+ ngff_version=ngff_version,
217
257
  chunks=chunks,
258
+ shards=shards,
218
259
  dtype=dtype,
219
260
  dimension_separator=dimension_separator,
220
261
  compressors=compressors,
262
+ extra_array_kwargs=extra_array_kwargs,
221
263
  overwrite=overwrite,
264
+ labels=labels,
265
+ pixel_size=pixel_size,
222
266
  )
223
267
 
224
268
  if name not in existing_labels:
@@ -229,110 +273,91 @@ class LabelsContainer:
229
273
 
230
274
 
231
275
  def derive_label(
276
+ *,
232
277
  store: StoreOrGroup,
233
278
  ref_image: Image | Label,
234
- name: str,
279
+ # Metadata parameters
235
280
  shape: Sequence[int] | None = None,
236
- pixel_size: PixelSize | None = None,
237
- axes_names: Sequence[str] | None = None,
238
- chunks: Sequence[int] | None = None,
281
+ pixelsize: float | tuple[float, float] | None = None,
282
+ z_spacing: float | None = None,
283
+ time_spacing: float | None = None,
284
+ name: str | None = None,
285
+ channels_policy: Literal["same", "squeeze"] | int = "squeeze",
286
+ ngff_version: NgffVersions | None = None,
287
+ # Zarr Array parameters
288
+ chunks: ChunksLike = "auto",
289
+ shards: ShardsLike | None = None,
290
+ dtype: str | None = None,
239
291
  dimension_separator: Literal[".", "/"] | None = None,
240
292
  compressors: CompressorLike | None = None,
241
- dtype: str = "uint32",
293
+ extra_array_kwargs: Mapping[str, Any] | None = None,
242
294
  overwrite: bool = False,
243
- ) -> None:
244
- """Create an empty OME-Zarr label from a reference image.
295
+ # Deprecated arguments
296
+ labels: Sequence[str] | None = None,
297
+ pixel_size: PixelSize | None = None,
298
+ ) -> ZarrGroupHandler:
299
+ """Derive a new OME-Zarr label from an existing image or label.
300
+
301
+ If a kwarg is not provided, the value from the reference image will be used.
245
302
 
246
303
  Args:
247
- store (StoreOrGroup): The Zarr store or group to create the image in.
248
- ref_image (Image | Label): A reference image that will be used to
249
- create the new image.
250
- name (str): The name of the new image.
251
- shape (Sequence[int] | None): The shape of the new image.
252
- pixel_size (PixelSize | None): The pixel size of the new image.
253
- axes_names (Sequence[str] | None): The axes names of the new image.
254
- For labels, the channel axis is not allowed.
255
- chunks (Sequence[int] | None): The chunk shape of the new image.
256
- dtype (str): The data type of the new label.
257
- dimension_separator (DIMENSION_SEPARATOR | None): The separator to use for
258
- dimensions. If None it will use the same as the reference image.
259
- compressors (CompressorLike | None): The compressor to use. If None it will use
260
- the same as the reference image.
261
- overwrite (bool): Whether to overwrite an existing image.
304
+ store (StoreOrGroup): The Zarr store or group to create the label in.
305
+ ref_image (Image | Label): The reference image to derive the new label from.
306
+ shape (Sequence[int] | None): The shape of the new label.
307
+ pixelsize (float | tuple[float, float] | None): The pixel size of the new label.
308
+ z_spacing (float | None): The z spacing of the new label.
309
+ time_spacing (float | None): The time spacing of the new label.
310
+ name (str | None): The name of the new label.
311
+ channels_policy (Literal["squeeze", "same"] | int): Possible policies:
312
+ - If "squeeze", the channels axis will be removed (no matter its size).
313
+ - If "same", the channels axis will be kept as is (if it exists).
314
+ - If an integer is provided, the channels axis will be changed to have that
315
+ size.
316
+ ngff_version (NgffVersions | None): The NGFF version to use.
317
+ chunks (ChunksLike): The chunk shape of the new label. Defaults to "auto".
318
+ shards (ShardsLike | None): The shard shape of the new label.
319
+ dtype (str | None): The data type of the new label.
320
+ dimension_separator (Literal[".", "/"] | None): The separator to use for
321
+ dimensions.
322
+ compressors (CompressorLike | None): The compressors to use.
323
+ extra_array_kwargs (Mapping[str, Any] | None): Extra arguments to pass to
324
+ the zarr array creation.
325
+ overwrite (bool): Whether to overwrite an existing label. Defaults to False.
326
+ labels (Sequence[str] | None): Deprecated. This argument is deprecated,
327
+ please use channels_meta instead.
328
+ pixel_size (PixelSize | None): Deprecated. The pixel size of the new label.
329
+ This argument is deprecated, please use pixelsize, z_spacing,
330
+ and time_spacing instead.
262
331
 
263
332
  Returns:
264
- None
333
+ ZarrGroupHandler: The group handler of the new label.
265
334
 
266
335
  """
267
- ref_meta = ref_image.meta
268
-
269
- if shape is None:
270
- shape = ref_image.shape
271
-
272
- if pixel_size is None:
273
- pixel_size = ref_image.pixel_size
274
-
275
- if axes_names is None:
276
- axes_names = ref_meta.axes_handler.axes_names
277
- c_axis = ref_meta.axes_handler.get_index("c")
278
- else:
279
- if "c" in axes_names:
280
- raise NgioValidationError(
281
- "Labels cannot have a channel axis. "
282
- "Please remove the channel axis from the axes names."
283
- )
284
- c_axis = None
285
-
286
- if len(axes_names) != len(shape):
287
- raise NgioValidationError(
288
- "The axes names of the new image does not match the reference image."
289
- f"Got {axes_names} for shape {shape}."
290
- )
291
-
292
- if chunks is None:
293
- chunks = ref_image.chunks
294
-
295
- if len(chunks) != len(shape):
296
- raise NgioValidationError(
297
- "The chunks of the new image does not match the reference image."
298
- f"Got {chunks} for shape {shape}."
299
- )
300
-
301
- if c_axis is not None:
302
- # remove channel if present
303
- shape = list(shape)
304
- shape = shape[:c_axis] + shape[c_axis + 1 :]
305
- chunks = list(chunks)
306
- chunks = chunks[:c_axis] + chunks[c_axis + 1 :]
307
- axes_names = list(axes_names)
308
- axes_names = axes_names[:c_axis] + axes_names[c_axis + 1 :]
309
-
310
- if dimension_separator is None:
311
- dimension_separator = find_dimension_separator(ref_image.zarr_array)
312
- if compressors is None:
313
- compressors = ref_image.zarr_array.compressors # type: ignore
314
-
315
- _ = create_empty_label_container(
336
+ if dtype is None and isinstance(ref_image, Image):
337
+ dtype = "uint32"
338
+ group_handler = abstract_derive(
339
+ ref_image=ref_image,
340
+ meta_type=NgioLabelMeta,
316
341
  store=store,
317
342
  shape=shape,
318
- pixelsize=ref_image.pixel_size.x,
319
- z_spacing=ref_image.pixel_size.z,
320
- time_spacing=ref_image.pixel_size.t,
321
- levels=ref_meta.paths,
322
- yx_scaling_factor=ref_meta.yx_scaling(),
323
- z_scaling_factor=ref_meta.z_scaling(),
324
- time_unit=ref_image.pixel_size.time_unit,
325
- space_unit=ref_image.pixel_size.space_unit,
326
- axes_names=axes_names,
343
+ pixelsize=pixelsize,
344
+ z_spacing=z_spacing,
345
+ time_spacing=time_spacing,
346
+ name=name,
347
+ channels_meta=None,
348
+ channels_policy=channels_policy,
349
+ ngff_version=ngff_version,
327
350
  chunks=chunks,
351
+ shards=shards,
328
352
  dtype=dtype,
329
353
  dimension_separator=dimension_separator,
330
354
  compressors=compressors,
355
+ extra_array_kwargs=extra_array_kwargs,
331
356
  overwrite=overwrite,
332
- ngff_version=ref_meta.version,
333
- name=name,
357
+ labels=labels,
358
+ pixel_size=pixel_size,
334
359
  )
335
- return None
360
+ return group_handler
336
361
 
337
362
 
338
363
  def build_masking_roi_table(label: Label) -> MaskingRoiTable: