ngio 0.2.9__py3-none-any.whl → 0.3.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 (38) hide show
  1. ngio/common/__init__.py +16 -0
  2. ngio/common/_array_pipe.py +50 -27
  3. ngio/common/_table_ops.py +471 -0
  4. ngio/hcs/__init__.py +1 -1
  5. ngio/hcs/{plate.py → _plate.py} +451 -78
  6. ngio/images/__init__.py +3 -3
  7. ngio/images/{image.py → _image.py} +26 -21
  8. ngio/images/{label.py → _label.py} +6 -4
  9. ngio/images/{masked_image.py → _masked_image.py} +2 -2
  10. ngio/images/{ome_zarr_container.py → _ome_zarr_container.py} +152 -86
  11. ngio/ome_zarr_meta/_meta_handlers.py +16 -8
  12. ngio/ome_zarr_meta/ngio_specs/_channels.py +41 -29
  13. ngio/tables/__init__.py +14 -2
  14. ngio/tables/_abstract_table.py +269 -0
  15. ngio/tables/{tables_container.py → _tables_container.py} +186 -100
  16. ngio/tables/backends/__init__.py +20 -0
  17. ngio/tables/backends/_abstract_backend.py +58 -80
  18. ngio/tables/backends/{_anndata_v1.py → _anndata.py} +5 -1
  19. ngio/tables/backends/_csv.py +35 -0
  20. ngio/tables/backends/{_json_v1.py → _json.py} +4 -1
  21. ngio/tables/backends/{_csv_v1.py → _non_zarr_backends.py} +61 -27
  22. ngio/tables/backends/_parquet.py +47 -0
  23. ngio/tables/backends/_table_backends.py +39 -18
  24. ngio/tables/backends/_utils.py +147 -1
  25. ngio/tables/v1/__init__.py +19 -3
  26. ngio/tables/v1/_condition_table.py +71 -0
  27. ngio/tables/v1/_feature_table.py +63 -129
  28. ngio/tables/v1/_generic_table.py +21 -159
  29. ngio/tables/v1/_roi_table.py +285 -201
  30. ngio/utils/_fractal_fsspec_store.py +29 -0
  31. {ngio-0.2.9.dist-info → ngio-0.3.0.dist-info}/METADATA +4 -3
  32. ngio-0.3.0.dist-info/RECORD +61 -0
  33. ngio/tables/_validators.py +0 -108
  34. ngio-0.2.9.dist-info/RECORD +0 -57
  35. /ngio/images/{abstract_image.py → _abstract_image.py} +0 -0
  36. /ngio/images/{create.py → _create.py} +0 -0
  37. {ngio-0.2.9.dist-info → ngio-0.3.0.dist-info}/WHEEL +0 -0
  38. {ngio-0.2.9.dist-info → ngio-0.3.0.dist-info}/licenses/LICENSE +0 -0
@@ -1,69 +1,32 @@
1
1
  """Implementation of a generic table class."""
2
2
 
3
- import pandas as pd
4
- from anndata import AnnData
5
-
6
- from ngio.tables.backends import (
7
- BackendMeta,
8
- ImplementedTableBackends,
9
- convert_anndata_to_pandas,
10
- convert_pandas_to_anndata,
11
- )
12
- from ngio.utils import NgioValueError, ZarrGroupHandler
3
+ from ngio.tables._abstract_table import AbstractBaseTable
4
+ from ngio.tables.backends import BackendMeta, TableBackend
5
+ from ngio.utils import ZarrGroupHandler
13
6
 
14
7
 
15
8
  class GenericTableMeta(BackendMeta):
16
- """Metadata for the ROI table."""
9
+ """Metadata for the generic table.
10
+
11
+ This is used to store metadata for a generic table.
12
+ It does not have a specific definition.
13
+ """
17
14
 
18
- fractal_table_version: str | None = None
19
- type: str | None = None
15
+ table_version: str | None = "1"
16
+ type: str | None = "generic_table"
20
17
 
21
18
 
22
- class GenericTable:
19
+ class GenericTable(AbstractBaseTable):
23
20
  """Class to a non-specific table.
24
21
 
25
22
  This can be used to load any table that does not have
26
23
  a specific definition.
27
24
  """
28
25
 
29
- def __init__(
30
- self,
31
- dataframe: pd.DataFrame | None = None,
32
- anndata: AnnData | None = None,
33
- ) -> None:
34
- """Initialize the GenericTable."""
35
- self._meta = GenericTableMeta()
36
- if dataframe is None and anndata is None:
37
- raise NgioValueError(
38
- "Either a DataFrame or an AnnData object must be provided."
39
- )
40
-
41
- if dataframe is not None and anndata is not None:
42
- raise NgioValueError(
43
- "Only one of DataFrame or AnnData object can be provided."
44
- )
45
-
46
- self._dataframe = dataframe
47
- self._anndata = anndata
48
-
49
- self.anndata_native = True if anndata is not None else False
50
-
51
- self._table_backend = None
52
-
53
- def __repr__(self) -> str:
54
- """Return a string representation of the table."""
55
- if self._dataframe is not None:
56
- num_rows = len(self.dataframe)
57
- num_columns = len(self.dataframe.columns)
58
- prop = f"num_rows={num_rows}, num_columns={num_columns}, mode=dataframe"
59
- else:
60
- prop = "mode=anndata"
61
- return f"GenericTable({prop})"
62
-
63
26
  @staticmethod
