ndx-hed extension for NWB
Neurodata Without Borders (NWB) is a data standard for organizing neurophysiology data. NWB is used extensively as the data representation for single cell and animal recordings as well as human neuroimaging modalities such as IEEG. NWB organizes all of the data from one recording session into a single file.
HED (Hierarchical Event Descriptors) is a system of standardized vocabularies and supporting tools that allows fine-grained annotation of data. HED annotations can now be used in NWB to provide HED annotations for any NWB dynamic table.
The HED integration for NWB gives a quickstart, examples and the API reference. The NWB annotation quickstart gives a brief overview of how to get started.
Installation
Python:
pip install -U ndx-hed
Matlab: The Matlab extension is under development.
Quick start
from pynwb import NWBFile
from ndx_hed import HedLabMetaData, HedTags
from datetime import datetime
# Create NWB file with HED metadata
nwbfile = NWBFile(
session_description="Example session with HED annotations",
identifier="example_session_001",
session_start_time=datetime.now(),
)
# Add HED schema metadata (required). A library schema merged with its partner is
# "8.5.0,score_2.2.0"; schemas in separate namespaces are a list, ["8.5.0", "sc:score_2.2.0"].
hed_metadata = HedLabMetaData(hed_schema_version="8.5.0")
nwbfile.add_lab_meta_data(hed_metadata)
# Add HED column to trials table
nwbfile.add_trial_column(name="HED", col_cls=HedTags, data=[], description="HED annotations for trials")
# Add trials with HED annotations
nwbfile.add_trial(start_time=0.0, stop_time=1.0, HED="Experimental-trial, (Sensory-event, Visual-presentation)")
Main classes
The ndx-hed extension provides four classes:
| Class | Purpose | Use Cases |
|---|---|---|
| HedLabMetaData | HED schema specification and lab-specific definitions | Required for all HED validation |
| HedTags | Row-specific HED annotations | Row-specific tags in any DynamicTable |
| HedValueVector | Column-wide HED templates | Shared annotations with value placeholders (#) |
| HedDurationVector | The EventsTable duration column with a HED template | Durations whose value is part of the annotation (#) |
HED annotation rules
HED annotations in NWB follow a small set of rules so that a table's HED can be assembled and validated consistently:
- A
HedTagscolumn must be namedHED. Because column names within a table are unique, aDynamicTabletherefore holds at most oneHedTagscolumn. - A
HedTagscolumn inside aMeaningsTablesupplies categorical (per-value) HED for the annotated column. In any otherDynamicTableit supplies per-row HED for that table. - A
HedValueVectorcolumn carries one HED template with a single#placeholder that applies to every value in the column. It may have any name and is identified by its type. - A
HedValueVectormay not appear in aMeaningsTable(a value template has no meaning for categorical values);HedNWBValidator.validate_tablereports one asMEANINGS_VALUE_VECTOR_INVALID. - The
durationcolumn of anEventsTablecarries HED as aHedDurationVector: the coreDurationVectorDatawith a template, validated as aHedValueVectoris (for example(Duration/# s, (Sensory-event))). Anonsetcolumn carries no HED, anddurationcannot be categorical.
Examples
The examples/ directory contains comprehensive runnable examples:
- 01_basic_hed_classes.py - Introduction to the three main classes
- 02_trials_with_hed.py - Adding HED to trials table
- 03_events_table_integration.py - Integration with the PyNWB core EventsTable
- 04_bids_conversion.py - Converting BIDS events to NWB with HED
- 05_hed_validation.py - Comprehensive validation examples
- 06_complete_workflow.py - End-to-end workflow demonstration
- 07_hed_definitions.py - Custom HED definitions usage
Run any example:
cd examples
python 01_basic_hed_classes.py
Or run all examples:
cd examples
python run_all_examples.py
Integration with NWB events
The ndx-hed extension works seamlessly with the PyNWB core EventsTable (NWBEP001, PyNWB >= 4.2.0) to provide comprehensive event annotation capabilities. HED annotations can be incorporated in three ways:
- Direct HED column - Event-specific annotations
- HedValueVector columns - Shared annotations with value placeholders
- Categorical columns with MeaningsTable - Category-based annotations
See examples/03_events_table_integration.py for detailed demonstrations.
BIDS compatibility
BIDS (Brain Imaging Data Structure) is a data standard for organizing neuroimaging and behavioral data from an entire experiment. The standard uses JSON files called "sidecars" to store metadata associated with its tabular files.
The ndx-hed extension provides utilities to convert between BIDS events files and NWB EventsTable format:
from ndx_hed.utils.bids2nwb import extract_meanings, get_events_table, get_bids_tabular
import pandas as pd
import json
# Convert BIDS to EventsTable
bids_events_file_path = "Your_events_path_here.tsv"
bids_sidecar_file_path = "Your_json_sidecar_path_here.json"
events_df = pd.read_csv(bids, sep="\t")
json_data = json.load(bids_sidecar_file_path)
meanings = extract_meanings(bids_sidecar_file_path)
events_table = get_events_table("task_events", "Task events", events_df, meanings)
# Convert EventsTable to BIDS
bids_df, sidecar_dict = get_bids_tabular(events_table)
See examples/04_bids_conversion.py for complete examples.
HED validation
Creating HED annotations for NWB data and saving these annotations as part of an NWBFile does not mean the annotations are valid. HED validation is performed to ensure they conform to the HED schema:
from ndx_hed import HedLabMetaData
from ndx_hed.utils.hed_nwb_validator import HedNWBValidator
# Create validator and validate entire file
hed_metadata = HedLabMetaData(hed_schema_version="8.5.0")
validator = HedNWBValidator(hed_metadata)
# Assume nwbfile has already been created
issues = validator.validate_file(nwbfile)
if not issues:
print("All HED annotations are valid!")
validate_file validates every DynamicTable (except MeaningsTable) by assembling each row's full HED annotation (its per-row HED column, its categorical HED from any MeaningsTable, and its value templates) and validating with the HED tools. Tables that are time-anchored (have an onset column, such as an EventsTable) are validated as a timeline, so temporal HED (onset/offset/duration scopes across rows) is checked; other tables are validated per row. A MeaningsTable is validated as part of the table whose column it annotates, not on its own.
See examples/05_hed_validation.py for comprehensive validation examples.
Additional resources
Contributing
Contributions are welcome! Feel free to submit issues or pull requests.
Metadata
Release files for ndx-hed 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ndx_hed-0.3.0.tar.gz | 103.0 kB | Without provenance | Details for ndx_hed-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ndx_hed-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for ndx_hed-0.3.0-py3-none-any.whl |
Total release size: 131.3 kB
Release files / ndx_hed-0.3.0.tar.gz
| Download URL | ndx_hed-0.3.0.tar.gz |
|---|---|
| Size | 103.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e45ebee8e1fd6a8947ca0f1e35743d6acfe572fd74e7b60c61e016d800efae7e
|
|
BLAKE2b-256 checksum How to use checksums |
499c4cf90e8b1295335bca64edc68e31329ecdd8acd4761ad0e05edc33c8609b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.9
|
Release files / ndx_hed-0.3.0-py3-none-any.whl
| Download URL | ndx_hed-0.3.0-py3-none-any.whl |
|---|---|
| Size | 28.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ccbfc2d99afa65d61150433cdba94e041ef299e9387ed51a7155342be5072a37
|
|
BLAKE2b-256 checksum How to use checksums |
c19c4130db924cb2fdbcfd54610f09d21e75619dec209884870a0d364433162f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.9
|
Security for ndx-hed 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.