Storage Specification

hdmf-zarr currently uses the Zarr LocalStore, which uses directories and files on a standard file system to serialize data.

Format Mapping

Here we describe the mapping of HDMF primitives (e.g., Groups, Datasets, Attributes, Links, etc.) used by the HDMF schema language to Zarr storage primitives. HDMF data modeling primitives were originally designed with HDF5 in mind. However, Zarr uses very similar primitives, and as such the high-level mapping between HDMF schema and Zarr storage is overall fairly simple. The main complication is that Zarr does not support links and references (see Zarr issue #389) and as such have to implemented by hdmf-zarr.

Mapping of groups

NWB Primitive

Zarr Primitive

Group

Group

Dataset

Dataset

Attribute

Attribute

Link

Stored as JSON formatted Attributes

Mapping of HDMF specification language keys

Here we describe the mapping of keys from the HDMF specification language to Zarr storage objects:

Groups

Mapping of groups

NWB Key

Zarr

name

Name of the Group in Zarr

doc

Zarr attribute doc on the Zarr group

groups

Zarr groups within the Zarr group

datasets

Zarr datasets within the Zarr group

attributes

Zarr attributes on the Zarr group

links

Stored as JSON formatted attributes on the Zarr Group

quantity

Not mapped; Number of appearances of the group

neurodata_type

Attribute neurodata_type on the Zarr Group

namespace ID

Attribute namespace on the Zarr Group

object ID

Attribute object_id on the Zarr Group

Reserved groups

The ZarrIO backend typically caches the schema used to create a file in the group /specifications (see also Caching format specifications)

Datasets

Mapping of datasets

HDMF Specification Key

Zarr

name

Name of the dataset in Zarr

doc

Zarr attribute doc on the Zarr dataset

dtype

Data type of the Zarr dataset (see dtype mappings table) and stored in reserved attributes

shape

Shape of the Zarr dataset if the shape is fixed, otherwise shape defines the maxshape

dims

Not mapped

attributes

Zarr attributes on the Zarr dataset

quantity

Not mapped; Number of appearances of the dataset

neurodata_type

Attribute neurodata_type on the Zarr dataset

namespace ID

Attribute namespace on the Zarr dataset

object ID

Attribute object_id on the Zarr dataset

Note

  • TODO Update mapping of dims

Attributes

Mapping of attributes

HDMF Specification Key

Zarr

name

Name of the attribute in Zarr

doc

Not mapped; Stored in schema only

dtype

Data type of the Zarr attribute

shape

Shape of the Zarr attribute if the shape is fixed, otherwise shape defines the maxshape

dims

Not mapped; Reflected by the shape of the attribute data

required

Not mapped; Stored in schema only

value

Data value of the attribute

Note

Attributes are stored as JSON documents in Zarr (using the LocalStore). As such, all attributes must be JSON serializable. The ZarrIO backend attempts to cast types (e.g., numpy arrays) to JSON serializable types as much as possible, but not all possible types may be supported. Float NaN, Infinity, and -Infinity values are written as the bare tokens NaN, Infinity, and -Infinity, following zarr-python. These tokens are a zarr-python extension to JSON rather than part of the JSON standard, so a store containing them is readable by Python’s json module and rejected by strict JSON parsers. Writing them this way keeps a float distinct from a string holding the same text, and keeps hdmf-zarr consistent with any other tool reading the store through zarr-python.

Reserved attributes

The ZarrIO backend defines a set of reserved attribute names defined in __reserve_attribute. These reserved attributes are used to implement functionality (e.g., links and object references, which are not natively supported by Zarr) and may be added on any Group or Dataset in the file.

Reserved Attribute Name

Usage

_LINKS

Attribute on Groups used to store links. See Links.

_DTYPE

Attribute on Datasets used to specify the data type. Set to "object_reference" for reference datasets. See Object References.

_REFERENCE_FIELDS

Attribute on compound Datasets listing field names that contain object references.

Note

Files written with hdmf-zarr < 0.14 (Zarr v2) use the attribute names zarr_link and zarr_dtype instead. These are still recognized on read so that legacy files can be opened with ZarrV2IO. See Legacy Zarr v2 Convention for the old definitions.

In addition, the following reserved attributes are added to the root Group of the file only:

Reserved Attribute Name

Usage

.specloc

Attribute storing the path to the Group where the scheme for the file are cached. See SPEC_LOC_ATTR

Object References

Object reference behave much the same way as Links, with the key difference that they are stored as part of datasets or attributes. This approach allows for storage of large collections of references as values of multi-dimensional arrays (i.e., the data type of the array is a reference type).

Storing object references in Datasets

To identify that a dataset contains object references, the reserved attribute _DTYPE is set to 'object_reference' (see also Reserved attributes). In this way, we can unambiguously determine if a dataset stores references that need to be resolved.

Each element of a reference dataset is stored as a plain target path string (e.g., "/general/extracellular_ephys/electrodes") in a variable-length string (StringDType) array. Since _DTYPE = "object_reference" already marks the dataset as containing references, there is no need to wrap each value in a dict. The source defaults to "." (same file). For future cross-file references, the format can be extended to store dicts instead of plain strings.

Object references are created via _create_ref() and resolved via resolve_ref().

Storing object references in Attributes

Object references are stored in attributes as dicts with a _REFERENCE wrapper key containing a dict with source and path keys:

For example in NWB, the attribute ElectricalSeries.electrodes.table would be defined as follows:

"table": {
    "_REFERENCE": {
        "path": "/general/extracellular_ephys/electrodes",
        "source": "."
    }
}

dtype mappings

The mappings of data types is as follows

dtype spec value

storage type

size

  • “float”

  • “float32”

single precision floating point

32 bit

  • “double”

  • “float64”

double precision floating point

64 bit

  • “long”

  • “int64”

signed 64 bit integer

64 bit

  • “int”

  • “int32”

signed 32 bit integer

32 bit

  • “int16”

signed 16 bit integer

16 bit

  • “int8”

signed 8 bit integer

8 bit

  • “uint32”

unsigned 32 bit integer

32 bit

  • “uint16”

unsigned 16 bit integer

16 bit

  • “uint8”

unsigned 8 bit integer

8 bit

  • “bool”

boolean

8 bit

  • “text”

  • “utf”

  • “utf8”

  • “utf-8”

unicode

variable

  • “ascii”

  • “bytes”

unicode. Zarr v3 has no byte-string data type, so these are stored as UTF-8 text and read back as str. Writing bytes that are not valid UTF-8 raises ValueError.

variable

  • “object”

  • a mapping with target_type and reftype: object

Reference to another group or dataset. See Object References

  • compound dtype

Compound data type. Uses zarr v3’s zarr v3 struct data type. Reference fields marked with _REFERENCE_FIELDS attribute.

  • “isodatetime”

unicode ISO 8601 datetime string. For example 2018-09-28T14:43:54.123+02:00

variable

Note

Compound data types use zarr v3’s struct data type, which carries full field information (names and types).

String and reference fields within compound dtypes are stored as fixed-length Unicode strings (FixedLengthUTF32). The string length is dynamically sized to fit the actual data, with a minimum of COMPOUND_DTYPE_MIN_STRING_LENGTH characters. Reference fields store plain target path strings (not JSON dicts).

If a compound dataset contains reference fields, the _REFERENCE_FIELDS attribute lists which field names contain references. For example: _REFERENCE_FIELDS = ["electrode", "group"].

Note

Scalar datasets are stored as zero-dimensional arrays with shape (). The dtype matches the original data type (numeric scalars preserve their native dtype; strings use StringDType). Scalar object references and scalar compound datasets are stored the same way.

Caching format specifications

In practice it is useful to cache the specification a file was created with (including extensions) directly in the Zarr file. Caching the specification in the file ensures that users can access the specification directly if necessary without requiring external resources. For the Zarr backend, caching of the schema is implemented as follows.

The ZarrIO` backend adds the reserved top-level group /specifications in which all format specifications (including extensions) are cached. The default name for this group is defined in DEFAULT_SPEC_LOC_DIR and caching of specifications is implemented in ZarrIO.__cache_spec. The /specifications group contains for each specification namespace a subgroup /specifications/<namespace-name>/<version> in which the specification for a particular version of a namespace are stored (e.g., /specifications/core/2.0.1 in the case of the NWB core namespace at version 2.0.1). The actual specification data is then stored as a JSON string in scalar datasets with a binary, variable-length string data type. The specification of the namespace is stored in /specifications/<namespace-name>/<version>/namespace while additional source files are stored in /specifications/<namespace-name>/<version>/<source-filename>. Here <source-filename> refers to the main name of the source-file without file extension (e.g., the core namespace defines nwb.ephys.yaml as source which would be stored in /specifications/core/2.0.1/nwb.ecephys).

Consolidating Metadata

Zarr allows users to consolidate all metadata for groups and arrays within the given store. By default, every file will consolidate all metadata within into the zarr.json file of the root group. This reduces the number of read operations when retrieving certain metadata in read mode.

Note

When updating a file, the consolidated metadata will also need to be updated via zarr.consolidate_metadata(path) to ensure the consolidated metadata is consistent with the file.

Legacy Zarr v2 Convention

Files written with hdmf-zarr < 0.14 use Zarr v2 and an older attribute convention. These files are read-only in hdmf-zarr >= 0.14 via ZarrV2IO and NWBZarrV2IO, and can be converted to the current convention with convert_to_v3(). The old definitions are recorded here for reference when debugging legacy data.

hdmf-zarr 0.13 and earlier encode object datasets with the pickle codec by default, so most of these files hold at least one pickle-encoded dataset (an electrodes table’s object-reference columns, for instance). Decoding pickle executes arbitrary code, so both reading and converting refuse it and raise UnsafePickleCodecError. Pass allow_pickle=True for a source you trust:

NWBZarrV2IO.read_nwb("old_v2.nwb.zarr", allow_pickle=True)
NWBZarrV2IO.convert_to_v3("old_v2.nwb.zarr", "new_v3.nwb.zarr", allow_pickle=True)

============================  ======================================================================================
Legacy Attribute Name         Usage
============================  ======================================================================================
``zarr_link``                 Attribute on Groups used to store links. Each entry is a dict with ``name``,
                              ``source``, and ``path`` keys. Replaced by ``_LINKS``.
``zarr_dtype``                Attribute on Datasets specifying the data type. Set to ``"object"`` for reference
                              datasets, ``"scalar"`` for scalar datasets, and to a list of ``{"name", "dtype"}``
                              dicts for compound datasets. Replaced by ``_DTYPE``, zero-dimensional arrays for
                              scalars, and the zarr v3 ``struct`` data type together with
                              ``_REFERENCE_FIELDS``.
============================  ======================================================================================

Object references in datasets were stored as dicts with source, path, object_id, and source_object_id keys, written to an object-dtype array (Zarr v2) or as JSON strings. Object references in attributes were stored as a dict with a zarr_dtype key set to "object" and a value key holding the reference dict:

"table": {
    "value": {
        "source": ".",
        "path": "/general/extracellular_ephys/electrodes",
        "object_id": "f6685427-3919-4e06-b195-ccb7ab42f0fa",
        "source_object_id": "6224bb89-578a-4839-b31c-83f11009292c"
    },
    "zarr_dtype": "object"
}

In the current convention this is written as {"_REFERENCE": {"source": ".", "path": "..."}}.