ngio 0.5.0__py3-none-any.whl → 0.5.0a2__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 (53) hide show
  1. ngio/__init__.py +2 -5
  2. ngio/common/__init__.py +6 -11
  3. ngio/common/_masking_roi.py +54 -34
  4. ngio/common/_pyramid.py +85 -309
  5. ngio/common/_roi.py +330 -258
  6. ngio/experimental/iterators/_feature.py +3 -3
  7. ngio/experimental/iterators/_rois_utils.py +11 -10
  8. ngio/hcs/_plate.py +60 -132
  9. ngio/images/_abstract_image.py +35 -539
  10. ngio/images/_create.py +287 -0
  11. ngio/images/_create_synt_container.py +42 -39
  12. ngio/images/_image.py +250 -516
  13. ngio/images/_label.py +172 -249
  14. ngio/images/_masked_image.py +2 -2
  15. ngio/images/_ome_zarr_container.py +241 -644
  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 +8 -6
  21. ngio/io_pipes/_ops_slices_utils.py +5 -8
  22. ngio/ome_zarr_meta/__init__.py +18 -29
  23. ngio/ome_zarr_meta/_meta_handlers.py +708 -392
  24. ngio/ome_zarr_meta/ngio_specs/__init__.py +0 -4
  25. ngio/ome_zarr_meta/ngio_specs/_axes.py +51 -152
  26. ngio/ome_zarr_meta/ngio_specs/_dataset.py +22 -13
  27. ngio/ome_zarr_meta/ngio_specs/_ngio_hcs.py +91 -129
  28. ngio/ome_zarr_meta/ngio_specs/_ngio_image.py +68 -57
  29. ngio/ome_zarr_meta/v04/__init__.py +1 -5
  30. ngio/ome_zarr_meta/v04/{_v04_spec.py → _v04_spec_utils.py} +85 -54
  31. ngio/ome_zarr_meta/v05/__init__.py +1 -5
  32. ngio/ome_zarr_meta/v05/{_v05_spec.py → _v05_spec_utils.py} +87 -64
  33. ngio/resources/__init__.py +1 -1
  34. ngio/resources/resource_model.py +1 -1
  35. ngio/tables/_tables_container.py +11 -62
  36. ngio/tables/backends/_anndata.py +8 -58
  37. ngio/tables/backends/_anndata_utils.py +6 -1
  38. ngio/tables/backends/_csv.py +19 -3
  39. ngio/tables/backends/_json.py +13 -10
  40. ngio/tables/backends/_non_zarr_backends.py +196 -0
  41. ngio/tables/backends/_parquet.py +31 -3
  42. ngio/tables/v1/_roi_table.py +24 -41
  43. ngio/utils/__init__.py +12 -6
  44. ngio/utils/_datasets.py +0 -6
  45. ngio/utils/_logger.py +50 -0
  46. ngio/utils/_zarr_utils.py +58 -167
  47. {ngio-0.5.0.dist-info → ngio-0.5.0a2.dist-info}/METADATA +4 -11
  48. ngio-0.5.0a2.dist-info/RECORD +89 -0
  49. {ngio-0.5.0.dist-info → ngio-0.5.0a2.dist-info}/WHEEL +1 -1
  50. ngio/images/_create_utils.py +0 -406
  51. ngio/tables/backends/_py_arrow_backends.py +0 -222
  52. ngio-0.5.0.dist-info/RECORD +0 -88
  53. {ngio-0.5.0.dist-info → ngio-0.5.0a2.dist-info}/licenses/LICENSE +0 -0
