units
ngtools.units
Tools to convert between units.
Functions:
| Name | Description |
|---|---|
|
Split prefix and unit |
|
Convert a value between units |
|
Convert to prefixless SI unit |
|
Return the scale of a arbitrary unit with respect to its SI equivalent |
|
Find OME version of a unit |
|
Find NIfTI version of a unit |
|
Find Neuroglancer version of a unit (= short SI) |
Attributes:
| Name | Type | Description |
|---|---|---|
|
Type hint for unit domains, i.e., |
|
|
|
Dictionary where values are lists of valid OME units. |
|
|
Dictionary where values are lists of valid NIfTI units. |
|
|
Dictionary where values are lists of valid Neuroglancer (short SI) units. |
|
|
Dictionary where values are lists of valid SI (long) units. |
|
|
Dictionary that maps short forms of SI prefixes to long forms |
|
|
Dictionary that maps long forms of SI prefixes to short forms |
|
|
Dictionary that maps short forms of SI prefixes to their exponent value |
|
|
Dictionary where values are dictionaries that map short forms of a unit to long forms |
|
|
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
Convert a value between different units.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
|
Value to convert |
required |
src
|
|
Source unit |
required |
dst
|
|
Destination unit |
required |
Returns:
| Name | Type | Description |
|---|---|---|
value |
|
Converted value |
normalize_unit
Normalize a unit (i.e., convert to prefixless SI unit).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
|
Value to convert |
required |
unit
|
|
Source unit |
required |
Returns:
| Name | Type | Description |
|---|---|---|
value |
|
Converted value |
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
|
|
Input value(s) |
required |
n
|
|
Target length of the list. If the input is longer, it gets cropped. Otherwise, it gets padded with the default value. |
None
|
default
|
|
Value to use to pad the list. If not provided, use last value in the list. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x |
|
|
as_short_unit
Return the shortest representation for this unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
|
Any unit. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
unit |
|
An short unit. |
Raises:
| Type | Description |
|---|---|
|
If the input unit is unknown. |
as_long_unit
Return the long representation for this unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
|
Any unit. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
unit |
|
An long unit. |
Raises:
| Type | Description |
|---|---|
|
If the input unit is unknown. |
as_neuroglancer_unit
Return the Neuroglancer representation of a unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
|
Any unit. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
unit |
|
An OME-compatible unit. |
Raises:
| Type | Description |
|---|---|
|
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
|
|
Any unit |
required |
Returns:
| Name | Type | Description |
|---|---|---|
unit |
|
An OME-compatible unit |
Raises:
| Type | Description |
|---|---|
|
If the input unit does not have an exactly equivalent representation in OME. |
as_nifti_unit
Return the NIfTI-compatible representation of a unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
|
Any unit |
required |
Returns:
| Name | Type | Description |
|---|---|---|
unit |
|
A NIfTI-compatible unit.
If the unit does not have a NIfTI representation, the NIfTI type
|
unit_to_scale
Return the scale of a arbitrary unit with respect to its SI equivalent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit
|
|
Any unit |
required |
Returns:
| Name | Type | Description |
|---|---|---|
scale |
|
The corresponding SI scale |
Raises:
| Type | Description |
|---|---|
|
If the unit does not have a SI equivalent. |