autofit.ModelMapper#

class autofit.ModelMapper(*arguments, **kwargs)[source]#

Bases: Collection

A mapper of priors formed by passing in classes to be reconstructed

@DynamicAttrs

The ModelMapper converts a set of classes whose input attributes may be modeled using a non-linear search, to parameters with priors attached.

A config is passed into the model mapper to provide default setup values for the priors:

mapper = ModelMapper(config)

All class instances that are to be generated by the model mapper are specified by adding classes to it:

mapper = ModelMapper()

mapper.sersic = al.lp.AbstractSersic mapper.gaussian = al.lp.Gaussian mapper.any_class = SomeClass

A Model instance is created each time we add a class to the mapper. We can access those models using # the mapper attributes:

sersic_model = mapper.sersic

This allows us to replace the default priors:

mapper.sersic.normalization = GaussianPrior(mean=2., sigma=5.)

Or maybe we want to tie two priors together:

mapper.sersic.two = mapper.other_sersic.two

This statement reduces the number of priors by one and means that the two sersic instances will always share # the same rotation two two.

We can then create instances of every class for a unit hypercube vector with length equal to # len(mapper.priors):

model_instance = mapper.model_instance_for_vector([.4, .2, .3, .1])

The attributes of the model_instance are named the same as those of the mapper:

sersic_1 = mapper.sersic_1

But this attribute is an instance of the actual AbstractSersic:P class

A ModelMapper can be concisely constructed using keyword arguments:

mapper = prior.ModelMapper(

source_light_profile=light_profile.AbstractSersic, lens_mass_profile=mass_profile.IsothermalCore, lens_light_profile=light_profile.SersicCore

)

The object multiple Python classes are input into to create model-components, which has free parameters that are fitted by a non-linear search.

Multiple Python classes can be input into a Collection in order to compose high dimensional models made of multiple model-components.

The Collection object is highly 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 document more clearly]

A prior model used to represent a list of prior models for convenience.

Arguments are flexibly converted into a collection.

Parameters:

arguments – Classes, prior models, instances or priors

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.Collection(gaussian_0=Gaussian, gaussian_1=Gaussian)

Methods

add_assertion

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

add_dict_items

all_paths_for_prior

Find all paths that points at the given tuple.

append

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

gaussian_prior_model_for_arguments

Create a new collection, updating its priors according to the argument dictionary.

has

param cls:

The type to check for

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_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.

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.

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

log_prior_list_from

log_prior_list_from_vector

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

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 no_limits: If True generated priors have infinite limits :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.

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

Construct a name for the prior.

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_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

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.

remove

replacing

replacing_for_path

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

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

tree_unflatten

unfreeze

Unfreeze this object.

vector_from_unit_vector

param unit_vector:

A unit hypercube 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.

with_prefix

Filter members of the collection, only returning those that start with a given prefix as a new collection.

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

component_number

composition

direct_deferred_tuples

direct_instance_tuples

direct_prior_model_tuples

direct_prior_tuples

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

direct_tuple_priors

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)]

joined_paths

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

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_priors_tuples

paths

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

prior_count

How many unique priors does this model contain?

prior_model_tuples

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

priors_ordered_by_id

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

unique_prior_paths

unique_prior_tuples

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

values

property prior_prior_model_dict#
returns: prior_prior_model_dict – A dictionary mapping priors to associated prior models. Each prior will only

have one prior model; if a prior is shared by two prior models then one of those prior models will be in this dictionary.

Return type:

{Prior: Model}