ACID_code_v2.Result

class ACID_code_v2.Result(Acid_or_Data_or_Sampler, 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.

Initiate the Result class

Parameters:
  • Acid_or_Data_or_Sampler (Acid | Data | emcee.EnsembleSampler) – An Acid object, Data object (contained in Acid class), or sampler object. 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, optional) – A sampler object to use if the Data object was provided. If an Acid object was provided, the sampler will be taken from there. If a sampler object was provided in the first argument, this will be ignored (with a warning), 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, None.

  • verbose (int | bool | str, optional) – Verbosity level, works exactly the same as Acid verbosity, if not provided defaults to provided Acid class verbosity otherwise defaults to 2.

  • production_run (#)

  • mode (# Whether Acid was run in production)

  • False (by default)

process_results() None[source]

Processes the MCMC sampler results to obtain the final LSD profiles and continuum fit, and errors on both.

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 Acid.continue_sampling() for more details on the parameters that can be passed.

Parameters:
  • nsteps (int | None, optional) – Number of additional MCMC steps to run. Passed to Acid.continue_sampling with the stored sampler.

  • max_steps (int | None, optional) – Maximum number of MCMC steps to run, by default None. Passed to Acid.continue_sampling with the stored sampler.

  • max_steps_kwards (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 Acid.continue_sampling with the stored sampler.

  • 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

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 (int | None, optional) – Optionally define the number of burnin steps, by default uses the burnin calculated when the sampler was initiated.

  • thin (int | None, 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_corner(sampler: EnsembleSampler | None = None, return_fig: bool = False, **kwargs) None | tuple[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(input_version: str = 'masked', grid: bool = True, labels: dict | None = None, return_fig: bool = False, subplot_kwargs: dict | None = None) None | tuple[source]

Plots the forward model fit to the observed spectrum.

Parameters:
  • input_version (str, optional) – Which input spectrum to use: ‘combined’, ‘input’, ‘masked’, by default ‘masked’

  • 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, burnin: IntLike | None = None, thin: IntLike | None = None, n_grid: ACID_code_v2.utils.IntLike = 12, c: float = 5.0, return_fig: bool = False, subplot_kwargs: dict | None = None, min_steps: ACID_code_v2.utils.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

  • burnin (int | None, optional) – Optional overrides. Defaults to self.burnin/self.thin from the sampler.

  • thin (int | None, optional) – Optional overrides. Defaults to self.burnin/self.thin from the sampler.

  • n_grid (int, 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 (int, 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 (int, 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) None[source]

Initiates the sampler attribute from an external sampler.

Parameters:

sampler (emcee.EnsembleSampler) – An emcee EnsembleSampler object to set as the sampler attribute.

initiate_data(data: Data) None[source]

Initiates the data attribute from an external Data object.

Parameters:

data (Data) – A Data object to set as the data attribute.

save_result(filename: str = 'result.pkl', store_sampler: bool = True) None[source]

Saves the Result object to a pickle file.

Parameters:
  • filename (str, optional) – Name of the file to save the Result object to, by default “result.pkl”

  • store_sampler (bool, optional) – Whether to store the sampler backend in the pickle file. If False, the sampler will not be stored, and the Result object will not be able to continue sampling or plot walkers/corner plots

classmethod load_result(result_object: str | object = 'result.pkl')[source]

Loads a Result object from a pickle file or from an object with the same attributes as a saved Result object.

Parameters:

result_object (str | object, 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