ngio 0.1.6__py3-none-any.whl → 0.2.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 (84) hide show
  1. ngio/__init__.py +31 -5
  2. ngio/common/__init__.py +44 -0
  3. ngio/common/_array_pipe.py +160 -0
  4. ngio/common/_axes_transforms.py +63 -0
  5. ngio/common/_common_types.py +5 -0
  6. ngio/common/_dimensions.py +113 -0
  7. ngio/common/_pyramid.py +223 -0
  8. ngio/{core/roi.py → common/_roi.py} +22 -23
  9. ngio/common/_slicer.py +97 -0
  10. ngio/{pipes/_zoom_utils.py → common/_zoom.py} +2 -78
  11. ngio/hcs/__init__.py +60 -0
  12. ngio/images/__init__.py +23 -0
  13. ngio/images/abstract_image.py +240 -0
  14. ngio/images/create.py +251 -0
  15. ngio/images/image.py +389 -0
  16. ngio/images/label.py +236 -0
  17. ngio/images/omezarr_container.py +535 -0
  18. ngio/ome_zarr_meta/__init__.py +35 -0
  19. ngio/ome_zarr_meta/_generic_handlers.py +320 -0
  20. ngio/ome_zarr_meta/_meta_handlers.py +142 -0
  21. ngio/ome_zarr_meta/ngio_specs/__init__.py +63 -0
  22. ngio/ome_zarr_meta/ngio_specs/_axes.py +481 -0
  23. ngio/ome_zarr_meta/ngio_specs/_channels.py +378 -0
  24. ngio/ome_zarr_meta/ngio_specs/_dataset.py +134 -0
  25. ngio/ome_zarr_meta/ngio_specs/_ngio_hcs.py +5 -0
  26. ngio/ome_zarr_meta/ngio_specs/_ngio_image.py +434 -0
  27. ngio/ome_zarr_meta/ngio_specs/_pixel_size.py +84 -0
  28. ngio/ome_zarr_meta/v04/__init__.py +11 -0
  29. ngio/ome_zarr_meta/v04/_meta_handlers.py +54 -0
  30. ngio/ome_zarr_meta/v04/_v04_spec_utils.py +412 -0
  31. ngio/tables/__init__.py +21 -5
  32. ngio/tables/_validators.py +192 -0
  33. ngio/tables/backends/__init__.py +8 -0
  34. ngio/tables/backends/_abstract_backend.py +71 -0
  35. ngio/tables/backends/_anndata_utils.py +194 -0
  36. ngio/tables/backends/_anndata_v1.py +75 -0
  37. ngio/tables/backends/_json_v1.py +56 -0
  38. ngio/tables/backends/_table_backends.py +102 -0
  39. ngio/tables/tables_container.py +300 -0
  40. ngio/tables/v1/__init__.py +6 -5
  41. ngio/tables/v1/_feature_table.py +161 -0
  42. ngio/tables/v1/_generic_table.py +99 -182
  43. ngio/tables/v1/_masking_roi_table.py +175 -0
  44. ngio/tables/v1/_roi_table.py +226 -0
  45. ngio/utils/__init__.py +23 -10
  46. ngio/utils/_datasets.py +51 -0
  47. ngio/utils/_errors.py +10 -4
  48. ngio/utils/_zarr_utils.py +378 -0
  49. {ngio-0.1.6.dist-info → ngio-0.2.0a2.dist-info}/METADATA +18 -39
  50. ngio-0.2.0a2.dist-info/RECORD +53 -0
  51. ngio/core/__init__.py +0 -7
  52. ngio/core/dimensions.py +0 -122
  53. ngio/core/image_handler.py +0 -228
  54. ngio/core/image_like_handler.py +0 -549
  55. ngio/core/label_handler.py +0 -410
  56. ngio/core/ngff_image.py +0 -387
  57. ngio/core/utils.py +0 -287
  58. ngio/io/__init__.py +0 -19
  59. ngio/io/_zarr.py +0 -88
  60. ngio/io/_zarr_array_utils.py +0 -0
  61. ngio/io/_zarr_group_utils.py +0 -60
  62. ngio/iterators/__init__.py +0 -1
  63. ngio/ngff_meta/__init__.py +0 -27
  64. ngio/ngff_meta/fractal_image_meta.py +0 -1267
  65. ngio/ngff_meta/meta_handler.py +0 -92
  66. ngio/ngff_meta/utils.py +0 -235
  67. ngio/ngff_meta/v04/__init__.py +0 -6
  68. ngio/ngff_meta/v04/specs.py +0 -158
  69. ngio/ngff_meta/v04/zarr_utils.py +0 -376
  70. ngio/pipes/__init__.py +0 -7
  71. ngio/pipes/_slicer_transforms.py +0 -176
  72. ngio/pipes/_transforms.py +0 -33
  73. ngio/pipes/data_pipe.py +0 -52
  74. ngio/tables/_ad_reader.py +0 -80
  75. ngio/tables/_utils.py +0 -301
  76. ngio/tables/tables_group.py +0 -252
  77. ngio/tables/v1/feature_tables.py +0 -182
  78. ngio/tables/v1/masking_roi_tables.py +0 -243
  79. ngio/tables/v1/roi_tables.py +0 -285
  80. ngio/utils/_common_types.py +0 -5
  81. ngio/utils/_pydantic_utils.py +0 -52
  82. ngio-0.1.6.dist-info/RECORD +0 -44
  83. {ngio-0.1.6.dist-info → ngio-0.2.0a2.dist-info}/WHEEL +0 -0
  84. {ngio-0.1.6.dist-info → ngio-0.2.0a2.dist-info}/licenses/LICENSE +0 -0
