autofit.Collection#
- class autofit.Collection(*arguments, **kwargs)[source]#
Bases:
AbstractPriorModelThe 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
Collectionobject 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
Modelto 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,normalizationandsigma).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_assertionAssert that some relationship holds between physical values associated with priors at the point an instance is created.
Add all entries from a dictionary, converting values to prior models.
all_paths_for_priorFind all paths that points at the given tuple.
Append an item to the collection with an auto-incremented numeric key.
Convert all prior models in this collection to Model instances.
attribute_tuples_with_typeTuples describing the name and instance for attributes in the model with a given type, recursively.
castCast models to a new type.
check_assertionsCheck that all assertions are satisfied by the given arguments.
copyCreate a copy of the model.
copy_with_fixed_priorsRecursively overwrite priors in the mapper with instance values from the instance except where the containing class is the descendant of a listed class.
dictA dictionary representation of this object
direct_tuples_with_typefreezeFreeze this object.
from_dictRecursively parse a dictionary returning the model, collection or instance that is represents.
from_instanceRecursively create a prior object model from an object model.
from_jsonLoads the model from a .json file, which was written using the model's dictionary (dict) attribute as follows:
from_objectConvert an arbitrary object into an appropriate prior model representation.
Create a new collection, updating its priors according to the argument dictionary.
hashas_instanceTrue iff this model contains an instance of type cls, recursively.
has_modelTrue iff this model contains a Model of type cls, recursively.
indexRetrieve the index of a given path in the model
instance_for_argumentsReturns an instance of the model for a set of arguments
instance_from_path_argumentsCreate an instance from a dictionary mapping paths to tuples to corresponding values.
instance_from_prior_mediansReturns 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_argumentsInstantiate the model from the names of priors and corresponding values.
instance_from_unit_vectorReturns a ModelInstance, which has an attribute and class instance corresponding to every Model attributed to this instance.
instance_from_vectorReturns a ModelInstance, which has an attribute and class instance corresponding to every Model attributed to this instance.
is_only_modelTrue iff this model contains at least one Model of type cls and contains no PriorModels that are not of type cls, recursively.
The (key, model_component) pairs in this collection.
log_prior_list_fromCompute the total log prior for each parameter vector in a list.
log_prior_list_from_vectorCompute the log priors of every parameter in a vector, using the Prior of every parameter.
mapper_from_partial_prior_argumentsReturns a new model mapper from a dictionary mapping existing priors to new priors, keeping existing priors where no mapping is provided.
mapper_from_prior_argumentsReturns a new model mapper from a dictionary mapping existing priors to new priors.
mapper_from_prior_meansThe 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_floatsThe widths of the new priors are the floats value minus and plus the input bound b.
mapper_via_defaults_fromThe 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_typeAll models of the class in this model which have at least one free parameter, recursively.
models_with_typeReturn all models of a given type in the model tree.
Construct a name for the prior.
next_idobject_for_pathGet the object at a given path.
path_for_nameFind the path to a prior in the model that matches a given name.
path_for_objectFind a path that points at the given object.
path_for_priorFind a path that points at the given tuple.
path_instance_tuples_for_classTuples containing the path tuple and instance for every instance of the class in the model tree.
prior_with_idRetrieve a prior by its unique integer id.
productCombine 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_instanceReturns a random instance of the model.
random_instance_from_priors_within_limitsReturns 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_limitsGenerate a random vector of unit values by drawing uniform random values between 0 and 1.
random_vector_from_priors_within_limitsReturns 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 an item from the collection by value equality.
replacingReturn a new model with some priors replaced.
replacing_for_pathCreate a new model replacing the value for a given path with a new value
set_item_at_pathSet an item at a path in the model.
sort_priors_alphabeticallySort priors by their paths according to this model.
take_attributesTake all attributes with a matching path from the source prior model.
Flatten this collection into a JAX-compatible PyTree representation.
Reconstruct a Collection from a flattened PyTree.
unfreezeUnfreeze this object.
vector_from_unit_vectorwith_limitsCreate a new instance of this model where each prior is updated to lie between new limits.
with_pathsRecursively generate a copy of this model retaining only objects specified by the list of paths.
Filter members of the collection, only returning those that start with a given prefix as a new collection.
without_attributesReturns a copy of this object with all priors, prior models and constants removed.
without_pathsRecursively generate a copy of this model retaining only objects not specified by the list of paths.
Attributes
all_name_prior_tuplesMaps a tuple containing all names for a given prior to that prior.
all_namesAll possible names for all priors grouped such that all names for a given prior are collected together in a tuple.
all_pathsAll possible paths to all priors grouped such that all paths to the same prior are collected together in a tuple.
all_paths_prior_tuplesMaps a tuple containing all paths to a given prior to that prior.
assertionsThe list of assertion constraints attached to this model.
component_numbercompositionA 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_tuplesreturns: direct_priors :rtype: [(String, Prior)]
direct_tuple_priors(name, tuple_prior) tuples for direct TuplePrior attributes.
identifierinfoUse the priors that make up the model_mapper to generate information on each parameter of the overall model.
instance_tuplesreturns: instances :rtype: [(String, instance)]
joined_pathsDot-joined path strings for each unique prior, ordered by id.
labelmean_fieldImplements the same interface as the graphical code
model_component_and_parameter_namesLists each parameter's name and path, and is used for labeling visualization with parameter labels.
nameThe class name of this prior model (e.g.
"Model"or"Collection").order_noA string that can be used to order models by their parametrisation.
parameter_labelsReturns a list of labels containing latex labels of every parameter in a model.
parameter_labels_with_superscriptsReturns a list of the latex parameter label and superscript of every parameter in a model.
parameter_labels_with_superscripts_latexReturns a list of the latex parameter label and superscript of every parameter in a model.
parameter_namesReturns a list of labels containing the name of every parameter in a model.
parameterizationDescribes 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_tuplesAll (path, prior) tuples in this model, sorted by prior id.
pathsA list of paths to all the priors in the model, ordered by their ids
paths_formattedA list of paths to all the priors in the model, ordered by their ids
physical_values_from_prior_mediansreturns: physical_values -- A list of physical values constructed by taking the mean possible value from
Map each prior to the class it will produce when instantiated.
prior_countHow many unique priors does this model contain?
prior_model_tuples(name, prior_model) tuples for direct child AbstractPriorModel attributes.
prior_prior_model_dictreturns: prior_prior_model_dict -- A dictionary mapping priors to associated prior models. Each prior will only
prior_tuplesreturns: priors :rtype: [(String, Prior))]
prior_tuples_ordered_by_idreturns: priors -- An ordered list of unique priors associated with this mapper :rtype: [Prior]
priorsA list of all Prior objects in this model (may contain duplicates for shared priors).
priors_ordered_by_idUnique priors sorted by their id, defining the canonical parameter ordering.
random_vector_from_priorsGenerate 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.
superscriptsReturns a list of the model component superscripts for every parameter in a model.
superscripts_overwrite_via_configReturns 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_parametersReturns the prior count, but with a name that is more easy to interpret for users.
tuple_prior_tuplesreturns: tuple_prior_tuples :rtype: [(String, TuplePrior)]
unique_path_prior_tuples(path, prior) tuples deduplicated by prior identity, ordered by id.
unique_prior_pathsPaths to each unique prior (deduplicated for shared priors), ordered by id.
unique_prior_tuplesreturns: prior_tuple_dict -- The set of all priors associated with this mapper :rtype: [(Prior, PriorTuple)]
The model components in this collection as a list.
- name_for_prior(prior: Prior) str[source]#
Construct a name for the prior. This is the path taken to get to the prior.
- Parameters:
prior
- Return type:
A string of object names joined by underscores
- tree_flatten()[source]#
Flatten this collection into a JAX-compatible PyTree representation.
- Returns:
A (children, aux_data) pair where children are the values and aux_data are the corresponding keys.
- Return type:
- classmethod tree_unflatten(aux_data, children)[source]#
Reconstruct a Collection from a flattened PyTree.
- Parameters:
aux_data – The keys of the collection items.
children – The values of the collection items.
- property values#
The model components in this collection as a list.
- with_prefix(prefix: str)[source]#
Filter members of the collection, only returning those that start with a given prefix as a new collection.
- as_model()[source]#
Convert all prior models in this collection to Model instances.
Returns a new Collection where each AbstractPriorModel child has been converted via its own as_model() method.
- add_dict_items(item_dict)[source]#
Add all entries from a dictionary, converting values to prior models.
- Parameters:
item_dict – A dictionary mapping string keys to classes, instances, or prior models.
- append(item)[source]#
Append an item to the collection with an auto-incremented numeric key.
The item is converted to an AbstractPriorModel if it is not already one.
- remove(item)[source]#
Remove an item from the collection by value equality.
- Parameters:
item – The item to remove. All entries whose value equals this item are deleted.
- gaussian_prior_model_for_arguments(arguments)[source]#
Create a new collection, updating its priors according to the argument dictionary.
- Parameters:
arguments – A dictionary of arguments
- Return type:
A new collection
- property prior_class_dict#
Map each prior to the class it will produce when instantiated.
For child prior models, delegates to their own prior_class_dict. Direct priors on the collection itself map to ModelInstance.