prospect.io#

prospect.io.read_results#

prospect.io.read_results.results_from(filename, model_file=None, dangerous=True, **kwargs)#

Read a results file with stored model and MCMC chains.

Parameters:
  • filename – Name and path to the file holding the results. If filename ends in “h5” then it is assumed that this is an HDF5 file, otherwise it is assumed to be a pickle.

  • dangerous – (default, True) If True, use the stored paramfile text to import the parameter file and reconstitute the model object. This executes code in the stored paramfile text during import, and is therefore dangerous.

Returns results:
A dictionary of various results including:
  • “chain” - Samples from the posterior probability (ndarray).

  • “lnprobability” - The posterior probability of each sample.

  • “weights” - The weight of each sample, if dynesty was used.

  • “theta_labels” - List of strings describing free parameters.

  • “bestfit” - The prediction of the data for the posterior sample with the highest “lnprobability”, as a dictionary.

  • “run_params” - A dictionary of arguments supplied to prospector at the time of the fit.

  • “paramfile_text” - Text of the file used to run prospector, string

Returns obs:

The obs dictionary

Returns model:

The models.SedModel() object, if it could be regenerated from the stored “paramfile_text”. Otherwise, None.

prospect.io.read_results.emcee_restarter(restart_from='', niter=32, **kwargs)#

Get the obs, model, and sps objects from a previous run, as well as the run_params and initial positions (which are determined from the end of the last run, and inserted into the run_params dictionary)

Parameters:
  • restart_from – Name of the file to restart the sampling from. An error is raised if this does not include an emcee style chain of shape (nwalker, niter, ndim)

  • niter – (default: 32) Number of additional iterations to do (added toi run_params)

Returns obs:

The obs dictionary used in the last run.

Returns model:

The model object used in the last run.

Returns sps:

The sps object used in the last run.

Returns noise:

A tuple of (None, None), since it is assumed the noise model in the last run was trivial.

Returns run_params:

A dictionary of parameters controlling the operation. This is the same as used in the last run, but with the “niter” key changed, and a new “initial_positions” key that gives the ending positions of the emcee walkers from the last run. The filename from which the run is restarted is also stored in the “restart_from” key.

prospect.io.read_results.get_sps(res)#

This gets exactly the SPS object used in the fiting (modulo any changes to FSPS itself).

It (scarily) imports the paramfile (stored as text in the results dictionary) as a module and then uses the load_sps method defined in the paramfile module.

Parameters:

res – A results dictionary (the output of results_from())

Returns sps:

An sps object (i.e. from prospect.sources)

prospect.io.read_results.get_model(res)#

This gets exactly the model object used in the fiting.

It (scarily) imports the paramfile (stored as text in the results dictionary) as a module and then uses the load_model method defined in the paramfile module, with run_params dictionary passed to it.

Parameters:

res – A results dictionary (the output of results_from())

Returns model:

A prospect.models.SedModel object

prospect.io.read_results.traceplot(results, showpars=None, start=0, chains=slice(None, None, None), figsize=None, truths=None, **plot_kwargs)#

Plot the evolution of each parameter value with iteration #, for each walker in the chain.

Parameters:
  • results – A Prospector results dictionary, usually the output of results_from('resultfile').

  • showpars – (optional) A list of strings of the parameters to show. Defaults to all parameters in the "theta_labels" key of the sample_results dictionary.

  • chains – If results are from an ensemble sampler, setting chain to an integer array of walker indices will cause only those walkers to be used in generating the plot. Useful for to keep the plot from getting too cluttered.

  • start – (optional, default: 0) Integer giving the iteration number from which to start plotting.

  • **plot_kwargs

    Extra keywords are passed to the matplotlib.axes._subplots.AxesSubplot.plot() method.

Returns tracefig:

A multipaneled Figure object that shows the evolution of walker positions in the parameters given by showpars, as well as ln(posterior probability)

prospect.io.read_results.subcorner(results, showpars=None, truths=None, start=0, thin=1, chains=slice(None, None, None), logify=['mass', 'tau'], **kwargs)#

Make a triangle plot of the (thinned, latter) samples of the posterior parameter space. Optionally make the plot only for a supplied subset of the parameters.

Parameters:
  • showpars – (optional) List of string names of parameters to include in the corner plot.

  • truths – (optional) List of truth values for the chosen parameters.

  • start – (optional, default: 0) The iteration number to start with when drawing samples to plot.

  • thin – (optional, default: 1) The thinning of each chain to perform when drawing samples to plot.

  • chains – (optional) If results are from an ensemble sampler, setting chain to an integer array of walker indices will cause only those walkers to be used in generating the plot. Useful for emoving stuck walkers.

  • kwargs – Remaining keywords are passed to the corner plotting package.

  • logify – A list of parameter names to plot in log10(parameter) instead of parameter

prospect.io.read_results.compare_paramfile(res, filename)#

Compare the runtime parameter file text stored in the res dictionary to the text of some existing file with fully qualified path filename.

prospect.io.write_results#

write_results.py - Methods for writing prospector ingredients and outputs to HDF5 files as well as to pickles.

prospect.io.write_results.write_pickles(run_params, model, obs, sampler, powell_results, outroot=None, tsample=None, toptimize=None, post_burnin_center=None, post_burnin_prob=None, sampling_initial_center=None, simpleout=False, **extras)#

Write results to two different pickle files. One (*_mcmc) contains only lists, dictionaries, and numpy arrays and is therefore robust to changes in object definitions. The other (*_model) contains the actual model object (and minimization result objects) and is therefore more fragile.

prospect.io.write_results.write_hdf5(hfile, run_params, model, obs, sampler=None, optimize_result_list=None, tsample=0.0, toptimize=0.0, sampling_initial_center=[], sps=None, write_model_params=True, **extras)#

Write output and information to an HDF5 file object (or group).

Parameters:
  • hfile – File to which results will be written. Can be a string name or an h5py.File object handle.

  • run_params – The dictionary of arguments used to build and fit a model.

  • model – The prospect.models.SedModel object.

  • obs – The dictionary of observations that were fit.

  • sampler – The emcee or dynesty sampler object used to draw posterior samples. Can be None if only optimization was performed.

  • optimize_result_list – A list of scipy.optimize.OptimizationResult objects generated during the optimization stage. Can be None if no optimization is performed

param sps: (optional, default: None)

If a prospect.sources.SSPBasis object is supplied, it will be used to generate and store