Skip to content

hertzforge.io.escdf

escdf

Read and write the Engineering Sciences Common Data Format (ESCDF) by its specification files.

ESCDF (its reference implementation is on GitHub, BSD-3-Clause) is one HDF5 file. Metadata groups are at the root (a geometry, a channel table, a parameter set), and an activities group holds one group per test or analysis, with the results it produced and a parameters list naming the metadata it links to. Each dataset is a group with the name of its specification, a descriptive name and a version, and has one HDF5 dataset per property, each with its type. The types are defined in plain-text specification files, one per type, and those files are the standard: a reader that parses them reads every type they define, and a new type needs only a new file. The files are included as package data (escdf_specifications/, with their license), and this module parses them by the grammar the reference implementation's documentation states.

This module is the generic layer. Specification is a parsed file; Dataset is one group's values against its specification, validated as the reference validates (every required property present, every shared dimension the same size wherever it appears, one alternative of every either-or group, every enumeration and pattern honored); File is the whole file with its activities. read and write move a File to and from disk with h5py. Converting these to and from this package's objects is done in escdf_objects.

The on-disk conventions, as measured from files the reference wrote: strings are variable-length UTF-8, and a scalar string is a zero-dimensional dataset; bytes are variable-length uint8; a variable_length property is a one-dimensional dataset of variable-length rows of its type; numbers are the type the specification names, where u8 means eight bytes (uint64) and c16 is complex128; every property dataset has a data_type attribute naming that type; every group has _specification_name, _descriptive_name and _version (three integers); an activity group has activity_name and activity_date (ISO 8601, UTC) and a parameters string dataset of the metadata names it links to; the file has created_by and created_date. A choice group is written as whichever alternative was chosen, under the shared property name, and read back by the shape and type found.

Classes:

Name Description
Property

One property of a specification: its name, type, shape and options.

Specification

One parsed specification file.

Dataset

One dataset: a group's values against its specification.

Activity

One activity: a test or an analysis, its results and the metadata

File

A whole file: who made it and when, the metadata at its root, and

Functions:

Name Description
valid_identifier

A name the format accepts for a group: ASCII letters, digits and

specifications

Every type the included specification files define, by name, each

write

Write a File as an ESCDF file, validating it first.

read

Read a whole ESCDF file: every dataset against its specification,

Classes

Property dataclass

Property(name: str, dtype: str, shape: tuple[str | int, ...] = (), optional: bool = False, variable_length: bool = False, enum: str | None = None, regex: str | None = None, choice: tuple[str, str] | None = None)

One property of a specification: its name, type, shape and options.

shape is the specification's own words: named dimensions ('num_nodes'), literal sizes (3), or () for a scalar. choice is (group, alternative) for a property in an either-or group, where exactly one alternative of the group is written.

Attributes:

Name Type Description
key str

The name of the dataset on disk: the property name, which the

Attributes
key property
key: str

The name of the dataset on disk: the property name, which the alternatives of a choice group share.

Specification dataclass

Specification(name: str, version: tuple[int, int, int], extends: str | None, properties: list[Property] = list(), enumerations: dict[str, list[str]] = dict(), doc: str = '', _parents: dict[str, Specification] = dict())

One parsed specification file.

Attributes: name: The type's name, the file's first word. version: Three integers from the first line's 'vX.Y.Z'. extends: The type this one inherits from, or None. properties: This file's own properties, in order. enumerations: Each enumeration's allowed values. doc: The prose between the header and the properties block.

Methods:

Name Description
all_properties

Every property, inherited ones first. A property this type

all_enumerations

Every enumeration this type can use, inherited ones included. One

is_a

Whether this type is name or extends it.

Methods:
all_properties
all_properties() -> list[Property]

Every property, inherited ones first. A property this type redefines replaces the parent's of the same name (as response_spectrum redefines data_type with a narrower enumeration).

all_enumerations
all_enumerations() -> dict[str, list[str]]

Every enumeration this type can use, inherited ones included. One this type redefines replaces the parent's of the same name.

is_a
is_a(name: str) -> bool

Whether this type is name or extends it.

Dataset dataclass

