Skip to content

hertzforge.io.drive_convert

drive_convert

Drive files for Playback: any supported file in, one native drive file out.

Playback plays a drive the operator made, open loop, on as many drives as the station has, and records every input and every drive. The control service reads one form of drive only, the native drive folder laid out in docs/drive-file.md; every other form is read here, on the analysis side, by the readers the package already has, and written to that folder. The control service checks the folder again before it arms.

  • inspect says what a file holds: each signal that could be a drive, with its name, length, the rate the file states (if any), the unit the file states (if any) and where it is in the file.
  • convert takes the signals chosen, in drive order, and writes a new drive folder; fix_ends writes a corrected copy of a drive folder whose ends are not at rest; read reads a drive folder back, checked.

Forms read (SUFFIXES): delimited text (CSV, TSV, TXT; a time column is optional), MATLAB .mat version 5 and 7.3, NumPy .npy and .npz (never unpickled), HDF5 .h5 and .hdf5 (every 1-D or 2-D numeric dataset), .hforge time histories, universal file dataset 58 time records, Excel .xlsx and WAV; and the time histories of an open project, passed as a mapping of name to object.

Signals. A 1-D array is one signal. A 2-D array offers its columns, samples down the rows, except a single row, which is one signal. A MATLAB 7.3 file's arrays are read in MATLAB's own orientation. A scalar named for a rate (fs, sample_rate, rate_hz, ...) or a step (dt, ...), or a 1-D array named for time, states the file's rate.

Refusals, each by name: a selection the file does not hold or that repeats; signals of unequal length; signals whose rates differ, or a rate the file states that disagrees with the rate given (by more than RATE_TOLERANCE of it; the given rate is then written); no rate at all; a time column whose step is not even; a unit that is not a voltage (V and mV, or any voltage unit, are converted to volts; a signal with no unit is taken as volts and the notes say so); a value that is not a finite number, or one too large for the drive output's 32-bit floating point; a NumPy array of Python objects, which is never unpickled; an output folder that already exists.

Rates. A rate taken from a file is kept to ten significant digits, so a time column's float rounding does not reach the drive file; a rate given is written as given. Nothing is resampled.

Fix ends. The control service refuses a drive whose ends are not at rest: treated as an acceleration and integrated with a plain trapezoid, the record must start and end with zero acceleration, velocity and displacement. fix_ends writes a new folder (the original is untouched) with a half-cosine taper at each end, then subtracts c(t) = a sin(pi t / T) + b sin(2 pi t / T), zero at both ends, with a and b solved so the corrected record ends with zero velocity and zero displacement. The taper, a, b and the change in rms of each channel are recorded in the new sidecar's fixes.

Classes:

Name Description
DriveFileError

A drive that is not written or not read, and why, in words a person

Signal

One signal a file offers as a drive.

Inspection

What inspect found: the source, its signals and notes.

DriveFile

A drive folder as read: values is (samples, channels) float64,

Conversion

What convert wrote: the folder, its sidecar and the notes; with

Functions:

Name Description
inspect

What a file, or a set of a project's objects, holds as drives.

default_folder

Where new drive folders are made: drives in the data folder

write

Write a drive folder from values in hand.

convert

Write the chosen signals, in drive order, as a new drive folder.

read

Read a drive folder, checked as the control service checks it: the

taper_samples

The samples of fix_ends' taper at each end: taper_s, or by

fix_ends

Write a copy of a drive folder whose ends are at rest.

Classes

DriveFileError

Bases: ValueError

A drive that is not written or not read, and why, in words a person can act on.

Signal dataclass

Signal(key: str, name: str, samples: int, rate_hz: float | None, unit: str | None, where: str, problem: str | None = None)

One signal a file offers as a drive.

key is what a selection names; where says where it is in words; rate_hz and unit are what the file states, or None; problem is why it cannot be a drive (its unit), or None.

Methods:

Name Description
as_dict

The signal as the analysis service sends it.

Methods:
as_dict
as_dict() -> dict[str, Any]

The signal as the analysis service sends it.

Inspection dataclass

Inspection(path: str | None, format: str, signals: list[Signal], notes: list[str] = list())

What inspect found: the source, its signals and notes.

Methods:

Name Description
as_dict

The inspection as the analysis service sends it.

Methods:
as_dict
as_dict() -> dict[str, Any]

The inspection as the analysis service sends it.

DriveFile dataclass

DriveFile(folder: str, values: ndarray, rate_hz: float, names: list[str], sidecar: dict[str, Any])

A drive folder as read: values is (samples, channels) float64, volts, and sidecar is drive.json whole.

Conversion dataclass

Conversion(folder: str, sidecar: dict[str, Any], notes: list[str] = list(), fixed: dict[str, Any] | None = None)

What convert wrote: the folder, its sidecar and the notes; with fix_ends, the corrected folder's summary as fixed.

Methods:

Name Description
as_dict

The conversion as the analysis service sends it.

Methods:
as_dict
as_dict() -> dict[str, Any]

The conversion as the analysis service sends it.

Functions:

inspect

inspect(source: str | PathLike | Mapping[str, Any], project_file: str | None = None) -> Inspection

