autofit.Model#

class autofit.Model(cls, **kwargs)[source]#

Bases: AbstractPriorModel

@DynamicAttrs

The object a Python class is input into to create a model-component, which has free parameters that are fitted by a non-linear search.

The Model object is flexible, and can create models from many input Python data structures (e.g. a list of classes, dictionary of classes, hierarchy of classes).

For a complete description of the model composition API, see the PyAutoFit model API cookbooks:

https://pyautofit.readthedocs.io/en/latest/cookbooks/cookbook_1_basics.html

The Python class input into a Model to create a model component is written using the following format:

  • The name of the class is the name of the model component (e.g. Gaussian).

  • The input arguments of the constructor are the parameters of the mode (e.g. centre, normalization and sigma).

  • The default values of the input arguments tell PyAutoFit whether a parameter is a single-valued float or a

multi-valued tuple.

[Rich explain everything else]

Parameters:

cls – The class associated with this instance

Examples

class Gaussian:

def __init__(

self, centre=0.0, # <- PyAutoFit recognises these normalization=0.1, # <- constructor arguments are sigma=0.01, # <- the Gaussian’s parameters.

):

self.centre = centre self.normalization = normalization self.sigma = sigma

model = af.Model(Gaussian)

Methods

add_assertion

Assert that some relationship holds between physical values associated with priors at the point an instance is created.

all_paths_for_prior

Find all paths that points at the given tuple.

as_model

attribute_tuples_with_type

Tuples describing the name and instance for attributes in the model with a given type, recursively.

cast

Cast models to a new type.

check_assertions

Check that all assertions are satisfied by the given arguments.

copy

Create a copy of the model.

copy_with_fixed_priors

Recursively overwrite priors in the mapper with instance values from the instance except where the containing class is the descendant of a listed class.

dict

A dictionary representation of this object

direct_tuples_with_type

freeze

Freeze this object.

from_dict

Recursively parse a dictionary returning the model, collection or instance that is represents.

from_instance

Recursively create a prior object model from an object model.

from_json

Loads the model from a .json file, which was written using the model's dictionary (dict) attribute as follows:

from_object

Convert an arbitrary object into an appropriate prior model representation.

gaussian_prior_model_for_arguments

Returns a new instance of model mapper with a set of Gaussian priors based on tuples provided by a previous nonlinear search.

has

has_instance

True iff this model contains an instance of type cls, recursively.

has_model

True iff this model contains a Model of type cls, recursively.

index

Retrieve the index of a given path in the model

instance_flatten

Flatten an instance of this model as a PyTree.

instance_for_arguments

Returns an instance of the model for a set of arguments

instance_from_path_arguments

Create an instance from a dictionary mapping paths to tuples to corresponding values.

instance_from_prior_medians

Returns a list of physical values from the median values of the priors. :returns: If True, the assertions attached to this model (e.g. that one parameter > another parameter) are ignored and not checked. :rtype: ignore_assertions.

instance_from_prior_name_arguments

Instantiate the model from the names of priors and corresponding values.

instance_from_unit_vector

Returns a ModelInstance, which has an attribute and class instance corresponding to every Model attributed to this instance.

instance_from_vector

Returns a ModelInstance, which has an attribute and class instance corresponding to every Model attributed to this instance.

instance_unflatten

Unflatten a PyTree into an instance of this model.

is_only_model

True iff this model contains at least one Model of type cls and contains no PriorModels that are not of type cls, recursively.

items

Return (name, value) pairs for all public, non-internal attributes.

log_prior_list_from

Compute the total log prior for each parameter vector in a list.

log_prior_list_from_vector

Compute the log priors of every parameter in a vector, using the Prior of every parameter.

make_prior

Returns a prior for an attribute of a class with a given name.

make_tuple_prior

mapper_from_partial_prior_arguments

Returns a new model mapper from a dictionary mapping existing priors to new priors, keeping existing priors where no mapping is provided.

mapper_from_prior_arguments

Returns a new model mapper from a dictionary mapping existing priors to new priors.

mapper_from_prior_means