64
- def type() -> str:
27
+ def table_type() -> str:
65
28
  """Return the type of the table."""
66
- return "generic"
29
+ return "generic_table"
67
30
 
68
31
  @staticmethod
69
32
  def version() -> str:
@@ -73,115 +36,14 @@ class GenericTable:
73
36
  """
74
37
  return "1"
75
38
 
76
- @property
77
- def backend_name(self) -> str | None:
78
- """Return the name of the backend."""
79
- if self._table_backend is None:
80
- return None
81
- return self._table_backend.backend_name()
82
-
83
- @property
84
- def dataframe(self) -> pd.DataFrame:
85
- """Return the table as a DataFrame."""
86
- if self._dataframe is not None:
87
- return self._dataframe
88
-
89
- if self._anndata is not None:
90
- return convert_anndata_to_pandas(self._anndata)
91
-
92
- raise NgioValueError("No table loaded.")
93
-
94
- @dataframe.setter
95
- def dataframe(self, dataframe: pd.DataFrame) -> None:
96
- """Set the table as a DataFrame."""
97
- self._dataframe = dataframe
98
- self.anndata_native = False
99
-
100
- @property
101
- def anndata(self) -> AnnData:
102
- """Return the table as an AnnData object."""
103
- if self._anndata is not None:
104
- return self._anndata
105
-
106
- if self._dataframe is not None:
107
- return convert_pandas_to_anndata(
108
- self._dataframe,
109
- )
110
- raise NgioValueError("No table loaded.")
111
-
112
- @anndata.setter
113
- def anndata(self, anndata: AnnData) -> None:
114
- """Set the table as an AnnData object."""
115
- self._anndata = anndata
116
- self.anndata_native = True
117
-
118
39
  @classmethod
119
- def _from_handler(
120
- cls, handler: ZarrGroupHandler, backend_name: str | None = None
121
- ) -> "GenericTable":
122
- """Create a new ROI table from a Zarr group handler."""
123
- meta = GenericTableMeta(**handler.load_attrs())
124
- if backend_name is None:
125
- backend = ImplementedTableBackends().get_backend(
126
- backend_name=meta.backend,
127
- group_handler=handler,
128
- index_key=None,
129
- )
130
- else:
131
- backend = ImplementedTableBackends().get_backend(
132
- backend_name=backend_name,
133
- group_handler=handler,
134
- index_key=None,
135
- )
136
- meta.backend = backend_name
137
-
138
- if backend.implements_anndata():
139
- anndata = backend.load_as_anndata()
140
- table = cls(anndata=anndata)
141
-
142
- elif backend.implements_pandas():
143
- dataframe = backend.load_as_pandas_df()
144
- table = cls(dataframe=dataframe)
145
- else:
146
- raise NgioValueError(
147
- "The backend does not implement the dataframe protocol."
148
- )
149
-
150
- table._meta = meta
151
- table._table_backend = backend
152
- return table
153
-
154
- def _set_backend(
155
- self,
40
+ def from_handler(
41
+ cls,
156
42
  handler: ZarrGroupHandler,
157
- backend_name: str | None = None,
158
- ) -> None:
159
- """Set the backend of the table."""
160
- backend = ImplementedTableBackends().get_backend(
161
- backend_name=backend_name,
162
- group_handler=handler,
163
- index_key=None,
43
+ backend: TableBackend | None = None,
44
+ ) -> "GenericTable":
45
+ return cls._from_handler(
46
+ handler=handler,
47
+ backend=backend,
48
+ meta_model=BackendMeta,
164
49
  )
165
- self._meta.backend = backend_name
166
- self._table_backend = backend
167
-
168
- def consolidate(self) -> None:
169
- """Write the current state of the table to the Zarr file."""
170
- if self._table_backend is None:
171
- raise NgioValueError(
172
- "No backend set for the table. "
173
- "Please add the table to a OME-Zarr Image before calling consolidate."
174
- )
175
-
176
- if self.anndata_native:
177
- self._table_backend.write(
178
- self.anndata,
179
- metadata=self._meta.model_dump(exclude_none=True),
180
- mode="anndata",
181
- )
182
- else:
183
- self._table_backend.write(
184
- self.dataframe,
185
- metadata=self._meta.model_dump(exclude_none=True),
186
- mode="pandas",
187
- )