Skip to content

hertzforge.io

io

File import and export.

Each importer and exporter is added to a registry here, so a new format is added without changing existing code.

Every object that can be imported can also be saved, though not always in the format it came from. .hforge (HDF5) is the native format and the one Save and Load use, and older .vdyn files open the same way; it keeps everything, including units, and so does .mat, which is the same layout in MATLAB's container. The other formats are for exchanging data with other tools, and each loses whatever it has no field for; see each module.

Classes:

Name Description
ImportNote

A reader's note about a file it imported: what it read differently

Functions:

Name Description
export_file

Write obj to a file in a format chosen by name or by suffix.

register_exporter

Add a format to the exporters.

load

Load a .hforge or .vdyn file: the object it holds, or a whole test.

save

Save one Hertzforge object to a .hforge (HDF5) file.

save_test

Save a whole test, every named object, to one .hforge file.

from_sep005

Convert SEP 005 timeseries to TimeHistory objects.

register_importer

Add a format to the importers. Registered importers are tried in

importers

Every format Hertzforge can read, in the order they are

project_type_of

The kind of project a file records itself as a run of, or None.

known_format

The name of a file's format when Hertzforge knows it, or

format_of

The name of a file's format, as a data index prints it.

import_file

Import a file, returning the Hertzforge object it contains.

Classes

ImportNote

Bases: UserWarning

A reader's note about a file it imported: what it read differently from how the file wrote it, and why.

Importer dataclass

Importer(name: str, description: str, sniff: Callable, load: Callable, project_type: Callable | None = None)

One format Hertzforge can read.

sniff(path) says whether a file is in this format, from its content where the content identifies the format, and load(path) returns the object, the dict of objects, or the whole Project the file holds. project_type(path), for a format that records one, gives the kind of project the file is a run of.

Functions:

export_file

export_file(obj: Any, path: str | PathLike, format: str | None = None, unit_system: UnitSystem | None = None, **kwargs: Any) -> None

Write obj to a file in a format chosen by name or by suffix.

unit_system is the system to write in; without one the stored values are written as they are. When no exporter can write the object to that format, the ValueError lists the formats it can be written as, since the fix is nearly always a different format. A ValueError is also raised when a format that writes DOFs as node numbers (UNV, ADF functions and time histories, Exodus, the sdynpy data array) is asked to write modal coordinates. A leading ~ in the path is expanded.

Parameters:

Name Type Description Default
obj object

What to write: a geometry, data, shapes, a project, and so on.

required
path str or PathLike

Where; the suffix picks the format when format is not given.

required
format str

An exporter by name (hertzforge.io.exporters), whatever the suffix says.

None
unit_system UnitSystem

The units to write in; the stored values as they are when omitted.

None
**kwargs Any

Passed to the format's writer, e.g. an ESCDF file's created_by.

{}

Returns:

Type Description
None

register_exporter

register_exporter(name: str, description: str, suffix: str, handles: Callable, save: Callable) -> None

Add a format to the exporters.

When export_file chooses by suffix, the first registered exporter that can write the object and matches the suffix is used, so of two exporters with one suffix the one registered first is the default.

load

load(path: str | PathLike, progress: Callable[[int, int], None] | None = None) -> Any

Load a .hforge or .vdyn file: the object it holds, or a whole test.

The stamp inside the file decides how it is read, not the suffix.

For a whole test, progress is called as (objects loaded, objects in the file), once at the start with 0 and once per object, because a large project can take minutes to load and only the reader knows how far along it is. For a single-object file it is not called, since loading one object is a single step.

Parameters:

Name Type Description Default
path str or PathLike

The .hforge or .vdyn file.

required
progress callable

Called as progress(loaded, total), once up front and once per object.

None

Returns:

Type Description
object

The object the file holds, or a Project for a whole test.

Raises:

Type Description
ValueError

When the file has neither the .hforge nor the .vdyn stamp, was written with a newer schema, or holds nothing this reader recognizes.

save

save(obj: Any, path: str | PathLike, compress: bool = False) -> None

Save one Hertzforge object to a .hforge (HDF5) file.

Raises TypeError for an object of a kind the format does not hold, and ValueError for a .vdyn path.

Parameters:

Name Type Description Default
obj object

The object to save.

required
path str or PathLike

Where; .hforge is added when the suffix is not there.

required
compress bool

Write the large arrays with gzip (compressed), for archiving.

False

Returns:

Type Description
None

save_test

save_test(path: str | PathLike, name: str, objects: Mapping[str, Any], active_geometry: str | None = None, project_type: str | None = None, object_groups: Sequence[Mapping[str, Any]] | None = None, provenance: Mapping[str, Any] | None = None, data_index: Sequence[Mapping[str, Any]] | None = None, compress: bool = False, projections: Sequence[Mapping[str, Any]] | None = None, references: Mapping[str, Reference] | None = None) -> None

Save a whole test, every named object, to one .hforge file.

Objects go in numbered groups with the name as an attribute, so a name may contain anything h5py would read as a path. objects maps each name to its object, in order. object_groups is the object groups, each a mapping with its members (object names); provenance records how each derived object was computed. Both are stored as JSON. data_index is the project's data index, its entries as plain values (DataIndex.records), stored as JSON in a data_index dataset at the root when there are any. .hforge is added to path when it is not there, and a .vdyn path raises ValueError. compress writes the large arrays with gzip (compressed), for archiving.

