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

Add all entries from a dictionary, converting values to prior models.

all_paths_for_prior

Find all paths that points at the given tuple.

append

Append an item to the collection with an auto-incremented numeric key.

as_model

Convert all prior models in this collection to Model instances.

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

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

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

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

The (key, model_component) pairs in this collection.

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.

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

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

remove

Remove an item from the collection by value equality.

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 collection into a JAX-compatible PyTree representation.

tree_unflatten

Reconstruct a Collection from a flattened PyTree.

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.

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

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.

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

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

values

The model components in this collection as a list.

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}