@@ -1,182 +0,0 @@
1
- """Implementation of the FeatureTableV1 class.
2
-
3
- This class follows the feature_table specification at:
4
- https://fractal-analytics-platform.github.io/fractal-tasks-core/tables/
5
- """
6
-
7
- from typing import Literal
8
-
9
- import pandas as pd
10
- import zarr
11
- from pydantic import BaseModel
12
-
13
- from ngio.tables.v1._generic_table import BaseTable, write_table_ad
14
-
15
-
16
- class FeatureTableV1Meta(BaseModel):
17
- """Metadata for the ROI table."""
18
-
19
- region: dict[Literal["path"], str] | None = None
20
- instance_key: str = "label"
21
- fractal_table_version: Literal["1"] = "1"
22
- type: Literal["feature_table"] = "feature_table"
23
-
24
-
25
- class FeatureTableV1:
26
- """Class to handle fractal Feature tables.
27
-
28
- To know more about the Feature table format, please refer to the
29
- https://fractal-analytics-platform.github.io/fractal-tasks-core/tables/
30
- """
31
-
32
- def __init__(
33
- self,
34
- group: zarr.Group,
35
- name: str,
36
- validate_metadata: bool = True,
37
- validate_table: bool = True,
38
- index_key: str | None = None,
39
- ):
40
- """Initialize the class from an existing group.
41
-
42
- Args:
43
- group (zarr.Group): The group containing the
44
- ROI table.
45
- name (str): The name of the ROI table.
46
- validate_metadata (bool): If True, the metadata is validated.
47
- validate_table (bool): If True, the table is validated.
48
- index_key (str): The column name to use as the index of the DataFrame.
49
- """
50
- if validate_metadata:
51
- self._meta = FeatureTableV1Meta(**group.attrs)
52
- else:
53
- self._meta = FeatureTableV1Meta.model_construct(**group.attrs)
54
-
55
- # Validate the table is not implemented for the Feature table
56
- validators = None
57
- validators = validators if validate_table else None
58
-
59
- index_key = index_key if index_key is not None else self._meta.instance_key
60
- self._table_handler = BaseTable(
61
- group=group,
62
- index_key=index_key,
63
- index_type="int",
64
- validators=validators,
65
- )
66
-
67
- self._name = name
68
-
69
- def __repr__(self) -> str:
70
- """Return the string representation of the class."""
71
- name = "FeatureTableV1("
72
- len_name = len(name)
73
- return (
74
- f"{name}"
75
- f"group_path={self.group_path}, \n"
76
- f"{' ':>{len_name}}name={self.name},\n"
77
- f"{' ':>{len_name}}features={self.table.columns.to_list()}, \n"
78
- f"{' ':>{len_name}}source_label={self.source_label()}, \n"
79
- f"{' ':>{len_name}}num_labels={len(self.table.index)}), \n"
80
- ")"
81
- )
82
-
83
- @classmethod
84
- def _new(
85
- cls,
86
- parent_group: zarr.Group,
87
- name: str,
88
- label_image: str | None = None,
89
- instance_key: str = "label",
90
- overwrite: bool = False,
91
- ) -> None:
92
- """Create a new Feature table.
93
-
94
- Args:
95
- parent_group (zarr.Group): The parent group where the ROI table
96
- will be created.
97
- name (str): The name of the ROI table.
98
- label_image (str | None): The relative path to the label image used to
99
- create the feature table.
100
- instance_key (str): The column name to use as the index of the DataFrame.
101
- Default is 'label'.
102
- overwrite (bool): If True, the table will be overwritten if it exists.
103
- Default is False.
104
- """
105
- group = parent_group.create_group(name, overwrite=overwrite)
106
- table = pd.DataFrame(index=pd.Index([], name=instance_key, dtype="int"))
107
-
108
- label_image = label_image if label_image is not None else "None"
109
- meta = FeatureTableV1Meta(
110
- region={"path": label_image}, instance_key=instance_key
111
- )
112
- write_table_ad(
113
- group=group,
114
- table=table,
115
- index_key=instance_key,
116
- index_type="int",
117
- meta=meta,
118
- )
119
- return cls(group=group, name=name)
120
-
121
- @property
122
- def name(self) -> str:
123
- """Return the name of the feature table."""
124
- return self._name
125
-
126
- @property
127
- def root_path(self) -> str:
128
- """Return the path of the root group."""
129
- return self._table_handler.root_path
130
-
131
- @property
132
- def group_path(self) -> str:
133
- """Return the path of the group."""
134
- return self._table_handler.group_path
135
-
136
- @property
137
- def meta(self) -> FeatureTableV1Meta:
138
- """Return the metadata of the feature table."""
139
- return self._meta
140
-
141
- @property
142
- def table_handler(self) -> BaseTable:
143
- """Return the table handler."""
144
- return self._table_handler
145
-
146
- @property
147
- def table(self) -> pd.DataFrame:
148
- """Return the feature table as a DataFrame."""
149
- return self._table_handler.table
150
-
151
- @table.setter
152
- def table(self, table: pd.DataFrame) -> None:
153
- """Set the feature table."""
154
- raise NotImplementedError(
155
- "Setting the table is not implemented. Please use the 'set_table' method."
156
- )
157
-
158
- def set_table(self, table: pd.DataFrame) -> None:
159
- """Set the feature table."""
160
- self._table_handler.set_table(table)
161
-
162
- def source_label(self, get_full_path: bool = False) -> str | None:
163
- """Return the name of the label image.
164
-
165
- The name is assumed to be after the last '/' in the path.
166
- Since this might fails, get_full_path is True, the full path is returned.
167
-
168
- Args:
169
- get_full_path (bool): If True, the full path is returned.
170
- """
171
- region = self.meta.region
172
- if region is None:
173
- return None
174
- path = region["path"]
175
-
176
- if get_full_path:
177
- return path
178
- return path.split("/")[-1]
179
-
180
- def consolidate(self) -> None:
181
- """Write the table to the group."""
182
- self._table_handler.consolidate(meta=self.meta)
@@ -1,243 +0,0 @@
1
- """Implementation of the Masking ROI Table class.
2
-
3
- This class follows the roi_table specification at:
4
- https://fractal-analytics-platform.github.io/fractal-tasks-core/tables/
5
- """
6
-
7
- from functools import partial
8
- from typing import Literal
9
-
10
- import pandas as pd
11
- import zarr
12
- from pydantic import BaseModel
13
-
14
- from ngio.core.label_handler import Label
15
- from ngio.core.roi import SpaceUnits, WorldCooROI
16
- from ngio.tables._utils import validate_columns
17
- from ngio.tables.v1._generic_table import REQUIRED_COLUMNS, BaseTable, write_table_ad
18
-
19
-
20
- class MaskingROITableV1Meta(BaseModel):
21
- """Metadata for the ROI table."""
22
-
23
- fractal_table_version: Literal["1"] = "1"
24
- type: Literal["masking_roi_table"] = "masking_roi_table"
25
- region: dict[Literal["path"], str]
26
- instance_key: str = "label"
27
-
28
-
29
- def create_empty_roi_table() -> pd.DataFrame:
30
- """Create an empty ROI table."""
31
- table = pd.DataFrame(
32
- index=pd.Index([], name="label", dtype="str"), columns=REQUIRED_COLUMNS
33
- )
34
-
35
- return table
36
-
37
-
38
- class MaskingROITableV1:
39
- """Class to handle fractal Masking ROI tables.
40
-
41
- To know more about the Masking ROI table format, please refer to the
42
- specification at:
43
- https://fractal-analytics-platform.github.io/fractal-tasks-core/tables/
44
- """
45
-
46
- def __init__(
47
- self,
48
- group: zarr.Group,
49
- name: str,
50
- validate_metadata: bool = True,
51
- validate_table: bool = True,
52
- index_key: str | None = None,
53
- ):
54
- """Initialize the class from an existing group.
55
-
56
- Args:
57
- group (zarr.Group): The group containing the
58
- ROI table.
59
- name (str): The name of the ROI table.
60
- validate_metadata (bool): If True, the metadata is validated.
61
- validate_table (bool): If True, the table is validated.
62
- index_key (str): The column name to use as the index of the DataFrame.
63
- """
64
- if validate_metadata:
65
- self._meta = MaskingROITableV1Meta(**group.attrs)
66
- else:
67
- self._meta = MaskingROITableV1Meta.model_construct(**group.attrs)
68
-
69
- # Validate the table is not implemented for the Masking ROI table
70
- validators = [partial(validate_columns, required_columns=REQUIRED_COLUMNS)]
71
- validators = validators if validate_table else None
72
-
73
- index_key = index_key if index_key is not None else self._meta.instance_key
74
- self._table_handler = BaseTable(
75
- group=group,
76
- index_key=index_key,
77
- index_type="int",
78
- validators=validators,
79
- )
80
- self._name = name
81
-
82
- def __repr__(self) -> str:
83
- """Return the string representation of the class."""
84
- name = "MaskingROITableV1("
85
- len_name = len(name)
86
- return (
87
- f"{name}"
88
- f"group_path={self.group_path}, \n"
89
- f"{' ':>{len_name}}name={self.name},\n"
90
- f"{' ':>{len_name}}source_label={self.source_label()}, \n"
91
- f"{' ':>{len_name}}num_labels={len(self.table.index)}), \n"
92
- ")"
93
- )
94
-
95
- @classmethod
96
- def _new(
97
- cls,
98
- parent_group: zarr.Group,
99
- name: str,
100
- label_image: str,
101
- instance_key: str = "label",
102
- overwrite: bool = False,
103
- ) -> "MaskingROITableV1":
104
- """Create a new Masking ROI table.
105
-
106
- Note this method is not meant to be called directly.
107
- But should be called from the TablesHandler class.
108
-
109
- Args:
110
- parent_group (zarr.Group): The parent group where the ROI table
111
- will be created.
112
- name (str): The name of the ROI table.
113
- label_image (str): Name of the label image.
114
- instance_key (str): The column name to use as the index of the DataFrame.
115
- Default is 'label'.
116
- overwrite (bool): Whether to overwrite the table if it already exists.
117
- """
118
- group = parent_group.create_group(name, overwrite=overwrite)
119
-
120
- table = pd.DataFrame(
121
- index=pd.Index([], name=instance_key, dtype="int"), columns=REQUIRED_COLUMNS
122
- )
123
-
124
- meta = MaskingROITableV1Meta(
125
- region={"path": label_image}, instance_key=instance_key
126
- )
127
- write_table_ad(
128
- group=group,
129
- table=table,
130
- index_key=instance_key,
131
- index_type="int",
132
- meta=meta,
133
- )
134
- return cls(group=group, name=name)
135
-
136
- @property
137
- def name(self) -> str:
138
- """Return the name of the ROI table."""
139
- return self._name
140
-
141
- @property
142
- def root_path(self) -> str:
143
- """Return the path of the root group."""
144
- return self._table_handler.root_path
145
-
146
- @property
147
- def group_path(self) -> str:
148
- """Return the path of the group."""
149
- return self._table_handler.group_path
150
-
151
- @property
152
- def meta(self) -> MaskingROITableV1Meta:
153
- """Return the metadata of the ROI table."""
154
- return self._meta
155
-
156
- @property
157
- def table_handler(self) -> BaseTable:
158
- """Return the table handler."""
159
- return self._table_handler
160
-
161
- @property
162
- def table(self) -> pd.DataFrame:
163
- """Return the ROI table as a DataFrame."""
164
- return self._table_handler.table
165
-
166
- @table.setter
167
- def table(self, table: pd.DataFrame) -> None:
168
- """Set the feature table."""
169
- raise NotImplementedError(
170
- "Setting the table is not implemented. Please use the 'set_table' method."
171
- )
172
-
173
- def source_label(self, get_full_path: bool = False) -> str | None:
174
- """Return the name of the label image.
175
-
176
- The name is assumed to be after the last '/' in the path.
177
- Since this might fails, get_full_path is True, the full path is returned.
178
-
179
- Args:
180
- get_full_path (bool): If True, the full path is returned.
181
- """
182
- region = self.meta.region
183
- if region is None:
184
- return None
185
- path = region["path"]
186
-
187
- if get_full_path:
188
- return path
189
- return path.split("/")[-1]
190
-
191
- def set_table(self, table: pd.DataFrame) -> None:
192
- """Set the feature table."""
193
- self._table_handler.set_table(table)
194
-
195
- @property
196
- def list_labels(self) -> list[int]:
197
- """Return a list of all field indexes in the table."""
198
- return self.table.index.tolist()
199
-
200
- def from_label(self, label: Label, overwrite: bool = False) -> None:
201
- """Create a new ROI table from a label.
202
-
203
- Args:
204
- label (Label): The label to create the ROI table from.
205
- overwrite (bool): Whether to overwrite the elements in the table.
206
- """
207
- if not overwrite and self.table.empty:
208
- raise ValueError(
209
- "The table is not empty. Set overwrite to True to proceed."
210
- )
211
- raise NotImplementedError("Method not implemented yet.")
212
-
213
- def get_roi(self, label: int) -> WorldCooROI:
214
- """Get an ROI from the table."""
215
- if label not in self.list_labels:
216
- raise ValueError(f"Label {label} not found in the table.")
217
-
218
- table_df = self.table.loc[label]
219
-
220
- roi = WorldCooROI(
221
- x=table_df.loc["x_micrometer"],
222
- y=table_df.loc["y_micrometer"],
223
- z=table_df.loc["z_micrometer"],
224
- x_length=table_df.loc["len_x_micrometer"],
225
- y_length=table_df.loc["len_y_micrometer"],
226
- z_length=table_df.loc["len_z_micrometer"],
227
- unit=SpaceUnits.micrometer,
228
- infos={
229
- "label": label,
230
- "source_label_path": self.source_label(get_full_path=True),
231
- "source_label": self.source_label(),
232
- },
233
- )
234
- return roi
235
-
236
- @property
237
- def rois(self) -> list[WorldCooROI]:
238
- """List all ROIs in the table."""
239
- return [self.get_roi(label) for label in self.list_labels]
240
-
241
- def consolidate(self) -> None:
242
- """Write the crrent state of the table to the Zarr file."""
243
- self._table_handler.consolidate(self.meta)