Skip to content

hertzforge.io.reference_convert

reference_convert

Convert a random specification into the controller's native reference file.

The controller's multiple-output random test runs to a reference: the spectral density matrix (SDM) on its grid of lines, the alarm and abort lines per control, the band and a record of where it came from. It reads that reference from its own file, a folder holding reference.bin (the numbers, little-endian) and reference.json (the description), laid out in docs/reference-file.md. This module writes that file from what the controller does not read itself:

  • a specification .mat file (MATLAB v5 through scipy, v7.3 through h5py): cpsd channels by channels by lines, f the lines in hertz, and optionally warning_upper, warning_lower, abort_upper and abort_lower, channels by lines (from_mat);
  • a .hforge file holding a random Specification (from_hforge).

It also reads the native file back (read), which is how its tests check a round trip.

Mapping onto the controller's grid follows the controller's own importer: no interpolation; a file frequency is placed on line round(f / df) when it is within 1e-9 of the spacing of that line, and refused by name otherwise; grid lines the file does not hold are zero; NaN in a limit means no line.

Convention. The controller holds G = E[X X^H] per line. The source files do not document theirs; the usual scientific-Python estimator gives the conjugate (the Bendat and Piersol form), so conjugate_on_import defaults to True, and the choice is written into the provenance. A test with a known phase pins the sign: a cross term at -phi in the file arrives at +phi.

The controller validates the reference again when it reads it; this module refuses early what it can (shape, finiteness, Hermitian symmetry, positive semi-definiteness, the band) so the message comes with the conversion. A line that is not positive semi-definite can instead be repaired with repair=True, as the controller repairs one, and the repair is recorded.

Classes:

Name Description
Transformation

A transformation matrix with its row and column labels:

NativeReference

The content of a native reference file.

Functions:

Name Description
encode

The bytes of reference.bin for a reference.

decode

A reference from the bytes of reference.bin (provenance empty).

sidecar

The sidecar for a reference, as the controller reads it: a capture

write

Write a reference into folder (created if needed), the binary first

read

Read a reference folder, checking the content hash and that the

build

A reference on the controller's grid from a specification's arrays.

from_mat

Convert a specification .mat file into a native reference folder.

from_hforge

Convert a .hforge file holding a random Specification into a

Classes

Transformation dataclass

Transformation(matrix: ndarray, row_labels: list[str], col_labels: list[str])

A transformation matrix with its row and column labels: (lines, rows, cols) complex, one line or one per grid line.

NativeReference dataclass

NativeReference(sdm: ndarray, df_hz: float, band: tuple[int, int], labels: list[str], unit: str, tolerances: dict[str, ndarray], provenance: dict[str, Any], response_transformation: Transformation | None = None, drive_transformation: Transformation | None = None)

The content of a native reference file.

Attributes:

Name Type Description
sdm ndarray

(lines, n, n) complex, Hermitian per line, E[X X^H].

df_hz float

The line spacing; line k is at k * df_hz.

band tuple of int

The half-open band (lo, hi) in lines.

labels list of str

The control-space labels.

unit str

The control unit (the SDM is this unit squared per hertz).

tolerances dict of str to numpy.ndarray

TOLERANCES by name, each (lines, n), NaN for no line.

provenance dict

How, from what, when and by which version it was made.

Functions:

encode

encode(ref: NativeReference) -> bytes

The bytes of reference.bin for a reference.

decode

decode(data: bytes) -> NativeReference

A reference from the bytes of reference.bin (provenance empty).

sidecar

sidecar(ref: NativeReference, binary: bytes) -> dict[str, Any]

The sidecar for a reference, as the controller reads it: a capture snapshot envelope whose payload describes the binary.

write

write(ref: NativeReference, folder: str | PathLike) -> str

Write a reference into folder (created if needed), the binary first and the sidecar last. Returns the content hash.

read

read(folder: str | PathLike) -> NativeReference

Read a reference folder, checking the content hash and that the sidecar describes the binary.

Raises ValueError when either check fails.

build

build(cpsd: ndarray, frequencies: ndarray, *, df_hz: float, lines: int, labels: list[str], unit: str, limits: dict[str, ndarray] | None = None, conjugate_on_import: bool = True, method: str, source: str, source_sha256: str | None = None, repair: bool = False, created_unix_secs: int | None = None) -> NativeReference

A reference on the controller's grid from a specification's arrays.

Parameters:

Name Type Description Default
cpsd ndarray

(lines in the file, n, n), the file's matrices.

required
frequencies ndarray

The file's lines in hertz.

required
df_hz (float, int)

The controller's grid.

required
lines (float, int)

The controller's grid.

required
labels list of str

One per channel, in the file's order.

required
unit str

The channels' unit; the file cannot state one.

required
limits dict

LIMITS keys, each (lines in the file, n).

None
conjugate_on_import bool

Conjugate every cross term (see the module notes).

True
method str

For the provenance.

required
source str

For the provenance.

required
source_sha256 str

For the provenance.

required
repair bool

Repair a line that is not positive semi-definite instead of refusing it: its negative eigenvalues are set to zero and its diagonal is rescaled to the specified PSDs (the controller's repair, at any depth), and the change is recorded in the provenance. Off by default, so a user sees the refusal before accepting a changed reference.

False
created_unix_secs int

The time to record; now when not given.

None

Raises:

Type Description
ValueError

For a shape, a frequency off the grid, a value that is not finite, a matrix that is not Hermitian or not positive semi-definite, or a channel with no level inside the band; each message says which.

from_mat

from_mat(path: str | PathLike, folder: str | PathLike, *, df_hz: float, lines: int, labels: list[str] | None = None, unit: str = 'g', conjugate_on_import: bool = True, repair: bool = False, created_unix_secs: int | None = None) -> str

Convert a specification .mat file into a native reference folder.

The file holds cpsd (channels by channels by lines), f (the lines in hertz) and optionally the four limits (channels by lines), the layout's arrays transposed from the .npz form. Returns the content hash.

Raises ValueError for a missing or unknown variable or any refusal of build, and TypeError for a v7.3 variable that is not a plain array.

from_hforge

from_hforge(path: str | PathLike, folder: str | PathLike, *, df_hz: float, lines: int, unit: str | None = None, conjugate_on_import: bool = True, repair: bool = False, created_unix_secs: int | None = None) -> str

Convert a .hforge file holding a random Specification into a native reference folder. Its matrix is formed as the controller's specification writer forms it (pairs not held are zero), its values are taken as stored, and its channel names become the labels. Returns the content hash.

Raises TypeError when the file holds something other than a specification, and ValueError for any refusal of build.