unit_checker#
- arte.utils.unit_checker.assert_unit_is_equivalent(var, ref)#
Make sure that
varhas a unit compatible withref
- arte.utils.unit_checker.make_sure_its_a(unit, v, name='', copy=True)#
Make sure that
vhas the astropy unitunit.If
vdoes not have any unit, applyunitand return the combined value. If it has one, check that it can be converted tounit, and return the converted value. Otherwise, raise astropy.units.UnitsError.- Parameters:
unit (astropy unit) – the wanted unit
v – the value under test
name (string) – description of
v. Will be used in error messages in case the conversion fails.
- Returns:
the original value converted to
unit.- Return type:
astropy Quantity
- Raises:
astropy.units.UnitsError – if the conversion to
unitfails.
- arte.utils.unit_checker.separate_value_and_unit(var)#
Returns the argument value and unit, if
varhas one.If not,
varis returned unchanged and unit is set to 1.- Parameters:
var (any type) – the variable to be tested
- Returns:
tuple with the value and the astropy unit of
varor 1 ifvardoes not have a unit.- Return type:
Examples
> a = 42 * u.m > separate_value_and_unit(a) (42.0, Unit(“m”))
> b = 42 > separate_value_and_unit(b) (42, 1)
- arte.utils.unit_checker.unit_check(f)#
Decorator to add type checking of astropy units to a function.
This decorator will ensure that, each time the decorated function is called, all the arguments have the correct astropy units, as defined in the default values of the decorated function, calling
make_sure_its_a()for each of them.If the function does not define a unit for a parameter, the corresponding argument is not modified, whether it has a unit or not.
If any of the checks fails, raises
TypeErrorwhen the decorated function is called.