ngio/images/_create.py ADDED
@@ -0,0 +1,287 @@
1
+ """Utility functions for working with OME-Zarr images."""
2
+
3
+ from collections.abc import Sequence
4
+ from typing import Literal, TypeVar
5
+
6
+ from zarr.core.array import CompressorLike
7
+
8
+ from ngio.common._pyramid import init_empty_pyramid
9
+ from ngio.ome_zarr_meta import (
10
+ NgioImageMeta,
11
+ NgioLabelMeta,
12
+ PixelSize,
13
+ get_image_meta_handler,
14
+ get_label_meta_handler,
15
+ )
16
+ from ngio.ome_zarr_meta.ngio_specs import (
17
+ DefaultNgffVersion,
18
+ DefaultSpaceUnit,
19
+ DefaultTimeUnit,
20
+ NgffVersions,
21
+ SpaceUnits,
22
+ TimeUnits,
23
+ canonical_axes_order,
24
+ canonical_label_axes_order,
25
+ )
26
+ from ngio.utils import NgioValueError, StoreOrGroup, ZarrGroupHandler
27
+
28
+ _image_or_label_meta = TypeVar("_image_or_label_meta", NgioImageMeta, NgioLabelMeta)
29
+
30
+
31
+ def _init_generic_meta(
32
+ meta_type: type[_image_or_label_meta],
33
+ pixelsize: float,
34
+ axes_names: Sequence[str],
35
+ z_spacing: float = 1.0,
36
+ time_spacing: float = 1.0,
37
+ levels: int | list[str] = 5,
38
+ yx_scaling_factor: float | tuple[float, float] = 2.0,
39
+ z_scaling_factor: float = 1.0,
40
+ space_unit: SpaceUnits | str | None = DefaultSpaceUnit,
41
+ time_unit: TimeUnits | str | None = DefaultTimeUnit,
42
+ name: str | None = None,
43
+ ngff_version: NgffVersions = DefaultNgffVersion,
44
+ ) -> tuple[_image_or_label_meta, list[float]]:
45
+ """Initialize the metadata for an image or label."""
46
+ scaling_factors = []
47
+ for ax in axes_names:
48
+ if ax == "z":
49
+ scaling_factors.append(z_scaling_factor)
50
+ elif ax in ["x"]:
51
+ if isinstance(yx_scaling_factor, tuple):
52
+ scaling_factors.append(yx_scaling_factor[1])
53
+ else:
54
+ scaling_factors.append(yx_scaling_factor)
55
+ elif ax in ["y"]:
56
+ if isinstance(yx_scaling_factor, tuple):
57
+ scaling_factors.append(yx_scaling_factor[0])
58
+ else:
59
+ scaling_factors.append(yx_scaling_factor)
60
+ else:
61
+ scaling_factors.append(1.0)
62
+
63
+ pixel_sizes = PixelSize(
64
+ x=pixelsize,
65
+ y=pixelsize,
66
+ z=z_spacing,
67
+ t=time_spacing,
68
+ space_unit=space_unit,
69
+ time_unit=time_unit,
70
+ )
71
+
72
+ meta = meta_type.default_init(
73
+ name=name,
74
+ levels=levels,
75
+ axes_names=axes_names,
76
+ pixel_size=pixel_sizes,
77
+ scaling_factors=scaling_factors,
78
+ version=ngff_version,
79
+ )
80
+ return meta, scaling_factors
81
+
82
+
83
+ def create_empty_label_container(
84
+ store: StoreOrGroup,
85
+ shape: Sequence[int],
86
+ pixelsize: float,
87
+ z_spacing: float = 1.0,
88
+ time_spacing: float = 1.0,
89
+ levels: int | list[str] = 5,
90
+ yx_scaling_factor: float | tuple[float, float] = 2.0,
91
+ z_scaling_factor: float = 1.0,
92
+ space_unit: SpaceUnits | str | None = DefaultSpaceUnit,
93
+ time_unit: TimeUnits | str | None = DefaultTimeUnit,
94
+ axes_names: Sequence[str] | None = None,
95
+ name: str | None = None,
96
+ chunks: Sequence[int] | Literal["auto"] = "auto",
97
+ dtype: str = "uint32",
98
+ dimension_separator: Literal[".", "/"] = "/",
99
+ compressors: CompressorLike = "auto",
100
+ overwrite: bool = False,
101
+ ngff_version: NgffVersions = DefaultNgffVersion,
102
+ ) -> ZarrGroupHandler:
103
+ """Create an empty label with the given shape and metadata.
104
+
105
+ Args:
106
+ store (StoreOrGroup): The Zarr store or group to create the image in.
107
+ shape (Sequence[int]): The shape of the image.
108
+ pixelsize (float): The pixel size in x and y dimensions.
109
+ z_spacing (float, optional): The spacing between z slices. Defaults to 1.0.
110
+ time_spacing (float, optional): The spacing between time points.
111
+ Defaults to 1.0.
112
+ levels (int | list[str], optional): The number of levels in the pyramid or a
113
+ list of level names. Defaults to 5.
114
+ yx_scaling_factor (float, optional): The down-scaling factor in x and y
115
+ dimensions. Defaults to 2.0.
116
+ z_scaling_factor (float, optional): The down-scaling factor in z dimension.
117
+ Defaults to 1.0.
118
+ space_unit (SpaceUnits, optional): The unit of space. Defaults to
119
+ DefaultSpaceUnit.
120
+ time_unit (TimeUnits, optional): The unit of time. Defaults to
121
+ DefaultTimeUnit.
122
+ axes_names (Sequence[str] | None, optional): The names of the axes.
123
+ If None the canonical names are used. Defaults to None.
124
+ name (str | None, optional): The name of the image. Defaults to None.
125
+ chunks (Sequence[int] | Literal["auto"]): The chunk shape. If None the shape
126
+ is used. Defaults to None.
127
+ dimension_separator (DIMENSION_SEPARATOR): The separator to use for
128
+ dimensions. Defaults to "/".
129
+ compressors (CompressorLike): The compressors to use. Defaults to "auto".
130
+ dtype (str, optional): The data type of the image. Defaults to "uint16".
131
+ overwrite (bool, optional): Whether to overwrite an existing image.
132
+ Defaults to True.
133
+ ngff_version (str, optional): The version of the OME-Zarr specification.
134
+ Defaults to DefaultVersion.
135
+
136
+ """
137
+ if axes_names is None:
138
+ axes_names = canonical_label_axes_order()[-len(shape) :]
139
+
140
+ if len(axes_names) != len(shape):
141
+ raise NgioValueError(
142
+ f"Number of axes names {axes_names} does not match the number of "
143
+ f"dimensions {shape}."
144
+ )
145
+
146
+ meta, scaling_factors = _init_generic_meta(
147
+ meta_type=NgioLabelMeta,
148
+ pixelsize=pixelsize,
149
+ z_spacing=z_spacing,
150
+ time_spacing=time_spacing,
151
+ levels=levels,
152
+ yx_scaling_factor=yx_scaling_factor,
153
+ z_scaling_factor=z_scaling_factor,
154
+ space_unit=space_unit,
155
+ time_unit=time_unit,
156
+ axes_names=axes_names,
157
+ name=name,
158
+ ngff_version=ngff_version,
159
+ )
160
+
161
+ mode = "w" if overwrite else "w-"
162
+ group_handler = ZarrGroupHandler(
163
+ store=store, mode=mode, cache=False, zarr_format=meta.zarr_format
164
+ )
165
+ image_handler = get_label_meta_handler(
166
+ version=ngff_version, group_handler=group_handler
167
+ )
168
+ image_handler.write_meta(meta)
169
+
170
+ init_empty_pyramid(
171
+ store=store,
172
+ paths=meta.paths,
173
+ scaling_factors=scaling_factors,
174
+ ref_shape=shape,
175
+ chunks=chunks,
176
+ axes=axes_names,
177
+ dtype=dtype,
178
+ mode="a",
179
+ dimension_separator=dimension_separator,
180
+ compressors=compressors,
181
+ )
182
+ group_handler._mode = "r+"
183
+ return group_handler
184
+
185
+
186
+ def create_empty_image_container(
187
+ store: StoreOrGroup,
188
+ shape: Sequence[int],
189
+ pixelsize: float,
190
+ z_spacing: float = 1.0,
191
+ time_spacing: float = 1.0,
192
+ levels: int | list[str] = 5,
193
+ yx_scaling_factor: float | tuple[float, float] = 2,
194
+ z_scaling_factor: float = 1.0,
195
+ space_unit: SpaceUnits | str | None = DefaultSpaceUnit,
196
+ time_unit: TimeUnits | str | None = DefaultTimeUnit,
197
+ axes_names: Sequence[str] | None = None,
198
+ name: str | None = None,
199
+ chunks: Sequence[int] | Literal["auto"] = "auto",
200
+ dtype: str = "uint16",
201
+ dimension_separator: Literal[".", "/"] = "/",
202
+ compressors: CompressorLike = "auto",
203
+ overwrite: bool = False,
204
+ ngff_version: NgffVersions = DefaultNgffVersion,
205
+ ) -> ZarrGroupHandler:
206
+ """Create an empty OME-Zarr image with the given shape and metadata.
207
+
208
+ Args:
209
+ store (StoreOrGroup): The Zarr store or group to create the image in.
210
+ shape (Sequence[int]): The shape of the image.
211
+ pixelsize (float): The pixel size in x and y dimensions.
212
+ z_spacing (float, optional): The spacing between z slices. Defaults to 1.0.
213
+ time_spacing (float, optional): The spacing between time points.
214
+ Defaults to 1.0.
215
+ levels (int | list[str], optional): The number of levels in the pyramid or a
216
+ list of level names. Defaults to 5.
217
+ yx_scaling_factor (float, optional): The down-scaling factor in x and y
218
+ dimensions. Defaults to 2.0.
219
+ z_scaling_factor (float, optional): The down-scaling factor in z dimension.
220
+ Defaults to 1.0.
221
+ space_unit (SpaceUnits, optional): The unit of space. Defaults to
222
+ DefaultSpaceUnit.
223
+ time_unit (TimeUnits, optional): The unit of time. Defaults to
224
+ DefaultTimeUnit.
225
+ axes_names (Sequence[str] | None, optional): The names of the axes.
226
+ If None the canonical names are used. Defaults to None.
227
+ name (str | None, optional): The name of the image. Defaults to None.
228
+ chunks (Sequence[int] | Literal["auto"]): The chunk shape. If None the shape
229
+ is used. Defaults to None.
230
+ dtype (str, optional): The data type of the image. Defaults to "uint16".
231
+ dimension_separator (DIMENSION_SEPARATOR): The separator to use for
232
+ dimensions. Defaults to "/".
233
+ compressors (CompressorLike): The compressors to use. Defaults to "auto".
234
+ overwrite (bool, optional): Whether to overwrite an existing image.
235
+ Defaults to True.
236
+ ngff_version (str, optional): The version of the OME-Zarr specification.
237
+ Defaults to DefaultVersion.
238
+
239
+ """
240
+ if axes_names is None:
241
+ axes_names = canonical_axes_order()[-len(shape) :]
242
+
243
+ if len(axes_names) != len(shape):
244
+ raise NgioValueError(
245
+ f"Number of axes names {axes_names} does not match the number of "
246
+ f"dimensions {shape}."
247
+ )
248
+
249
+ meta, scaling_factors = _init_generic_meta(
250
+ meta_type=NgioImageMeta,
251
+ pixelsize=pixelsize,
252
+ z_spacing=z_spacing,
253
+ time_spacing=time_spacing,
254
+ levels=levels,
255
+ yx_scaling_factor=yx_scaling_factor,
256
+ z_scaling_factor=z_scaling_factor,
257
+ space_unit=space_unit,
258
+ time_unit=time_unit,
259
+ axes_names=axes_names,
260
+ name=name,
261
+ ngff_version=ngff_version,
262
+ )
263
+ mode = "w" if overwrite else "w-"
264
+ group_handler = ZarrGroupHandler(
265
+ store=store, mode=mode, cache=False, zarr_format=meta.zarr_format
266
+ )
267
+ image_handler = get_image_meta_handler(
268
+ version=ngff_version, group_handler=group_handler
269
+ )
270
+ image_handler.write_meta(meta)
271
+
272
+ init_empty_pyramid(
273
+ store=store,
274
+ paths=meta.paths,
275
+ scaling_factors=scaling_factors,
276
+ ref_shape=shape,
277
+ chunks=chunks,
278
+ axes=axes_names,
279
+ dtype=dtype,
280
+ mode="a",
281
+ dimension_separator=dimension_separator,
282
+ compressors=compressors,
283
+ zarr_format=meta.zarr_format,
284
+ )
285
+
286
+ group_handler._mode = "r+"
287
+ return group_handler
@@ -1,17 +1,15 @@
1
1
  """Abstract class for handling OME-NGFF images."""