Dataset(name: str, kind: str, descriptive_name: str = '', values: dict[str, Any] = dict(), version: tuple[int, int, int] | None = None, extras: dict[str, Any] = dict())

One dataset: a group's values against its specification.

Attributes: name: The group's name, a valid identifier. kind: The specification's name ('geometry', 'data', ...). descriptive_name: Free text, the reference's _descriptive_name. values: Property name to value. A string property is a str or an array of str; a bytes property a list of uint8 arrays; a variable-length property a list of arrays; numbers arrays. version: The specification version the group claims, or None to write the vendored specification's own. extras: Datasets in the group the specification does not name, kept as read so a re-export loses nothing.

Methods:

Name Description
problems

Why this dataset does not validate against its specification,

Attributes:

Name Type Description
specification Specification | None

The included specification for kind, or None for a type no

Attributes
specification property
specification: Specification | None

The included specification for kind, or None for a type no included file defines, whose dataset keeps everything in extras.

Methods:
problems
problems() -> list[str]

Why this dataset does not validate against its specification, as the reference would refuse it: a required property missing, a choice group with no alternative or two chosen, a shared dimension with two sizes, a value its type cannot hold, an enumeration or pattern not honored. Empty when valid. A type no specification defines may hold only extras, so it is a problem only when it has values.

Returns:

Type Description
list of str

Activity dataclass

Activity(name: str, descriptive_name: str = '', date: datetime | None = None, links: list[str] = list(), data: dict[str, Dataset] = dict())

One activity: a test or an analysis, its results and the metadata it links to.

File dataclass

File(created_by: str = '', created_date: datetime | None = None, metadata: dict[str, Dataset] = dict(), activities: dict[str, Activity] = dict())

A whole file: who made it and when, the metadata at its root, and its activities.

Methods:

Name Description
problems

Every dataset's problems, each prefixed with where it is, plus

Methods:
problems
problems() -> list[str]

Every dataset's problems, each prefixed with where it is, plus each link that names no metadata and each known type in an activity that is not an activity result.

Functions:

valid_identifier

valid_identifier(name: str, prefix: str = 'item_') -> str

A name the format accepts for a group: ASCII letters, digits and underscores, starting with a letter.

Whitespace becomes underscores, other characters are dropped, and prefix is put in front when what is left does not start with a letter. This is the reference implementation's repair, so a name repaired here is the name it would give.

Parameters:

Name Type Description Default
name str

Any text.

required
prefix str

What to put in front when the repaired name does not start with a letter (it starts with a digit or an underscore, or is empty).

'item_'

Returns:

Type Description
str

parse_specification

parse_specification(text: str) -> Specification

A specification file's text, parsed by the documented grammar.

The first line is 'name - vX.Y.Z', underlined; an 'extends: other' line names the parent; prose follows until a 'properties' header; each property line is 'name - type [- shape [- options]]', the fields split on ' - ', the shape comma-separated names or numbers or the word 'scalar', the options comma-separated from 'optional', 'variable_length', 'enum:', 'regex:' and 'or::'; an 'enumerations' block lists 'name - a, b, c'; a 'notes' or 'Examples' block ends it.

Parameters:

Name Type Description Default
text str

The file's contents.

required

Returns:

Type Description
Specification

specifications

specifications() -> dict[str, Specification]

Every type the included specification files define, by name, each with its parent types resolved.

Returns:

Type Description
dict of str to Specification

write

write(file: File, path: str | PathLike) -> None

Write a File as an ESCDF file, validating it first.

Parameters:

Name Type Description Default
file File

What to write; file.problems() must be empty.

required
path path - like

The file to write; replaced if it exists.

required

Raises:

Type Description
ValueError

Listing the problems, when file.problems() is not empty. Nothing is written.

read

read(path: str | PathLike) -> File

Read a whole ESCDF file: every dataset against its specification, with unknown types and properties no specification names kept as they are.

Parameters:

Name Type Description Default
path path - like

The file.

required

Returns:

Type Description
File

sniff

sniff(path: str | PathLike) -> bool

True for an HDF5 file with an activities group and a created_by attribute.