units
ngtools.units
Tools to convert between units.
Functions:
| Name | Description |
|---|---|
split_unit |
Split prefix and unit |
convert_unit |
Convert a value between units |
normalize_unit |
Convert to prefixless SI unit |
unit_to_scale |
Return the scale of a arbitrary unit with respect to its SI equivalent |
as_ome_unit |
Find OME version of a unit |
as_nifti_unit |
Find NIfTI version of a unit |
as_neuroglancer_unit |
Find Neuroglancer version of a unit (= short SI) |
Attributes:
| Name | Type | Description |
|---|---|---|
UnitDomain |
Type hint for unit domains, i.e., |
|
OME_UNITS |
dict[UnitDomain, list[str]]
|
Dictionary where values are lists of valid OME units. |
NIFTI_UNITS |
dict[UnitDomain, list[str]]
|
Dictionary where values are lists of valid NIfTI units. |
SI_UNITS_SHORT |
dict[UnitDomain, list[str]]
|
Dictionary where values are lists of valid Neuroglancer (short SI) units. |
SI_UNITS_LONG |
dict[UnitDomain, list[str]]
|
Dictionary where values are lists of valid SI (long) units. |
SI_PREFIX_SHORT2LONG |
dict[str, str]
|
Dictionary that maps short forms of SI prefixes to long forms |
SI_PREFIX_LONG2SHORT |
dict[str, str]
|
Dictionary that maps long forms of SI prefixes to short forms |
SI_PREFIX_EXPONENT |
dict[str, float]
|
Dictionary that maps short forms of SI prefixes to their exponent value |
UNIT_SHORT2LONG |
dict[UnitDomain, dict[str, str]]
|
Dictionary where values are dictionaries that map short forms of a unit to long forms |
UNIT_LONG2SHORT |
dict[UnitDomain, dict[str, str]]
|
Dictionary where values are dictionaries that map long forms of a unit to short forms |
UNIT_SHORT2LONG
module-attribute
Mapping from short name to long name (per domain).
UNIT_LONG2SHORT
module-attribute
Mapping from long name to short name (per domain).
split_unit
Split prefix and unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
[sequence of] str
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
prefix |
[sequence of] str
|
|
unit |
[sequence of] str
|
|
convert_unit
normalize_unit
ensure_list
Ensure that x is a list.
Arrays are converted to nested lists, whereas scalars are insereted into a list of length 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
object
|
Input value(s) |
required |
n
|
int
|
Target length of the list. If the input is longer, it gets cropped. Otherwise, it gets padded with the default value. |
None
|
default
|
any
|
Value to use to pad the list. If not provided, use last value in the list. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x |
list
|
|
as_short_unit
Return the shortest representation for this unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
str
|
Any unit. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
unit |
str
|
An short unit. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input unit is unknown. |
as_long_unit
Return the long representation for this unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
str
|
Any unit. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
unit |
str
|
An long unit. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input unit is unknown. |
as_neuroglancer_unit
Return the Neuroglancer representation of a unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
str
|
Any unit. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
unit |
str
|
An OME-compatible unit. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input unit does not have an exactly equivalent representation in OME. |
as_ome_unit
Return the OME-compatible representation of a unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
str
|
Any unit |
required |
Returns:
| Name | Type | Description |
|---|---|---|
unit |
str
|
An OME-compatible unit |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input unit does not have an exactly equivalent representation in OME. |
as_nifti_unit
unit_to_scale
Return the scale of a arbitrary unit with respect to its SI equivalent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
str
|
Any unit |
required |
Returns:
| Name | Type | Description |
|---|---|---|
scale |
float
|
The corresponding SI scale |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the unit does not have a SI equivalent. |