references are the objects written as references to other files rather than copied, by name (Reference): a name in objects is written as its reference in its place, and any other is written after the objects, as a reference kept for an object that is not loaded. A file with any reference is stamped schema 2, and one without, schema 1.

from_sep005

from_sep005(timeseries: dict[str, Any] | list[dict[str, Any]]) -> Any

Convert SEP 005 timeseries to TimeHistory objects.

history = hertzforge.from_sep005({'data': y, 'fs': 256.0,
                                      'name': 'run 4',
                                      'unit_str': 'm/s²'})

One dict returns one TimeHistory. A list, the standard's form for several series, returns {name: TimeHistory}, with a repeated name numbered 'name (2)' and so on, as in the project tree. The channels are named by channel_name, or numbered from 1 without it, and the series name becomes the comment.

A unit_str entry that parses is declared on its channel, so the values are converted to SI as define_units converts them, since the producer stated the unit. One that does not parse leaves that channel's values as they are and keeps the unit string in dimension_hint, unless the series quantity letter has already put a dimension there. Values are never scaled by a guess.

Raises ValueError, with the reason, for a series with no data, data with more than two dimensions, neither fs nor time, a time vector of the wrong length, or a channel_name list that does not match the channel count.

Parameters:

Name Type Description Default
timeseries dict or list of dict

One SEP 005 series, or a list of them.

required

Returns:

Type Description
TimeHistory or dict of str to TimeHistory

One for one dict; for a list, every series by name.

register_importer

register_importer(name: str, description: str, sniff: Callable, load: Callable, project_type: Callable | None = None) -> None

Add a format to the importers. Registered importers are tried in order, so one added later is tried last.

Parameters:

Name Type Description Default
name str

The importer's name, which import_file(format=) takes.

required
description str

What the format is, as a file dialog lists it.

required
sniff callable

sniff(path) -> bool: whether a file is this format.

required
load callable

load(path, **kwargs): the file read into objects.

required
project_type callable

project_type(path) -> str or None: the project type the file declares, for a format that carries one.

None

Returns:

Type Description
None

importers

importers() -> list[Importer]

Every format Hertzforge can read, in the order they are tried.

project_type_of

project_type_of(path: str | PathLike) -> str | None

The kind of project a file records itself as a run of, or None.

A controller's own save records whether it was a modal test or a random vibration run, and this reads it, before or after the file is imported. Anything else (a geometry, a photo, a file no importer recognizes, or one that cannot be read) gives None rather than raising, because a file with no project type is the ordinary case, not an error.

known_format

known_format(path: str | PathLike) -> str | None

The name of a file's format when Hertzforge knows it, or None.

Known are a Hertzforge file, a .vdyn file, a document the record carries beside its data, named by its kind from its suffix, and any file an importer recognizes, named by that importer's description. A sniffer that trips over the file is not its format, so this does not raise for a readable path. format_of is this with a name for the rest.

format_of

format_of(path: str | PathLike) -> str

The name of a file's format, as a data index prints it.

A .hforge or .vdyn file is named as one. A document the record carries beside its data (a settings list, a log, a scanned procedure) is named by its kind, from its suffix. Any other file an importer recognizes is named by that importer's description ('Rattlesnake controller output (.nc4)'), and an unknown file by its suffix (known_format says which were known). A sniffer that trips over the file is not its format, so this does not raise for a readable path.

identify_runs

identify_runs(path: str, result: Any) -> Any

Give each time history result holds the run identity its file's name says, and return result.

The name is the one place a run's axis, number, level and start often are, and it is not kept after the import, so it is read here (core.runs.read_name), with a Rattlesnake run's environment names beside it. A name that says nothing gives nothing, and the history is left as it was. A field the names give two ways, or a level without a sign, is not read: an ImportNote names the file and the field, to be entered. A history that already carries an identity (a reader's own) keeps it.

import_file

import_file(path: str | PathLike, format: str | None = None, progress: Any | None = None, **kwargs: Any) -> Any

Import a file, returning the Hertzforge object it contains.

Units may be declared here (for example length_unit='m') for sources that do not record them; without a declaration the object imports without units, holding the file's values as they are until define_units() is called. format chooses an importer by name instead of by recognizing the file; a .hforge or .vdyn file is always read by the native loader, and a control run's folder (or the run.json in it) is read as the project file it converts to, converting it first when it has none or the run has changed since (run_convert.converted). progress is a (done, total) callable, used where the reader can count (a project file's objects, a Rattlesnake run's samples) and ignored where it cannot: a UFF file is read in one parse with no partial progress to report. A leading ~ in the path is expanded.

Raises FileNotFoundError when there is no file at path, IsADirectoryError for a folder that is not a run folder, and ValueError for an unknown format or a file no importer recognizes (the message says what the file appears to be, where that can be told).

Parameters:

Name Type Description Default
path str or PathLike

The file to read.

required
format str

An importer by name, rather than the one the file is recognized as.

None
progress callable

Called as progress(done, total) where the reader can count.

None
**kwargs Any

Passed to the importer: declared units (length_unit='m'), a window of a long run (start=, stop=), channels, and so on.

{}

Returns:

Type Description
object

The object the file holds; a dict of them by name for a file that holds several, and a Project for a project file.