6. Infrastructure and tools

The HED infrastructure includes libraries written in Python, Matlab, and JavaScript that support the use of HED in validation and/or applications. This chapter describes the expected behavior of the HED infrastructure and its integration into other systems such as BIDS and NWB.

In general, tools SHOULD either explicitly call HED validation to assure that the input tag strings are valid or SHOULD make explicit that they assume the HED has already been validated. Most tools will use the latter approach.

See 3.2. Annotation rules for more detailed specifications of HED formats.

See 4. Basic annotation and 5. Advanced annotation for examples and usage.

6.1. Basic tag handling

HED-compliant tools SHOULD be able to handle a HED string in its equivalent forms and using various valid syntax as described in this section.

6.1.1. Tag forms

Warning

HED-compliant tools SHOULD be able to handle tags in long-form, short-form or any valid intermediate-form.

Tools may assume that validated HED tags do not have leading, trailing, or consecutive forward slashes in their names.

In addition to being properly formed, validated HED strings will correspond to terms in the schemas under which they were validated.

Tools treat case as stated in 3.2.3. Tag case-sensitivity.

Tools may assume that the individual tags within validated HED strings have values of the proper form and that the units, if provided, are consistent with any unit classes.

Note: Units are optional; what a value without units means is stated in 3.2.4. Tags that take values.

See 3.2.2. Tag forms for more information on tag forms.

6.1.2. Parentheses and commas

Tools may assume that validated HED strings have no duplicates, empty tags, empty groups (parentheses enclosing only whitespace), or mismatched parentheses.

Grouping with parentheses in HED indicates that the tags are associated. Where possible, parentheses SHOULD be preserved.

Warning

HED-compliant tools SHOULD be able to handle arbitrary correctly nested parentheses and correctly distinguish differences in grouping.

6.1.3. Tag ordering

Any ordering of HED tags and HED tag groups at the same level within a HED tag group is equivalent.

Any ordering of top-level HED tags and HED tag groups in a HED string is equivalent.

Warning

HED-compliant tools SHOULD NOT rely on the order that HED tags appear within a string or group during processing.

6.1.4. Definitions

Warning

HED-compliant tools SHOULD be able to expand, shrink, or remove definitions.

Where definitions may appear is stated in 3.2.8.1. The Definition tag.

6.2. Table-level handling

Dataset formats such as BIDS (Brain Imaging Data Structure) allow users to provide HED tags in multiple places. For example, the timeline tables in BIDS events.tsv files often use local codes to identify event markers and then provide dictionaries called JSON sidecars to map local codes to annotations.

The introduction of definitions and temporal scope has added additional complexity to validation and processing. Instead of being able to validate the HED string for each event individually, HED validators MUST also check the rules of 3.2.10.4. Table-level processing across all rows of a table.

Tools SHOULD make explicit whether they support temporal scope. Tools that support temporal scope SHOULD be able to add scoped event information to the Event-context tag group of the intermediate events upon request.

Tools SHOULD make explicit whether they support insertion of actual events for Delay tag expansions and for the offsets of Duration tags. This information will allow analysts to call HED tools that support these operations to appropriately modify timeline tables as a preamble to processing if the tool does not support these tags.

6.3. HED support of BIDS

BIDS (Brain Imaging Data Structure) is a widely-adopted specification and supporting tools for organizing and describing brain imaging and behavioral data.

6.3.1. BIDS tabular files

A BIDS tabular file is a tab-separated value file with extension .tsv. The file contains a single table with a one-line header containing the column names. The remaining lines in the file represent rows in the table. HED’s rules for such a file are stated in 3.2.10. Tabular files and structures. HED has been incorporated into the BIDS standard as the mechanism for annotating BIDS tabular files. A BIDS tabular file can be optionally accompanied by one or more similarly named .json files (i.e., sidecars) containing metadata for the file.

The most common types of BIDS tabular files are shown in the following table.

Ends with

Time?

A row represents

events.tsv

Yes

Markers on the timeline of another file.

participants.tsv

No

Metadata about one dataset subject.

scans.tsv

No

Metadata about one dataset recording.

beh.tsv

No

A measurement not associated with an onset.

samples.tsv

No

Properties of a particular sample (e.g., tissue).

