autofit.Nautilus#
- class Nautilus[source]#
Bases:
AbstractNestA Nautilus non-linear search.
Nautilus is an optional requirement and must be installed manually via the command pip install nautilus-sampler. It is optional as it has certain dependencies which are generally straight forward to install.
For a full description of Nautilus checkout its Github and documentation webpages:
https://github.com/johannesulf/nautilus https://nautilus-sampler.readthedocs.io/en/stable/index.html
- Parameters:
name (
Optional[str]) – The name of the search, controlling the last folder results are output.path_prefix (
Optional[str]) – The path of folders prefixing the name folder where results are output.unique_tag (
Optional[str]) – The name of a unique tag for this model-fit, which will be given a unique entry in the sqlite database and also acts as the folder after the path prefix and before the search name.n_live (
int) – Number of live points used for sampling.n_batch (
int) – Number of likelihood evaluations performed at each step.n_like_max (
float) – Maximum number of likelihood evaluations before stopping.f_live (
float) – Maximum fraction of evidence in the live set before terminating.n_eff (
int) – Minimum effective sample size before stopping.iterations_per_full_update (
int) – The number of iterations performed between update (e.g. output latest model to hard-disk, visualization).number_of_cores (
int) – The number of cores sampling is performed using a Python multiprocessing Pool instance.silence (
bool) – If True, the default print output of the non-linear search is silenced.force_x1_cpu (
bool) – If True, force single-CPU mode even when number_of_cores > 1.session (
Optional[Session]) – An SQLalchemy session instance so the results of the model-fit are written to an SQLite database.
Methods
Override in subclasses to reduce sampler iterations for test mode.
The x1 CPU and multiprocessing searches both call this function to perform the non-linear search.
check_modelcopy_with_pathsexact_fitfitFit a model, M with some function f that takes instances of the class represented by model M and gives a score for their fitness.
Perform the non-linear search, using multiple CPU cores parallelized via Python's multiprocessing module.
Perform the non-linear search, using one CPU core.
Returns the next number of iterations that a dynesty call will use and the total number of iterations that have been performed so far.
make_poolMake the pool instance used to parallelize a NonLinearSearch alongside a set of unique ids for every process in the pool.
make_sneakier_poolmake_sneaky_poolCreate a pool for multiprocessing that uses slight-of-hand to avoid copying the fitness function between processes multiple times.
optimisePerform optimisation for expectation propagation.
Output the sampler results to hard-disk in their internal format.
perform_updatePerform an update of the non-linear search's model-fitting results.
perform_visualizationPerform visualization of the non-linear search's model-fitting results.
plot_resultsplot_start_pointVisualize the starting point of the non-linear search, using an instance of the model at the starting point of the maximum likelihood estimator.
post_fit_outputCleans up the output folderds after a completed non-linear search.
pre_fit_outputOutputs attributes of fit before the non-linear search begins.
result_via_completed_fitReturns the result of the non-linear search of a completed model-fit.
samples_fromLoads the samples of a non-linear search from its output files.
Returns a Samples object from the nautilus internal results.
start_resume_fitAttributes
The path to the file used for checkpointing.
loggerLog 'msg % args' with severity 'DEBUG'.
namepathsshould_plot_start_pointtimerReturns the timer of the search, which is used to output informaiton such as how long the search took and how much parallelization sped up the search time.
- apply_test_mode()[source]#
Override in subclasses to reduce sampler iterations for test mode.
Called during __init__ when test mode is active (level 1). Subclasses should directly mutate instance attributes to minimize the number of iterations the sampler performs.
- property sampler_cls#
- property checkpoint_file#
The path to the file used for checkpointing.
If autofit is not outputting results to hard-disk (e.g. paths is NullPaths), this function is bypassed.
- fit_x1_cpu(fitness, model, analysis)[source]#
Perform the non-linear search, using one CPU core.
This is used if the likelihood function calls external libraries that cannot be parallelized or use threading in a way that conflicts with the parallelization of the non-linear search.
- Parameters:
fitness – The function which takes a model instance and returns its log likelihood via the Analysis class
model – The model which maps parameters chosen via the non-linear search (e.g. via the priors or sampling) to instances of the model, which are passed to the fitness function.
analysis – Contains the data and the log likelihood function which fits an instance of the model to the data, returning the log likelihood the search maximizes.
- fit_multiprocessing(fitness, model, analysis)[source]#
Perform the non-linear search, using multiple CPU cores parallelized via Python’s multiprocessing module.
This uses PyAutoFit’s sneaky pool class, which allows us to use the multiprocessing module in a way that plays nicely with the non-linear search (e.g. exception handling, keyboard interupts, etc.).
Multiprocessing parallelization can only parallelize across multiple cores on a single device, it cannot be distributed across multiple devices or computing nodes. For that, use the fit_mpi method.
- Parameters:
fitness – The function which takes a model instance and returns its log likelihood via the Analysis class
model – The model which maps parameters chosen via the non-linear search (e.g. via the priors or sampling) to instances of the model, which are passed to the fitness function.
analysis – Contains the data and the log likelihood function which fits an instance of the model to the data, returning the log likelihood the search maximizes.
- call_search(search_internal, model, analysis, fitness)[source]#
The x1 CPU and multiprocessing searches both call this function to perform the non-linear search.
This function calls the search a reduced number of times, corresponding to the iterations_per_full_update of the search. This allows the search to output results on-the-fly, for example writing to the hard-disk the latest model and samples.
It tracks how often to do this update alongside the maximum number of iterations the search will perform. This ensures that on-the-fly output is performed at regular intervals and that the search does not perform more iterations than the n_like_max input variable.
- Parameters:
search_internal – The single CPU or multiprocessing search which is run and performs nested sampling.
model – The model which maps parameters chosen via the non-linear search (e.g. via the priors or sampling) to instances of the model, which are passed to the fitness function.
analysis – Contains the data and the log likelihood function which fits an instance of the model to the data, returning the log likelihood the search maximizes.
- iterations_from(search_internal)[source]#
Returns the next number of iterations that a dynesty call will use and the total number of iterations that have been performed so far.
This is used so that the iterations_per_full_update input leads to on-the-fly output of dynesty results.
It also ensures dynesty does not perform more samples than the n_like_max input variable.
- Parameters:
search_internal – The Dynesty sampler (static or dynamic) which is run and performs nested sampling.
- Returns:
The next number of iterations that a dynesty run sampling will perform and the total number of iterations
it has performed so far.
- output_search_internal(search_internal)[source]#
Output the sampler results to hard-disk in their internal format.
The multiprocessing Pool object cannot be pickled and thus the sampler cannot be saved to hard-disk. This function therefore extracts the necessary information from the sampler and saves it to hard-disk.
- Parameters:
sampler – The nautilus sampler object containing the results of the model-fit.
- samples_via_internal_from(model, search_internal=None)[source]#
Returns a Samples object from the nautilus internal results.
The samples contain all information on the parameter space sampling (e.g. the parameters, log likelihoods, etc.).
The internal search results are converted from the native format used by the search to lists of values (e.g. parameter_lists, log_likelihood_list).
- Parameters:
model (
AbstractPriorModel) – Maps input vectors of unit parameter values to physical values and model instances via priors.
- property batch_size#