visualdynamics.units¶
units
¶
Unit handling for visualdynamics.
Values whose units are known are stored in SI (m, kg, s, N, Pa, K, ...).
Values imported from a source that does not declare units are stored exactly
as they appear in the file and tagged unknown until the user defines units;
defining units converts them to SI once and records the unit chosen, so a
wrong guess can be corrected without losing anything.
Conversions are affine — si = raw * scale + offset — so offset scales like
degC and degF are handled correctly alongside purely multiplicative units.
pint provides the registry but is kept internal to this module: core objects carry plain numpy arrays plus a dimension tag (a string like 'length', 'acceleration', or a compound expression like 'acceleration/force').
Classes:
| Name | Description |
|---|---|
UnitError |
A unit was wrong: unreadable, or not the dimension asked for. |
UnitsRequired |
Raised when an operation needs units that have not been defined. |
UnitSystem |
A named mapping of dimension -> display unit. |
Functions:
| Name | Description |
|---|---|
unit_registry |
The one pint registry, built on first use. |
normalize_unit |
A unit string as visualdynamics reads it, before pint sees it. |
pretty_unit |
A unit string with real exponents, for showing: 'm/s**2' -> 'm/s²'. |
plain_unit |
The inverse of |
is_compound_unit |
Does it need brackets before something is done to the whole of it? |
parse_dimension |
Parse a dimension expression into (base_dimension, power) parts. |
si_transform |
(scale, offset) such that |
si_factor |
Multiplier converting values in |
to_si |
|
from_si |
SI |
convert |
|
dimension_of |
The visualdynamics base dimension matching a unit string, or None. |
Classes¶
UnitError
¶
Bases: ValueError
A unit was wrong: unreadable, or not the dimension asked for.
A ValueError, because that is what a bad value is, and a named one so the app can tell a unit problem it should explain from a programming error it should not swallow.
UnitsRequired
¶
Bases: UnitError
Raised when an operation needs units that have not been defined.
Source code in src/visualdynamics/units.py
UnitSystem
dataclass
¶
UnitSystem(
name: str,
units: dict = dict(),
base: UnitSystem | None = None,
)
A named mapping of dimension -> display unit.
base is the coherent system this one came from — the same object for
a coherent system, and the parent for one carrying display-only
overrides such as accelerations in g. Exports use it, because no
foreign format can record "g" as a unit.
Methods:
| Name | Description |
|---|---|
unit |
Display unit for a dimension or dimension expression. |
transform |
(scale, offset) converting a display value to SI. |
factor |
SI-per-display-unit scale (offset-free dimensions only). |
from_si |
Convert SI values to this system's display unit for |
to_si |
Convert values from this system's units into SI. |
label |
Display unit text; empty when the dimension is undefined. |
label_text |
Display unit for plain-text output, with real exponents: in/s². |
label_ascii |
Display unit in plain ASCII, exponents as carets: in/s^2. |
label_html |
Display unit as HTML — a stacked fraction when it has one. |
with_units |
A copy of this system with per-dimension unit overrides. |
Attributes:
| Name | Type | Description |
|---|---|---|
coherent |
UnitSystem
|
This system with display-only overrides stripped. |
Attributes¶
Methods:¶
unit
¶
Display unit for a dimension or dimension expression.
Expressions compose from the base units: with in-lbf-s, 'acceleration/force' -> '(in/s**2)/lbf'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
str
|
A dimension tag, such as 'acceleration' or 'force'. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The unit string for this dimension. |
Source code in src/visualdynamics/units.py
transform
¶
(scale, offset) converting a display value to SI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
str
|
A dimension tag, such as 'acceleration' or 'force'. |
required |
Returns:
| Type | Description |
|---|---|
tuple of (float, float)
|
The scale and offset taking SI to display — an offset matters for temperature and nothing else. |
Source code in src/visualdynamics/units.py
factor
¶
SI-per-display-unit scale (offset-free dimensions only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
str
|
A dimension tag, such as 'acceleration' or 'force'. |
required |
Returns:
| Type | Description |
|---|---|
float
|
What an SI value is multiplied by to display it. |
Source code in src/visualdynamics/units.py
from_si
¶
Convert SI values to this system's display unit for dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
array_like
|
Values in SI. |
required |
dimension
|
str
|
A dimension tag, such as 'acceleration' or 'force'. |
required |
Returns:
| Type | Description |
|---|---|
array_like
|
The same values in this system's units. |
Source code in src/visualdynamics/units.py
to_si
¶
Convert values from this system's units into SI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
array_like
|
Values in this system's units. |
required |
dimension
|
str
|
A dimension tag, such as 'acceleration'. |
required |
Returns:
| Type | Description |
|---|---|
array_like
|
The same values in SI. |
Source code in src/visualdynamics/units.py
label
¶
Display unit text; empty when the dimension is undefined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
str
|
A dimension tag, such as 'acceleration' or 'force'. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The unit's name in this system. |
Source code in src/visualdynamics/units.py
label_text
¶
Display unit for plain-text output, with real exponents: in/s².
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
str
|
A dimension tag, such as 'acceleration' or 'force'. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The label as plain text. |
Source code in src/visualdynamics/units.py
label_ascii
¶
Display unit in plain ASCII, exponents as carets: in/s^2.
For renderers that quietly drop what they cannot draw: VTK's
3-D axis titles lose unicode superscripts in every text mode,
so label_text's (in/s²)²/Hz read (in/s)/Hz off the waterfall
— wrong by two squarings, with nothing saying so.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
str
|
A dimension tag, such as 'acceleration' or 'force'. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The label with no unicode, for renderers that drop it. |
Source code in src/visualdynamics/units.py
label_html
¶
Display unit as HTML — a stacked fraction when it has one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dimension
|
str
|
A dimension tag, such as 'acceleration' or 'force'. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The label with HTML superscripts. |
Source code in src/visualdynamics/units.py
with_units
¶
with_units(
name: str | None = None, **overrides
) -> UnitSystem
A copy of this system with per-dimension unit overrides.
Example: IN_LBF_S.with_units(acceleration='g') displays acceleration in g while everything else stays inch-pound-second.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
What to call the derived system. |
None
|
**overrides
|
Dimension/unit pairs to change. |
{}
|
Returns:
| Type | Description |
|---|---|
UnitSystem
|
A copy with those units replaced. |
Source code in src/visualdynamics/units.py
Functions:¶
unit_registry
¶
The one pint registry, built on first use.
One, because pint units from two registries do not compare — and lazily, because building it costs a tenth of a second that a script doing no unit conversion should not pay.
Source code in src/visualdynamics/units.py
normalize_unit
¶
A unit string as visualdynamics reads it, before pint sees it.
Source code in src/visualdynamics/units.py
pretty_unit
¶
plain_unit
¶
The inverse of pretty_unit, so a shown unit can be read back.
Source code in src/visualdynamics/units.py
is_compound_unit
¶
parse_dimension
¶
Parse a dimension expression into (base_dimension, power) parts.
Memoized: this is a pure function of a short string, and the plot asks for the same handful of expressions once per record. Drawing one curve out of a 1356-record FRF used to reparse and reconvert 1356 times.
Source code in src/visualdynamics/units.py
si_transform
cached
¶
(scale, offset) such that si_value = value * scale + offset.
Offset is non-zero only for affine units such as degC and degF.
Source code in src/visualdynamics/units.py
si_factor
¶
Multiplier converting values in unit to SI.
Raises for affine units (degC, degF), which need to_si/from_si.
Source code in src/visualdynamics/units.py
to_si
¶
values, read as unit, in SI. Handles offsets, so degrees
Celsius arrive as kelvin rather than as a scaled nonsense.
Source code in src/visualdynamics/units.py
from_si
¶
SI values expressed in unit — the inverse of to_si.
convert
¶
dimension_of
¶
The visualdynamics base dimension matching a unit string, or None.
Only single base dimensions are recognized ('m/s**2' -> 'acceleration'); compound quantities like FRFs carry their dimension expression explicitly.