hdmf.common.alignedtable module
Collection of Container classes for interacting with aligned and hierarchical dynamic tables
- class hdmf.common.alignedtable.AlignedDynamicTable(name, description, id=None, columns=None, colnames=None, target_tables=None, category_tables=None, categories=None)
Bases:
DynamicTable
DynamicTable container that supports storing a collection of subtables. Each sub-table is a DynamicTable itself that is aligned with the main table by row index. I.e., all DynamicTables stored in this group MUST have the same number of rows. This type effectively defines a 2-level table in which the main data is stored in the main table implemented by this type and additional columns of the table are grouped into categories, with each category being’ represented by a separate DynamicTable stored within the group.
- NOTE: To remain compatible with DynamicTable, the attribute colnames represents only the
columns of the main table (not including the category tables). To get the full list of column names, use the get_colnames() function instead.
- Parameters:
name (
str
) – the name of this tabledescription (
str
) – a description of what is in this tableid (
ndarray
orlist
ortuple
orDataset
orArray
orStrDataset
orHDMFDataset
orAbstractDataChunkIterator
orDataIO
orElementIdentifiers
) – the identifiers for this tablecolnames (
ndarray
orlist
ortuple
orDataset
orArray
orStrDataset
orHDMFDataset
orAbstractDataChunkIterator
) – the ordered names of the columns in this table. columns must also be provided.target_tables (
dict
) – dict mapping DynamicTableRegion column name to the table that the DTR points to. The column is added to the table if it is not already present (i.e., when it is optional).category_tables (
list
) – List of DynamicTables to be added to the container. NOTE - Only regular DynamicTables are allowed. Using AlignedDynamicTable as a category for AlignedDynamicTable is currently not supported.categories (
ndarray
orlist
ortuple
orDataset
orArray
orStrDataset
orHDMFDataset
orAbstractDataChunkIterator
) – List of names with the ordering of category tables
- property category_tables
List of DynamicTables to be added to the container. NOTE - Only regular DynamicTables are allowed. Using AlignedDynamicTable as a category for AlignedDynamicTable is currently not supported.
- property categories
Get the list of names the categories
Short-hand for list(self.category_tables.keys())
- Raises:
KeyError if the given name is not in self.category_tables
- add_category(category)
Add a new DynamicTable to the AlignedDynamicTable to create a new category in the table.
NOTE: The table must align with (i.e, have the same number of rows as) the main data table (and other category tables). I.e., if the AlignedDynamicTable is already populated with data then we have to populate the new category with the corresponding data before adding it.
- raises:
ValueError is raised if the input table does not have the same number of rows as the main table. ValueError is raised if the table is an AlignedDynamicTable instead of regular DynamicTable.
- Parameters:
category (
DynamicTable
) – Add a new DynamicTable category
- add_column(name, description, data=[], table=False, index=False, enum=False, col_cls=None, check_ragged=True, category=None)
Add a column to the table
- raises:
KeyError if the category does not exist
- Parameters:
name (
str
) – the name of this VectorDatadescription (
str
) – a description for this columndata (
ndarray
orlist
ortuple
orDataset
orArray
orStrDataset
orHDMFDataset
orAbstractDataChunkIterator
orDataIO
) – a dataset where the first dimension is a concatenation of multiple vectorstable (
bool
orDynamicTable
) – whether or not this is a table region or the table the region applies toindex (
bool
orVectorIndex
orndarray
orlist
ortuple
orDataset
orArray
orStrDataset
orHDMFDataset
orAbstractDataChunkIterator
orint
) –False
(default): do not generate a VectorIndexTrue
: generate one empty VectorIndexVectorIndex
: Use the supplied VectorIndexarray-like of ints: Create a VectorIndex and use these values as the data
int
: Recursively create n VectorIndex objects for a multi-ragged array
enum (
bool
orndarray
orlist
ortuple
orDataset
orArray
orStrDataset
orHDMFDataset
orAbstractDataChunkIterator
) – whether or not this column contains data from a fixed set of elementscol_cls (
type
) – class to use to represent the column data. If table=True, this field is ignored and a DynamicTableRegion object is used. If enum=True, this field is ignored and a EnumData object is used.check_ragged (
bool
) – whether or not to check for ragged arrays when adding data to the table. Set to False to avoid checking every element if performance issues occur.category (
str
) – The category the column should be added to
- add_row(data=None, id=None, enforce_unique_id=False)
We can either provide the row data as a single dict or by specifying a dict for each category
- get_colnames(include_category_tables=False, ignore_category_ids=False)
Get the full list of names of columns for this table
- returns:
List of tuples (str, str) where the first string is the name of the DynamicTable that contains the column and the second string is the name of the column. If include_category_tables is False, then a list of column names is returned.
- to_dataframe(ignore_category_ids=False)
Convert the collection of tables to a single pandas DataFrame
- Parameters:
ignore_category_ids (
bool
) – Ignore id columns of sub-category tables
- __getitem__(item)
Called to implement standard array slicing syntax.
Same as
self.get(item)
. Seeget
for details.
- get(item, **kwargs)
Access elements (rows, columns, category tables etc.) from the table. Instead of calling this function directly, the class also implements standard array slicing syntax via
__getitem__
(which calls this function). For example, instead of callingself.get(item=slice(2,5))
we may use the often more convenient form ofself[2:5]
instead.- Parameters:
item – Selection defining the items of interest. This may be either a:
- int, list, array, sliceReturn one or multiple row of the table as a pandas.DataFrame. For example:
self[0]
: Select the first row of the tableself[[0,3]]
: Select the first and fourth row of the tableself[1:4]
: Select the rows with index 1,2,3 from the table
- stringReturn a column from the main table or a category table. For example:
self['column']
: Return the column from the main table.self['my_category']
: Returns a DataFrame of themy_category
category table. This is a shorthand forself.get_category('my_category').to_dataframe()
.
tuple: Get a column, row, or cell from a particular category table. The tuple is expected to consist of the following elements:
category
: string with the name of the category. To select from the main table useself.name
orNone
.column
: string with the name of the column, androw
: integer index of the row.
The tuple itself then may take the following forms:
- Select a single column from a table via:
self[category, column]
- Select a single full row of a given category table via:
self[row, category]
(recommended, for consistency with DynamicTable)self[category, row]
- Select a single cell via:
self[row, (category, column)]
(recommended, for consistency with DynamicTable)self[row, category, column]
self[category, column, row]
- Returns:
Depending on the type of selection the function returns a:
pandas.DataFrame: when retrieving a row or category table
array : when retrieving a single column
single value : when retrieving a single cell. The data type and shape will depend on the data type and shape of the cell/column.
- has_foreign_columns(ignore_category_tables=False)
Does the table contain DynamicTableRegion columns
- returns:
True if the table or any of the category tables contains a DynamicTableRegion column, else False
- Parameters:
ignore_category_tables (
bool
) – Ignore the category tables and only check in the main table columns
- get_foreign_columns(ignore_category_tables=False)
- Determine the names of all columns that link to another DynamicTable, i.e.,
find all DynamicTableRegion type columns. Similar to a foreign key in a database, a DynamicTableRegion column references elements in another table.
- returns:
List of tuples (str, str) where the first string is the name of the category table (or None if the column is in the main table) and the second string is the column name.
- Parameters:
ignore_category_tables (
bool
) – Ignore the category tables and only check in the main table columns
- get_linked_tables(other_tables=None, ignore_category_tables=False)
- Get a list of the full list of all tables that are being linked to directly or indirectly
from this table via foreign DynamicTableColumns included in this table or in any table that can be reached through DynamicTableRegion columns
- Returns: List of dicts with the following keys:
‘source_table’ : The source table containing the DynamicTableRegion column
‘source_column’ : The relevant DynamicTableRegion column in the ‘source_table’
‘target_table’ : The target DynamicTable; same as source_column.table.
- Parameters:
- data_type = 'AlignedDynamicTable'
- namespace = 'hdmf-common'