2
2
 
3
- from collections.abc import Mapping, Sequence
4
- from typing import Any, Literal
3
+ from collections.abc import Sequence
4
+ from typing import Literal
5
5
 
6
6
  import numpy as np
7
7
  import PIL.Image
8
8
  from zarr.core.array import CompressorLike
9
9
 
10
- from ngio.common._pyramid import ChunksLike, ShardsLike
11
10
  from ngio.common._synt_images_utils import fit_to_shape
12
11
  from ngio.images._ome_zarr_container import OmeZarrContainer, create_ome_zarr_from_array
13
12
  from ngio.ome_zarr_meta.ngio_specs import (
14
- Channel,
15
13
  DefaultNgffVersion,
16
14
  NgffVersions,
17
15
  )
@@ -30,48 +28,52 @@ def create_synthetic_ome_zarr(
30
28
  shape: Sequence[int],
31
29
  reference_sample: AVAILABLE_SAMPLES | SampleInfo = "Cardiomyocyte",
32
30
  levels: int | list[str] = 5,
33
- translation: Sequence[float] | None = None,
34
- table_backend: TableBackend = DefaultTableBackend,
35
- scaling_factors: Sequence[float] | Literal["auto"] = "auto",
31
+ xy_scaling_factor: float = 2,
32
+ z_scaling_factor: float = 1.0,
36
33
  axes_names: Sequence[str] | None = None,
37
- channels_meta: Sequence[str | Channel] | None = None,
38
- ngff_version: NgffVersions = DefaultNgffVersion,
39
- chunks: ChunksLike = "auto",
40
- shards: ShardsLike | None = None,
34
+ chunks: Sequence[int] | Literal["auto"] = "auto",
35
+ channel_labels: list[str] | None = None,
36
+ channel_wavelengths: list[str] | None = None,
37
+ channel_colors: Sequence[str] | None = None,
38
+ channel_active: Sequence[bool] | None = None,
39
+ table_backend: TableBackend = DefaultTableBackend,
41
40
  dimension_separator: Literal[".", "/"] = "/",
42
41
  compressors: CompressorLike = "auto",
43
- extra_array_kwargs: Mapping[str, Any] | None = None,
44
42
  overwrite: bool = False,
43
+ ngff_version: NgffVersions = DefaultNgffVersion,
45
44
  ) -> OmeZarrContainer:
