Create a model from a user defined function. The inputs and parameters of the model will be inferred from the arguments of the function.
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.
Parameters: | func : function
func_fit_deriv : function, optional
|
---|
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, func_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 beta function model, and also demonstrates the support for docstrings (this example could also include a derivative, but it has been ommitted for simplicity):
>>> @custom_model
... def Beta2D(x, y, amplitude=1.0, x_0=0.0, y_0=0.0, gamma=1.0,
... alpha=1.0):
... """Two dimensional beta function."""
... rr_gg = ((x - x_0) ** 2 + (y - y_0) ** 2) / gamma ** 2
... return amplitude * (1 + rr_gg) ** (-alpha)
...
>>> print(Beta2D.__doc__)
Two dimensional beta function.
>>> model = Beta2D()
>>> model(1, 1)
0.3333333333333333