Parameter¶
-
class
astropy.modeling.
Parameter
(name='', description='', default=None, unit=None, getter=None, setter=None, fixed=False, tied=False, min=None, max=None, bounds=None, prior=None, posterior=None, model=None)[source] [edit on github]¶ Bases:
astropy.utils.misc.OrderedDescriptor
Wraps individual parameters.
This class represents a model’s parameter (in a somewhat broad sense). It acts as both a descriptor that can be assigned to a class attribute to describe the parameters accepted by an individual model (this is called an “unbound parameter”), or it can act as a proxy for the parameter values on an individual model instance (called a “bound parameter”).
Parameter instances never store the actual value of the parameter directly. Rather, each instance of a model stores its own parameters parameter values in an array. A bound Parameter simply wraps the value in a Parameter proxy which provides some additional information about the parameter such as its constraints. In other words, this is a high-level interface to a model’s adjustable parameter values.
Unbound Parameters are not associated with any specific model instance, and are merely used by model classes to determine the names of their parameters and other information about each parameter such as their default values and default constraints.
See Parameters for more details.
Parameters: - name : str
parameter name
- description : str
parameter description
- default : float or array
default value to use for this parameter
- unit :
Unit
if specified, the parameter will be in these units, and when the parameter is updated in future, it should be set to a
Quantity
that has equivalent units.- getter : callable
a function that wraps the raw (internal) value of the parameter when returning the value through the parameter proxy (eg. a parameter may be stored internally as radians but returned to the user as degrees)
- setter : callable
a function that wraps any values assigned to this parameter; should be the inverse of getter
- fixed : bool
if True the parameter is not varied during fitting
- tied : callable or False
if callable is supplied it provides a way to link the value of this parameter to another parameter (or some other arbitrary function)
- min : float
the lower bound of a parameter
- max : float
the upper bound of a parameter
- bounds : tuple
specify min and max as a single tuple–bounds may not be specified simultaneously with min or max
- model :
Model
instance binds the the
Parameter
instance to a specific model upon instantiation; this should only be used internally for creating bound Parameters, and should not be used forParameter
descriptors defined as class attributes
Attributes Summary
bounds
The minimum and maximum values of a parameter as a tuple constraints
Types of constraints a parameter can have. default
Parameter default value fixed
Boolean indicating if the parameter is kept fixed during fitting. max
A value used as an upper bound when fitting a parameter min
A value used as a lower bound when fitting a parameter name
Parameter name posterior
prior
quantity
This parameter, as a Quantity
instance.shape
The shape of this parameter’s value array. size
The size of this parameter’s value array. tied
Indicates that this parameter is linked to another one. unit
The unit attached to this parameter, if any. validator
Used as a decorator to set the validator method for a Parameter
.value
The unadorned value proxied by this parameter. Methods Summary
copy
([name, description, default, unit, …])Make a copy of this Parameter
, overriding any of its core attributes in the process (or an exact copy).Attributes Documentation
-
bounds
¶ The minimum and maximum values of a parameter as a tuple
-
constraints
= ('fixed', 'tied', 'bounds', 'prior', 'posterior')¶ Types of constraints a parameter can have. Excludes ‘min’ and ‘max’ which are just aliases for the first and second elements of the ‘bounds’ constraint (which is represented as a 2-tuple). ‘prior’ and ‘posterior’ are available for use by user fitters but are not used by any built-in fitters as of this writing.
-
default
¶ Parameter default value
-
fixed
¶ Boolean indicating if the parameter is kept fixed during fitting.
-
max
¶ A value used as an upper bound when fitting a parameter
-
min
¶ A value used as a lower bound when fitting a parameter
-
name
¶ Parameter name
-
posterior
¶
-
prior
¶
-
shape
¶ The shape of this parameter’s value array.
-
size
¶ The size of this parameter’s value array.
-
tied
¶ Indicates that this parameter is linked to another one.
A callable which provides the relationship of the two parameters.
-
unit
¶ The unit attached to this parameter, if any.
On unbound parameters (i.e. parameters accessed through the model class, rather than a model instance) this is the required/ default unit for the parameter.
-
validator
¶ Used as a decorator to set the validator method for a
Parameter
. The validator method validates any value set for that parameter. It takes two arguments–self
, which refers to theModel
instance (remember, this is a method defined on aModel
), and the value being set for this parameter. The validator method’s return value is ignored, but it may raise an exception if the value set on the parameter is invalid (typically anInputParameterError
should be raised, though this is not currently a requirement).The decorator returns the
Parameter
instance that the validator is set on, so the underlying validator method should have the same name as theParameter
itself (think of this as analogous toproperty.setter
). For example:>>> from astropy.modeling import Fittable1DModel >>> class TestModel(Fittable1DModel): ... a = Parameter() ... b = Parameter() ... ... @a.validator ... def a(self, value): ... # Remember, the value can be an array ... if np.any(value < self.b): ... raise InputParameterError( ... "parameter 'a' must be greater than or equal " ... "to parameter 'b'") ... ... @staticmethod ... def evaluate(x, a, b): ... return a * x + b ... >>> m = TestModel(a=1, b=2) Traceback (most recent call last): ... InputParameterError: parameter 'a' must be greater than or equal to parameter 'b' >>> m = TestModel(a=2, b=2) >>> m.a = 0 Traceback (most recent call last): ... InputParameterError: parameter 'a' must be greater than or equal to parameter 'b'
On bound parameters this property returns the validator method itself, as a bound method on the
Parameter
. This is not often as useful, but it allows validating a parameter value without setting that parameter:>>> m.a.validator(42) # Passes >>> m.a.validator(-42) Traceback (most recent call last): ... InputParameterError: parameter 'a' must be greater than or equal to parameter 'b'
-
value
¶ The unadorned value proxied by this parameter.
Methods Documentation
-
copy
(name=None, description=None, default=None, unit=None, getter=None, setter=None, fixed=False, tied=False, min=None, max=None, bounds=None, prior=None, posterior=None)[source] [edit on github]¶ Make a copy of this
Parameter
, overriding any of its core attributes in the process (or an exact copy).The arguments to this method are the same as those for the
Parameter
initializer. This simply returns a newParameter
instance with any or all of the attributes overridden, and so returns the equivalent of:Parameter(self.name, self.description, ...)