ngio 0.3.4__py3-none-any.whl → 0.4.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 (73) hide show
  1. ngio/__init__.py +7 -2
  2. ngio/common/__init__.py +5 -52
  3. ngio/common/_dimensions.py +270 -55
  4. ngio/common/_masking_roi.py +38 -10
  5. ngio/common/_pyramid.py +51 -30
  6. ngio/common/_roi.py +269 -82
  7. ngio/common/_synt_images_utils.py +101 -0
  8. ngio/common/_zoom.py +49 -19
  9. ngio/experimental/__init__.py +5 -0
  10. ngio/experimental/iterators/__init__.py +15 -0
  11. ngio/experimental/iterators/_abstract_iterator.py +390 -0
  12. ngio/experimental/iterators/_feature.py +189 -0
  13. ngio/experimental/iterators/_image_processing.py +130 -0
  14. ngio/experimental/iterators/_mappers.py +48 -0
  15. ngio/experimental/iterators/_rois_utils.py +127 -0
  16. ngio/experimental/iterators/_segmentation.py +235 -0
  17. ngio/hcs/_plate.py +41 -36
  18. ngio/images/__init__.py +22 -1
  19. ngio/images/_abstract_image.py +403 -176
  20. ngio/images/_create.py +31 -15
  21. ngio/images/_create_synt_container.py +138 -0
  22. ngio/images/_image.py +452 -63
  23. ngio/images/_label.py +56 -30
  24. ngio/images/_masked_image.py +387 -129
  25. ngio/images/_ome_zarr_container.py +237 -67
  26. ngio/{common → images}/_table_ops.py +41 -41
  27. ngio/io_pipes/__init__.py +75 -0
  28. ngio/io_pipes/_io_pipes.py +361 -0
  29. ngio/io_pipes/_io_pipes_masked.py +488 -0
  30. ngio/io_pipes/_io_pipes_roi.py +152 -0
  31. ngio/io_pipes/_io_pipes_types.py +56 -0
  32. ngio/io_pipes/_match_shape.py +376 -0
  33. ngio/io_pipes/_ops_axes.py +344 -0
  34. ngio/io_pipes/_ops_slices.py +446 -0
  35. ngio/io_pipes/_ops_slices_utils.py +196 -0
  36. ngio/io_pipes/_ops_transforms.py +104 -0
  37. ngio/io_pipes/_zoom_transform.py +175 -0
  38. ngio/ome_zarr_meta/__init__.py +4 -2
  39. ngio/ome_zarr_meta/ngio_specs/__init__.py +4 -10
  40. ngio/ome_zarr_meta/ngio_specs/_axes.py +186 -175
  41. ngio/ome_zarr_meta/ngio_specs/_channels.py +55 -18
  42. ngio/ome_zarr_meta/ngio_specs/_dataset.py +48 -122
  43. ngio/ome_zarr_meta/ngio_specs/_ngio_hcs.py +6 -15
  44. ngio/ome_zarr_meta/ngio_specs/_ngio_image.py +38 -87
  45. ngio/ome_zarr_meta/ngio_specs/_pixel_size.py +17 -1
  46. ngio/ome_zarr_meta/v04/_v04_spec_utils.py +34 -31
  47. ngio/resources/20200812-CardiomyocyteDifferentiation14-Cycle1_B03/mask.png +0 -0
  48. ngio/resources/20200812-CardiomyocyteDifferentiation14-Cycle1_B03/nuclei.png +0 -0
  49. ngio/resources/20200812-CardiomyocyteDifferentiation14-Cycle1_B03/raw.jpg +0 -0
  50. ngio/resources/__init__.py +55 -0
  51. ngio/resources/resource_model.py +36 -0
  52. ngio/tables/backends/_abstract_backend.py +5 -6
  53. ngio/tables/backends/_anndata.py +1 -2
  54. ngio/tables/backends/_anndata_utils.py +3 -3
  55. ngio/tables/backends/_non_zarr_backends.py +1 -1
  56. ngio/tables/backends/_table_backends.py +0 -1
  57. ngio/tables/backends/_utils.py +3 -3
  58. ngio/tables/v1/_roi_table.py +165 -70
  59. ngio/transforms/__init__.py +5 -0
  60. ngio/transforms/_zoom.py +19 -0
  61. ngio/utils/__init__.py +2 -3
  62. ngio/utils/_datasets.py +5 -0
  63. ngio/utils/_logger.py +19 -0
  64. ngio/utils/_zarr_utils.py +6 -6
  65. {ngio-0.3.4.dist-info → ngio-0.4.0.dist-info}/METADATA +24 -22
  66. ngio-0.4.0.dist-info/RECORD +85 -0
  67. ngio/common/_array_pipe.py +0 -288
  68. ngio/common/_axes_transforms.py +0 -64
  69. ngio/common/_common_types.py +0 -5
  70. ngio/common/_slicer.py +0 -96
  71. ngio-0.3.4.dist-info/RECORD +0 -61
  72. {ngio-0.3.4.dist-info → ngio-0.4.0.dist-info}/WHEEL +0 -0
  73. {ngio-0.3.4.dist-info → ngio-0.4.0.dist-info}/licenses/LICENSE +0 -0