phenotype/xxx.tsv

No

A measurement from a particular participant.

HED treats the onset column of tables such as the ones in events.tsv files as expressing markers on a timeline. These timeline tables allow Onset, Inset, Offset, and Delay tags in their annotations and receive additional table-level processing to assure that these tags are properly matched.

The tags allowed only in timeline tables are given by the Timeline only column of 3.2.8. Reserved tags.

The following shows an excerpt from a BIDS events.tsv file:

Example: Excerpt from a BIDS events.tsv file.

onset  duration  trial_type  response_time  HED
1.2    0.6       go          1.435          Label/Starting-point, Quiet
5.6    0.6       stop        1.739          n/a

The first two columns in a BIDS events.tsv file are required to be onset and duration, respectively. The onset is the time in seconds of an event marker relative to the start of its corresponding data recording, while the duration represents the duration in seconds of some aspect of the event. The duration column has no HED meaning of its own: it is not a Duration tag and does not end an event process. It contributes to a row’s annotation only through a sidecar entry that annotates it, for example "duration": {"HED": "(Duration/# s, (Sensory-event))"}. The remaining columns in this file are optional.

BIDS reserves an optional column named HED to contain HED strings relevant for the instance represented by that row. In the above example, the first row HED column contains Label/Starting-point, Quiet, meaning that the event that starts at time 1.2 has these particular annotations in addition to annotations contributed by any relevant sidecars. The HED column in the second row contains n/a, indicating that entry should be ignored. In this case only annotations from applicable sidecars are applied.

HED annotations can also be associated with entries in other columns of the file through an associated JSON sidecar as described in 6.3.3. BIDS sidecars.

6.3.2. BIDS timeseries

BIDS also includes another type of file with extension .tsv that represents continuous timeseries data. For example, the motion capture _motion.tsv files give samples from channels of a motion capture apparatus, while physiological data files _physio.tsv and _stim.tsv represent continuous physiological recordings such as cardiac and respiratory signals. These tabular files do not have column headers, but rather use other files to define the column names. BIDS currently does not support HED in these types of files.

6.3.3. BIDS sidecars

BIDS also recommends data dictionaries in the form of JSON sidecars to document the meaning of the data in the events.tsv files. HEDTools supports BIDS dataset format, where event metadata is contained in compatibly-named sidecars. See the example sidecar in Chapter 3 for an explanation of the different sidecar entries.

6.3.4. Annotation assembly

HED tools assemble the annotation for each row of a table from its HED column and from the sidecar entries for its other columns. The procedure and a worked example (an events excerpt, its sidecar, and the assembled annotation) are in 3.2.10.3. Event-level processing.

6.3.5. HED version in BIDS

The HED version is included as the value of the "HEDVersion" key in the dataset_description.json metadata file located at the top level in a BIDS dataset. HEDTools retrieve the appropriate HED schema directly from GitHub or from locally cached versions when needed.

The following example dataset_description.json specifies that HED version 8.5.0 is used for a dataset called “A wonderful experiment”.

Example: BIDS dataset description using HED version 8.5.0.

{
   "Name": "A wonderful experiment",
   "BIDSVersion": "1.10.0",
   "HEDVersion": "8.5.0"
}

It is possible to include library schema in the HED version specification of the dataset_description.json file as shown by the following example:

Example: BIDS dataset description using HED version 8.5.0 and score library 2.2.0.

{
  "Name": "A great experiment",
  "BIDSVersion": "1.10.0",
  "HEDVersion": ["8.5.0", "sc:score_2.2.0"]
}

The version specification indicates that tags from the score library are prefixed with sc: in dataset HED annotations, placing them in the sc namespace.

As stated in 3.1.1. Schema versions, a namespace prefix (such as sc: for the score library in the previous example) is written on the tags of every schema that the version specification lists with a prefix. Schemas listed without a prefix form one merge group, and their tags are written without a prefix. A standard schema may also be given a prefix, as the following example illustrates.

Example: Prefixed standard schema in BIDS dataset description version specification.

{
  "Name": "A great experiment",
  "BIDSVersion": "1.10.0",
  "HEDVersion": ["st:8.5.0", "sc:score_2.2.0"]
}

