Unit Conversion

Handles unit conversions utilizing the pint module.
Written By: Matthew Stadelman
Date Written: 2017/03/05
Last Modifed: 2017/04/30

apmapflow.unit_conversion.convert_value(value, unit_in, unit_out='SI')[source]

Returns a converted value, in the specified output units or SI.

Parameters:
  • value (float) – edge length of a voxel cube.
  • unit_in (string) – the current units of the value
  • unit_out (string, optional) – the desired output units, if omitted they are converted to SI
Returns:

Return type:

A floating point value converted to the desired units.

Examples

>>> import apmapflow.unit_conversion as uc
>>> print(uc.convert_value(26.8, 'um'))
>>> print(uc.convert_value(26.8, 'um', 'cm'))
apmapflow.unit_conversion.get_conversion_factor(unit_in, unit_out='SI')[source]

Returns a conversion factor between the input unit and output unit or to SI if no output unit is provided.

Parameters:
  • unit_in (string) – the desired input units
  • unit_out (string, optional) – the desired output units, if omitted the SI value is used
Returns:

Return type:

A floating point value that can be used to convert between the two units.

Examples

>>> import apmapflow.unit_conversion as uc
>>> print(uc.get_conversion_factor('micron'))
>>> print(uc.get_conversion_factor('um', 'mm'))
apmapflow.unit_conversion.register_voxel_unit(voxel_size, unit)[source]

Registers the ‘voxel’ unit with the unit_registry. voxel_size is the length of an edge of the voxel in the specified units.

Parameters:
  • voxel_size (float) – edge length of a voxel cube.
  • unit (string) – units the voxel size is defined in.

Examples

>>> import apmapflow.unit_conversion as uc
>>> uc.register_voxel_unit(26.8, 'um')
>>> print(uc.unit_registry('voxel').to('m').magnitude)