Plots#

This module provides some plotting routines for plotting the marginalized posterior distribution and performance of the algorithm.

Module gathering general plots.

(Other plots are in methods of some classes, e.g. Progress contains the timings plot.)

plots.simple_latex_sci_notation(string)[source]#

If string contains a %g or %e number representation, substitutes the e for a power of 10.

It does not add dollars around the string.

NB: it assumes that the string passed contains a single number, and nothing else.

plots.param_samples_for_slices(X, i, bounds, n=200)[source]#

From an array of points X = [X^i] = [[X^1_1, X^1_2,…], [X^2_1,…], …], it generates a list of points per sample, where the i coordinate is sliced within the region defined by bounds, and the rest of them are kept fixed.

plots.prepare_slices_func(func, X_fiducial, bounds, indices=None, n=50)[source]#

Prepare slices of the given function,

Parameters:
  • func (callable) – Function for which to prepare slices. It needs to take arguments in the way X_fiducial is passed: func(*X_fiducial) if X_fiducual is a list, or func(**X_fiducial) if X_fiducial is a dictionary.

  • X_fiducial (array-like, shape = (n_dimensions), or dict) – Fiducial point for the slices: slice i corresponds to fixing all parameters but that with index i, which is evaluaded on a grid within its bounds. It can be a dictionary with arguments of func as keys.

  • bounds (array-like, shape = (n_dimensions, 2), or dict) – Bounds for the slices per parameter.

  • indices (list, optional) – A list of integers (if X_fiducial is a list) or parameter names (if X_fiducial is a dict), denoting the parameters for which the slices will be prepared (all of them, if left unspecified).

  • n (int) – Number of samples per slice (default: 50). Careful if the posterior is slow!

Returns:

indices, params, Xs, ys – shape=(len(indices), n, dim)), array-like shape=(len(indices, n))

Return type:

list(int), list(str) len=len(indices), array-like

plots.plot_slices_func(func, X_fiducial, bounds, indices=None, n=50, fig_kwargs=None, labels=None)[source]#

Plot slices of the given function,

Parameters:
  • func (callable) – Function for which to prepare slices. It needs to take arguments in the way X_fiducial is passed: func(*X_fiducial) if X_fiducual is a list, or func(**X_fiducial) if X_fiducial is a dictionary.

  • X_fiducial (array-like, shape = (n_dimensions), or dict) – Fiducial point for the slices: slice i corresponds to fixing all parameters but that with index i, which is evaluaded on a grid within its bounds. It can be a dictionary with arguments of func as keys.

  • bounds (array-like, shape = (n_dimensions, 2), or dict) – Bounds for the slices per parameter.

  • indices (list, optional) – A list of integers (if X_fiducial is a list) or parameter names (if X_fiducial is a dict), denoting the parameters for which the slices will be prepared (all of them, if left unspecified).

  • n (int) – Number of samples per slice (default: 50). Careful if the posterior is slow!

  • fig_kwargs (dict, optional) – Dict of kw arguments to pass to the subplots constructor. Only layout, dpi considered safe.

  • labels (lst(str), optional) – Strings (possibly Latex) to use for axes labels. Length cases: None or len=0: plain parameter names for x labels and no y label; len=1: used as y label, plain names for x labels; len=len(indices): used as x labels; len=len(indices)+1: used as x labels and y label, in that order.

Returns:

fig, axarr

Return type:

figure and array of axes used for the plot.

plots.plot_slices(truth, gpr, acquisition, X=None, reference=None)[source]#

Plots slices along parameter coordinates for a series X of given points (the GPR training set if not specified). For each coordinate, there is a slice per point, leaving all coordinates of that point fixed except for the one being sliced.

Lines are coloured according to the value of the mean GP at points X.

# TODO: make acq func optional

plots.plot_slices_reference(truth, gpr, X, plot_truth=True, reference=None)[source]#

Plots slices of the gpr model and true log-posterior (if plot_truth=True) along parameter coordinates for a given point X, leaving all coordinates of that point fixed except for the one being sliced.

plots.plot_corner_getdist(mc_samples, params=None, bounds=None, filled=None, training=None, training_highlight_last=False, markers=None, output=None, output_dpi=200, subplot_size=2)[source]#

Creates a corner plot the given MC samples, and optionally shows evaluation locations.

If called repeatedly, it may leak memory, unfortunately. To avoid this, execute it like this:

# Temporarily switch to Agg backend
prev_backend = matplotlib.get_backend()
matplotlib.use("Agg")
try:
    plot_corner_getdist(...)
except:
    ...
finally:
    # Switch back to prev backend
    mpl.use(prev_backend)
