ngio 0.3.5__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 +3 -3
  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 -1
  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.5.dist-info → ngio-0.4.0.dist-info}/METADATA +16 -14
  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.5.dist-info/RECORD +0 -61
  72. {ngio-0.3.5.dist-info → ngio-0.4.0.dist-info}/WHEEL +0 -0
  73. {ngio-0.3.5.dist-info → ngio-0.4.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,446 @@
1
+ import math
2
+ from collections.abc import Mapping, Sequence
3
+ from typing import TypeAlias, assert_never
4
+ from warnings import warn
5
+
6
+ import dask.array as da
7
+ import numpy as np
8
+ import zarr
9
+ from pydantic import BaseModel, ConfigDict
10
+
11
+ from ngio.common._dimensions import Dimensions
12
+ from ngio.io_pipes._ops_slices_utils import compute_slice_chunks
13
+ from ngio.ome_zarr_meta.ngio_specs import Axis
14
+ from ngio.utils import NgioValueError
15
+
16
+ SlicingInputType: TypeAlias = slice | Sequence[int] | int | None
17
+ SlicingType: TypeAlias = slice | tuple[int, ...] | int
18
+
19
+ ##############################################################
20
+ #
21
+ # "SlicingOps" model
22
+ #
23
+ ##############################################################
24
+
25
+
26
+ def _int_boundary_check(value: int, shape: int) -> int:
27
+ """Ensure that the integer value is within the boundaries of the array shape."""
28
+ if value < 0 or value >= shape:
29
+ raise NgioValueError(
30
+ f"Invalid index {value}. Index is out of bounds for axis with size {shape}."
31
+ )
32
+ return value
33
+
34
+
35
+ def _slicing_tuple_boundary_check(
36
+ slicing_tuple: tuple[SlicingType, ...],
37
+ array_shape: tuple[int, ...],
38
+ ) -> tuple[SlicingType, ...]:
39
+ """Ensure that the slicing tuple is within the boundaries of the array shape.
40
+
41
+ This function normalizes the slicing tuple to ensure that the selection
42
+ is within the boundaries of the array shape.
43
+ """
44
+ if len(slicing_tuple) != len(array_shape):
45
+ raise NgioValueError(
46
+ f"Invalid slicing tuple {slicing_tuple}. "
47
+ f"Length {len(slicing_tuple)} does not match array shape {array_shape}."
48
+ )
49
+ out_slicing_tuple = []
50
+ for sl, sh in zip(slicing_tuple, array_shape, strict=True):
51
+ if isinstance(sl, slice):
52
+ start, stop, step = sl.start, sl.stop, sl.step
53
+ if start is not None:
54
+ start = math.floor(start)
55
+ start = max(0, min(start, sh))
56
+ if stop is not None:
57
+ stop = math.ceil(stop)
58
+ stop = max(0, min(stop, sh))
59
+ out_slicing_tuple.append(slice(start, stop, step))
60
+ elif isinstance(sl, int):
61
+ _int_boundary_check(sl, shape=sh)
62
+ out_slicing_tuple.append(sl)
63
+ elif isinstance(sl, tuple):
64
+ [_int_boundary_check(i, shape=sh) for i in sl]
65
+ out_slicing_tuple.append(sl)
66
+ else:
67
+ assert_never(sl)
68
+
69
+ return tuple(out_slicing_tuple)
70
+
71
+
72
+ class SlicingOps(BaseModel):
73
+ """Class to hold slicing operations."""
74
+
75
+ on_disk_axes: tuple[str, ...]
76
+ on_disk_shape: tuple[int, ...]
77
+ on_disk_chunks: tuple[int, ...]
78
+ slicing_tuple: tuple[SlicingType, ...]
79
+ model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
80
+
81
+ @property
82
+ def normalized_slicing_tuple(self) -> tuple[SlicingType, ...]:
83
+ """Normalize the slicing tuple to be within the array shape boundaries."""
84
+ return _slicing_tuple_boundary_check(
85
+ slicing_tuple=self.slicing_tuple,
86
+ array_shape=self.on_disk_shape,
87
+ )
88
+
89
+ @property
90
+ def slice_axes(self) -> tuple[str, ...]:
91
+ """The axes after slicing."""
92
+ in_memory_axes = []
93
+ for ax, sl in zip(self.on_disk_axes, self.slicing_tuple, strict=True):
94
+ if isinstance(sl, int):
95
+ continue
96
+ in_memory_axes.append(ax)
97
+ return tuple(in_memory_axes)
98
+
99
+ def slice_chunks(self) -> set[tuple[int, ...]]:
100
+ """The required to read or write the slice."""
101
+ return compute_slice_chunks(
102
+ shape=self.on_disk_shape,
103
+ chunks=self.on_disk_chunks,
104
+ slicing_tuple=self.normalized_slicing_tuple,
105
+ )
106
+
107
+ def get(self, ax_name: str, normalize: bool = False) -> SlicingType:
108
+ """Get the slicing tuple."""
109
+ slicing_tuple = (
110
+ self.slicing_tuple if not normalize else self.normalized_slicing_tuple
111
+ )
112
+ if ax_name not in self.on_disk_axes:
113
+ return slice(None)
114
+ ax_index = self.on_disk_axes.index(ax_name)
115
+ return slicing_tuple[ax_index]
116
+
117
+
118
+ def _check_tuple_in_slicing_tuple(
119
+ slicing_tuple: tuple[SlicingType, ...],
120
+ ) -> tuple[None, None] | tuple[int, tuple[int, ...]]:
121
+ """Check if there are any tuple in the slicing tuple.
122
+
123
+ The zarr python api only supports int or slices, not tuples.
124
+ Ngio support a single tuple in the slicing tuple to allow non-contiguous
125
+ selection (main use case: selecting multiple channels).
126
+ """
127
+ # Find if the is any tuple in the slicing tuple
128
+ # If there is one we need to handle it differently
129
+ tuple_in_slice = [
130
+ (i, s) for i, s in enumerate(slicing_tuple) if isinstance(s, tuple)
131
+ ]
132
+ if not tuple_in_slice:
133
+ # No tuple in the slicing tuple
134
+ return None, None
135
+
136
+ if len(tuple_in_slice) > 1:
137
+ raise NotImplementedError(
138
+ "Slicing with multiple non-contiguous tuples/lists "
139
+ "is not supported yet in Ngio. Use directly the "
140
+ "zarr.Array api to get the correct array slice."
141
+ )
142
+ # Complex case, we have exactly one tuple in the slicing tuple
143
+ ax, first_tuple = tuple_in_slice[0]
144
+ if len(first_tuple) > 100:
145
+ warn(
146
+ "Performance warning: "
147
+ "Non-contiguous slicing with a tuple/list with more than 100 elements is "
148
+ "not natively supported by zarr. This is implemented by Ngio by performing "
149
+ "multiple reads and stacking the result.",
150
+ stacklevel=2,
151
+ )
152
+ return ax, first_tuple
153
+
154
+
155
+ ##############################################################
156
+ #
157
+ # Slicing implementations
158
+ #
159
+ ##############################################################
160
+
161
+
162
+ def get_slice_as_numpy(zarr_array: zarr.Array, slicing_ops: SlicingOps) -> np.ndarray:
163
+ """Get a slice of a zarr array as a numpy array."""
164
+ slicing_tuple = slicing_ops.normalized_slicing_tuple
165
+ # Find if the is any tuple in the slicing tuple
166
+ # If there is one we need to handle it differently
167
+ ax, first_tuple = _check_tuple_in_slicing_tuple(slicing_tuple)
168
+ if ax is None:
169
+ # Simple case, no tuple in the slicing tuple
170
+ return zarr_array[slicing_tuple]
171
+
172
+ assert first_tuple is not None
173
+ slices = [
174
+ zarr_array[(*slicing_tuple[:ax], idx, *slicing_tuple[ax + 1 :])]
175
+ for idx in first_tuple
176
+ ]
177
+ out_array = np.stack(slices, axis=ax)
178
+ return out_array
179
+
180
+
181
+ def get_slice_as_dask(zarr_array: zarr.Array, slicing_ops: SlicingOps) -> da.Array:
182
+ """Get a slice of a zarr array as a dask array."""
183
+ da_array = da.from_zarr(zarr_array)
184
+ slicing_tuple = slicing_ops.normalized_slicing_tuple
185
+ # Find if the is any tuple in the slicing tuple
186
+ # If there is one we need to handle it differently
187
+ ax, first_tuple = _check_tuple_in_slicing_tuple(slicing_tuple)
188
+ if ax is None:
189
+ # Base case, no tuple in the slicing tuple
190
+ return da_array[slicing_tuple]
191
+
192
+ assert first_tuple is not None
193
+ slices = [
194
+ da_array[(*slicing_tuple[:ax], idx, *slicing_tuple[ax + 1 :])]
195
+ for idx in first_tuple
196
+ ]
197
+ out_array = da.stack(slices, axis=ax)
198
+ return out_array
199
+
200
+
201
+ def set_slice_as_numpy(
202
+ zarr_array: zarr.Array,
203
+ patch: np.ndarray,
204
+ slicing_ops: SlicingOps,
205
+ ) -> None:
206
+ slice_tuple = slicing_ops.normalized_slicing_tuple
207
+ ax, first_tuple = _check_tuple_in_slicing_tuple(slice_tuple)
208
+ if ax is None:
209
+ # Base case, no tuple in the slicing tuple
210
+ zarr_array[slice_tuple] = patch
211
+ return
212
+
213
+ # Complex case, we have exactly one tuple in the slicing tuple
214
+ assert first_tuple is not None
215
+ for i, idx in enumerate(first_tuple):
216
+ _sub_slice = (*slice_tuple[:ax], idx, *slice_tuple[ax + 1 :])
217
+ zarr_array[_sub_slice] = np.take(patch, indices=i, axis=ax)
218
+
219
+
220
+ def handle_int_set_as_dask(
221
+ patch: da.Array,
222
+ slicing_tuple: tuple[SlicingType, ...],
223
+ ) -> tuple[da.Array, tuple[SlicingType, ...]]:
224
+ """Handle the case where the slicing tuple contains integers.
225
+
226
+ In this case we need to expand the patch array to match the slicing tuple.
227
+ """
228
+ new_slicing_tuple = list(slicing_tuple)
229
+ for i, sl in enumerate(slicing_tuple):
230
+ if isinstance(sl, int):
231
+ patch = da.expand_dims(patch, axis=i)
232
+ new_slicing_tuple[i] = slice(sl, sl + 1)
233
+ return patch, tuple(new_slicing_tuple)
234
+
235
+
236
+ def set_slice_as_dask(
237
+ zarr_array: zarr.Array, patch: da.Array, slicing_ops: SlicingOps
238
+ ) -> None:
239
+ slice_tuple = slicing_ops.normalized_slicing_tuple
240
+ ax, first_tuple = _check_tuple_in_slicing_tuple(slice_tuple)
241
+ patch, slice_tuple = handle_int_set_as_dask(patch, slice_tuple)
242
+ if ax is None:
243
+ # Base case, no tuple in the slicing tuple
244
+ # assert False
245
+ da.to_zarr(arr=patch, url=zarr_array, region=slice_tuple)
246
+ return
247
+
248
+ # Complex case, we have exactly one tuple in the slicing tuple
249
+ assert first_tuple is not None
250
+ for i, idx in enumerate(first_tuple):
251
+ _sub_slice = (*slice_tuple[:ax], slice(idx, idx + 1), *slice_tuple[ax + 1 :])
252
+ sub_patch = da.take(patch, indices=i, axis=ax)
253
+ sub_patch = da.expand_dims(sub_patch, axis=ax)
254
+ da.to_zarr(arr=sub_patch, url=zarr_array, region=_sub_slice)
255
+
256
+
257
+ ##############################################################
258
+ #
259
+ # Builder functions
260
+ #
261
+ ##############################################################
262
+
263
+
264
+ def _try_to_slice(value: Sequence[int]) -> slice | tuple[int, ...]:
265
+ """Try to convert a list of integers into a slice if they are contiguous.
266
+
267
+ - If the input is empty, return an empty tuple.
268
+ - If the input is sorted, and contains contiguous integers,
269
+ return a slice from the minimum to the maximum integer.
270
+ - Otherwise, return the input as a tuple.
271
+
272
+ This is useful for optimizing array slicing operations
273
+ by allowing the use of slices when possible, which can be more efficient.
274
+ """
275
+ if not value:
276
+ raise NgioValueError("Ngio does not support empty sequences as slice input.")
277
+
278
+ if not all(isinstance(i, int) for i in value):
279
+ _value = []
280
+ for i in value:
281
+ try:
282
+ _value.append(int(i))
283
+ except Exception as e:
284
+ raise NgioValueError(
285
+ f"Invalid value {i} of type {type(i)} in sequence {value}"
286
+ ) from e
287
+ value = _value
288
+ # If the input is not sorted, return it as a tuple
289
+ max_input = max(value)
290
+ min_input = min(value)
291
+ assert min_input >= 0, "Input must contain non-negative integers"
292
+
293
+ if sorted(value) == list(range(min_input, max_input + 1)):
294
+ return slice(min_input, max_input + 1)
295
+
296
+ return tuple(value)
297
+
298
+
299
+ def _remove_channel_slicing(
300
+ slicing_dict: dict[str, SlicingInputType],
301
+ dimensions: Dimensions,
302
+ ) -> dict[str, SlicingInputType]:
303
+ """This utility function removes the channel selection from the slice kwargs.
304
+
305
+ if ignore_channel_selection is True, it will remove the channel selection
306
+ regardless of the dimensions. If the ignore_channel_selection is False
307
+ it will fail.
308
+ """
309
+ if dimensions.is_multi_channels:
310
+ return slicing_dict
311
+
312
+ if "c" in slicing_dict:
313
+ slicing_dict.pop("c", None)
314
+ return slicing_dict
315
+
316
+
317
+ def _check_slicing_virtual_axes(slice_: SlicingInputType) -> bool:
318
+ """Check if the slice_ is compatible with virtual axes.
319
+
320
+ Virtual axes are axes that are not present in the actual data,
321
+ such as time or channel axes in some datasets.
322
+ So the only valid slices for virtual axes are:
323
+ - None: means all data along the axis
324
+ - 0: means the first element along the axis
325
+ - slice([0, None], [1, None])
326
+ """
327
+ if slice_ is None or slice_ == 0:
328
+ return True
329
+ if isinstance(slice_, slice):
330
+ if slice_.start is None and slice_.stop is None:
331
+ return True
332
+ if slice_.start == 0 and slice_.stop is None:
333
+ return True
334
+ if slice_.start is None and slice_.stop == 0:
335
+ return True
336
+ if slice_.start == 0 and slice_.stop == 1:
337
+ return True
338
+ if isinstance(slice_, Sequence):
339
+ if len(slice_) == 1 and slice_[0] == 0:
340
+ return True
341
+ return False
342
+
343
+
344
+ def _clean_slicing_dict(
345
+ dimensions: Dimensions,
346
+ slicing_dict: Mapping[str, SlicingInputType],
347
+ remove_channel_selection: bool = False,
348
+ ) -> dict[str, SlicingInputType]:
349
+ """Clean the slicing dict.
350
+
351
+ This function will:
352
+ - Validate that the axes in the slicing_dict are present in the dimensions.
353
+ - Make sure that the slicing_dict uses the on-disk axis names.
354
+ - Check for duplicate axis names in the slicing_dict.
355
+ - Clean up channel selection if the dimensions
356
+ """
357
+ clean_slicing_dict: dict[str, SlicingInputType] = {}
358
+ for axis_name, slice_ in slicing_dict.items():
359
+ axis = dimensions.axes_handler.get_axis(axis_name)
360
+ if axis is None:
361
+ # Virtual axes should be allowed to be selected
362
+ # Common use case is still allowing channel_selection
363
+ # When the zarr has not channel axis.
364
+ if not _check_slicing_virtual_axes(slice_):
365
+ raise NgioValueError(
366
+ f"Invalid axis selection:{axis_name}={slice_}. "
367
+ f"Not found on the on-disk axes {dimensions.axes}."
368
+ )
369
+ # Virtual axes can be safely ignored
370
+ continue
371
+ if axis.name in clean_slicing_dict:
372
+ raise NgioValueError(
373
+ f"Duplicate axis {axis.name} in slice kwargs. "
374
+ "Please provide unique axis names."
375
+ )
376
+ clean_slicing_dict[axis.name] = slice_
377
+
378
+ if remove_channel_selection:
379
+ clean_slicing_dict = _remove_channel_slicing(
380
+ slicing_dict=clean_slicing_dict, dimensions=dimensions
381
+ )
382
+ return clean_slicing_dict
383
+
384
+
385
+ def _normalize_slicing_tuple(
386
+ axis: Axis,
387
+ slicing_dict: dict[str, SlicingInputType],
388
+ ) -> SlicingType:
389
+ """Normalize the slicing dict to tuple.
390
+
391
+ Since the slicing dict can contain different types of values
392
+ We need to normalize them to more predictable types.
393
+ The output types are:
394
+ - slice
395
+ - int
396
+ - tuple of int (for non-contiguous selection)
397
+ """
398
+ axis_name = axis.name
399
+ if axis_name not in slicing_dict:
400
+ # If no slice is provided for the axis, use a full slice
401
+ return slice(None)
402
+
403
+ value = slicing_dict[axis_name]
404
+ if value is None:
405
+ return slice(None)
406
+ if isinstance(value, slice) or isinstance(value, int):
407
+ return value
408
+ elif isinstance(value, Sequence):
409
+ # If a contiguous sequence of integers is provided,
410
+ # convert it to a slice for simplicity.
411
+ # Alternatively, it will be converted to a tuple of ints
412
+ return _try_to_slice(value)
413
+
414
+ raise NgioValueError(
415
+ f"Invalid slice definition {value} of type {type(value)}. "
416
+ "Allowed types are: int, slice, sequence of int or None."
417
+ )
418
+
419
+
420
+ def build_slicing_ops(
421
+ *,
422
+ dimensions: Dimensions,
423
+ slicing_dict: dict[str, SlicingInputType] | None,
424
+ remove_channel_selection: bool = False,
425
+ ) -> SlicingOps:
426
+ """Assemble slices to be used to query the array."""
427
+ slicing_dict = slicing_dict or {}
428
+ _slicing_dict = _clean_slicing_dict(
429
+ dimensions=dimensions,
430
+ slicing_dict=slicing_dict,
431
+ remove_channel_selection=remove_channel_selection,
432
+ )
433
+
434
+ slicing_tuple = tuple(
435
+ _normalize_slicing_tuple(
436
+ axis=axis,
437
+ slicing_dict=_slicing_dict,
438
+ )
439
+ for axis in dimensions.axes_handler.axes
440
+ )
441
+ return SlicingOps(
442
+ on_disk_axes=dimensions.axes_handler.axes_names,
443
+ on_disk_shape=dimensions.shape,
444
+ on_disk_chunks=dimensions.chunks,
445
+ slicing_tuple=slicing_tuple,
446
+ )
@@ -0,0 +1,196 @@
1
+ from collections.abc import Iterable, Iterator
2
+ from itertools import product
3
+ from typing import TypeAlias, TypeVar
4
+
5
+ from ngio.utils import NgioValueError, ngio_logger
6
+
7
+ T = TypeVar("T")
8
+
9
+ ##############################################################
10
+ #
11
+ # Check slice overlaps
12
+ #
13
+ ##############################################################
14
+
15
+
16
+ def _pairs_stream(iterable: Iterable[T]) -> Iterator[tuple[T, T]]:
17
+ # Same as combinations but yields pairs as soon as they are generated
18
+ seen: list[T] = []
19
+ for a in iterable:
20
+ for b in seen:
21
+ yield b, a
22
+ seen.append(a)
23
+
24
+
25
+ SlicingType: TypeAlias = slice | tuple[int, ...] | int
26
+
27
+
28
+ def check_elem_intersection(s1: SlicingType, s2: SlicingType) -> bool:
29
+ """Compare if two SlicingType elements intersect.
30
+
31
+ If they are a slice, check if they overlap.
32
+ If they are integers, check if they are equal.
33
+ If they are tuples, check if they have any common elements.
34
+ """
35
+ if not isinstance(s1, type(s2)):
36
+ raise NgioValueError(
37
+ f"Slices must be of the same type. Got {type(s1)} and {type(s2)}"
38
+ )
39
+
40
+ if isinstance(s1, slice) and isinstance(s2, slice):
41
+ # Handle slice objects
42
+ start1, stop1, step1 = s1.start or 0, s1.stop or float("inf"), s1.step or 1
43
+ start2, stop2, step2 = s2.start or 0, s2.stop or float("inf"), s2.step or 1
44
+
45
+ if step1 is not None and step2 != 1:
46
+ raise NotImplementedError(
47
+ "Intersection for slices with step != 1 is not implemented"
48
+ )
49
+
50
+ if step2 is not None and step1 != 1:
51
+ raise NotImplementedError(
52
+ "Intersection for slices with step != 1 is not implemented"
53
+ )
54
+
55
+ return not (stop1 <= start2 or stop2 <= start1)
56
+ elif isinstance(s1, int) and isinstance(s2, int):
57
+ # Handle integer indices
58
+ return s1 == s2
59
+ elif isinstance(s1, tuple) and isinstance(s2, tuple):
60
+ if set(s1) & set(s2):
61
+ return True
62
+ return False
63
+ else:
64
+ raise TypeError("Unsupported slice type")
65
+
66
+
67
+ def check_slicing_tuple_intersection(
68
+ s1: tuple[SlicingType, ...], s2: tuple[SlicingType, ...]
69
+ ) -> bool:
70
+ """For a tuple of SlicingType, check if all elements intersect."""
71
+ if len(s1) != len(s2):
72
+ raise NgioValueError("Slices must have the same length")
73
+ return all(check_elem_intersection(a, b) for a, b in zip(s1, s2, strict=True))
74
+
75
+
76
+ def check_if_regions_overlap(slices: Iterable[tuple[SlicingType, ...]]) -> bool:
77
+ """Check for overlaps in a list of slicing tuples using brute-force method.
78
+
79
+ This is O(n^2) and not efficient for large lists.
80
+ Returns True if any overlaps are found.
81
+ """
82
+ for it, (si, sj) in enumerate(_pairs_stream(slices)):
83
+ overalap = check_slicing_tuple_intersection(si, sj)
84
+ if overalap:
85
+ return True
86
+
87
+ if it == 10_000:
88
+ ngio_logger.warning(
89
+ "Performance Warning check_for_overlaps is O(n^2) and may be slow for "
90
+ "large numbers of regions."
91
+ )
92
+ return False
93
+
94
+
95
+ ##############################################################
96
+ #
97
+ # Check chunk overlaps
98
+ #
99
+ ##############################################################
100
+
101
+
102
+ def _normalize_slice(slc: slice, size: int) -> tuple[int, int]:
103
+ if slc.step not in (None, 1):
104
+ raise NgioValueError(f"Only step=1 slices supported, got step={slc.step}")
105
+ start = 0 if slc.start is None else slc.start
106
+ stop = size if slc.stop is None else slc.stop
107
+ if start < 0 or stop < 0:
108
+ raise NgioValueError("Negative slice bounds are not supported")
109
+ # clamp to [0, size]
110
+ start = min(start, size)
111
+ stop = min(stop, size)
112
+ if start > stop:
113
+ # empty selection
114
+ return (0, 0)
115
+ return start, stop
116
+
117
+
118
+ def _chunk_indices_for_axis(sel: SlicingType, size: int, csize: int) -> list[int]:
119
+ """From a selection for a single axis, return the list chunk indices touched."""
120
+ if isinstance(sel, slice):
121
+ start, stop = _normalize_slice(sel, size)
122
+ if start >= stop: # empty
123
+ return []
124
+ first = start // csize
125
+ last = (stop - 1) // csize
126
+ return list(range(first, last + 1))
127
+
128
+ if isinstance(sel, int):
129
+ if sel < 0 or sel >= size:
130
+ raise IndexError(f"index {sel} out of bounds for axis of size {size}")
131
+ return [sel // csize]
132
+
133
+ if isinstance(sel, tuple):
134
+ if not sel:
135
+ return []
136
+ chunks_hit = {}
137
+ for v in sel:
138
+ if not isinstance(v, int):
139
+ raise TypeError("Only integers allowed inside tuple selections")
140
+ if v < 0 or v >= size:
141
+ raise IndexError(f"index {v} out of bounds for axis of size {size}")
142
+ chunks_hit[v // csize] = None
143
+ return sorted(chunks_hit.keys())
144
+
145
+ raise TypeError(f"Unsupported index type: {type(sel)!r}")
146
+
147
+
148
+ def compute_slice_chunks(
149
+ shape: tuple[int, ...],
150
+ chunks: tuple[int, ...],
151
+ slicing_tuple: tuple[SlicingType, ...],
152
+ ) -> set[tuple[int, ...]]:
153
+ """Compute the set of chunk coordinates touched by `slicing_tuple`.
154
+
155
+ Args:
156
+ shape: overall array shape (s1, s2, ...)
157
+ chunks: chunk shape (c1, c2, ...)
158
+ slicing_tuple: tuple of slices, ints, or tuples of ints
159
+ """
160
+ if len(slicing_tuple) != len(shape):
161
+ raise NgioValueError(
162
+ f"key must have {len(shape)} items, got {len(slicing_tuple)}"
163
+ )
164
+
165
+ per_axis_chunks: list[list[int]] = [
166
+ _chunk_indices_for_axis(sel, size, csize)
167
+ for sel, size, csize in zip(slicing_tuple, shape, chunks, strict=True)
168
+ ]
169
+
170
+ # If any axis yields no chunks, the overall selection is empty.
171
+ if any(len(ax) == 0 for ax in per_axis_chunks):
172
+ return set()
173
+
174
+ return {tuple(idx) for idx in product(*per_axis_chunks)}
175
+
176
+
177
+ def check_if_chunks_overlap(
178
+ slices: Iterable[tuple[SlicingType, ...]],
179
+ shape: tuple[int, ...],
180
+ chunks: tuple[int, ...],
181
+ ) -> bool:
182
+ """Check for overlaps in a list of slicing tuples using brute-force method.
183
+
184
+ This is O(n^2) and not efficient for large lists.
185
+ Returns True if any overlaps are found.
186
+ """
187
+ slices_chunks = (compute_slice_chunks(shape, chunks, si) for si in slices)
188
+ for it, (si, sj) in enumerate(_pairs_stream(slices_chunks)):
189
+ if si & sj:
190
+ return True
191
+ if it == 10_000:
192
+ ngio_logger.warning(
193
+ "Performance Warning check_for_chunks_overlaps is O(n^2) and may be "
194
+ "slow for large numbers of regions."
195
+ )
196
+ return False