What a file, or a set of a project's objects, holds as drives.

Parameters:

Name Type Description Default
source (str, PathLike or mapping)

The file's path on this computer, or a mapping of name to object (the time histories of an open project).

required
project_file str

The project's file, for objects, as their provenance.

None

Returns:

Type Description
Inspection

Each signal (Signal) and the notes the readers made.

Raises:

Type Description
FileNotFoundError

When there is no such file.

ValueError

When the file cannot be read as a drive, saying why.

default_folder

default_folder() -> str

Where new drive folders are made: drives in the data folder (hertzforge.io.data_folder.data_root), which is local to the computer that runs the test.

Returns:

Type Description
str

The folder's absolute path. It need not exist yet.

write

write(values: ndarray, rate_hz: float, names: Sequence[str], *, source: Mapping[str, Any], out: str | PathLike | None = None, fixes: Sequence[Mapping[str, Any]] = (), channel_sources: Sequence[Any] | None = None, created_utc: str | None = None, tool: str | None = None) -> str

Write a drive folder from values in hand.

Parameters:

Name Type Description Default
values ndarray

(samples, channels), volts, every value finite.

required
rate_hz float

Samples per second.

required
names sequence of str

Each channel's name.

required
source mapping

The sidecar's source: path, sha256, format, selections.

required
out str or PathLike

The folder to make; refused when it exists. A free name under default_folder when omitted.

None
fixes sequence of mapping

The sidecar's fixes.

()
channel_sources sequence

Each channel's source; its name when omitted.

None
created_utc str

The sidecar's fields, the time now and this build when omitted (fixed for a golden folder).

None
tool str

The sidecar's fields, the time now and this build when omitted (fixed for a golden folder).

None

Returns:

Type Description
str

The folder written.

convert

convert(source: str | PathLike | Mapping[str, Any], selections: Sequence[Any], *, rate_hz: float | None = None, out: str | PathLike | None = None, fix_ends: bool = False, taper_s: float | None = None, project_file: str | None = None, created_utc: str | None = None) -> Conversion

Write the chosen signals, in drive order, as a new drive folder.

Parameters:

Name Type Description Default
source (str, PathLike or mapping)

The file's path on this computer, or a mapping of name to object (the time histories of an open project).

required
selections sequence

The drives in order: each a signal's key from inspect, or {"signal": key, "name": text} to name the channel.

required
rate_hz float

The rate; it must agree with any rate the file states. Needed when the file states none.

None
out str or PathLike

The folder to make; refused when it exists. A free name under default_folder when omitted.

None
fix_ends bool

Also write a corrected copy (fix_ends), returned as fixed.

False
taper_s float

The taper of that copy, in seconds.

None
project_file str

The project's file, for objects, as their provenance.

None
created_utc str

The sidecar's time, now when omitted.

None

Returns:

Type Description
Conversion

The folder written, its sidecar and the notes.

Raises:

Type Description
FileNotFoundError

When there is no such file.

ValueError

For every refusal the module docstring lists, by name. Nothing is left on disk then.

read

read(folder: str | PathLike) -> DriveFile

Read a drive folder, checked as the control service checks it: the magic, the size, the SHA-256 its sidecar records, and every value finite.

Parameters:

Name Type Description Default
folder str or PathLike

The drive folder.

required

Returns:

Type Description
DriveFile

Its values, (samples, channels) float64 volts, and its sidecar.

Raises:

Type Description
ValueError

When a check fails, saying which.

taper_samples

taper_samples(samples: int, rate_hz: float, taper_s: float | None = None) -> int

The samples of fix_ends' taper at each end: taper_s, or by default TAPER_FRACTION of the record within TAPER_MIN_S and TAPER_MAX_S; at most half the record.

Parameters:

Name Type Description Default
samples int

The record's length.

required
rate_hz float

Its rate.

required
taper_s float

The taper in seconds.

None

Returns:

Type Description
int

The taper's length in samples.

fix_ends

fix_ends(folder: str | PathLike, *, taper_s: float | None = None, out: str | PathLike | None = None, created_utc: str | None = None) -> dict[str, Any]

Write a copy of a drive folder whose ends are at rest.

Each channel is tapered at both ends with a half-cosine, then c(t) = a sin(pi t / T) + b sin(2 pi t / T) (T the record's length, so c is zero at both ends) is subtracted, a and b solved so the record, integrated as an acceleration with a plain trapezoid, ends with zero velocity and zero displacement. A channel that is zero throughout is left as it is. The original folder is not changed.

Parameters:

Name Type Description Default
folder str or PathLike

The drive folder to correct.

required
taper_s float

The taper at each end, seconds; taper_samples by default.

None
out str or PathLike

The folder to make; refused when it exists. Beside folder, named <name>-fixed, when omitted.

None
created_utc str

The new sidecar's time, now when omitted.

None

Returns:

Type Description
dict

folder, drive (the new sidecar), from (the original folder and its SHA-256), taper_s, taper_samples, and per channel its name, a, b, rms_before, rms_after, and before and after, each start, end, peak, velocity_end, velocity_peak, displacement_end and displacement_peak (volts, volt seconds and volt seconds squared).