ACID_code.Result

class ACID_code.Result(data: Data | object, sampler: EnsembleSampler | None = None, process_results: bool = True, verbose: IntLike | bool | str | None = None)[source]

Class to handle the results from the Acid MCMC sampling, and results processing. Fundamentally, this class requires two objects to run, the Sampler object and the Data object, both of which can be obtained from the Acid object. If one or the other is not provided, some methods will not work.

Initialize the Result class

Parameters:
  • data (Data | Acid) – An Acid object or Data object (contained in Acid class). If an Acid object is provided, all other arguments are taken from there. If a Data object is provided, a sampler can be provided in the second argument. If a sampler object is provided, it will be used as the sampler, but all other attributes will need to be set manually for the Result object to be fully functional.

  • sampler (emcee.EnsembleSampler | dynesty.Sampler, optional) – Sets and overwrites the sampler in the Data object with this if provided, by default None.

  • process_results (bool, optional) – Whether to process the results from the Acid object upon initialisation, by default True. If False, the profiles attribute will not be available until Result.process_results() is called. The process_results functions does a LSD call, which can be skipped to save time and use the Result object for methods that do not require the profiles attribute, such as continue_sampling() or plot_walkers(). This requires a Data object with the necessary attributes, and a sampler object in the initialisation, or an Acid object with the necessary attributes already set. By default, True.

  • verbose (IntLike | bool | str, optional) – Verbosity level, works exactly the same as Acid, if not provided defaults to provided Acid/Data class verbosity (which itself defaults to 2). Overwrites any value passed trough the Data object.

process_results() None[source]

Processes the MCMC sampler results to obtain the final LSD profiles and continuum fit, and errors on both. This is effectively the final step in the ACID pipeline, and must be run before the profiles attribute is available. This is automatically called if process_results is True during Acid initialization. This function is stored here instead of the Acid class because it is not necessary to have the final profiles to use some of the methods contained within this class.

continue_sampling(process_results: bool = True, sampler: EnsembleSampler | None = None, **kwargs) None[source]

Continue MCMC sampling for additional steps. Passes the stored sampler into a Acid instance with the saved data. See :py:function:`Acid.continue_sampling` for more details on the parameters that can be passed.

Parameters:
  • process_results (bool, optional) – Whether to process the results after continuing sampling, by default True. If False, the profiles attribute will not be updated until Result.process_results() is called.

  • sampler (emcee.EnsembleSampler | None, optional) – Optionally provide a different sampler to continue sampling from, otherwise, takes the sampler from the Result object, by default None

  • nsteps (IntLike, optional) – Number of additional MCMC steps to run. Passed to :py:function:`Acid.continue_sampling` through **kwargs.

  • max_steps (IntLike, optional) – Maximum number of MCMC steps to run, by default None. Passed to :py:function:`Acid.continue_sampling` through **kwargs.

  • max_steps_kwargs (dict, optional) – Additional keyword arguments to be passed to the run_mcmc_until_converged function if max_steps is specified, by default None. The kwargs description can be found in Acid.ACID(), they are the 4 kwargs appearing after max_steps. Typos for kwargs are silently ignored. Passed to :py:function:`Acid.continue_sampling` through **kwargs.

  • parallel (bool, optional) – Overwrites config with whether to run the MCMC in parallel. If None, uses already existing configuration. Default is None. Passed to :py:function:`Acid.continue_sampling` through **kwargs.

  • cores (int, optional) – Overwrites config with the number of cores to use for parallel MCMC. If None, uses already existing configuration. Default is None. Passed to :py:function:`Acid.continue_sampling` through **kwargs.

  • moves (dict, optional) – Overwrites config with the dictionary specifying the moves to use for MCMC sampling. If None, uses already existing configuration. Default is None. See :py:function:`Acid.ACID` for format. Passed to :py:function:`Acid.continue_sampling` through **kwargs.

plot_walkers(sampler: EnsembleSampler | None = None, burnin: IntLike | None = None, thin: IntLike | None = None, return_fig: bool = False) None | tuple[source]

Plots, at maximum, the last 10 MCMC walkers for the LSD profile and continuum polynomial coefficients.

Parameters:
  • sampler (emcee.EnsembleSampler, optional) – Optionally provide a different sampler to plot from, otherwise, takes the sampler from the Result object, by default None

  • burnin (IntLike, optional) – Optionally define the number of burnin steps, by default uses the burnin calculated when the sampler was initiated.

  • thin (IntLike, optional) – Optionally define the number of thinning steps, by default uses the thinning calculated when the sampler was initiated.

  • return_fig (bool, optional) – Whether to return the figure and axis objects instead of showing the plot, by default False

Return type:

If return_fig is True, returns a tuple (fig, ax) of the figure and axes objects containing, else None

plot_traceplot(return_fig: bool = False) None | tuple[source]
plot_corner(sampler: EnsembleSampler | None = None, return_fig: bool = False, **kwargs) None | Figure[source]

Creates a corner plot for at maximum the last 8 LSD profile and continuum polynomial coefficients.

Parameters:
  • sampler (emcee.EnsembleSampler | None, optional) – Optionally provide a different sampler to plot from, otherwise, takes the sampler from the Result object, by default None

  • return_fig (bool, optional) – Whether to return the figure object instead of showing the plot, by default False

  • **kwargs – Additional keyword arguments to pass to corner.corner().

Return type:

If return_fig is True, returns the figure object containing the corner plot, else None

plot_profiles(grid: bool = True, labels: dict | None = None, return_fig: bool = False, subplot_kwargs: dict | None = None, errorbar_kwargs: dict | None = None, fig_ax=None) None | tuple[source]

