UnitsMapping

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

Bases: 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:
mappingpython:tuple

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_equivalenciespython:dict, 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_dimensionlesspython:dict 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.

namepython:str, 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(*args)

Evaluate the model on some input variables.

Attributes Documentation

input_units
inputs
mapping
n_inputs

The number of inputs.

n_outputs

The number of outputs.

outputs

Methods Documentation

evaluate(*args)[source]

Evaluate the model on some input variables.