ngio 0.5.0a1__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 (48) 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 +218 -78
  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 +114 -123
  9. ngio/images/_abstract_image.py +417 -35
  10. ngio/images/_create_synt_container.py +36 -43
  11. ngio/images/_create_utils.py +423 -0
  12. ngio/images/_image.py +155 -177
  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 +25 -20
  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 +44 -27
  39. ngio/utils/__init__.py +6 -12
  40. ngio/utils/_cache.py +48 -0
  41. ngio/utils/_zarr_utils.py +285 -245
  42. {ngio-0.5.0a1.dist-info → ngio-0.5.0a3.dist-info}/METADATA +8 -4
  43. {ngio-0.5.0a1.dist-info → ngio-0.5.0a3.dist-info}/RECORD +45 -45
  44. {ngio-0.5.0a1.dist-info → ngio-0.5.0a3.dist-info}/WHEEL +1 -1
  45. ngio/images/_create.py +0 -283
  46. ngio/tables/backends/_non_zarr_backends.py +0 -196
  47. ngio/utils/_logger.py +0 -50
  48. {ngio-0.5.0a1.dist-info → ngio-0.5.0a3.dist-info}/licenses/LICENSE +0 -0
@@ -1,18 +1,24 @@
1
1
  """Abstract class for handling OME-NGFF images."""
2
2
 
3
3
  import warnings
4
- from collections.abc import Sequence
5
- from typing import Literal
4
+ from collections.abc import Mapping, Sequence
5
+ from typing import Any, Literal
6
6
 
7
7
  import numpy as np
8
8
  from zarr.core.array import CompressorLike
9
9
 
10
- from ngio.images._create import create_empty_image_container
10
+ from ngio.common._pyramid import ChunksLike, ShardsLike
11
+ from ngio.images._create_utils import init_image_like
11
12
  from ngio.images._image import Image, ImagesContainer
12
13
  from ngio.images._label import Label, LabelsContainer
13
14
  from ngio.images._masked_image import MaskedImage, MaskedLabel