Plots the LSD profile result from Acid.

Parameters:
  • grid (bool, optional) – Show or hide grid, by default True

  • labels (dict | None, optional) – Keys: ‘xlabel’, ‘ylabel’, and ‘title’. Allows label overrides., by default None

  • return_fig (bool, optional) – Whether to return the figure and axis objects instead of showing the plot, by default False

  • subplot_kwargs (dict | None, optional) – Keyword arguments to be passed to plt.subplots(), by default None

  • errorbar_kwargs (dict | None, optional) – Keyword arguments to be passed to ax.errorbar(), by default None

  • fig_ax (tuple[matplotlib.figure.Figure, matplotlib.axes.Axes] | None, optional) – Optionally provide an existing fig/axis tuple to plot on, by default None

Returns:

  • If return_fig is True, returns a tuple (fig, ax) of the figure and axes objects containing the plot.

  • Otherwise, displays the plot and returns None.

plot_forward_model(fig_ax: tuple | None = None, grid: bool = True, labels: dict | None = None, return_fig: bool = False, subplot_kwargs: dict | None = None) None | tuple[source]

Plots the forward model calculated from the final profiles to the combined input spectrum.

Parameters:
  • fig_ax (tuple | None) – Optionally provide an existing fig/axis tuple to plot on, by default None and creates a new figure and axis. The axis must be a 2 element array of axes, where the first axis is for the spectrum and forward model, and the second axis is for the residuals. If provided, the grid, labels, and titles should be set by you.

  • grid (bool, optional) – Show or hide grid, by default True

  • labels (dict | None, optional) – Keys: ‘xlabel’, ‘ylabel’, ‘title’, and ‘residuals_ylabel’. Allows label overrides, by default None

  • return_fig (bool, optional) – Whether to return the figure and axis objects instead of showing the plot, by default False

  • subplot_kwargs (dict | None, optional) – Keyword arguments to be passed to plt.subplots(). Allows label overrides, by default None

Returns:

  • If return_fig is True, returns a tuple (fig, ax) of the figure and axes objects containing the plot.

  • Otherwise, displays the plot and returns None.

plot_autocorrelation(sampler: EnsembleSampler | None = None, n_grid: ACID_code.IntLike = 12, c: float = 5.0, return_fig: bool = False, subplot_kwargs: dict | None = None, min_steps: ACID_code.IntLike = 100) None | tuple[source]

Plot estimated integrated autocorrelation time as a function of chain length.

From the emcee docs: - For several prefixes of the chain, estimate tau with Sokal windowing. - Plot tau(N) and the reference line tau = N/50.

Parameters:
  • sampler (emcee.EnsembleSampler | None, optional) – Optionally provide a different sampler to plot from, otherwise, takes the sampler from the Result object, by default None

  • n_grid (IntLike, optional) – Number of N values (prefix lengths) to evaluate, by default 12.

  • c (float, optional) – Sokal window constant, by default 5.0.

  • return_fig (bool, optional) – Whether to return the figure and axes objects, by default False

  • subplot_kwargs (dict | None, optional) – Keyword arguments to be passed to plt.subplots(). Allows label overrides, by default None

  • min_steps (IntLike, optional) – Minimum number of post-burnin samples required to attempt autocorrelation estimation, by default 100 If you decrease this, you may get unreliable estimates or errors from the autocorrelation time estimation.

Returns:

  • If return_fig is True, returns a tuple (fig, ax) of the figure and axes objects containing

  • the plot. Otherwise, displays the plot and returns None.

plot_acf(sampler: EnsembleSampler | None = None, max_lag: IntLike | None = None, return_fig: bool = False, subplot_kwargs: dict | None = None) None | tuple[source]

Plot the autocorrelation function (ACF) for each parameter, averaged across walkers.

Parameters:
  • sampler (emcee.EnsembleSampler, optional) – Optionally provide a different sampler to plot from, otherwise, takes the sampler from the Result object, by default None

  • max_lag (IntLike, optional) – Maximum lag to plot, by default None (plots up to min(5000, nsteps-1))

  • return_fig (bool, optional) – Whether to return the figure and axes objects, by default False

  • subplot_kwargs (dict, optional) – Keyword arguments to be passed to plt.subplots(). Allows label overrides, by default None

Returns:

  • If return_fig is True, returns a tuple (fig, ax) of the figure and axes objects containing

  • the plot. Otherwise, displays the plot and returns None.

initiate_sampler(sampler: EnsembleSampler | None, _method_name=None) None[source]

Initiates the sampler attribute from an external sampler.

Parameters:
  • sampler (emcee.EnsembleSampler or object, optional) – An emcee EnsembleSampler object or a compatible sampler object to set as the sampler attribute.

  • _method_name (str, optional) – Internal parameter used to track which method is calling initiate_sampler, for error messages. Not intended for user input, by default None.

property sampler: EnsembleSampler | None

Returns the sampler attribute, by default is None if not saved.

save(*args, **kwargs) None[source]

Saves the Data instance which the Result class inherits from Acid. See :py:function:`Data.save` for more details on the parameters that can be passed.

classmethod load(data: str | Result | Data = 'result.pkl') Result[source]

Loads a Result object from a pickle file or from a Data/Result object.

Parameters:

data (str | :py:class:`Result | Data`, optional) – A pickle file name or an object with the same attributes as a saved Result object, by default “result.pkl”

Returns:

A Result object loaded from the pickle file or from the provided object.

Return type:

Result