brainhops.datamodel.units
Units of measurement.
A Unit is created from a name, for example Unit("mm") or
Unit("s/mm^2"). Units are immutable, hashable and picklable. They are also
interned: any two spellings of the same unit, such as "mm" and
"millimeter", produce the same object.
Names are parsed by a private pint
registry, which adds index units (index, voxel and pixel), the
arbitrary unit (a.u.) and the Hounsfield unit (HU) to pint's defaults.
The registry is built the first time a name needs pint, not when brainhops
is imported. Pint types appear only through Unit.to_pint and
Unit.from_pint.
Units restricted to a dimension
A subclass of Unit can be restricted to a dimension, in which case it
only builds units of that dimension. SpaceUnit, TimeUnit and
IndexUnit are declared in this way:
Subscripting Unit builds and caches the class of any other dimension:
DiffusionUnit = Unit["time / length ** 2"]
DiffusionUnit("s/mm^2") # 'second / millimeter ** 2'
DiffusionUnit("mm") # ValueError: 'millimeter' is not a ...
These classes are the types that field annotations use. For example, a
field typed Optional[Union[SpaceUnit, IndexUnit]] holds a length unit, an
index unit or nothing. A dimension is written in terms of pint's base
dimensions and the additional index dimension, with or without brackets.
Unit(name) returns an instance of the class declared for the dimension of
the unit, so isinstance(Unit("mm"), SpaceUnit) is true.
Angles
As in pint, angles are dimensionless, so a degree is compatible with a percent.
Classes
Unit
A unit of measurement.
The name is parsed as pint parses it, as in "mm", "µm", "Hz",
"s/mm^2" or "voxel". The result is a SpaceUnit,
TimeUnit or IndexUnit for a length, a duration or an
index, and a plain Unit otherwise. Called without a name,
IndexUnit returns index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str or Unit
|
The name of a unit, or an existing unit. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Attributes
dimension
class-attribute
dimension: str | None = None
The dimension the class is restricted to, or None.
dimensionality
property
dimensionality: str
The dimension without brackets, such as "length" or
"1 / time".
type
property
type: str
The kind of quantity, such as "space", "time" or "index".
For any other unit, the type is its dimension. Units of the same type convert into one another.
scale
property
scale: float
The size of the unit in base units, such as meters or seconds.
The scale of an index unit is 1.0. Reading the scale of a non-multiplicative unit raises a ValueError.
log10_scale
property
The base-10 logarithm of scale, an int for a power of ten.
Methods:
__class_getitem__
Return the class of units of a dimension.
The class is built once per dimension: Unit["length"] is
SpaceUnit.
from_pint
classmethod
Build a unit from a pint unit of any registry.
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
to_pint
Return the unit in the private pint registry.
This is the converse of from_pint.
SpaceUnit
Bases: Unit
A unit of length, such as the millimeter.
Attributes
dimension
class-attribute
dimension: str | None = None
The dimension the class is restricted to, or None.
dimensionality
property
dimensionality: str
The dimension without brackets, such as "length" or
"1 / time".
type
property
type: str
The kind of quantity, such as "space", "time" or "index".
For any other unit, the type is its dimension. Units of the same type convert into one another.
scale
property
scale: float
The size of the unit in base units, such as meters or seconds.
The scale of an index unit is 1.0. Reading the scale of a non-multiplicative unit raises a ValueError.
log10_scale
property
The base-10 logarithm of scale, an int for a power of ten.
Methods:
__class_getitem__
Return the class of units of a dimension.
The class is built once per dimension: Unit["length"] is
SpaceUnit.
from_pint
classmethod
Build a unit from a pint unit of any registry.
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
to_pint
Return the unit in the private pint registry.
This is the converse of from_pint.
TimeUnit
Bases: Unit
A unit of time, such as the second.
Attributes
dimension
class-attribute
dimension: str | None = None
The dimension the class is restricted to, or None.
dimensionality
property
dimensionality: str
The dimension without brackets, such as "length" or
"1 / time".
type
property
type: str
The kind of quantity, such as "space", "time" or "index".
For any other unit, the type is its dimension. Units of the same type convert into one another.
scale
property
scale: float
The size of the unit in base units, such as meters or seconds.
The scale of an index unit is 1.0. Reading the scale of a non-multiplicative unit raises a ValueError.
log10_scale
property
The base-10 logarithm of scale, an int for a power of ten.
Methods:
__class_getitem__
Return the class of units of a dimension.
The class is built once per dimension: Unit["length"] is
SpaceUnit.
from_pint
classmethod
Build a unit from a pint unit of any registry.
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
to_pint
Return the unit in the private pint registry.
This is the converse of from_pint.
IndexUnit
Bases: Unit
A unit of coordinates that count array elements.
The index units are index, voxel and pixel, which convert into
one another with a factor of 1. An axis with an index unit is an array
axis: its coordinates are positions 0 to N-1, so reversing it shifts
the origin by N-1 instead of flipping the sign. OME-Zarr gives such an
axis no unit.
Note
An index unit differs from unit=None, which makes no claim about
the axis and allows no conversion. An index unit also differs from
dimensionless physical units such as the percent, because an index
unit never converts into a physical unit.
Attributes
dimension
class-attribute
dimension: str | None = None
The dimension the class is restricted to, or None.
dimensionality
property
dimensionality: str
The dimension without brackets, such as "length" or
"1 / time".
type
property
type: str
The kind of quantity, such as "space", "time" or "index".
For any other unit, the type is its dimension. Units of the same type convert into one another.
scale
property
scale: float
The size of the unit in base units, such as meters or seconds.
The scale of an index unit is 1.0. Reading the scale of a non-multiplicative unit raises a ValueError.
log10_scale
property
The base-10 logarithm of scale, an int for a power of ten.
Methods:
__class_getitem__
Return the class of units of a dimension.
The class is built once per dimension: Unit["length"] is
SpaceUnit.
from_pint
classmethod
Build a unit from a pint unit of any registry.
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
to_pint
Return the unit in the private pint registry.
This is the converse of from_pint.
Functions:
is_indexunit
Return whether a unit, or a unit class, is an index unit.
is_physicalunit
Return whether a unit measures a physical quantity.
The result is false for None, for an IndexUnit and for a
class. Two units have a conversion factor exactly when both are
physical and share a dimension.
is_spaceunit
Return whether a unit, or a unit class, is a unit of length.