HDMF: Hierarchical Data Modeling Framework

5.1.0 · active · verified Thu Apr 16

HDMF (Hierarchical Data Modeling Framework) is a Python package designed for standardizing, reading, and writing hierarchical object data. It provides APIs for defining data models, interacting with various storage backends, and representing data using Python objects. Developed as a core component of the Neurodata Without Borders (NWB) project, HDMF offers a flexible and extensible approach to data modeling for scientific communities. The library is actively maintained, with frequent releases, currently at version 5.1.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple data specification, use a common HDMF data type (DynamicTable), and perform basic HDF5 file I/O operations (write and read) using the `hdmf.backends.hdf5.HDF5IO` backend.

import os
from hdmf.spec import GroupSpec, DatasetSpec, NamespaceBuilder
from hdmf.common import DynamicTable, VectorData
from hdmf.backends.hdf5 import HDF5IO

# 1. Define a custom data type specification
my_dataset_spec = DatasetSpec(name='my_data', doc='An example dataset', dtype='float32')
my_group_spec = GroupSpec(name='MyTypeContainer', doc='A custom data type container', datasets=[my_dataset_spec])

# 2. Create a namespace for your specification
namespace_builder = NamespaceBuilder(
    doc='My Custom HDMF Extension',
    name='my_extension',
    full_name='My Custom Extension',
    version='0.1.0',
    auto_detect_namespace=True
)
# In a real scenario, you would save this to a YAML file and load it
# For quickstart, we'll demonstrate using built-in common types

# 3. Work with common HDMF data types, e.g., DynamicTable
table = DynamicTable(name='example_table', description='An example table of items')
table.add_column('item_name', 'Name of the item', dtype='text')
table.add_column('quantity', 'Quantity of the item', dtype='int')

table.add_row(item_name='Apple', quantity=10)
table.add_row(item_name='Banana', quantity=5)

# 4. Save to HDF5 file
file_name = 'my_hdmf_data.h5'
with HDF5IO(file_name, 'w') as io:
    io.write(table)

print(f"DynamicTable saved to {file_name}")

# 5. Read from HDF5 file
with HDF5IO(file_name, 'r') as io:
    read_table = io.read()

print("\nRead DynamicTable:")
print(read_table)

# Clean up
os.remove(file_name)

view raw JSON →