ngio 0.2.9__py3-none-any.whl → 0.3.0a1__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.
- ngio/common/__init__.py +16 -0
- ngio/common/_table_ops.py +471 -0
- ngio/hcs/plate.py +430 -72
- ngio/images/ome_zarr_container.py +99 -68
- ngio/ome_zarr_meta/_meta_handlers.py +16 -8
- ngio/ome_zarr_meta/ngio_specs/_axes.py +7 -4
- ngio/tables/__init__.py +13 -1
- ngio/tables/abstract_table.py +269 -0
- ngio/tables/backends/__init__.py +20 -0
- ngio/tables/backends/_abstract_backend.py +58 -80
- ngio/tables/backends/{_anndata_v1.py → _anndata.py} +5 -1
- ngio/tables/backends/_csv.py +35 -0
- ngio/tables/backends/{_json_v1.py → _json.py} +4 -1
- ngio/tables/backends/{_csv_v1.py → _non_zarr_backends.py} +61 -27
- ngio/tables/backends/_parquet.py +47 -0
- ngio/tables/backends/_table_backends.py +39 -18
- ngio/tables/backends/_utils.py +147 -1
- ngio/tables/tables_container.py +180 -92
- ngio/tables/v1/__init__.py +19 -3
- ngio/tables/v1/_condition_table.py +71 -0
- ngio/tables/v1/_feature_table.py +63 -129
- ngio/tables/v1/_generic_table.py +21 -159
- ngio/tables/v1/_roi_table.py +285 -201
- ngio/utils/_fractal_fsspec_store.py +29 -0
- {ngio-0.2.9.dist-info → ngio-0.3.0a1.dist-info}/METADATA +4 -3
- {ngio-0.2.9.dist-info → ngio-0.3.0a1.dist-info}/RECORD +28 -24
- ngio/tables/_validators.py +0 -108
- {ngio-0.2.9.dist-info → ngio-0.3.0a1.dist-info}/WHEEL +0 -0
- {ngio-0.2.9.dist-info → ngio-0.3.0a1.dist-info}/licenses/LICENSE +0 -0
ngio/tables/v1/_feature_table.py
CHANGED
|
@@ -6,12 +6,12 @@ https://fractal-analytics-platform.github.io/fractal-tasks-core/tables/
|
|
|
6
6
|
|
|
7
7
|
from typing import Literal
|
|
8
8
|
|
|
9
|
-
import
|
|
10
|
-
from pydantic import BaseModel
|
|
9
|
+
from pydantic import BaseModel, Field
|
|
11
10
|
|
|
12
|
-
from ngio.tables.
|
|
13
|
-
from ngio.tables.backends
|
|
14
|
-
from ngio.utils import NgioValueError
|
|
11
|
+
from ngio.tables.abstract_table import AbstractBaseTable
|
|
12
|
+
from ngio.tables.backends import BackendMeta, TableBackend, TabularData
|
|
13
|
+
from ngio.utils import NgioValueError
|
|
14
|
+
from ngio.utils._zarr_utils import ZarrGroupHandler
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
class RegionMeta(BaseModel):
|
|
@@ -23,42 +23,52 @@ class RegionMeta(BaseModel):
|
|
|
23
23
|
class FeatureTableMeta(BackendMeta):
|
|
24
24
|
"""Metadata for the ROI table."""
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
table_version: Literal["1"] = "1"
|
|
27
27
|
type: Literal["feature_table"] = "feature_table"
|
|
28
28
|
region: RegionMeta | None = None
|
|
29
|
-
instance_key: str = "label"
|
|
29
|
+
instance_key: str = "label" # Legacy field, kept for compatibility
|
|
30
|
+
# Backend metadata
|
|
31
|
+
index_key: str | None = "label"
|
|
32
|
+
index_type: Literal["int", "str"] | None = "int"
|
|
33
|
+
# Columns optional types
|
|
34
|
+
categorical_columns: list[str] = Field(default_factory=list)
|
|
35
|
+
measurement_columns: list[str] = Field(default_factory=list)
|
|
36
|
+
metadata_columns: list[str] = Field(default_factory=list)
|
|
30
37
|
|
|
31
38
|
|
|
32
|
-
class FeatureTableV1:
|
|
33
|
-
"""Class to represent a feature table.
|
|
34
|
-
|
|
35
|
-
This can be used to load any table that does not have
|
|
36
|
-
a specific definition.
|
|
37
|
-
"""
|
|
38
|
-
|
|
39
|
+
class FeatureTableV1(AbstractBaseTable):
|
|
39
40
|
def __init__(
|
|
40
41
|
self,
|
|
41
|
-
|
|
42
|
+
table_data: TabularData | None = None,
|
|
43
|
+
*,
|
|
42
44
|
reference_label: str | None = None,
|
|
45
|
+
meta: FeatureTableMeta | None = None,
|
|
43
46
|
) -> None:
|
|
44
47
|
"""Initialize the GenericTable."""
|
|
45
|
-
if
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
if meta is None:
|
|
49
|
+
meta = FeatureTableMeta()
|
|
50
|
+
|
|
51
|
+
if reference_label is not None:
|
|
48
52
|
path = f"../labels/{reference_label}"
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
else:
|
|
55
|
-
self._dataframe = normalize_pandas_df(
|
|
56
|
-
dataframe,
|
|
57
|
-
index_key=self._instance_key,
|
|
58
|
-
index_type="int",
|
|
59
|
-
reset_index=False,
|
|
53
|
+
meta = FeatureTableMeta(region=RegionMeta(path=path))
|
|
54
|
+
|
|
55
|
+
if table_data is not None and not isinstance(table_data, TabularData):
|
|
56
|
+
raise NgioValueError(
|
|
57
|
+
f"The table is not of type SupportedTables. Got {type(table_data)}"
|
|
60
58
|
)
|
|
61
|
-
|
|
59
|
+
|
|
60
|
+
if meta.index_key is None:
|
|
61
|
+
meta.index_key = "label"
|
|
62
|
+
|
|
63
|
+
if meta.index_type is None:
|
|
64
|
+
meta.index_type = "int"
|
|
65
|
+
|
|
66
|
+
meta.instance_key = meta.index_key
|
|
67
|
+
|
|
68
|
+
super().__init__(
|
|
69
|
+
table_data=table_data,
|
|
70
|
+
meta=meta,
|
|
71
|
+
)
|
|
62
72
|
|
|
63
73
|
def __repr__(self) -> str:
|
|
64
74
|
"""Return a string representation of the table."""
|
|
@@ -69,8 +79,29 @@ class FeatureTableV1:
|
|
|
69
79
|
properties += f", reference_label={self.reference_label}"
|
|
70
80
|
return f"FeatureTableV1({properties})"
|
|
71
81
|
|
|
82
|
+
@classmethod
|
|
83
|
+
def from_handler(
|
|
84
|
+
cls,
|
|
85
|
+
handler: ZarrGroupHandler,
|
|
86
|
+
backend: TableBackend | None = None,
|
|
87
|
+
) -> "FeatureTableV1":
|
|
88
|
+
return cls._from_handler(
|
|
89
|
+
handler=handler,
|
|
90
|
+
backend=backend,
|
|
91
|
+
meta_model=FeatureTableMeta,
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
@property
|
|
95
|
+
def meta(self) -> FeatureTableMeta:
|
|
96
|
+
"""Return the metadata of the table."""
|
|
97
|
+
if not isinstance(self._meta, FeatureTableMeta):
|
|
98
|
+
raise NgioValueError(
|
|
99
|
+
"The metadata of the table is not of type FeatureTableMeta."
|
|
100
|
+
)
|
|
101
|
+
return self._meta
|
|
102
|
+
|
|
72
103
|
@staticmethod
|
|
73
|
-
def
|
|
104
|
+
def table_type() -> str:
|
|
74
105
|
"""Return the type of the table."""
|
|
75
106
|
return "feature_table"
|
|
76
107
|
|
|
@@ -82,110 +113,13 @@ class FeatureTableV1:
|
|
|
82
113
|
"""
|
|
83
114
|
return "1"
|
|
84
115
|
|
|
85
|
-
@property
|
|
86
|
-
def backend_name(self) -> str | None:
|
|
87
|
-
"""Return the name of the backend."""
|
|
88
|
-
if self._table_backend is None:
|
|
89
|
-
return None
|
|
90
|
-
return self._table_backend.backend_name()
|
|
91
|
-
|
|
92
116
|
@property
|
|
93
117
|
def reference_label(self) -> str | None:
|
|
94
118
|
"""Return the reference label."""
|
|
95
|
-
path = self.
|
|
119
|
+
path = self.meta.region
|
|
96
120
|
if path is None:
|
|
97
121
|
return None
|
|
98
122
|
|
|
99
123
|
path = path.path
|
|
100
124
|
path = path.split("/")[-1]
|
|
101
125
|
return path
|
|
102
|
-
|
|
103
|
-
@property
|
|
104
|
-
def dataframe(self) -> pd.DataFrame:
|
|
105
|
-
"""Return the table as a DataFrame."""
|
|
106
|
-
if self._dataframe is None and self._table_backend is None:
|
|
107
|
-
raise NgioValueError(
|
|
108
|
-
"The table does not have a DataFrame in memory nor a backend."
|
|
109
|
-
)
|
|
110
|
-
|
|
111
|
-
if self._dataframe is None and self._table_backend is not None:
|
|
112
|
-
self._dataframe = self._table_backend.load_as_pandas_df()
|
|
113
|
-
|
|
114
|
-
if self._dataframe is None:
|
|
115
|
-
raise NgioValueError(
|
|
116
|
-
"The table does not have a DataFrame in memory nor a backend."
|
|
117
|
-
)
|
|
118
|
-
return self._dataframe
|
|
119
|
-
|
|
120
|
-
@dataframe.setter
|
|
121
|
-
def dataframe(self, dataframe: pd.DataFrame) -> None:
|
|
122
|
-
"""Set the table as a DataFrame."""
|
|
123
|
-
self._dataframe = normalize_pandas_df(
|
|
124
|
-
dataframe,
|
|
125
|
-
index_key=self._instance_key,
|
|
126
|
-
index_type="int",
|
|
127
|
-
reset_index=False,
|
|
128
|
-
)
|
|
129
|
-
|
|
130
|
-
@classmethod
|
|
131
|
-
def _from_handler(
|
|
132
|
-
cls, handler: ZarrGroupHandler, backend_name: str | None = None
|
|
133
|
-
) -> "FeatureTableV1":
|
|
134
|
-
"""Create a new ROI table from a Zarr group handler."""
|
|
135
|
-
meta = FeatureTableMeta(**handler.load_attrs())
|
|
136
|
-
instance_key = "label" if meta.instance_key is None else meta.instance_key
|
|
137
|
-
if backend_name is None:
|
|
138
|
-
backend = ImplementedTableBackends().get_backend(
|
|
139
|
-
backend_name=meta.backend,
|
|
140
|
-
group_handler=handler,
|
|
141
|
-
index_key=instance_key,
|
|
142
|
-
index_type="int",
|
|
143
|
-
)
|
|
144
|
-
else:
|
|
145
|
-
backend = ImplementedTableBackends().get_backend(
|
|
146
|
-
backend_name=backend_name,
|
|
147
|
-
group_handler=handler,
|
|
148
|
-
index_key=instance_key,
|
|
149
|
-
index_type="int",
|
|
150
|
-
)
|
|
151
|
-
meta.backend = backend_name
|
|
152
|
-
|
|
153
|
-
if not backend.implements_pandas:
|
|
154
|
-
raise NgioValueError(
|
|
155
|
-
"The backend does not implement the dataframe protocol."
|
|
156
|
-
)
|
|
157
|
-
|
|
158
|
-
table = cls()
|
|
159
|
-
table._meta = meta
|
|
160
|
-
table._table_backend = backend
|
|
161
|
-
return table
|
|
162
|
-
|
|
163
|
-
def _set_backend(
|
|
164
|
-
self,
|
|
165
|
-
handler: ZarrGroupHandler,
|
|
166
|
-
backend_name: str | None = None,
|
|
167
|
-
) -> None:
|
|
168
|
-
"""Set the backend of the table."""
|
|
169
|
-
instance_key = "label" if self._instance_key is None else self._instance_key
|
|
170
|
-
backend = ImplementedTableBackends().get_backend(
|
|
171
|
-
backend_name=backend_name,
|
|
172
|
-
group_handler=handler,
|
|
173
|
-
index_key=instance_key,
|
|
174
|
-
index_type="int",
|
|
175
|
-
)
|
|
176
|
-
self._meta.backend = backend_name
|
|
177
|
-
self._table_backend = backend
|
|
178
|
-
|
|
179
|
-
def consolidate(self) -> None:
|
|
180
|
-
"""Write the current state of the table to the Zarr file."""
|
|
181
|
-
if self._table_backend is None:
|
|
182
|
-
raise NgioValueError(
|
|
183
|
-
"No backend set for the table. "
|
|
184
|
-
"Please add the table to a OME-Zarr Image before calling consolidate."
|
|
185
|
-
)
|
|
186
|
-
|
|
187
|
-
self._table_backend.write(
|
|
188
|
-
self.dataframe,
|
|
189
|
-
metadata=self._meta.model_dump(exclude_none=True),
|
|
190
|
-
mode="pandas",
|
|
191
|
-
)
|
ngio/tables/v1/_generic_table.py
CHANGED
|
@@ -1,69 +1,32 @@
|
|
|
1
1
|
"""Implementation of a generic table class."""
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
from
|
|
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
|
|
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
|
-
|
|
19
|
-
type: str | 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
|
|
27
|
+
def table_type() -> str:
|
|
65
28
|
"""Return the type of the table."""
|
|
66
|
-
return "
|
|
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
|
|
120
|
-
cls,
|
|
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
|
-
|
|
158
|
-
) ->
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
-
)
|