pytac.units#

Classes for use in unit conversion.

Members

NullUnitConv

Returns input value without performing any conversions.

PchipUnitConv

Piecewise Cubic Hermite Interpolating Polynomial unit conversion.

PolyUnitConv

Linear interpolation for converting between physics and engineering units.

UnitConv

Class to convert between physics and engineering units.

unit_function

Default value for the pre and post functions used in unit conversion.

pytac.units.unit_function(value)[source]#

Default value for the pre and post functions used in unit conversion.

Parameters:

value (float) – The value to be converted.

Returns:

The result of the conversion.

Return type:

float

class pytac.units.UnitConv(post_eng_to_phys=<function unit_function>, pre_phys_to_eng=<function unit_function>, engineering_units='', physics_units='', name=None)[source]#

Class to convert between physics and engineering units.

This class does not do conversion but does return values if the target units are the same as the provided units. Subclasses should implement _raw_eng_to_phys() and _raw_phys_to_eng() in order to provide complete unit conversion.

The two arguments to this function represent functions that are applied to the result of the initial conversion. One happens after the conversion, the other happens before the conversion back.

Attributes:

name#

An identifier for the unit conversion object.

Type:

str

eng_units#

The unit type of the post conversion engineering value.

Type:

str

phys_units#

The unit type of the post conversion physics value.

Type:

str

Parameters:
  • post_eng_to_phys (function) – Function to be applied after the initial conversion.

  • pre_phys_to_eng (function) – Function to be applied before the initial conversion.

  • engineering_units (str) – The unit type of the post conversion engineering value.

  • physics_units (str) – The unit type of the post conversion physics value.

  • name (str) – An identifier for the unit conversion object.

Methods:

set_post_eng_to_phys(post_eng_to_phys)[source]#

Set the function to be applied after the initial conversion.

Parameters:

post_eng_to_phys (function) – Function to be applied after the initial conversion.

set_pre_phys_to_eng(pre_phys_to_eng)[source]#

Set the function to be applied before the initial conversion.

Parameters:

pre_phys_to_eng (function) – Function to be applied before the initial conversion.

eng_to_phys(value)[source]#

Function that does the unit conversion.

Conversion from engineering to physics units. An additional function may be cast on the initial conversion.

Parameters:

value (float) – Value to be converted from engineering to physics units.

Returns:

The result value.

Return type:

float

Raises:

UnitsException – If the conversion is invalid; i.e. if there are no solutions, or multiple, within conversion limits.

phys_to_eng(value)[source]#

Function that does the unit conversion.

Conversion from physics to engineering units. An additional function may be cast on the initial conversion.

Parameters:

value (float) – Value to be converted from physics to engineering units.

Returns:

The result value.

Return type:

float

Raises:

UnitsException – If the conversion is invalid; i.e. if there are no solutions, or multiple, within conversion limits.

convert(value, origin, target)[source]#

Convert between two different unit types and check the validity of the result.

Parameters:
  • value (float) – the value to be converted

  • origin (str) – pytac.ENG or pytac.PHYS

  • target (str) – pytac.ENG or pytac.PHYS

Returns:

The resulting value.

Return type:

float

Raises:

UnitsException – If the conversion is invalid; i.e. if there are no solutions, or multiple, within conversion limits.

set_conversion_limits(lower_limit, upper_limit)[source]#

Conversion limits to be applied before or after a conversion take place. Limits should be set in in engineering units.

Parameters:
  • lower_limit (float) – the lower conversion limit

  • upper_limit (float) – the upper conversion limit

get_conversion_limits(units='engineering')[source]#

Return the current conversion limits in the specified unit type.

Parameters:

units

Returns:

the conversion limits in the desired unit type,

format: [lower_limit, upper_limit]

Return type:

list

class pytac.units.PolyUnitConv(coef, post_eng_to_phys=<function unit_function>, pre_phys_to_eng=<function unit_function>, engineering_units='', physics_units='', name=None)[source]#

Linear interpolation for converting between physics and engineering units.

Attributes:

p#

A one-dimensional polynomial of coefficients.

Type:

poly1d

name#

An identifier for the unit conversion object.

Type:

str

eng_units#

The unit type of the post conversion engineering value.

Type:

str

phys_units#

The unit type of the post conversion physics value.

Type:

str

Parameters:
  • coef (array-like) – The polynomial’s coefficients, in decreasing powers.

  • post_eng_to_phys (float) – The value after conversion between ENG and PHYS.

  • pre_eng_to_phys (float) – The value before conversion.

  • engineering_units (str) – The unit type of the post conversion engineering value.

  • physics_units (str) – The unit type of the post conversion physics value.

  • name (str) – An identifier for the unit conversion object.

class pytac.units.PchipUnitConv(x, y, post_eng_to_phys=<function unit_function>, pre_phys_to_eng=<function unit_function>, engineering_units='', physics_units='', name=None)[source]#

Piecewise Cubic Hermite Interpolating Polynomial unit conversion.

Attributes:

x#

A list of points on the x axis. These must be in increasing order for the interpolation to work. Otherwise, a ValueError is raised.

Type:

list

y#

A list of points on the y axis. These must be in increasing or decreasing order. Otherwise, a ValueError is raised.

Type:

list

pp#

A pchip one-dimensional monotonic cubic interpolation of points on both x and y axes.

Type:

PchipInterpolator

name#

An identifier for the unit conversion object.

Type:

str

eng_units#

The unit type of the post conversion engineering value.

Type:

str

phys_units#

The unit type of the post conversion physics value.

Type:

str

Parameters:
  • x (list) – A list of points on the x axis. These must be in increasing order for the interpolation to work. Otherwise, a ValueError is raised.

  • y (list) – A list of points on the y axis. These must be in increasing or decreasing order. Otherwise, a ValueError is raised.

  • engineering_units (str) – The unit type of the post conversion engineering value.

  • physics_units (str) – The unit type of the post conversion physics value.

  • name (str) – An identifier for the unit conversion object.

Raises:

ValueError – if coefficients are not appropriately monotonic.

class pytac.units.NullUnitConv(engineering_units='', physics_units='')[source]#

Returns input value without performing any conversions.

Attributes:

eng_units#

The unit type of the post conversion engineering value.

Type:

str

phys_units#

The unit type of the post conversion physics value.

Type:

str

Parameters:
  • engineering_units (str) – The unit type of the post conversion engineering value.

  • physics_units (str) – The unit type of the post conversion physics value.