14
- from ngio.ome_zarr_meta import NgioImageMeta, PixelSize, find_label_meta_handler
15
+ from ngio.ome_zarr_meta import (
16
+ LabelMetaHandler,
17
+ NgioImageMeta,
18
+ PixelSize,
19
+ )
15
20
  from ngio.ome_zarr_meta.ngio_specs import (
21
+ Channel,
16
22
  DefaultNgffVersion,
17
23
  DefaultSpaceUnit,
18
24
  DefaultTimeUnit,
@@ -35,6 +41,7 @@ from ngio.tables import (
35
41
  )
36
42
  from ngio.utils import (
37
43
  AccessModeLiteral,
44
+ NgioError,
38
45
  NgioValidationError,
39
46
  NgioValueError,
40
47
  StoreOrGroup,
@@ -42,18 +49,26 @@ from ngio.utils import (
42
49
  )
43
50
 
44
51
 
45
- def _default_table_container(handler: ZarrGroupHandler) -> TablesContainer | None:
52
+ def _try_get_table_container(
53
+ handler: ZarrGroupHandler, create_mode: bool = True
54
+ ) -> TablesContainer | None:
46
55
  """Return a default table container."""
47
- success, table_handler = handler.safe_derive_handler("tables")
48
- if success and isinstance(table_handler, ZarrGroupHandler):
56
+ try:
57
+ table_handler = handler.get_handler("tables", create_mode=create_mode)
49
58
  return TablesContainer(table_handler)
59
+ except NgioError:
60
+ return None
50
61
 
51
62
 
52
- def _default_label_container(handler: ZarrGroupHandler) -> LabelsContainer | None:
63
+ def _try_get_label_container(
64
+ handler: ZarrGroupHandler, create_mode: bool = True
65
+ ) -> LabelsContainer | None:
53
66
  """Return a default label container."""
54
- success, label_handler = handler.safe_derive_handler("labels")
55
- if success and isinstance(label_handler, ZarrGroupHandler):
67
+ try:
68
+ label_handler = handler.get_handler("labels", create_mode=create_mode)
56
69
  return LabelsContainer(label_handler)
70
+ except NgioError:
71
+ return None
57
72
 
58
73
 
59
74
  class OmeZarrContainer:
@@ -128,13 +143,15 @@ class OmeZarrContainer:
128
143
  """
129
144
  return self._images_container
130
145
 
131
- def _get_labels_container(self) -> LabelsContainer | None:
146
+ def _get_labels_container(self, create_mode: bool = True) -> LabelsContainer | None:
132
147
  """Return the labels container."""
133
- if self._labels_container is None:
134
- _labels_container = _default_label_container(self._group_handler)
135
- if _labels_container is None:
136
- return None
137
- self._labels_container = _labels_container
148
+ if self._labels_container is not None:
149
+ return self._labels_container
150
+
151
+ _labels_container = _try_get_label_container(
152
+ self._group_handler, create_mode=create_mode
153
+ )
154
+ self._labels_container = _labels_container
138
155
  return self._labels_container
139
156
 
140
157
  @property
@@ -145,13 +162,15 @@ class OmeZarrContainer:
145
162
  raise NgioValidationError("No labels found in the image.")
146
163
  return _labels_container
147
164
 
148
- def _get_tables_container(self) -> TablesContainer | None:
165
+ def _get_tables_container(self, create_mode: bool = True) -> TablesContainer | None:
149
166
  """Return the tables container."""
150
- if self._tables_container is None:
151
- _tables_container = _default_table_container(self._group_handler)
152
- if _tables_container is None:
153
- return None
154
- self._tables_container = _tables_container
167
+ if self._tables_container is not None:
168
+ return self._tables_container
169
+
170
+ _tables_container = _try_get_table_container(
171
+ self._group_handler, create_mode=create_mode
172
+ )
173
+ self._tables_container = _tables_container
155
174
  return self._tables_container
156
175
 
157
176
  @property
@@ -403,61 +422,88 @@ class OmeZarrContainer:
403
422
  self,
404
423
  store: StoreOrGroup,
405
424
  ref_path: str | None = None,
425
+ # Metadata parameters
406
426
  shape: Sequence[int] | None = None,
407
- labels: Sequence[str] | None = None,
408
- pixel_size: PixelSize | None = None,
409
- axes_names: Sequence[str] | None = None,
427
+ pixelsize: float | tuple[float, float] | None = None,
428
+ z_spacing: float | None = None,
429
+ time_spacing: float | None = None,
410
430
  name: str | None = None,
411
- chunks: Sequence[int] | None = None,
412
- dtype: str | None = None,
413
- dimension_separator: Literal[".", "/"] | None = None,
414
- compressors: CompressorLike | None = None,
415
- copy_labels: bool = False,
416
- copy_tables: bool = False,
431
+ channels_meta: Sequence[str | Channel] | None = None,
417
432
  ngff_version: NgffVersions | None = None,
433
+ # Zarr Array parameters
434
+ chunks: ChunksLike = "auto",
435
+ shards: ShardsLike | None = None,
436
+ dtype: str = "uint16",
437
+ dimension_separator: Literal[".", "/"] = "/",
438
+ compressors: CompressorLike = "auto",
439
+ extra_array_kwargs: Mapping[str, Any] | None = None,
418
440
  overwrite: bool = False,
441
+ # Copy from current image
442
+ copy_labels: bool = False,
443
+ copy_tables: bool = False,
444
+ # Deprecated arguments
445
+ labels: Sequence[str] | None = None,
446
+ pixel_size: PixelSize | None = None,
419
447
  ) -> "OmeZarrContainer":
420
- """Create an empty OME-Zarr container from an existing image.
448
+ """Derive a new OME-Zarr container from the current image.
449
+
450
+ If a kwarg is not provided, the value from the reference image will be used.
421
451
 
422
452
  Args:
423
453
  store (StoreOrGroup): The Zarr store or group to create the image in.
424
- ref_path (str | None): The path to the reference image in
425
- the image container.
454
+ ref_path (str | None): The path to the reference image in the image
455
+ container.
426
456
  shape (Sequence[int] | None): The shape of the new image.
427
- labels (Sequence[str] | None): The labels of the new image.
428
- pixel_size (PixelSize | None): The pixel size of the new image.
429
- axes_names (Sequence[str] | None): The axes names of the new image.
430
- chunks (Sequence[int] | None): The chunk shape of the new image.
431
- dtype (str | None): The data type of the new image.
457
+ pixelsize (float | tuple[float, float] | None): The pixel size of the new
458
+ image.
459
+ z_spacing (float | None): The z spacing of the new image.
460
+ time_spacing (float | None): The time spacing of the new image.
432
461
  name (str | None): The name of the new image.
433
- dimension_separator (DIMENSION_SEPARATOR | None): The dimension
434
- separator to use. If None, the dimension separator of the
435
- reference image will be used.
436
- compressors (CompressorLike | None): The compressors to use. If None,
437
- the compressors of the reference image will be used.
438
- copy_labels (bool): Whether to copy the labels from the reference image.
439
- copy_tables (bool): Whether to copy the tables from the reference image.
440
- ngff_version (NgffVersions): The NGFF version to use.
441
- overwrite (bool): Whether to overwrite an existing image.
462
+ channels_meta (Sequence[str | Channel] | None): The channels metadata
463
+ of the new image.
464
+ ngff_version (NgffVersions | None): The NGFF version to use.
465
+ chunks (ChunksLike): The chunk shape of the new image. Defaults to "auto".
466
+ shards (ShardsLike | None): The shard shape of the new image.
467
+ dtype (str): The data type of the new image. Defaults to "uint16".
468
+ dimension_separator (Literal[".", "/"]): The separator to use for
469
+ dimensions. Defaults to "/".
470
+ compressors (CompressorLike): The compressors to use. Defaults to "auto".
471
+ extra_array_kwargs (Mapping[str, Any] | None): Extra arguments to pass to
472
+ the zarr array creation.
473
+ overwrite (bool): Whether to overwrite an existing image. Defaults to False.
474
+ copy_labels (bool): Whether to copy the labels from the current image.
475
+ Defaults to False.
476
+ copy_tables (bool): Whether to copy the tables from the current image.
477
+ Defaults to False.
478
+ labels (Sequence[str] | None): Deprecated. This argument is deprecated,
479
+ please use channels_meta instead.
480
+ pixel_size (PixelSize | None): Deprecated. The pixel size of the new image.
481
+ This argument is deprecated, please use pixelsize, z_spacing,
482
+ and time_spacing instead.
442
483
 
443
484
  Returns:
444
- OmeZarrContainer: The new image container.
485
+ OmeZarrContainer: The new derived OME-Zarr container.
445
486
 
446
487
  """
447
488
  new_container = self._images_container.derive(
448
489
  store=store,
449
490
  ref_path=ref_path,
450
491
  shape=shape,
451
- labels=labels,
452
- pixel_size=pixel_size,
453
- axes_names=axes_names,
492
+ pixelsize=pixelsize,
493
+ z_spacing=z_spacing,
494
+ time_spacing=time_spacing,
454
495
  name=name,
496
+ channels_meta=channels_meta,
497
+ ngff_version=ngff_version,
455
498
  chunks=chunks,
499
+ shards=shards,
456
500
  dtype=dtype,
457
501
  dimension_separator=dimension_separator,
458
502
  compressors=compressors,
459
- ngff_version=ngff_version,
503
+ extra_array_kwargs=extra_array_kwargs,
460
504
  overwrite=overwrite,
505
+ labels=labels,
506
+ pixel_size=pixel_size,
461
507
  )
462
508
 
463
509
  new_ome_zarr = OmeZarrContainer(
@@ -466,19 +512,19 @@ class OmeZarrContainer:
466
512
  )
467
513
 
468
514
  if copy_labels:
469
- self.labels_container._group_handler.copy_handler(
470
- new_ome_zarr.labels_container._group_handler
515
+ self.labels_container._group_handler.copy_group(
516
+ new_ome_zarr.labels_container._group_handler.group
471
517
  )
472
518
 
473
519
  if copy_tables:
474
- self.tables_container._group_handler.copy_handler(
475
- new_ome_zarr.tables_container._group_handler
520
+ self.tables_container._group_handler.copy_group(
521
+ new_ome_zarr.tables_container._group_handler.group
476
522
  )
477
523
  return new_ome_zarr
478
524
 
479
525
  def list_tables(self, filter_types: TypedTable | str | None = None) -> list[str]:
480
526
  """List all tables in the image."""
481
- table_container = self._get_tables_container()
527
+ table_container = self._get_tables_container(create_mode=False)
482
528
  if table_container is None:
483
529
  return []
484
530
 
@@ -621,7 +667,7 @@ class OmeZarrContainer:
621
667
 
622
668
  def list_labels(self) -> list[str]:
623
669
  """List all labels in the image."""
624
- label_container = self._get_labels_container()
670
+ label_container = self._get_labels_container(create_mode=False)
625
671
  if label_container is None:
626
672
  return []
627
673
  return label_container.list()
@@ -688,38 +734,62 @@ class OmeZarrContainer:
688
734
  self,
689
735
  name: str,
690
736
  ref_image: Image | Label | None = None,
737
+ # Metadata parameters
691
738
  shape: Sequence[int] | None = None,
692
- pixel_size: PixelSize | None = None,
693
- axes_names: Sequence[str] | None = None,
694
- chunks: Sequence[int] | None = None,
695
- dtype: str = "uint32",
739
+ pixelsize: float | tuple[float, float] | None = None,
740
+ z_spacing: float | None = None,
741
+ time_spacing: float | None = None,
742
+ channels_policy: Literal["same", "squeeze"] | int = "squeeze",
743
+ ngff_version: NgffVersions | None = None,
744
+ # Zarr Array parameters
745
+ chunks: ChunksLike = "auto",
746
+ shards: ShardsLike | None = None,
747
+ dtype: str | None = None,
696
748
  dimension_separator: Literal[".", "/"] | None = None,
697
749
  compressors: CompressorLike | None = None,
750
+ extra_array_kwargs: Mapping[str, Any] | None = None,
698
751
  overwrite: bool = False,
752
+ # Deprecated arguments
753
+ labels: Sequence[str] | None = None,
754
+ pixel_size: PixelSize | None = None,
699
755
  ) -> "Label":
700
- """Create an empty OME-Zarr label from a reference image.
756
+ """Derive a new label from an existing image or label.
701
757
 
702
- And add the label to the /labels group.
758
+ If a kwarg is not provided, the value from the reference image will be used.
703
759
 
704
760
  Args:
705
- name (str): The name of the new image.
706
- ref_image (Image | Label | None): A reference image that will be used
707
- to create the new image.
708
- shape (Sequence[int] | None): The shape of the new image.
709
- pixel_size (PixelSize | None): The pixel size of the new image.
710
- axes_names (Sequence[str] | None): The axes names of the new image.
711
- For labels, the channel axis is not allowed.
712
- chunks (Sequence[int] | None): The chunk shape of the new image.
713
- dtype (str): The data type of the new label.
714
- dimension_separator (DIMENSION_SEPARATOR | None): The dimension
715
- separator to use. If None, the dimension separator of the
716
- reference image will be used.
717
- compressors (CompressorLike | None): The compressors to use. If None,
718
- the compressors of the reference image will be used.
719
- overwrite (bool): Whether to overwrite an existing image.
761
+ name (str): The name of the new label.
762
+ ref_image (Image | Label | None): The reference image to derive the new
763
+ label from. If None, the first level image will be used.
764
+ shape (Sequence[int] | None): The shape of the new label.
765
+ pixelsize (float | tuple[float, float] | None): The pixel size of the new
766
+ label.
767
+ z_spacing (float | None): The z spacing of the new label.
768
+ time_spacing (float | None): The time spacing of the new label.
769
+ channels_policy (Literal["same", "squeeze"] | int): Possible policies:
770
+ - If "squeeze", the channels axis will be removed (no matter its size).
771
+ - If "same", the channels axis will be kept as is (if it exists).
772
+ - If an integer is provided, the channels axis will be changed to have
773
+ that size.
774
+ Defaults to "squeeze".
775
+ ngff_version (NgffVersions | None): The NGFF version to use.
776
+ chunks (ChunksLike): The chunk shape of the new label. Defaults to "auto".
777
+ shards (ShardsLike | None): The shard shape of the new label.
778
+ dtype (str | None): The data type of the new label.
779
+ dimension_separator (Literal[".", "/"] | None): The separator to use for
780
+ dimensions.
781
+ compressors (CompressorLike | None): The compressors to use.
782
+ extra_array_kwargs (Mapping[str, Any] | None): Extra arguments to pass to
783
+ the zarr array creation.
784
+ overwrite (bool): Whether to overwrite an existing label. Defaults to False.
785
+ labels (Sequence[str] | None): Deprecated. This argument is deprecated,
786
+ please use channels_meta instead.
787
+ pixel_size (PixelSize | None): Deprecated. The pixel size of the new label.
788
+ This argument is deprecated, please use pixelsize, z_spacing,
789
+ and time_spacing instead.
720
790
 
721
791
  Returns:
722
- Label: The new label.
792
+ Label: The new derived label.
723
793
 
724
794
  """
725
795
  if ref_image is None:
@@ -728,13 +798,20 @@ class OmeZarrContainer:
728
798
  name=name,
729
799
  ref_image=ref_image,
730
800
  shape=shape,
731
- pixel_size=pixel_size,
732
- axes_names=axes_names,
801
+ pixelsize=pixelsize,
802
+ z_spacing=z_spacing,
803
+ time_spacing=time_spacing,
804
+ channels_policy=channels_policy,
805
+ ngff_version=ngff_version,
733
806
  chunks=chunks,
807
+ shards=shards,
734
808
  dtype=dtype,
735
809
  dimension_separator=dimension_separator,
736
810
  compressors=compressors,
811
+ extra_array_kwargs=extra_array_kwargs,
737
812
  overwrite=overwrite,
813
+ labels=labels,
814
+ pixel_size=pixel_size,
738
815
  )
739
816
 
740
817
 
@@ -808,10 +885,12 @@ def open_label(
808
885
  """
809
886
  group_handler = ZarrGroupHandler(store=store, cache=cache, mode=mode)
810
887
  if name is None:
811
- label_meta_handler = find_label_meta_handler(group_handler)
812
- path = label_meta_handler.meta.get_dataset(
813
- path=path, pixel_size=pixel_size, strict=strict
814
- ).path
888
+ label_meta_handler = LabelMetaHandler(group_handler)
889
+ path = (
890
+ label_meta_handler.get_meta()
891
+ .get_dataset(path=path, pixel_size=pixel_size, strict=strict)
892
+ .path
893
+ )
815
894
  return Label(group_handler, path, label_meta_handler)
816
895
 
817
896
  labels_container = LabelsContainer(group_handler)
@@ -826,196 +905,282 @@ def open_label(
826
905
  def create_empty_ome_zarr(
827
906
  store: StoreOrGroup,
828
907
  shape: Sequence[int],
829
- xy_pixelsize: float,
908
+ pixelsize: float | tuple[float, float] | None = None,
830
909
  z_spacing: float = 1.0,
831
910
  time_spacing: float = 1.0,
911
+ scaling_factors: Sequence[float] | Literal["auto"] = "auto",
832
912
  levels: int | list[str] = 5,
833
- xy_scaling_factor: float = 2,
834
- z_scaling_factor: float = 1.0,
835
913
  space_unit: SpaceUnits = DefaultSpaceUnit,
836
914
  time_unit: TimeUnits = DefaultTimeUnit,
837
915
  axes_names: Sequence[str] | None = None,
916
+ channels_meta: Sequence[str | Channel] | None = None,
838
917
  name: str | None = None,
839
- chunks: Sequence[int] | Literal["auto"] = "auto",
918
+ ngff_version: NgffVersions = DefaultNgffVersion,
919
+ chunks: ChunksLike = "auto",
920
+ shards: ShardsLike | None = None,
840
921
  dtype: str = "uint16",
841
922
  dimension_separator: Literal[".", "/"] = "/",
842
923
  compressors: CompressorLike = "auto",
924
+ extra_array_kwargs: Mapping[str, Any] | None = None,
925
+ overwrite: bool = False,
926
+ # Deprecated arguments
927
+ xy_pixelsize: float | None = None,
928
+ xy_scaling_factor: float | None = None,
929
+ z_scaling_factor: float | None = None,
843
930
  channel_labels: list[str] | None = None,
844
931
  channel_wavelengths: list[str] | None = None,
845
932
  channel_colors: Sequence[str] | None = None,
846
933
  channel_active: Sequence[bool] | None = None,
847
- overwrite: bool = False,
848
- version: NgffVersions = DefaultNgffVersion,
849
934
  ) -> OmeZarrContainer:
850
935
  """Create an empty OME-Zarr image with the given shape and metadata.
851
936
 
852
937
  Args:
853
938
  store (StoreOrGroup): The Zarr store or group to create the image in.
854
939
  shape (Sequence[int]): The shape of the image.
855
- xy_pixelsize (float): The pixel size in x and y dimensions.
856
- z_spacing (float, optional): The spacing between z slices. Defaults to 1.0.
857
- time_spacing (float, optional): The spacing between time points.
858
- Defaults to 1.0.
859
- levels (int | list[str], optional): The number of levels in the pyramid or a
860
- list of level names. Defaults to 5.
861
- xy_scaling_factor (float, optional): The down-scaling factor in x and y
862
- dimensions. Defaults to 2.0.
863
- z_scaling_factor (float, optional): The down-scaling factor in z dimension.
864
- Defaults to 1.0.
865
- space_unit (SpaceUnits, optional): The unit of space. Defaults to
866
- DefaultSpaceUnit.
867
- time_unit (TimeUnits, optional): The unit of time. Defaults to
868
- DefaultTimeUnit.
869
- axes_names (Sequence[str] | None, optional): The names of the axes.
870
- If None the canonical names are used. Defaults to None.
871
- name (str | None, optional): The name of the image. Defaults to None.
872
- chunks (Sequence[int] | None, optional): The chunk shape. If None the shape
873
- is used. Defaults to None.
874
- dtype (str, optional): The data type of the image. Defaults to "uint16".
875
- dimension_separator (DIMENSION_SEPARATOR): The dimension
876
- separator to use. Defaults to "/".
877
- compressors (CompressorLike): The compressor to use. Defaults to "auto".
878
- channel_labels (list[str] | None, optional): The labels of the channels.
879
- Defaults to None.
880
- channel_wavelengths (list[str] | None, optional): The wavelengths of the
881
- channels. Defaults to None.
882
- channel_colors (Sequence[str] | None, optional): The colors of the channels.
940
+ pixelsize (float | tuple[float, float] | None): The pixel size in x and y
941
+ dimensions.
942
+ z_spacing (float): The spacing between z slices. Defaults to 1.0.
943
+ time_spacing (float): The spacing between time points. Defaults to 1.0.
944
+ scaling_factors (Sequence[float] | Literal["auto"]): The down-scaling factors
945
+ for the pyramid levels. Defaults to "auto".
946
+ levels (int | list[str]): The number of levels in the pyramid or a list of
947
+ level names. Defaults to 5.
948
+ space_unit (SpaceUnits): The unit of space. Defaults to DefaultSpaceUnit.
949
+ time_unit (TimeUnits): The unit of time. Defaults to DefaultTimeUnit.
950
+ axes_names (Sequence[str] | None): The names of the axes. If None the
951
+ canonical names are used. Defaults to None.
952
+ channels_meta (Sequence[str | Channel] | None): The channels metadata.
883
953
  Defaults to None.
884
- channel_active (Sequence[bool] | None, optional): Whether the channels are
885
- active. Defaults to None.
886
- overwrite (bool, optional): Whether to overwrite an existing image.
887
- Defaults to True.
888
- version (NgffVersion, optional): The version of the OME-Zarr specification.
954
+ name (str | None): The name of the image. Defaults to None.
955
+ ngff_version (NgffVersions): The version of the OME-Zarr specification.
889
956
  Defaults to DefaultNgffVersion.
957
+ chunks (ChunksLike): The chunk shape. Defaults to "auto".
958
+ shards (ShardsLike | None): The shard shape. Defaults to None.
959
+ dtype (str): The data type of the image. Defaults to "uint16".
960
+ dimension_separator (Literal[".", "/"]): The dimension separator to use.
961
+ Defaults to "/".
962
+ compressors (CompressorLike): The compressor to use. Defaults to "auto".
963
+ extra_array_kwargs (Mapping[str, Any] | None): Extra arguments to pass to
964
+ the zarr array creation. Defaults to None.
965
+ overwrite (bool): Whether to overwrite an existing image. Defaults to False.
966
+ xy_pixelsize (float | None): Deprecated. Use pixelsize instead.
967
+ xy_scaling_factor (float | None): Deprecated. Use scaling_factors instead.
968
+ z_scaling_factor (float | None): Deprecated. Use scaling_factors instead.
969
+ channel_labels (list[str] | None): Deprecated. Use channels_meta instead.
970
+ channel_wavelengths (list[str] | None): Deprecated. Use channels_meta instead.
971
+ channel_colors (Sequence[str] | None): Deprecated. Use channels_meta instead.
972
+ channel_active (Sequence[bool] | None): Deprecated. Use channels_meta instead.
890
973
  """
891
- handler = create_empty_image_container(
974
+ if xy_pixelsize is not None:
975
+ warnings.warn(
976
+ "'xy_pixelsize' is deprecated and will be removed in a future "
977
+ "version. Please use 'pixelsize' instead.",
978
+ DeprecationWarning,
979
+ stacklevel=2,
980
+ )
981
+ pixelsize = xy_pixelsize
982
+ if xy_scaling_factor is not None or z_scaling_factor is not None:
983
+ warnings.warn(
984
+ "'xy_scaling_factor' and 'z_scaling_factor' are deprecated and will be "
985
+ "removed in a future version. Please use 'scaling_factors' instead.",
986
+ DeprecationWarning,
987
+ stacklevel=2,
988
+ )
989
+ xy_scaling_factor_ = xy_scaling_factor or 2.0
990
+ z_scaling_factor_ = z_scaling_factor or 1.0
991
+ if len(shape) == 2:
992
+ scaling_factors = (xy_scaling_factor_, xy_scaling_factor_)
993
+ else:
994
+ zyx_factors = (z_scaling_factor_, xy_scaling_factor_, xy_scaling_factor_)
995
+ scaling_factors = (1.0,) * (len(shape) - 3) + zyx_factors
996
+
997
+ if channel_labels is not None:
998
+ warnings.warn(
999
+ "'channel_labels' is deprecated and will be removed in a future "
1000
+ "version. Please use 'channels_meta' instead.",
1001
+ DeprecationWarning,
1002
+ stacklevel=2,
1003
+ )
1004
+ channels_meta = channel_labels
1005
+
1006
+ if channel_wavelengths is not None:
1007
+ warnings.warn(
1008
+ "'channel_wavelengths' is deprecated and will be removed in a future "
1009
+ "version. Please use 'channels_meta' instead.",
1010
+ DeprecationWarning,
1011
+ stacklevel=2,
1012
+ )
1013
+ if channel_colors is not None:
1014
+ warnings.warn(
1015
+ "'channel_colors' is deprecated and will be removed in a future "
1016
+ "version. Please use 'channels_meta' instead.",
1017
+ DeprecationWarning,
1018
+ stacklevel=2,
1019
+ )
1020
+ if channel_active is not None:
1021
+ warnings.warn(
1022
+ "'channel_active' is deprecated and will be removed in a future "
1023
+ "version. Please use 'channels_meta' instead.",
1024
+ DeprecationWarning,
1025
+ stacklevel=2,
1026
+ )
1027
+
1028
+ if pixelsize is None:
1029
+ raise NgioValueError("pixelsize must be provided.")
1030
+
1031
+ handler = init_image_like(
892
1032
  store=store,
1033
+ meta_type=NgioImageMeta,
893
1034
  shape=shape,
894
- pixelsize=xy_pixelsize,
1035
+ pixelsize=pixelsize,
895
1036
  z_spacing=z_spacing,
896
1037
  time_spacing=time_spacing,
1038
+ scaling_factors=scaling_factors,
897
1039
  levels=levels,
898
- yx_scaling_factor=xy_scaling_factor,
899
- z_scaling_factor=z_scaling_factor,
900
1040
  space_unit=space_unit,
901
1041
  time_unit=time_unit,
902
1042
  axes_names=axes_names,
1043
+ channels_meta=channels_meta,
903
1044
  name=name,
1045
+ ngff_version=ngff_version,
904
1046
  chunks=chunks,
1047
+ shards=shards,
905
1048
  dtype=dtype,
906
1049
  dimension_separator=dimension_separator,
907
1050
  compressors=compressors,
1051
+ extra_array_kwargs=extra_array_kwargs,
908
1052
  overwrite=overwrite,
909
- version=version,
910
1053
  )
911
1054
 
912
1055
  ome_zarr = OmeZarrContainer(group_handler=handler)
913
- ome_zarr.set_channel_meta(
914
- labels=channel_labels,
915
- wavelength_id=channel_wavelengths,
916
- percentiles=None,
917
- colors=channel_colors,
918
- active=channel_active,
919
- )
1056
+ if (
1057
+ channel_wavelengths is not None
1058
+ or channel_colors is not None
1059
+ or channel_active is not None
1060
+ ):
1061
+ channel_names = ome_zarr.channel_labels
1062
+ ome_zarr.set_channel_meta(
1063
+ labels=channel_names,
1064
+ wavelength_id=channel_wavelengths,
1065
+ percentiles=None,
1066
+ colors=channel_colors,
1067
+ active=channel_active,
1068
+ )
1069
+ else:
1070
+ ome_zarr.set_channel_meta(
1071
+ labels=ome_zarr.channel_labels,
1072
+ percentiles=None,
1073
+ )
920
1074
  return ome_zarr
921
1075
 
922
1076
 
923
1077
  def create_ome_zarr_from_array(
924
1078
  store: StoreOrGroup,
925
1079
  array: np.ndarray,
926
- xy_pixelsize: float,
1080
+ pixelsize: float | tuple[float, float] | None = None,
927
1081
  z_spacing: float = 1.0,
928
1082
  time_spacing: float = 1.0,
1083
+ scaling_factors: Sequence[float] | Literal["auto"] = "auto",
929
1084
  levels: int | list[str] = 5,
930
- xy_scaling_factor: float = 2.0,
931
- z_scaling_factor: float = 1.0,
932
1085
  space_unit: SpaceUnits = DefaultSpaceUnit,
933
1086
  time_unit: TimeUnits = DefaultTimeUnit,
934
1087
  axes_names: Sequence[str] | None = None,
935
- channel_labels: list[str] | None = None,
936
- channel_wavelengths: list[str] | None = None,
937
- percentiles: tuple[float, float] | None = (0.1, 99.9),
938
- channel_colors: Sequence[str] | None = None,
939
- channel_active: Sequence[bool] | None = None,
1088
+ channels_meta: Sequence[str | Channel] | None = None,
1089
+ percentiles: tuple[float, float] = (0.1, 99.9),
940
1090
  name: str | None = None,
941
- chunks: Sequence[int] | Literal["auto"] = "auto",
1091
+ ngff_version: NgffVersions = DefaultNgffVersion,
1092
+ chunks: ChunksLike = "auto",
1093
+ shards: ShardsLike | None = None,
942
1094
  dimension_separator: Literal[".", "/"] = "/",
943
1095
  compressors: CompressorLike = "auto",
1096
+ extra_array_kwargs: Mapping[str, Any] | None = None,
944
1097
  overwrite: bool = False,
945
- version: NgffVersions = DefaultNgffVersion,
1098
+ # Deprecated arguments
1099
+ xy_pixelsize: float | None = None,
1100
+ xy_scaling_factor: float | None = None,
1101
+ z_scaling_factor: float | None = None,
1102
+ channel_labels: list[str] | None = None,
1103
+ channel_wavelengths: list[str] | None = None,
1104
+ channel_colors: Sequence[str] | None = None,
1105
+ channel_active: Sequence[bool] | None = None,
946
1106
  ) -> OmeZarrContainer:
947
1107
  """Create an OME-Zarr image from a numpy array.
948
1108
 
949
1109
  Args:
950
1110
  store (StoreOrGroup): The Zarr store or group to create the image in.
951
1111
  array (np.ndarray): The image data.
952
- xy_pixelsize (float): The pixel size in x and y dimensions.
953
- z_spacing (float, optional): The spacing between z slices. Defaults to 1.0.
954
- time_spacing (float, optional): The spacing between time points.
955
- Defaults to 1.0.
956
- levels (int | list[str], optional): The number of levels in the pyramid or a
957
- list of level names. Defaults to 5.
958
- xy_scaling_factor (float, optional): The down-scaling factor in x and y
959
- dimensions. Defaults to 2.0.
960
- z_scaling_factor (float, optional): The down-scaling factor in z dimension.
961
- Defaults to 1.0.
962
- space_unit (SpaceUnits, optional): The unit of space. Defaults to
963
- DefaultSpaceUnit.
964
- time_unit (TimeUnits, optional): The unit of time. Defaults to
965
- DefaultTimeUnit.
966
- axes_names (Sequence[str] | None, optional): The names of the axes.
967
- If None the canonical names are used. Defaults to None.
968
- name (str | None, optional): The name of the image. Defaults to None.
969
- chunks (Sequence[int] | None, optional): The chunk shape. If None the shape
970
- is used. Defaults to None.
971
- channel_labels (list[str] | None, optional): The labels of the channels.
1112
+ pixelsize (float | tuple[float, float] | None): The pixel size in x and y
1113
+ dimensions.
1114
+ z_spacing (float): The spacing between z slices. Defaults to 1.0.
1115
+ time_spacing (float): The spacing between time points. Defaults to 1.0.
1116
+ scaling_factors (Sequence[float] | Literal["auto"]): The down-scaling factors
1117
+ for the pyramid levels. Defaults to "auto".
1118
+ levels (int | list[str]): The number of levels in the pyramid or a list of
1119
+ level names. Defaults to 5.
1120
+ space_unit (SpaceUnits): The unit of space. Defaults to DefaultSpaceUnit.
1121
+ time_unit (TimeUnits): The unit of time. Defaults to DefaultTimeUnit.
1122
+ axes_names (Sequence[str] | None): The names of the axes. If None the
1123
+ canonical names are used. Defaults to None.
1124
+ channels_meta (Sequence[str | Channel] | None): The channels metadata.
972
1125
  Defaults to None.
973
- channel_wavelengths (list[str] | None, optional): The wavelengths of the
974
- channels. Defaults to None.
975
- percentiles (tuple[float, float] | None, optional): The percentiles of the
976
- channels. Defaults to None.
977
- channel_colors (Sequence[str] | None, optional): The colors of the channels.
978
- Defaults to None.
979
- channel_active (Sequence[bool] | None, optional): Whether the channels are
980
- active. Defaults to None.
981
- dimension_separator (DIMENSION_SEPARATOR): The separator to use for
1126
+ percentiles (tuple[float, float]): The percentiles of the channels for
1127
+ computing display ranges. Defaults to (0.1, 99.9).
1128
+ name (str | None): The name of the image. Defaults to None.
1129
+ ngff_version (NgffVersions): The version of the OME-Zarr specification.
1130
+ Defaults to DefaultNgffVersion.
1131
+ chunks (ChunksLike): The chunk shape. Defaults to "auto".
1132
+ shards (ShardsLike | None): The shard shape. Defaults to None.
1133
+ dimension_separator (Literal[".", "/"]): The separator to use for
982
1134
  dimensions. Defaults to "/".
983
1135
  compressors (CompressorLike): The compressors to use. Defaults to "auto".
984
- overwrite (bool, optional): Whether to overwrite an existing image.
985
- Defaults to True.
986
- version (str, optional): The version of the OME-Zarr specification.
987
- Defaults to DefaultNgffVersion.
1136
+ extra_array_kwargs (Mapping[str, Any] | None): Extra arguments to pass to
1137
+ the zarr array creation. Defaults to None.
1138
+ overwrite (bool): Whether to overwrite an existing image. Defaults to False.
1139
+ xy_pixelsize (float | None): Deprecated. Use pixelsize instead.
1140
+ xy_scaling_factor (float | None): Deprecated. Use scaling_factors instead.
1141
+ z_scaling_factor (float | None): Deprecated. Use scaling_factors instead.
1142
+ channel_labels (list[str] | None): Deprecated. Use channels_meta instead.
1143
+ channel_wavelengths (list[str] | None): Deprecated. Use channels_meta instead.
1144
+ channel_colors (Sequence[str] | None): Deprecated. Use channels_meta instead.
1145
+ channel_active (Sequence[bool] | None): Deprecated. Use channels_meta instead.
988
1146
  """
989
- handler = create_empty_image_container(
1147
+ ome_zarr = create_empty_ome_zarr(
990
1148
  store=store,
991
1149
  shape=array.shape,
992
- pixelsize=xy_pixelsize,
1150
+ pixelsize=pixelsize,
993
1151
  z_spacing=z_spacing,
994
1152
  time_spacing=time_spacing,
1153
+ scaling_factors=scaling_factors,
995
1154
  levels=levels,
996
- yx_scaling_factor=xy_scaling_factor,
997
- z_scaling_factor=z_scaling_factor,
998
1155
  space_unit=space_unit,
999
1156
  time_unit=time_unit,
1000
1157
  axes_names=axes_names,
1158
+ channels_meta=channels_meta,
1001
1159
  name=name,
1160
+ ngff_version=ngff_version,
1002
1161
  chunks=chunks,
1003
- dtype=str(array.dtype),
1004
- overwrite=overwrite,
1162
+ shards=shards,
1005
1163
  dimension_separator=dimension_separator,
1006
1164
  compressors=compressors,
1007
- version=version,
1165
+ extra_array_kwargs=extra_array_kwargs,
1166
+ overwrite=overwrite,
1167
+ xy_pixelsize=xy_pixelsize,
1168
+ xy_scaling_factor=xy_scaling_factor,
1169
+ z_scaling_factor=z_scaling_factor,
1170
+ channel_labels=channel_labels,
1171
+ channel_wavelengths=channel_wavelengths,
1172
+ channel_colors=channel_colors,
1173
+ channel_active=channel_active,
1008
1174
  )
1009
-
1010
- ome_zarr = OmeZarrContainer(group_handler=handler)
1011
1175
  image = ome_zarr.get_image()
1012
1176
  image.set_array(array)
1013
1177
  image.consolidate()
1014
- ome_zarr.set_channel_meta(
1015
- labels=channel_labels,
1016
- wavelength_id=channel_wavelengths,
1017
- percentiles=percentiles,
1018
- colors=channel_colors,
1019
- active=channel_active,
1178
+ if len(percentiles) != 2:
1179
+ raise NgioValueError(
1180
+ f"'percentiles' must be a tuple of two values. Got {percentiles}"
1181
+ )
1182
+ ome_zarr.set_channel_percentiles(
1183
+ start_percentile=percentiles[0],
1184
+ end_percentile=percentiles[1],
1020
1185
  )
1021
1186
  return ome_zarr