Skip to content

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:

class SpaceUnit(Unit, dimension="length"): ...

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

Unit(*args: Any, **kwargs: Any)

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 name is neither a string nor a unit.

ValueError

If name is empty or unknown, or if its dimension is one that the class does not accept.

Attributes

dimension class-attribute
dimension: str | None = None

The dimension the class is restricted to, or None.

name property
name: str

The full name, such as "millimeter" or "second / millimeter ** 2".

symbol property
symbol: str

The symbol, such as "mm", "μm" or "s / mm ** 2".

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.

prefix property
prefix: str | None

The SI prefix, such as "milli", or None.

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
log10_scale: int | float

The base-10 logarithm of scale, an int for a power of ten.

Methods:

__class_getitem__
__class_getitem__(dimension: str) -> Type[Unit]

Return the class of units of a dimension.

The class is built once per dimension: Unit["length"] is SpaceUnit.

from_pint classmethod
from_pint(unit: Any) -> Self

Build a unit from a pint unit of any registry.

Raises:

Type Description
TypeError

If unit is not a pint unit.

__str__
__str__() -> str

Return the name, such as "millimeter".

__repr__
__repr__() -> str

Return the quoted name.

to_pint
to_pint() -> Unit

Return the unit in the private pint registry.

This is the converse of from_pint.

is_compatible_with
is_compatible_with(other: Unit | str) -> bool

Return whether the unit converts into other, a unit or a name.

SpaceUnit

SpaceUnit(*args: Any, **kwargs: Any)

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.

name property
name: str

The full name, such as "millimeter" or "second / millimeter ** 2".

symbol property
symbol: str

The symbol, such as "mm", "μm" or "s / mm ** 2".

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.

prefix property
prefix: str | None

The SI prefix, such as "milli", or None.

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
log10_scale: int | float

The base-10 logarithm of scale, an int for a power of ten.

Methods:

__class_getitem__
__class_getitem__(dimension: str) -> Type[Unit]

Return the class of units of a dimension.

The class is built once per dimension: Unit["length"] is SpaceUnit.

from_pint classmethod
from_pint(unit: Any) -> Self

Build a unit from a pint unit of any registry.

Raises:

Type Description
TypeError

If unit is not a pint unit.

__str__
__str__() -> str

Return the name, such as "millimeter".

__repr__
__repr__() -> str

Return the quoted name.

to_pint
to_pint() -> Unit

Return the unit in the private pint registry.

This is the converse of from_pint.

is_compatible_with
is_compatible_with(other: Unit | str) -> bool

Return whether the unit converts into other, a unit or a name.

TimeUnit

TimeUnit(*args: Any, **kwargs: Any)

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.

name property
name: str

The full name, such as "millimeter" or "second / millimeter ** 2".

symbol property
symbol: str

The symbol, such as "mm", "μm" or "s / mm ** 2".

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.

prefix property
prefix: str | None

The SI prefix, such as "milli", or None.

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
log10_scale: int | float

The base-10 logarithm of scale, an int for a power of ten.

Methods:

__class_getitem__
__class_getitem__(dimension: str) -> Type[Unit]

Return the class of units of a dimension.

The class is built once per dimension: Unit["length"] is SpaceUnit.

from_pint classmethod
from_pint(unit: Any) -> Self

Build a unit from a pint unit of any registry.

Raises:

Type Description
TypeError

If unit is not a pint unit.

__str__
__str__() -> str

Return the name, such as "millimeter".

__repr__
__repr__() -> str

Return the quoted name.

to_pint
to_pint() -> Unit

Return the unit in the private pint registry.

This is the converse of from_pint.

is_compatible_with
is_compatible_with(other: Unit | str) -> bool

Return whether the unit converts into other, a unit or a name.

IndexUnit

IndexUnit(*args: Any, **kwargs: Any)

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.

name property
name: str

The full name, such as "millimeter" or "second / millimeter ** 2".

symbol property
symbol: str

The symbol, such as "mm", "μm" or "s / mm ** 2".

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.

prefix property
prefix: str | None

The SI prefix, such as "milli", or None.

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
log10_scale: int | float

The base-10 logarithm of scale, an int for a power of ten.

Methods:

__class_getitem__
__class_getitem__(dimension: str) -> Type[Unit]

Return the class of units of a dimension.

The class is built once per dimension: Unit["length"] is SpaceUnit.

from_pint classmethod
from_pint(unit: Any) -> Self

Build a unit from a pint unit of any registry.

Raises:

Type Description
TypeError

If unit is not a pint unit.

__str__
__str__() -> str

Return the name, such as "millimeter".

__repr__
__repr__() -> str

Return the quoted name.

to_pint
to_pint() -> Unit

Return the unit in the private pint registry.

This is the converse of from_pint.

is_compatible_with
is_compatible_with(other: Unit | str) -> bool

Return whether the unit converts into other, a unit or a name.

Functions:

is_indexunit

is_indexunit(unit: Unit | Type[Unit] | None) -> bool

Return whether a unit, or a unit class, is an index unit.

is_physicalunit

is_physicalunit(unit: Unit | Type[Unit] | None) -> bool

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

is_spaceunit(unit: Unit | Type[Unit] | None) -> bool

Return whether a unit, or a unit class, is a unit of length.

is_timeunit

is_timeunit(unit: Unit | Type[Unit] | None) -> bool

Return whether a unit, or a unit class, is a unit of time.