The widths of the new priors are taken from the width_config. The new gaussian priors must be provided in the same order as the priors associated with model. If a is not None then all priors are created with an absolute width of a. If r is not None then all priors are created with a relative width of r. :param means: The median PDF value of every Gaussian, which centres each GaussianPrior. :param r: The relative width to be assigned to gaussian priors :param a: print(tuples[i][1], width) The absolute width to be assigned to gaussian priors :param tuples: A list of tuples each containing the mean and width of a prior.

mapper_from_uniform_floats

The widths of the new priors are the floats value minus and plus the input bound b.

mapper_via_defaults_from

The widths of the new priors are taken from the width_config. The new gaussian priors must be provided in the same order as the priors associated with model. If a is not None then all priors are created with an absolute width of a. If r is not None then all priors are created with a relative width of r. :param means: The median PDF value of every Gaussian, which centres each GaussianPrior. :param r: The relative width to be assigned to gaussian priors :param a: print(tuples[i][1], width) The absolute width to be assigned to gaussian priors :param tuples: A list of tuples each containing the mean and width of a prior.

model_tuples_with_type

All models of the class in this model which have at least one free parameter, recursively.

models_with_type

Return all models of a given type in the model tree.

name_for_prior

Get the underscore-separated name for a prior in this model.

next_id

object_for_path

Get the object at a given path.

path_for_name

Find the path to a prior in the model that matches a given name.

path_for_object

Find a path that points at the given object.

path_for_prior

Find a path that points at the given tuple.

path_instance_tuples_for_class

Tuples containing the path tuple and instance for every instance of the class in the model tree.

prior_with_id

Retrieve a prior by its unique integer id.

product

Combine multiple models with the same structure by replacing priors with priors that contain a message which is the product of the messages of the priors with the same path in each model.

random_instance

Returns a random instance of the model.

random_instance_from_priors_within_limits

Returns a random instance of physical values by drawing uniform random values between lower and upper limits defined as unit values, using the model priors to map them from unit values to physical values.

random_unit_vector_within_limits

Generate a random vector of unit values by drawing uniform random values between 0 and 1.

random_vector_from_priors_within_limits

Returns a random vector of physical values by drawing uniform random values between lower and upper limits defined as unit values, using the model priors to map them from unit values to physical values.

replacing

Return a new model with some priors replaced.

replacing_for_path

Create a new model replacing the value for a given path with a new value

set_item_at_path

Set an item at a path in the model.

sort_priors_alphabetically

Sort priors by their paths according to this model.

take_attributes

Take all attributes with a matching path from the source prior model.

tree_flatten

Flatten this model as a PyTree.

tree_unflatten

Unflatten a PyTree into a model.

unfreeze

Unfreeze this object.

vector_from_unit_vector

with_limits

Create a new instance of this model where each prior is updated to lie between new limits.

with_paths

Recursively generate a copy of this model retaining only objects specified by the list of paths.

without_attributes

Returns a copy of this object with all priors, prior models and constants removed.

without_paths

Recursively generate a copy of this model retaining only objects not specified by the list of paths.

Attributes

all_name_prior_tuples

Maps a tuple containing all names for a given prior to that prior.

all_names

All possible names for all priors grouped such that all names for a given prior are collected together in a tuple.

all_paths

All possible paths to all priors grouped such that all paths to the same prior are collected together in a tuple.

all_paths_prior_tuples

Maps a tuple containing all paths to a given prior to that prior.

assertions

The list of assertion constraints attached to this model.

component_number

composition

A list of dot-separated path strings for each prior, ordered by prior id.

constructor_argument_names

The argument names of the constructor of the class of this model.

direct_argument_names

The names of priors, constants and other attributes that are direct attributes of this model.

direct_deferred_tuples

(name, deferred_argument) tuples for direct DeferredArgument attributes.

direct_instance_tuples

(name, value) tuples for direct float and Constant attributes.

direct_prior_model_tuples

(name, prior_model) tuples for immediate child prior models (non-recursive).

direct_prior_tuples

returns: direct_priors :rtype: [(String, Prior)]

direct_tuple_priors

(name, tuple_prior) tuples for direct TuplePrior attributes.

identifier

info

Use the priors that make up the model_mapper to generate information on each parameter of the overall model.

instance_tuples

returns: instances :rtype: [(String, instance)]

is_deferred_arguments

joined_paths

Dot-joined path strings for each unique prior, ordered by id.

label

mean_field

Implements the same interface as the graphical code

model_component_and_parameter_names