For this specification tags from the standard schema are prefixed by st: and tags from the score library by sc:; no schema is listed without a prefix, so every tag carries one. The namespace names (sc in the previous example and st in this one) are short strings of ASCII letters (see 3.1.1. Schema versions) chosen by the annotator and are specific to each dataset based on its version specification; the prefix is the name followed by a colon.

Warning

HED-compliant tools MUST be able to handle multiple schemas and prefixed tags.

6.3.6. HED in the BIDS validator

HED provides a JavaScript validator in the hed-javascript repository, which is available as an installable package via npm. The BIDS validator incorporates calls to this package to validate HED tags in BIDS datasets.

6.3.7. HED Python tools

The hedtools package includes input functions that use Pandas data frames to construct internal representations of HED-annotated tables.

HED schema developers generally do initial development of the schema using .mediawiki format. The tools to convert a schema among the MediaWiki, XML, JSON, and TSV formats are in the hed.schema module of the hed-python GitHub repository. A schema in any of these formats is loaded into a HedSchema object, which can be saved in any of them.

6.4. HED support of NWB

NWB (Neurodata Without Borders) is a data standard for neurophysiology data that stores the data of one recording session in a single file. HED annotations are stored in an NWB file with the ndx-hed extension, which is documented in HED integration for NWB. This section describes ndx-hed 0.3.0.

6.4.1. NWB tables

A table in an NWB file is a DynamicTable: a set of named columns that have the same number of rows. HED treats a DynamicTable as a table in the same way that it treats the contents of a BIDS .tsv file, and the column-name rules of 3.2.10. Tabular files and structures apply to it.

An EventsTable is a DynamicTable for events. Its TimestampVectorData column named timestamp is the equivalent of the onset column of a BIDS events.tsv file, and its optional DurationVectorData column named duration is the equivalent of the duration column. Which NWB tables are timeline tables is stated in 3.3.4. Timeline versus descriptor tables.

6.4.2. HED types in NWB

The ndx-hed extension defines four types.

Type

Extends

Holds

HedLabMetaData

LabMetaData

The HED version of the file and, optionally, HED definitions.

HedTags

VectorData

A column named HED that has one HED string for each row. NWB allows at most one per table; NWB checks this, not HED validation.

HedValueVector

VectorData

A column of values with one HED annotation, containing a # placeholder, that applies to every value of the column.

HedDurationVector

DurationVectorData

The duration column of an EventsTable with such an annotation.

A column of categorical values has no HED type of its own. It is a VectorData column that is annotated by a MeaningsTable, which lists the values of the column, and a HedTags column in that MeaningsTable gives the HED annotation of each value.

The following table shows where a BIDS dataset and an NWB file hold the same information.

Information

BIDS

NWB with ndx-hed

HED annotation of a row

HED column of a .tsv file

HedTags column of a DynamicTable

HED annotation of a value column

sidecar entry with a # placeholder

hed attribute of a HedValueVector or HedDurationVector

HED annotation of categorical values

sidecar entry with one annotation for each value

HedTags column of the MeaningsTable that annotates the column

HED version

HEDVersion in dataset_description.json

hed_schema_version attribute of the HedLabMetaData

HED definitions

sidecar entries

definitions attribute of the HedLabMetaData

6.4.3. Annotation assembly in NWB

How HED tools assemble the annotation of each row of an NWB DynamicTable and validate it is not yet finalized. This includes how the HedTags column, the MeaningsTable annotations of categorical columns, and the HedValueVector and HedDurationVector columns are combined, and how column references in NWB annotations are resolved.

6.4.4. HED version in NWB

The hed_schema_version attribute of the HedLabMetaData gives the HED schemas used in the file. An NWB file has at most one HedLabMetaData, which NWB checks, not HED validation. The value is text in one of three forms: one version, several versions of one namespace joined by commas, or a JSON array of versions.

Example: The three forms of the HED version in an NWB file.

8.5.0
8.5.0,score_2.2.0
["8.5.0", "sc:score_2.2.0"]

See 3.1.2. Version combinations for the schemas that may be combined.

6.4.5. HED tools for NWB

The ndx-hed Python package provides the four types for PyNWB and a validator, HedNWBValidator, which assembles the annotations of a table or of every table in a file and validates them with hedtools.