A hierarchical data modeling framework for modern science data standards
—
HDMF's container system provides the foundation for organizing hierarchical data structures with metadata, parent-child relationships, and data management capabilities. The system is built around abstract and concrete container classes that enable flexible data organization and manipulation.
The foundational abstract base class for all container objects in HDMF, providing core functionality for hierarchical data organization.
class AbstractContainer:
"""
Abstract base class for all container objects.
Properties:
- name (str): Name of the container
- parent (AbstractContainer): Parent container
- children (tuple): Child containers
- fields (dict): Field values
- object_id (str): Unique object identifier
- modified (bool): Whether container has been modified
- container_source (str): Source information
"""
def __init__(self, name: str):
"""
Initialize container with name.
Args:
name: Name for the container
"""
def get_ancestor(self, neurodata_type: str = None) -> 'AbstractContainer':
"""
Get ancestor container of specified type.
Args:
neurodata_type: Type of ancestor to find
Returns:
Ancestor container or None
"""
def all_children(self) -> Generator['AbstractContainer', None, None]:
"""
Get generator for all child containers recursively.
Returns:
Generator yielding child containers
"""
def generate_new_id(self) -> str:
"""
Generate new unique object ID.
Returns:
New unique identifier string
"""
def set_modified(self, modified: bool = True):
"""
Mark container as modified or unmodified.
Args:
modified: Whether container is modified
"""
def reset_parent(self):
"""Reset parent relationship for this container."""
@property
def name(self) -> str:
"""Name of the container."""
@property
def parent(self) -> 'AbstractContainer':
"""Parent container."""
@property
def children(self) -> tuple:
"""Tuple of child containers."""Concrete container class extending AbstractContainer with enhanced display and I/O functionality.
class Container(AbstractContainer):
"""
Container that can hold other containers with enhanced printing/HTML functionality.
"""
def __init__(self, name: str):
"""
Initialize container.
Args:
name: Name for the container
"""
def set_data_io(self, data_io):
"""
Set data I/O object for this container.
Args:
data_io: Data I/O configuration object
"""
def _repr_html_(self) -> str:
"""
Generate HTML representation for Jupyter notebooks.
Returns:
HTML string representation
"""Specialized container for holding dataset information with data manipulation capabilities.
class Data(AbstractContainer):
"""
Container class for representing datasets with data manipulation capabilities.
Properties:
- data: The actual data content
- shape: Shape of the data array
"""
def __init__(self, name: str, data):
"""
Initialize data container.
Args:
name: Name for the data container
data: Data content (array-like)
"""
def get(self):
"""
Get the data content.
Returns:
The data content
"""
def append(self, arg):
"""
Append data to existing data.
Args:
arg: Data to append
"""
def extend(self, arg):
"""
Extend data with iterable.
Args:
arg: Iterable to extend data with
"""
def transform(self, transform_func):
"""
Apply transformation function to data.
Args:
transform_func: Function to transform data
"""
def set_data_io(self, data_io):
"""
Set data I/O configuration.
Args:
data_io: Data I/O configuration object
"""
@property
def data(self):
"""The data content."""
@property
def shape(self) -> tuple:
"""Shape of the data array."""Dynamic container class for managing multiple containers of the same type with auto-generated methods.
class MultiContainerInterface:
"""
Base class for containers that hold multiple containers of the same type.
Features:
- Auto-generates methods based on __clsconf__ attribute
- Provides dictionary-like access to child containers
- Supports iteration over contained items
"""
def __init__(self, **kwargs):
"""Initialize multi-container interface."""
def __getitem__(self, key):
"""Get container by key."""
def __iter__(self):
"""Iterate over contained items."""Manager for handling external resources and references (HERD - Hierarchical External Resource Descriptor).
class HERDManager:
"""
Manager for external resources using HERD (Hierarchical External Resource Descriptor).
"""
def __init__(self):
"""Initialize HERD manager."""
def link_resources(self, container: Container, resources: dict):
"""
Link external resources to a container.
Args:
container: Container to link resources to
resources: Dictionary of resource specifications
"""
def get_linked_resources(self, container: Container) -> dict:
"""
Get linked resources for a container.
Args:
container: Container to get resources for
Returns:
Dictionary of linked resources
"""Specialized containers for tabular data structures with row-based access and DataFrame integration.
class Table(Data):
"""
Base table implementation with column support and row-based access.
"""
def __init__(self, name: str, description: str, **kwargs):
"""
Initialize table.
Args:
name: Name of the table
description: Description of the table
"""
def add_row(self, **kwargs):
"""
Add a row to the table.
Args:
**kwargs: Column values for the new row
"""
def which(self, **kwargs) -> list:
"""
Find rows matching criteria.
Args:
**kwargs: Search criteria
Returns:
List of matching row indices
"""
def to_dataframe(self):
"""
Convert table to pandas DataFrame.
Returns:
pandas.DataFrame representation
"""
@classmethod
def from_dataframe(cls, df, name: str, **kwargs):
"""
Create table from pandas DataFrame.
Args:
df: pandas DataFrame
name: Name for the table
Returns:
New Table instance
"""
class Row:
"""
Represents a row from a Table object.
Properties:
- idx: Row index
- table: Parent table
"""
def __init__(self, table: Table, idx: int):
"""
Initialize row.
Args:
table: Parent table
idx: Row index
"""
def todict(self) -> dict:
"""
Convert row to dictionary.
Returns:
Dictionary representation of row
"""
class RowGetter:
"""
Helper class providing __getitem__ functionality for Table rows.
"""
def __init__(self, table: Table):
"""
Initialize row getter.
Args:
table: Parent table
"""
def __getitem__(self, key):
"""Get row(s) by index or slice."""from hdmf import Container, Data
import numpy as np
# Create data container
data = np.array([[1, 2, 3], [4, 5, 6]])
data_container = Data(name='sample_data', data=data)
# Create parent container
parent = Container(name='experiment')
parent.add_child(data_container)
# Access data
print(f"Data shape: {data_container.shape}")
print(f"Data content: {data_container.get()}")from hdmf.container import Table
import pandas as pd
# Create table from DataFrame
df = pd.DataFrame({
'id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Charlie'],
'score': [95.5, 87.2, 91.8]
})
table = Table.from_dataframe(df, name='results', description='Test results')
# Add new row
table.add_row(id=4, name='Diana', score=88.9)
# Query rows
high_scores = table.which(score__gt=90)
print(f"High scorers: {high_scores}")
# Convert back to DataFrame
result_df = table.to_dataframe()from hdmf import Container, MultiContainerInterface
class ExperimentContainer(Container, MultiContainerInterface):
__clsconf__ = {
'attr': 'trials',
'type': Container,
'add': 'add_trial',
'get': 'get_trial'
}
def __init__(self, name):
super().__init__(name=name)
self.trials = []
# Usage
experiment = ExperimentContainer(name='behavioral_experiment')
trial1 = Container(name='trial_001')
experiment.add_trial(trial1)
# Access trial
retrieved_trial = experiment.get_trial('trial_001')Install with Tessl CLI
npx tessl i tessl/pypi-hdmf