ngio.tables API reference¶
ngio.tables
¶
Ngio Tables implementations.
ROI_TABLE_TYPES
module-attribute
¶
TypedTable
module-attribute
¶
TypedTable = Literal[
"generic_table",
"roi_table",
"masking_roi_table",
"generic_roi_table",
"feature_table",
"condition_table",
]
TableBackend
module-attribute
¶
TableBackend = (
Literal["anndata", "json", "csv", "parquet"]
| str
| TableBackendProtocol
)
AbstractBaseTable
¶
Bases: ABC
Abstract base class for a table.
This is used to define common methods and properties for all tables.
This class is not meant to be used directly.
Initialize the table.
Source code in src/ngio/tables/_abstract_table.py
backend_name
property
¶
Return the name of the backend.
If no backend is attached yet, the backend name stored in the table metadata is returned.
table_type
abstractmethod
staticmethod
¶
version
abstractmethod
staticmethod
¶
The generic table does not have a version.
Since does not follow a specific schema.
load_as_anndata
¶
Load the table as an AnnData object.
load_as_pandas_df
¶
Load the table as a pandas DataFrame.
load_as_polars_lf
¶
Load the table as a polars LazyFrame.
set_table_data
¶
Set the table.
If an object is passed, it will be used as the table. If None is passed, the table will be loaded from the backend.
If refresh is True, the table will be reloaded from the backend. If table is not None, this will be ignored.
Source code in src/ngio/tables/_abstract_table.py
set_backend
¶
set_backend(
handler: ZarrGroupHandler | None = None,
backend: TableBackend | None = None,
) -> None
Set the backend of the table.
If backend is None, the backend stored in the table metadata
is used.
If no handler is provided and the table is not yet attached to a
Zarr group, a string backend is only recorded as the table's
preferred backend; the backend is instantiated when the table is
written (e.g. by add_table).
Source code in src/ngio/tables/_abstract_table.py
from_handler
abstractmethod
classmethod
¶
from_handler(
handler: ZarrGroupHandler,
backend: TableBackend | None = None,
attrs: dict | None = None,
) -> Self
Create a new ROI table from a Zarr group handler.
Source code in src/ngio/tables/_abstract_table.py
from_table_data
classmethod
¶
Create a new ROI table from a Zarr group handler.
consolidate
¶
Write the current state of the table to the Zarr file.
Source code in src/ngio/tables/_abstract_table.py
ImplementedTables
¶
A singleton class to manage the available table handler plugins.
available_implementations
¶
get_table
¶
get_table(
meta: TableMeta,
handler: ZarrGroupHandler,
backend: TableBackend | None = None,
strict: bool = True,
attrs: dict | None = None,
) -> Table
Try to get a handler for the given store based on the metadata version.
attrs are the group attributes meta was decoded from, forwarded so
the concrete table does not read the same document again.
Source code in src/ngio/tables/_tables_container.py
add_implementation
¶
add_implementation(
handler: type[Table],
overwrite: bool = False,
aliases: list[str] | None = None,
) -> None
Register a new table handler.
Source code in src/ngio/tables/_tables_container.py
Table
¶
Bases: Protocol
Placeholder class for a table.
table_type
staticmethod
¶
version
staticmethod
¶
set_table_data
¶
Make sure that the table data is set (exist in memory).
If an object is passed, it will be used as the table. If None is passed, the table will be loaded from the backend.
If refresh is True, the table will be reloaded from the backend. If table is not None, this will be ignored.
Source code in src/ngio/tables/_tables_container.py
set_backend
¶
set_backend(
handler: ZarrGroupHandler | None = None,
backend: TableBackend | None = None,
) -> None
Set the backend store and path for the table.
If the handler is None it will be inferred from the current backend.
If the backend is None, the table's own backend is preserved.
Source code in src/ngio/tables/_tables_container.py
from_handler
classmethod
¶
from_handler(
handler: ZarrGroupHandler,
backend: TableBackend | None = None,
attrs: dict | None = None,
) -> Self
Create a new table from a Zarr group handler.
from_table_data
classmethod
¶
TablesContainer
¶
TablesContainer(group_handler: ZarrGroupHandler)
A class to handle the /tables group in an OME-NGFF file.
Initialize the TablesContainer.
Source code in src/ngio/tables/_tables_container.py
table_types
¶
Return the table type of each table, keyed by name.
The type lives only in each table's own attributes — the /tables
group records names — so learning it costs one group open per table.
Under cache=True that result is memoised: a table's type does not
change without going through add/delete here, and those drop the
memo. Under cache=False every call re-reads each type, so a table
overwritten with a different type by another handle is picked up.
Names are re-read every call in both modes.
Parameters:
-
names(Sequence[str] | None, default:None) –Restrict to these tables. Defaults to every table listed.
Source code in src/ngio/tables/_tables_container.py
list
¶
list(
filter_types: TypedTable | str | None = None,
) -> list[str]
List all tables in the group.
Parameters:
-
filter_types(TypedTable | str | None, default:None) –If provided, only return tables of this type.
Returns:
-
list[str]–A list of table names.
Source code in src/ngio/tables/_tables_container.py
get
¶
get(
name: str,
backend: TableBackend | None = None,
strict: bool = True,
) -> Table
Get a table from the group.
Parameters:
-
name(str) –The name of the table.
-
backend(TableBackend | None, default:None) –The backend to use for reading the table.
-
strict(bool, default:True) –If True, raise an error if the table type is not implemented.
Returns:
-
Table–The table object.
Source code in src/ngio/tables/_tables_container.py
get_as
¶
get_as(
name: str,
table_cls: type[TableType],
backend: TableBackend | None = None,
) -> TableType
Get a table from the group as a specific type.
Parameters:
-
name(str) –The name of the table.
-
table_cls(type[TableType]) –The table class to use for loading the table.
-
backend(TableBackend | None, default:None) –The backend to use for reading the table.
Returns:
-
TableType–The table object of the specified type.
Source code in src/ngio/tables/_tables_container.py
delete
¶
Delete a table from the group.
Parameters:
-
name(str) –The name of the table to delete.
-
missing_ok(bool, default:False) –If True, do not raise an error if the table does not exist.
Source code in src/ngio/tables/_tables_container.py
add
¶
add(
name: str,
table: Table,
backend: TableBackend | None = None,
overwrite: bool = False,
) -> None
Add a table to the group.
Parameters:
-
name(str) –The name of the table.
-
table(Table) –The table object to add.
-
backend(TableBackend | None, default:None) –The backend to use for writing the table. If
None(default), the table's own backend is preserved. -
overwrite(bool, default:False) –Whether to overwrite an existing table with the same name.
Source code in src/ngio/tables/_tables_container.py
ImplementedTableBackends
¶
A class to manage the available table backends.
normalize_backend_name
¶
Resolve a backend name or alias to its canonical name.
Raises:
-
NgioValueError–If the backend name is not implemented.
Source code in src/ngio/tables/backends/_table_backends.py
get_backend
¶
get_backend(
*,
group_handler: ZarrGroupHandler,
backend_name: str,
index_key: str | None = None,
index_type: Literal["int", "str"] | None = None,
) -> TableBackendProtocol
Instantiate the named backend and attach it to group_handler.
Raises:
-
NgioValueError–If
backend_nameis not registered.
Source code in src/ngio/tables/backends/_table_backends.py
add_backend
¶
add_backend(
table_backend: type[TableBackendProtocol],
overwrite: bool = False,
aliases: list[str] | None = None,
) -> None
Register a new handler.
Source code in src/ngio/tables/backends/_table_backends.py
TableBackendProtocol
¶
Bases: Protocol
set_group_handler
¶
set_group_handler(
group_handler: ZarrGroupHandler,
index_key: str | None = None,
index_type: Literal["int", "str"] | None = None,
) -> None
Attach a group handler to the backend.
Index keys and index types are used to ensure that the serialization and deserialization of the table is consistent across different backends.
Making sure that this is consistent is a duty of the backend implementations.
Source code in src/ngio/tables/backends/_table_backends.py
backend_name
staticmethod
¶
Return the name of the backend.
As a convention we set name as
{backend_name}_v{version}
Where the version is a integer.
implements_anndata
staticmethod
¶
Check if the backend implements the anndata protocol.
If this is True, the backend should implement the
write_from_anndata method.
AnnData objects are more complex than DataFrames, so if this is true the backend should implement the full serialization of the AnnData object.
If this is False, these methods should raise a
NotImplementedError.
Source code in src/ngio/tables/backends/_table_backends.py
implements_pandas
staticmethod
¶
Check if the backend implements the pandas protocol.
If this is True, the backend should implement the
write_from_dataframe methods.
If this is False, these methods should raise a
NotImplementedError.
Source code in src/ngio/tables/backends/_table_backends.py
implements_polars
staticmethod
¶
Check if the backend implements the polars protocol.
If this is True, the backend should implement the
write_from_polars methods.
If this is False, these methods should raise a
NotImplementedError.
Source code in src/ngio/tables/backends/_table_backends.py
load_as_anndata
¶
load_as_pandas_df
¶
load_as_polars_lf
¶
load
¶
The default load method.
This method will be default way to load the table
from the backend. This method should wrap one of the
load_as_anndata, load_as_dataframe or load_as_polars
methods depending on the backend implementation.
Source code in src/ngio/tables/backends/_table_backends.py
write_from_pandas
¶
write_from_anndata
¶
write_from_polars
¶
write
¶
write(
table_data: DataFrame | AnnData | DataFrame | LazyFrame,
metadata: dict[str, str] | None = None,
) -> None
This is a generic write method.
Will call the appropriate write method depending on the type of the table.
Moreover it will also write the metadata if provided, and the backend methadata
the backend should write in the zarr group attributes - backend: the backend name (self.backend_name()) - index_key: the index key - index_type: the index type
Source code in src/ngio/tables/backends/_table_backends.py
GenericTable
¶
Bases: AbstractBaseTable
Class to a non-specific table.
This can be used to load any table that does not have a specific definition.
Initialize the table.
Source code in src/ngio/tables/_abstract_table.py
backend_name
property
¶
Return the name of the backend.
If no backend is attached yet, the backend name stored in the table metadata is returned.
load_as_anndata
¶
Load the table as an AnnData object.
load_as_pandas_df
¶
Load the table as a pandas DataFrame.
load_as_polars_lf
¶
Load the table as a polars LazyFrame.
set_table_data
¶
Set the table.
If an object is passed, it will be used as the table. If None is passed, the table will be loaded from the backend.
If refresh is True, the table will be reloaded from the backend. If table is not None, this will be ignored.
Source code in src/ngio/tables/_abstract_table.py
set_backend
¶
set_backend(
handler: ZarrGroupHandler | None = None,
backend: TableBackend | None = None,
) -> None
Set the backend of the table.
If backend is None, the backend stored in the table metadata
is used.
If no handler is provided and the table is not yet attached to a
Zarr group, a string backend is only recorded as the table's
preferred backend; the backend is instantiated when the table is
written (e.g. by add_table).
Source code in src/ngio/tables/_abstract_table.py
from_table_data
classmethod
¶
Create a new ROI table from a Zarr group handler.
consolidate
¶
Write the current state of the table to the Zarr file.
Source code in src/ngio/tables/_abstract_table.py
table_type
staticmethod
¶
version
staticmethod
¶
from_handler
classmethod
¶
from_handler(
handler: ZarrGroupHandler,
backend: TableBackend | None = None,
attrs: dict | None = None,
) -> GenericTable
Source code in src/ngio/tables/v1/_generic_table.py
open_table
¶
open_table(
store: StoreOrGroup,
backend: TableBackend | None = None,
cache: bool = False,
mode: AccessModeLiteral = "r+",
) -> Table
Open a table from a Zarr store.
Source code in src/ngio/tables/_tables_container.py
open_table_as
¶
open_table_as(
store: StoreOrGroup,
table_cls: type[TableType],
backend: TableBackend | None = None,
cache: bool = False,
mode: AccessModeLiteral = "r+",
) -> TableType
Open a table from a Zarr store as a specific type.
Source code in src/ngio/tables/_tables_container.py
open_tables_container
¶
open_tables_container(
store: StoreOrGroup,
cache: bool = False,
mode: AccessModeLiteral = "r+",
) -> TablesContainer
Open a table handler from a Zarr store.
Source code in src/ngio/tables/_tables_container.py
write_table
¶
write_table(
store: StoreOrGroup,
table: Table,
backend: TableBackend | None = None,
cache: bool = False,
mode: AccessModeLiteral = "a",
) -> None
Write a table to a Zarr store.
A table will be created at the given store location.
Parameters:
-
store(StoreOrGroup) –The Zarr store or group to write the table to.
-
table(Table) –The table to write.
-
backend(TableBackend, default:None) –The backend to use for writing the table. If
None(default), the table's own backend is preserved. -
cache(bool, default:False) –Whether to use caching for the Zarr group handler.
-
mode(AccessModeLiteral, default:'a') –The access mode to use for the Zarr group handler.