astro py:docs

- Index
- Modules

### Navigation

- next »
- « previous |
- Astropy v5.2.1 »
- Models and Fitting (`astropy.modeling`) »
- Reference/API »
- custom_model

# custom_model

astropy.modeling.custom_model(*args, fit_deriv=None)

  Create a model from a user defined function. The inputs and parameters of the model will be inferred from the arguments of the function.

  This can be used either as a function or as a decorator. See below for examples of both usages.

  The model is separable only if there is a single input.

  Note

  All model parameters have to be defined as keyword arguments with default values in the model function. Use `None` as a default argument value if you do not want to have a default value for that parameter.

  The standard settable model properties can be configured by default using keyword arguments matching the name of the property; however, these values are not set as model “parameters”. Moreover, users cannot use keyword arguments matching non-settable model properties, with the exception of `n_outputs` which should be set to the number of outputs of your function.

  Parameters:
    func python:function

      Function which defines the model. It should take N positional arguments where `N` is dimensions of the model (the number of independent variable in the model), and any number of keyword arguments (the parameters). It must return the value of the model (typically as an array, but can also be a scalar for scalar inputs). This corresponds to the `evaluate` method.

    fit_deriv python:function, optional

      Function which defines the Jacobian derivative of the model. I.e., the derivative with respect to the parameters of the model. It should have the same argument signature as `func`, but should return a sequence where each element of the sequence is the derivative with respect to the corresponding argument. This corresponds to the `fit_deriv()` method.

  Examples

  Define a sinusoidal model function as a custom 1D model:

      >>> from astropy.modeling.models import custom_model
      >>> import numpy as np
      >>> def sine_model(x, amplitude=1., frequency=1.):
      ...     return amplitude * np.sin(2 * np.pi * frequency * x)
      >>> def sine_deriv(x, amplitude=1., frequency=1.):
      ...     return 2 * np.pi * amplitude * np.cos(2 * np.pi * frequency * x)
      >>> SineModel = custom_model(sine_model, fit_deriv=sine_deriv)

  Create an instance of the custom model and evaluate it:

      >>> model = SineModel()
      >>> model(0.25)
      1.0

  This model instance can now be used like a usual astropy model.

  The next example demonstrates a 2D Moffat function model, and also demonstrates the support for docstrings (this example could also include a derivative, but it has been omitted for simplicity):

      >>> @custom_model
      ... def Moffat2D(x, y, amplitude=1.0, x_0=0.0, y_0=0.0, gamma=1.0,
      ...            alpha=1.0):
      ...     """Two dimensional Moffat function."""
      ...     rr_gg = ((x - x_0) ** 2 + (y - y_0) ** 2) / gamma ** 2
      ...     return amplitude * (1 + rr_gg) ** (-alpha)
      ...
      >>> print(Moffat2D.__doc__)
      Two dimensional Moffat function.
      >>> model = Moffat2D()
      >>> model(1, 1)
      0.3333333333333333