UnitsMapping

class astropy.modeling.mappings.UnitsMapping(mapping, input_units_equivalencies=None, input_units_allow_dimensionless=False, name=None, meta=None)[source]

Bases: astropy.modeling.Model

Mapper that operates on the units of the input, first converting to canonical units, then assigning new units without further conversion. Used by Model.coerce_units to support units on otherwise unitless models such as Polynomial1D.

Parameters
mappingtuple

A tuple of (input_unit, output_unit) pairs, one per input, matched to the inputs by position. The first element of the each pair is the unit that the model will accept (specify dimensionless_unscaled to accept dimensionless input). The second element is the unit that the model will return. Specify dimensionless_unscaled to return dimensionless Quantity, and None to return raw values without Quantity.

input_units_equivalenciesdict, optional

Default equivalencies to apply to input values. If set, this should be a dictionary where each key is a string that corresponds to one of the model inputs.

input_units_allow_dimensionlessdict or bool, optional

Allow dimensionless input. If this is True, input values to evaluate will gain the units specified in input_units. If this is a dictionary then it should map input name to a bool to allow dimensionless numbers for that input.

namestr, optional

A human-friendly name associated with this model instance (particularly useful for identifying the individual components of a compound model).

metadict-like, optional

Free-form metadata to associate with this model.

Examples

Wrapping a unitless model to require and convert units:

>>> from astropy.modeling.models import Polynomial1D, UnitsMapping
>>> from astropy import units as u
>>> poly = Polynomial1D(1, c0=1, c1=2)
>>> model = UnitsMapping(((u.m, None),)) | poly
>>> model = model | UnitsMapping(((None, u.s),))
>>> model(u.Quantity(10, u.m))  
<Quantity 21. s>
>>> model(u.Quantity(1000, u.cm)) 
<Quantity 21. s>
>>> model(u.Quantity(10, u.cm)) 
<Quantity 1.2 s>

Wrapping a unitless model but still permitting unitless input:

>>> from astropy.modeling.models import Polynomial1D, UnitsMapping
>>> from astropy import units as u
>>> poly = Polynomial1D(1, c0=1, c1=2)
>>> model = UnitsMapping(((u.m, None),), input_units_allow_dimensionless=True) | poly
>>> model = model | UnitsMapping(((None, u.s),))
>>> model(u.Quantity(10, u.m))  
<Quantity 21. s>
>>> model(10)  
<Quantity 21. s>

Attributes Summary

input_units

This property is used to indicate what units or sets of units the evaluate method expects, and returns a dictionary mapping inputs to units (or None if any units are accepted).

inputs

mapping

n_inputs

n_outputs

outputs

Methods Summary

evaluate(self, *args)

Evaluate the model on some input variables.

Attributes Documentation

input_units

This property is used to indicate what units or sets of units the evaluate method expects, and returns a dictionary mapping inputs to units (or None if any units are accepted).

Model sub-classes can also use function annotations in evaluate to indicate valid input units, in which case this property should not be overridden since it will return the input units based on the annotations.

inputs
mapping
n_inputs
n_outputs
outputs

Methods Documentation

evaluate(self, *args)[source]

Evaluate the model on some input variables.