Lists each parameter's name and path, and is used for labeling visualization with parameter labels.

name

The class name of this prior model (e.g. "Model" or "Collection").

order_no

A string that can be used to order models by their parametrisation.

parameter_labels

Returns a list of labels containing latex labels of every parameter in a model.

parameter_labels_with_superscripts

Returns a list of the latex parameter label and superscript of every parameter in a model.

parameter_labels_with_superscripts_latex

Returns a list of the latex parameter label and superscript of every parameter in a model.

parameter_names

Returns a list of labels containing the name of every parameter in a model.

parameterization

Describes the path to each of the PriorModels, its class and its number of free parameters

path_float_tuples

(path, float) tuples for all fixed float values, excluding Prior objects.

path_priors_tuples

All (path, prior) tuples in this model, sorted by prior id.

paths

A list of paths to all the priors in the model, ordered by their ids

paths_formatted

A list of paths to all the priors in the model, ordered by their ids

physical_values_from_prior_medians

returns: physical_values -- A list of physical values constructed by taking the mean possible value from

prior_class_dict

Map each prior to the class it will produce when instantiated.

prior_count

How many unique priors does this model contain?

prior_model_tuples

(name, prior_model) tuples for direct child AbstractPriorModel attributes.

prior_prior_model_dict

returns: prior_prior_model_dict -- A dictionary mapping priors to associated prior models. Each prior will only

prior_tuples

returns: priors :rtype: [(String, Prior))]

prior_tuples_ordered_by_id

returns: priors -- An ordered list of unique priors associated with this mapper :rtype: [Prior]

priors

A list of all Prior objects in this model (may contain duplicates for shared priors).

priors_ordered_by_id

Unique priors sorted by their id, defining the canonical parameter ordering.

random_vector_from_priors

Generate a random vector of physical values by drawing uniform random values between 0 and 1 and using the model priors to map them from unit values to physical values.

superscripts

Returns a list of the model component superscripts for every parameter in a model.

superscripts_overwrite_via_config

Returns a list of the model component superscripts for every parameter in a model, which can be used to overwrite the default superscripts used in the function above.

total_free_parameters

Returns the prior count, but with a name that is more easy to interpret for users.

tuple_prior_tuples

returns: tuple_prior_tuples :rtype: [(String, TuplePrior)]

unique_path_prior_tuples

(path, prior) tuples deduplicated by prior identity, ordered by id.

unique_prior_paths

Paths to each unique prior (deduplicated for shared priors), ordered by id.

unique_prior_tuples

returns: prior_tuple_dict -- The set of all priors associated with this mapper :rtype: [(Prior, PriorTuple)]

property name#

The class name of this prior model (e.g. "Model" or "Collection").

property direct_argument_names: List[str]#

The names of priors, constants and other attributes that are direct attributes of this model.

instance_flatten(instance)[source]#

Flatten an instance of this model as a PyTree.

instance_unflatten(aux_data, children)[source]#

Unflatten a PyTree into an instance of this model.

Parameters:
  • aux_data

  • children

Return type:

An instance of this model.

tree_flatten()[source]#

Flatten this model as a PyTree.

classmethod tree_unflatten(aux_data, children)[source]#

Unflatten a PyTree into a model.

dict()[source]#

A dictionary representation of this object

property constructor_argument_names: List[str]#

The argument names of the constructor of the class of this model.

make_prior(attribute_name)[source]#

Returns a prior for an attribute of a class with a given name. The prior is created by searching the default prior config for the attribute.

Entries in configuration with a u become uniform priors; with a g become gaussian priors; with a c become instances.

If prior configuration for a given attribute is not specified in the configuration for a class then the configuration corresponding to the parents of that class is searched. If no configuration can be found then a prior exception is raised.

Parameters:

attribute_name (str) – The name of the attribute for which a prior is created

Returns:

prior – A prior

Return type:

p.Prior

Raises:

exc.PriorException – If no configuration can be found

gaussian_prior_model_for_arguments(arguments)[source]#

Returns a new instance of model mapper with a set of Gaussian priors based on tuples provided by a previous nonlinear search.

Parameters:

arguments ([(float, float)]) – Tuples providing the mean and sigma of gaussians

Returns:

new_model – A new model mapper populated with Gaussian priors

Return type:

ModelMapper