ngio/images/_create.py CHANGED
@@ -1,8 +1,10 @@
1
1
  """Utility functions for working with OME-Zarr images."""
2
2
 
3
- from collections.abc import Collection
3
+ from collections.abc import Sequence
4
4
  from typing import TypeVar
5
5
 
6
+ from zarr.types import DIMENSION_SEPARATOR
7
+
6
8
  from ngio.common._pyramid import init_empty_pyramid
7
9
  from ngio.ome_zarr_meta import (
8
10
  NgioImageMeta,
@@ -29,7 +31,7 @@ _image_or_label_meta = TypeVar("_image_or_label_meta", NgioImageMeta, NgioLabelM
29
31
  def _init_generic_meta(
30
32
  meta_type: type[_image_or_label_meta],
31
33
  pixelsize: float,
32
- axes_names: Collection[str],
34
+ axes_names: Sequence[str],
33
35
  z_spacing: float = 1.0,
34
36
  time_spacing: float = 1.0,
35
37
  levels: int | list[str] = 5,
@@ -80,7 +82,7 @@ def _init_generic_meta(
80
82
 
81
83
  def create_empty_label_container(
82
84
  store: StoreOrGroup,
83
- shape: Collection[int],
85
+ shape: Sequence[int],
84
86
  pixelsize: float,
85
87
  z_spacing: float = 1.0,
86
88
  time_spacing: float = 1.0,
@@ -89,10 +91,12 @@ def create_empty_label_container(
89
91
  z_scaling_factor: float = 1.0,
90
92
  space_unit: SpaceUnits | str | None = DefaultSpaceUnit,
91
93
  time_unit: TimeUnits | str | None = DefaultTimeUnit,
92
- axes_names: Collection[str] | None = None,
94
+ axes_names: Sequence[str] | None = None,
93
95
  name: str | None = None,
94
- chunks: Collection[int] | None = None,
95
- dtype: str = "uint16",
96
+ chunks: Sequence[int] | None = None,
97
+ dtype: str = "uint32",
98
+ dimension_separator: DIMENSION_SEPARATOR = "/",
99
+ compressor="default",
96
100
  overwrite: bool = False,
97
101
  version: NgffVersions = DefaultNgffVersion,
98
102
  ) -> ZarrGroupHandler:
@@ -100,7 +104,7 @@ def create_empty_label_container(
100
104
 
101
105
  Args:
102
106
  store (StoreOrGroup): The Zarr store or group to create the image in.
103
- shape (Collection[int]): The shape of the image.
107
+ shape (Sequence[int]): The shape of the image.
104
108
  pixelsize (float): The pixel size in x and y dimensions.
105
109
  z_spacing (float, optional): The spacing between z slices. Defaults to 1.0.
106
110
  time_spacing (float, optional): The spacing between time points.
@@ -115,11 +119,14 @@ def create_empty_label_container(
115
119
  DefaultSpaceUnit.
116
120
  time_unit (TimeUnits, optional): The unit of time. Defaults to
117
121
  DefaultTimeUnit.
118
- axes_names (Collection[str] | None, optional): The names of the axes.
122
+ axes_names (Sequence[str] | None, optional): The names of the axes.
119
123
  If None the canonical names are used. Defaults to None.
120
124
  name (str | None, optional): The name of the image. Defaults to None.
121
- chunks (Collection[int] | None, optional): The chunk shape. If None the shape
125
+ chunks (Sequence[int] | None, optional): The chunk shape. If None the shape
122
126
  is used. Defaults to None.
127
+ dimension_separator (DIMENSION_SEPARATOR): The separator to use for
128
+ dimensions. Defaults to "/".
129
+ compressor: The compressor to use. Defaults to "default".
123
130
  dtype (str, optional): The data type of the image. Defaults to "uint16".
124
131
  overwrite (bool, optional): Whether to overwrite an existing image.
125
132
  Defaults to True.
@@ -164,6 +171,8 @@ def create_empty_label_container(
164
171
  chunks=chunks,
165
172
  dtype=dtype,
166
173
  mode="a",
174
+ dimension_separator=dimension_separator,
175
+ compressor=compressor,
167
176
  )
168
177
  group_handler._mode = "r+"
169
178
  return group_handler
@@ -171,7 +180,7 @@ def create_empty_label_container(
171
180
 
172
181
  def create_empty_image_container(
173
182
  store: StoreOrGroup,
174
- shape: Collection[int],
183
+ shape: Sequence[int],
175
184
  pixelsize: float,
176
185
  z_spacing: float = 1.0,
177
186
  time_spacing: float = 1.0,
@@ -180,10 +189,12 @@ def create_empty_image_container(
180
189
  z_scaling_factor: float = 1.0,
181
190
  space_unit: SpaceUnits | str | None = DefaultSpaceUnit,
182
191
  time_unit: TimeUnits | str | None = DefaultTimeUnit,
183
- axes_names: Collection[str] | None = None,
192
+ axes_names: Sequence[str] | None = None,
184
193
  name: str | None = None,
185
- chunks: Collection[int] | None = None,
194
+ chunks: Sequence[int] | None = None,
186
195
  dtype: str = "uint16",
196
+ dimension_separator: DIMENSION_SEPARATOR = "/",
197
+ compressor="default",
187
198
  overwrite: bool = False,
188
199
  version: NgffVersions = DefaultNgffVersion,
189
200
  ) -> ZarrGroupHandler:
@@ -191,7 +202,7 @@ def create_empty_image_container(
191
202
 
192
203
  Args:
193
204
  store (StoreOrGroup): The Zarr store or group to create the image in.
194
- shape (Collection[int]): The shape of the image.
205
+ shape (Sequence[int]): The shape of the image.
195
206
  pixelsize (float): The pixel size in x and y dimensions.
196
207
  z_spacing (float, optional): The spacing between z slices. Defaults to 1.0.
197
208
  time_spacing (float, optional): The spacing between time points.
@@ -206,12 +217,15 @@ def create_empty_image_container(
206
217
  DefaultSpaceUnit.
207
218
  time_unit (TimeUnits, optional): The unit of time. Defaults to
208
219
  DefaultTimeUnit.
209
- axes_names (Collection[str] | None, optional): The names of the axes.
220
+ axes_names (Sequence[str] | None, optional): The names of the axes.
210
221
  If None the canonical names are used. Defaults to None.
211
222
  name (str | None, optional): The name of the image. Defaults to None.
212
- chunks (Collection[int] | None, optional): The chunk shape. If None the shape
223
+ chunks (Sequence[int] | None, optional): The chunk shape. If None the shape
213
224
  is used. Defaults to None.
214
225
  dtype (str, optional): The data type of the image. Defaults to "uint16".
226
+ dimension_separator (DIMENSION_SEPARATOR): The separator to use for
227
+ dimensions. Defaults to "/".
228
+ compressor: The compressor to use. Defaults to "default".
215
229
  overwrite (bool, optional): Whether to overwrite an existing image.
216
230
  Defaults to True.
217
231
  version (str, optional): The version of the OME-Zarr specification.
@@ -254,6 +268,8 @@ def create_empty_image_container(
254
268
  chunks=chunks,
255
269
  dtype=dtype,
256
270
  mode="a",
271
+ dimension_separator=dimension_separator,
272
+ compressor=compressor,
257
273
  )
258
274
 
259
275
  group_handler._mode = "r+"
@@ -0,0 +1,138 @@
1
+ """Abstract class for handling OME-NGFF images."""
2
+
3
+ from collections.abc import Sequence
4
+
5
+ import numpy as np
6
+ import PIL.Image
7
+ from zarr.types import DIMENSION_SEPARATOR
8
+
9
+ from ngio.common._synt_images_utils import fit_to_shape
10
+ from ngio.images._ome_zarr_container import OmeZarrContainer, create_ome_zarr_from_array
11
+ from ngio.ome_zarr_meta.ngio_specs import (
12
+ DefaultNgffVersion,
13
+ NgffVersions,
14
+ )
15
+ from ngio.resources import AVAILABLE_SAMPLES, SampleInfo, get_sample_info
16
+ from ngio.tables import (
17
+ DefaultTableBackend,
18
+ TableBackend,
19
+ )
20
+ from ngio.utils import (
21
+ StoreOrGroup,
22
+ )
23
+
24
+
25
+ def create_synthetic_ome_zarr(
26
+ store: StoreOrGroup,
27
+ shape: Sequence[int],
28
+ reference_sample: AVAILABLE_SAMPLES | SampleInfo = "Cardiomyocyte",
29
+ levels: int | list[str] = 5,
30
+ xy_scaling_factor: float = 2,
31
+ z_scaling_factor: float = 1.0,
32
+ axes_names: Sequence[str] | None = None,
33
+ chunks: Sequence[int] | None = None,
34
+ channel_labels: list[str] | None = None,
35
+ channel_wavelengths: list[str] | None = None,
36
+ channel_colors: Sequence[str] | None = None,
37
+ channel_active: Sequence[bool] | None = None,
38
+ table_backend: TableBackend = DefaultTableBackend,
39
+ dimension_separator: DIMENSION_SEPARATOR = "/",
40
+ compressor="default",
41
+ overwrite: bool = False,
42
+ version: NgffVersions = DefaultNgffVersion,
43
+ ) -> OmeZarrContainer:
44
+ """Create an empty OME-Zarr image with the given shape and metadata.
45
+
46
+ Args:
47
+ store (StoreOrGroup): The Zarr store or group to create the image in.
48
+ shape (Sequence[int]): The shape of the image.
49
+ reference_sample (AVAILABLE_SAMPLES | SampleInfo): The reference sample to use.
50
+ levels (int | list[str], optional): The number of levels in the pyramid or a
51
+ list of level names. Defaults to 5.
52
+ xy_scaling_factor (float, optional): The down-scaling factor in x and y
53
+ dimensions. Defaults to 2.0.
54
+ z_scaling_factor (float, optional): The down-scaling factor in z dimension.
55
+ Defaults to 1.0.
56
+ axes_names (Sequence[str] | None, optional): The names of the axes.
57
+ If None the canonical names are used. Defaults to None.
58
+ chunks (Sequence[int] | None, optional): The chunk shape. If None the shape
59
+ is used. Defaults to None.
60
+ channel_labels (list[str] | None, optional): The labels of the channels.
61
+ Defaults to None.
62
+ channel_wavelengths (list[str] | None, optional): The wavelengths of the
63
+ channels. Defaults to None.
64
+ channel_colors (Sequence[str] | None, optional): The colors of the channels.
65
+ Defaults to None.
66
+ channel_active (Sequence[bool] | None, optional): Whether the channels are
67
+ active. Defaults to None.
68
+ table_backend (TableBackend): Table backend to be used to store tables
69
+ dimension_separator (DIMENSION_SEPARATOR): The separator to use for
70
+ dimensions. Defaults to "/".
71
+ compressor: The compressor to use. Defaults to "default".
72
+ overwrite (bool, optional): Whether to overwrite an existing image.
73
+ Defaults to True.
74
+ version (NgffVersion, optional): The version of the OME-Zarr specification.
75
+ Defaults to DefaultNgffVersion.
76
+ """
77
+ if isinstance(reference_sample, str):
78
+ sample_info = get_sample_info(reference_sample)
79
+ else:
80
+ sample_info = reference_sample
81
+
82
+ raw = np.asarray(PIL.Image.open(sample_info.img_path))
83
+ raw = fit_to_shape(arr=raw, out_shape=tuple(shape))
84
+ raw = raw / np.max(raw) * (2**16 - 1)
85
+ raw = raw.astype(np.uint16)
86
+ ome_zarr = create_ome_zarr_from_array(
87
+ store=store,
88
+ array=raw,
89
+ xy_pixelsize=sample_info.xy_pixelsize,
90
+ z_spacing=sample_info.z_spacing,
91
+ time_spacing=sample_info.time_spacing,
92
+ levels=levels,
93
+ xy_scaling_factor=xy_scaling_factor,
94
+ z_scaling_factor=z_scaling_factor,
95
+ space_unit=sample_info.space_unit,
96
+ time_unit=sample_info.time_unit,
97
+ axes_names=axes_names,
98
+ channel_labels=channel_labels,
99
+ channel_wavelengths=channel_wavelengths,
100
+ channel_colors=channel_colors,
101
+ channel_active=channel_active,
102
+ name=sample_info.name,
103
+ chunks=chunks,
104
+ overwrite=overwrite,
105
+ dimension_separator=dimension_separator,
106
+ compressor=compressor,
107
+ version=version,
108
+ )
109
+
110
+ image = ome_zarr.get_image()
111
+ well_table = image.build_image_roi_table()
112
+ ome_zarr.add_table("well_ROI_table", table=well_table, backend=table_backend)
113
+
114
+ for label_info in sample_info.labels:
115
+ ome_zarr.derive_label(name=label_info.name)
116
+ label = ome_zarr.get_label(name=label_info.name)
117
+
118
+ ref_label = np.asarray(PIL.Image.open(label_info.label_path))
119
+ ref_label = ref_label.astype(label_info.dtype)
120
+
121
+ ref_label = fit_to_shape(
122
+ arr=ref_label,
123
+ out_shape=label.shape,
124
+ ensure_unique_info=label_info.ensure_unique_labels,
125
+ )
126
+ ref_label = ref_label.astype(np.uint32)
127
+ label.set_array(ref_label)
128
+ label.consolidate()
129
+
130
+ if label_info.create_masking_table:
131
+ masking_table = label.build_masking_roi_table()
132
+ ome_zarr.add_table(
133
+ name=f"{label_info.name}_masking_table",
134
+ table=masking_table,
135
+ backend=table_backend,
136
+ )
137
+
138
+ return ome_zarr