Skip to main content
`Ctrl`+`K`

# CompoundModel

class astropy.modeling.CompoundModel(op, left, right, name=None, *, unit_change_composition=False)

  Bases: `Model`

  Base class for compound models.

  While it can be used directly, the recommended way to combine models is through the model operators.

  Attributes Summary

  `col_fit_deriv` |
  `eqcons` | List of parameter equality constraints.
  `fit_deriv` |
  `fittable` | Set the fittable attribute on a compound model.
  `has_user_bounding_box` | A flag indicating whether or not a custom bounding_box has been assigned to this model by a user, via assignment to `model.bounding_box`.
  `ineqcons` | List of parameter inequality constraints.
  `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).
  `input_units_allow_dimensionless` | Allow dimensionless input (and corresponding output).
  `input_units_equivalencies` | The type of the None singleton.
  `input_units_strict` | Enforce strict units on inputs to evaluate.
  `isleaf` |
  `n_inputs` | The number of inputs of a model.
  `n_outputs` | The number of outputs of a model.
  `n_submodels` | Return the number of components in a single model, which is obviously 1.
  `param_names` | An ordered list of parameter names.
  `return_units` | This property is used to indicate what units or sets of units the output of evaluate should be in, and returns a dictionary mapping outputs to units (or `None` if any units are accepted).
  `submodel_names` | Return the names of submodels in a `CompoundModel`.
  `unit_change_composition` | A flag indicating whether or not the unit change composition has been set for this model.

  Methods Summary

  `evaluate`(*args, **kw) | Evaluate the model on some input variables.
  `inputs_map`() | Map the names of the inputs to this ExpressionTree to the inputs to the leaf models.
  `outputs_map`() | Map the names of the outputs to this ExpressionTree to the outputs to the leaf models.
  `rename`(name) | Creates a copy of this model class with a new name, inputs or outputs.
  `render`([out, coords]) | Evaluate a model at fixed positions, respecting the `bounding_box`.
  `replace_submodel`(name, model) | Construct a new `CompoundModel` instance from an existing CompoundModel, replacing the named submodel with a new model.
  `traverse_postorder`([include_operator]) | Postorder traversal of the CompoundModel tree.
  `with_units_from_data`(**kwargs) | See `with_units_from_data` for overview of this method.
  `without_units_for_data`(**kwargs) | See `without_units_for_data` for overview of this method.

  Attributes Documentation

  col_fit_deriv
  eqcons
  fit_deriv
  fittable

    Set the fittable attribute on a compound model.

  has_user_bounding_box

    A flag indicating whether or not a custom bounding_box has been assigned to this model by a user, via assignment to `model.bounding_box`.

  ineqcons
  input_units
  input_units_allow_dimensionless
  input_units_equivalencies
  input_units_strict
  isleaf
  n_inputs

    The number of inputs of a model.

  n_outputs

    The number of outputs of a model.

  n_submodels
  param_names

    Names of the parameters that describe models of this type.

    The parameters in this tuple are in the same order they should be passed in when initializing a model of a specific type. Some types of models, such as polynomial models, have a different number of parameters depending on some other property of the model, such as the degree.

    When defining a custom model class the value of this attribute is automatically set by the `Parameter` attributes defined in the class body.

  return_units
  submodel_names

    Return the names of submodels in a `CompoundModel`.

  unit_change_composition

    A flag indicating whether or not the unit change composition has been set for this model.

  Methods Documentation

  evaluate(*args, **kw)

    Evaluate the model on some input variables.

  inputs_map()

    Map the names of the inputs to this ExpressionTree to the inputs to the leaf models.

  outputs_map()

    Map the names of the outputs to this ExpressionTree to the outputs to the leaf models.

  rename(name)

    Creates a copy of this model class with a new name, inputs or outputs.

    The new class is technically a subclass of the original class, so that instance and type checks will still work. For example:

        >>> from astropy.modeling.models import Rotation2D
        >>> SkyRotation = Rotation2D.rename('SkyRotation')
        >>> SkyRotation
        <class 'astropy.modeling.core.SkyRotation'>
        Name: SkyRotation (Rotation2D)
        N_inputs: 2
        N_outputs: 2
        Fittable parameters: ('angle',)
        >>> issubclass(SkyRotation, Rotation2D)
        True
        >>> r = SkyRotation(90)
        >>> isinstance(r, Rotation2D)
        True

  render(out=None, coords=None)

    Evaluate a model at fixed positions, respecting the `bounding_box`.

    The key difference relative to evaluating the model directly is that this method is limited to a bounding box if the `bounding_box` attribute is set.

    Parameters:
      out `numpy.ndarray`, optional

        An array that the evaluated model will be added to. If this is not given (or given as `None`), a new array will be created.

      coords numpy:array_like, optional

        An array to be used to translate from the model’s input coordinates to the `out` array. It should have the property that `self(coords)` yields the same shape as `out`. If `out` is not specified, `coords` will be used to determine the shape of the returned array. If this is not provided (or None), the model will be evaluated on a grid determined by `bounding_box`.

    Returns:
      out `numpy.ndarray`

        The model added to `out` if `out` is not `None`, or else a new array from evaluating the model over `coords`. If `out` and `coords` are both `None`, the returned array is limited to the `bounding_box` limits. If `bounding_box` is `None`, `arr` or `coords` must be passed.

    Raises:
      `ValueError`

        If `coords` are not given and the `bounding_box` of this model is not set.

    Examples

    Efficient Model Rendering with Bounding Boxes

  replace_submodel(name, model)

    Construct a new `CompoundModel` instance from an existing CompoundModel, replacing the named submodel with a new model.

    In order to ensure that inverses and names are kept/reconstructed, it’s necessary to rebuild the CompoundModel from the replaced node all the way back to the base. The original CompoundModel is left untouched.

    Parameters:
      name `python:str`

        name of submodel to be replaced

      model `Model`

        replacement model

  traverse_postorder(include_operator=False)

    Postorder traversal of the CompoundModel tree.

  with_units_from_data(**kwargs)

    See `with_units_from_data` for overview of this method.

    Notes

    This modifies the behavior of the base method to account for the case where the sub-models of a compound model have different output units. This is only valid for compound * and / compound models as in that case it is reasonable to mix the output units. In order to do this it requires some additional information output by `without_units_for_data` passed as keyword arguments under the keywords `_left_kwargs` and `_right_kwargs`.

    Outside the mixed output units, this method is identical to the base method.

  without_units_for_data(**kwargs)

    See `without_units_for_data` for overview of this method.

    Notes

    This modifies the behavior of the base method to account for the case where the sub-models of a compound model have different output units. This is only valid for compound *, / and | (only if `unit_change_composition` is `True` on the instance) compound models as in that case it is reasonable to mix the output units. It does this by modifying the output units of each sub model by using the output units of the other sub model so that we can apply the original function and get the desired result.

    Additional data has to be output in the mixed output unit case so that the units can be properly rebuilt by `with_units_from_data`.

    Outside the mixed output units, this method is identical to the base method.