46
- """Create a synthetic OME-Zarr image with the given shape and metadata.
45
+ """Create an empty OME-Zarr image with the given shape and metadata.
47
46
 
48
47
  Args:
49
48
  store (StoreOrGroup): The Zarr store or group to create the image in.
50
49
  shape (Sequence[int]): The shape of the image.
51
50
  reference_sample (AVAILABLE_SAMPLES | SampleInfo): The reference sample to use.
52
- Defaults to "Cardiomyocyte".
53
- levels (int | list[str]): The number of levels in the pyramid or a list of
54
- level names. Defaults to 5.
55
- translation (Sequence[float] | None): The translation for each axis
56
- at the highest resolution level. Defaults to None.
57
- table_backend (TableBackend): Table backend to be used to store tables.
58
- Defaults to DefaultTableBackend.
59
- scaling_factors (Sequence[float] | Literal["auto"]): The down-scaling factors
60
- for the pyramid levels. Defaults to "auto".
61
- axes_names (Sequence[str] | None): The names of the axes. If None the
62
- canonical names are used. Defaults to None.
63
- channels_meta (Sequence[str | Channel] | None): The channels metadata.
51
+ levels (int | list[str], optional): The number of levels in the pyramid or a
52
+ list of level names. Defaults to 5.
53
+ xy_scaling_factor (float, optional): The down-scaling factor in x and y
54
+ dimensions. Defaults to 2.0.
55
+ z_scaling_factor (float, optional): The down-scaling factor in z dimension.
56
+ Defaults to 1.0.
57
+ axes_names (Sequence[str] | None, optional): The names of the axes.
58
+ If None the canonical names are used. Defaults to None.
59
+ chunks (Sequence[int] | Literal["auto"]): The chunk shape. If None the shape
60
+ is used. Defaults to "auto".
61
+ channel_labels (list[str] | None, optional): The labels of the channels.
64
62
  Defaults to None.
