nessai.flowsampler
Main code that handles running and checkpoiting the sampler.
Module Contents
Classes
Main class to handle running the nested sampler. |
- class nessai.flowsampler.FlowSampler(model, output=os.getcwd(), importance_nested_sampler=False, resume=True, resume_file='nested_sampler_resume.pkl', resume_data=None, weights_file=None, weights_path=None, signal_handling=True, exit_code=130, pytorch_threads=1, close_pool=True, eps=None, torch_dtype=None, disable_vectorisation=False, likelihood_chunksize=None, allow_multi_valued_likelihood=None, parallelise_prior=None, result_extension='hdf5', **kwargs)
Main class to handle running the nested sampler.
- Parameters:
- model
nessai.model.Model
User-defined model.
- outputstr, optional
Output directory
- resumebool, optional
If True try to resume the sampler is the resume file exists.
- resume_filestr, optional
File to resume sampler from.
- resume_data: Any, optional
Data to resume the sampler from instead of a resume file. The data will be passed to the
resume_from_pickled_sampler
of the relevant class.- weights_pathstr, optional
Path to either the weights file or directory containing subdirectories with weight files.
- weights_filestr, optional
Weights file to use, same as
weights_path
and included for backwards_compatibility. Will be ignored isweights_paths
has also been specified.- pytorch_threadsint
Maximum number of threads to use for torch. If
None
torch uses all available threads.- signal_handlingbool
Enable or disable signal handling.
- exit_codeint, optional
Exit code to use when forcibly exiting the sampler.
- close_poolbool
If True, the multiprocessing pool will be closed once the run method has been called. Disables the option in
NestedSampler
if enabled.- disable_vectorisationbool
Disable likelihood vectorisation. Overrides the value of
nessai.model.Model.allow_vectorised
.- likelihood_chunksizeOptional[int]
Chunksize used when evaluating a vectorised likelihood. Overrides the of
nessai.model.Model.likelihood_chunksize
. Set to None to evaluate the likelihood with all available points.- allow_multi_valued_likelihoodOptional[bool]
Allow for a multi-valued likelihood function that will return different likelihood values for the same point in parameter space. See
nessai.model.Model.allow_multi_valued_likelihood
for more details.- parallelise_priorOptional[bool]
If true, and a multiprocessing pool has been specified, then the log-prior calculation will be parallelised using multiprocessing. If false, then the pool with not be used. Overrides the value of the parallelise_prior attribute in the model class, which is false by default.
- result_extensionstr
Extension used when saving the result format. Defaults to HDF5, but also supports JSON.
- kwargs
Keyword arguments passed to
NestedSampler
.
- model
- property log_evidence
Return the most recent log evidence
- property log_evidence_error
Return the most recent log evidence error
- property nested_samples
Return the nested samples
- check_resume(resume_file, resume_data)
Check if it is possible to resume the sampler
- run(plot=True, save=True, posterior_sampling_method=None, close_pool=None, **kwargs)
Run the nested sampler.
Will pick the correct run method given the configuration used.
- Parameters:
- plotbool
Toggle all plots produced once the sampler has converged.
- savebool, optional
Toggle automatic saving of results
- posterior_sampling_methodstr, optional
Method used for drawing posterior samples. Defaults to rejection sampling.
- close_poolbool, optional
Boolean to indicated if the pool should be closed at the end of the run function. If False, the user must manually close the pool. If specified, this value overrides the value passed when initialising the FlowSampler class.
- run_standard_sampler(plot=True, plot_indices=True, plot_posterior=True, plot_logXlogL=True, save=True, posterior_sampling_method=None, close_pool=None)
Run the standard nested sampler.
- Parameters:
- plotbool
Toggle all plots produced once the sampler has converged.
- plot_indicesbool
Toggle the insertion indices plot.
- plot_posteriorbool
Toggle the posterior distribution plot.
- plot_logXlogLbool
Toggle the log-prior volume vs log-likelihood plot.
- save
Enable or disable saving of a results file.
- posterior_sampling_method
Method used for drawing posterior samples. Defaults to rejection sampling.
- close_poolbool
Boolean to indicated if the pool should be closed at the end of the run function. If False, the user must manually close the pool. If specified, this value overrides the value passed when initialising the class.
- run_importance_nested_sampler(plot=True, plot_posterior=True, save=True, posterior_sampling_method=None, redraw_samples=False, n_posterior_samples=None, compute_initial_posterior=False, close_pool=None, **kwargs)
Run the importance nested sampler.
- Parameters:
- plot
Enable or disable plotting. Independent of the value passed to the
NestedSampler
object.- save
Enable or disable saving of a results file.
- posterior_sampling_method
Method used for drawing posterior samples. Defaults to importance sampling.
- redraw_samples
If True after the sampling is finished, samples are redrawn from the meta proposal and used to compute an updated evidence estimate and posterior. This can reduce biases in the results.
- n_posterior_samples
Number of posterior samples to draw when when redrawing samples.
- compute_initial_posterior
Enables or disables computing the posterior before redrawing samples. If
redraw_samples
is False, then this flag is ignored.- close_poolbool
Boolean to indicated if the pool should be closed at the end of the run function. If False, the user must manually close the pool. If specified, this value overrides the value passed when initialising the class.
- kwargs
Keyword arguments passed to
draw_final_samples()
- save_kwargs(kwargs: dict) None
Save the dictionary of keyword arguments used.
Uses an encoder class to handle numpy arrays.
- Parameters:
- kwargsdict
Dictionary of kwargs to save.
- save_results(filename: str, extension: str | None = None) None
Save the results from sampling to a specific results file.
- Parameters:
- filenamestr
Name of file to save results to.
- extensionOptional[str]
File extension to used. If not specified, it will be inferred from the filename.
- terminate_run(code=None)
Terminate a sampling run.
- Parameters:
- codeint, optional
Code passed to
close_pool
- safe_exit(signum=None, frame=None)
Safely exit. This includes closing the multiprocessing pool.