Parameters:
  • mc_samples (dict(str, (cobaya.SampleCollection, getdist.MCSamples, str))) – Dict of MC samples, with their plot label as key, and the sample as value, either as GetDist or Cobaya samples, or as a path where there are samples saved.

  • params (list(str), optional) – List of parameter names to be plotted, by default all of the ones in the first MC sample, included derived ones like probability densities.

  • bounds (array-like, shape = (len(params), 2), list(array-like, shape = (2))) – Dict or list (sorted as params) of parameter bounds.

  • filled (dict(str, bool), list) – Dictionary with labels as keys specifying the filled property of the contours. Contours are filled by default when unspecified (including key missing for a passed sample). If it is a list, the same order as in mc_samples is assumed.

  • training (GaussianProcessRegressor, dict(str or tuple, GaussianProcessRegressor), optional) – If a GPR is passed, it plots the training samples (including the discarded ones) on top of the contours. Samples outside the axes ranges are not plotted. The parameters of the GPR need to be assumed, since the GPR does not save names: if a GPR is passed, the sampled parameters of the first MC sample will be used; if a single-key dict is passed with a str as a key, it will used the parameter names from the MC sample with that label; if the key is a tuple of strings, they will be used as parameters

  • subplot_size (float, default = 2) – Size of each subplot in the corner plot.

  • output (str, optional (default=None)) – Path, including name and extension, of the saved figure. Not saved if left unspecified.

  • output_dpi (int (default: 200)) – The resolution of the generated plot in DPI.

Return type:

getdist.plots.GetDistPlotter object containing the figure.

plots.getdist_add_training(getdist_plot, params, gpr, colormap='viridis', marker='.', marker_inf='x', highlight_last=False)[source]#

Adds the training points to a GetDist triangle plot, coloured according to their log-posterior value.

Parameters:
  • getdist_plot (GetDist triangle plot) – Contains the marginalized contours and potentially other things.

  • params (list(str)) – The assumed parameter names for the GPR samples. Need to be a subset of the ones plotted by the GetDistPlotter.

  • gpr (GaussianProcessRegressor) – The trained GP Regressor containing the samples.

  • colormap (matplotlib colormap, optional (default="viridis")) – Color map from which to get the color scale to represent the GP model value for the training points.

  • marker (matplotlib marker, optional (default=".")) – Marker to be used for the training points.

  • marker_inf (matplotlib marker, optional (default=".")) – Marker to be used for the non-finite training points.

  • highlight_last (bool (default=False)) – Draw a red circle around the points added in the last iteration

Return type:

The GetDist triangle plot with the added training points.

plots.plot_convergence(convergence_criterion, evaluations='total', marker='', axes=None, ax_labels=True, legend_loc='upper right')[source]#

Plots the value of the convergence criterion as function of the number of (accepted) training points.

Parameters:
  • convergence_criterion (The instance of the convergence criterion which has) – been called in the BO loop

  • evaluations ("total" or "accepted") – Whether to plot the total number of posterior evaluations or only the accepted steps.

  • marker (matplotlib marker, optional (default="")) – Marker used for the plot. Will be passed to matplotlib.pyplot.plot.

  • axes (matplotlib axes, optional) – Axes to be used, if passed.

  • ax_labels (bool, optional (default: True)) – Add axes labels.

  • legend_loc (str (default: "upper right")) – Location of the legend.

Return type:

The plot convergence criterion vs. number of training points

plots.plot_trace(truth, gpr, convergence_criterion, progress, colormap='viridis', reference=None)[source]#

Plots the evolution of the run along true model evaluations, showing evolution of the convergence criterion and the values of the log-posterior and the individual parameters.

Can take a reference sample or reference bounds (dict with parameters as keys and 5 sorted bounds as values, or alternatively just a central value).

plots.plot_distance_distribution(points, mean, covmat, density=False, show_added=True, ax=None)[source]#

Plots a histogram of the distribution of points with respect to the number of standard deviations. Confidence level boundaries (Gaussian approximantion, dimension-dependent) are shown too.

Parameters:
  • points (array-like, with shape (N_points, N_dimensions), or GPR instance) – Points to be used for the histogram.

  • mean (array-like, (N_dimensions).) – Mean of the distribution.

  • covmat (array-like, (N_dimensions, N_dimensions).) – Covariance matrix of the distribution.

  • density (bool (default: False)) – If True, bin height is normalised to the (hyper)volume of the (hyper)spherical shell corresponding to each standard deviation.

  • show_added (bool (default True)) – Colours the stacks depending on how early or late the corresponding points were added (bluer stacks represent newer points).

  • ax (matplotlib axes) – If provided, they will be used for the plot.

Return type:

Tuple of current figure and axes (fig, ax).