65
- ngff_version (NgffVersions): The version of the OME-Zarr specification.
66
- Defaults to DefaultNgffVersion.
67
- chunks (ChunksLike): The chunk shape. Defaults to "auto".
68
- shards (ShardsLike | None): The shard shape. Defaults to None.
69
- dimension_separator (Literal[".", "/"]): The separator to use for
63
+ channel_wavelengths (list[str] | None, optional): The wavelengths of the
64
+ channels. Defaults to None.
65
+ channel_colors (Sequence[str] | None, optional): The colors of the channels.
66
+ Defaults to None.
67
+ channel_active (Sequence[bool] | None, optional): Whether the channels are
68
+ active. Defaults to None.
69
+ table_backend (TableBackend): Table backend to be used to store tables
70
+ dimension_separator (DIMENSION_SEPARATOR): The separator to use for
70
71
  dimensions. Defaults to "/".
71
72
  compressors (CompressorLike): The compressors to use. Defaults to "auto".
72
- extra_array_kwargs (Mapping[str, Any] | None): Extra arguments to pass to
73
- the zarr array creation. Defaults to None.
74
- overwrite (bool): Whether to overwrite an existing image. Defaults to False.
73
+ overwrite (bool, optional): Whether to overwrite an existing image.
74
+ Defaults to True.
75
+ ngff_version (NgffVersion, optional): The version of the OME-Zarr specification.
76
+ Defaults to DefaultNgffVersion.
75
77
  """
76
78
  if isinstance(reference_sample, str):
77
79
  sample_info = get_sample_info(reference_sample)
@@ -85,20 +87,21 @@ def create_synthetic_ome_zarr(
85
87
  ome_zarr = create_ome_zarr_from_array(
86
88
  store=store,
87
89
  array=raw,
88
- pixelsize=sample_info.pixelsize,
90
+ xy_pixelsize=sample_info.xy_pixelsize,
89
91
  z_spacing=sample_info.z_spacing,
90
92
  time_spacing=sample_info.time_spacing,
91
93
  levels=levels,
92
- translation=translation,
94
+ xy_scaling_factor=xy_scaling_factor,
95
+ z_scaling_factor=z_scaling_factor,
93
96
  space_unit=sample_info.space_unit,
94
97
  time_unit=sample_info.time_unit,
95
98
  axes_names=axes_names,
96
- channels_meta=channels_meta,
97
- scaling_factors=scaling_factors,
98
- extra_array_kwargs=extra_array_kwargs,
99
+ channel_labels=channel_labels,
100
+ channel_wavelengths=channel_wavelengths,
101
+ channel_colors=channel_colors,
102
+ channel_active=channel_active,
99
103
  name=sample_info.name,
100
104
  chunks=chunks,
101
- shards=shards,
102
105
  overwrite=overwrite,
103
106
  dimension_separator=dimension_separator,
104
107
  compressors=compressors,