Visualizations

Helper functions to visualize results of ABCSMC runs.

pyabc.visualization.plot_acceptance_rates_trajectory(histories: Union[List, pyabc.storage.history.History], labels: Union[List, str] = None, rotation: int = 0, title: str = 'Acceptance rates', yscale: str = 'lin', size: tuple = None, ax: matplotlib.axes._axes.Axes = None, colors: List[str] = None)[source]

Plot of acceptance rates over all iterations, i.e. one trajectory per history.

Parameters
  • histories (Union[List, History]) – The histories to plot from. History ids must be set correctly.

  • labels (Union[List ,str], optional) – Labels corresponding to the histories. If None are provided, indices are used as labels.

  • rotation (int, optional (default = 0)) – Rotation to apply to the plot’s x tick labels. For longer labels, a tilting of 45 or even 90 can be preferable.

  • title (str, optional (default = "Acceptance rates")) – Title for the plot.

  • yscale (str, optional (default = 'lin')) – The scale on which to plot the counts. Can be one of ‘lin’, ‘log’ (basis e) or ‘log10’

  • size (tuple of float, optional) – The size of the plot in inches.

  • ax (matplotlib.axes.Axes, optional) – The axis object to use.

Returns

ax

Return type

Axis of the generated plot.

pyabc.visualization.plot_credible_intervals(history: pyabc.storage.history.History, m: int = 0, ts: Union[List[int], int] = None, par_names: List = None, levels: List = None, show_mean: bool = False, show_kde_max: bool = False, show_kde_max_1d: bool = False, size: tuple = None, refval: dict = None, refval_color: str = 'C1', kde: pyabc.transition.base.Transition = None, kde_1d: pyabc.transition.base.Transition = None, arr_ax: List = None)[source]

Plot credible intervals over time.

Parameters
  • history (History) – The history to extract data from.

  • m (int, optional (default = 0)) – The id of the model to plot for.

  • ts (Union[List[int], int], optional (default = all)) – The time points to plot for.

  • par_names (List[str], optional) – The parameter to plot for. If None, then all parameters are used.

  • levels (List[float], optional (default = [0.95])) – Confidence intervals to compute.

  • show_mean (bool, optional (default = False)) – Whether to show the mean apart from the median as well.

  • show_kde_max (bool, optional (default = False)) – Whether to show the one of the sampled points that gives the highest KDE value for the specified KDE. Note: It is not attemtped to find the overall hightest KDE value, but rather the sampled point with the highest value is taken as an approximation (of the MAP-value).

  • show_kde_max_1d (bool, optional (default = False)) – Same as show_kde_max, but here the KDE is applied componentwise.

  • size (tuple of float) – Size of the plot.

  • refval (dict, optional (default = None)) – A dictionary of reference parameter values to plot for each of par_names.

  • refval_color (str, optional) – Color to use for the reference value.

  • kde (Transition, optional (default = MultivariateNormalTransition)) – The KDE to use for show_kde_max.

  • kde_1d (Transition, optional (default = MultivariateNormalTransition)) – The KDE to use for show_kde_max_1d.

  • arr_ax (List, optional) – Array of axes to use. Assumed to be a 1-dimensional list.

Returns

arr_ax

Return type

Array of generated axes.

pyabc.visualization.plot_credible_intervals_for_time(histories: Union[List[pyabc.storage.history.History], pyabc.storage.history.History], labels: Union[List[str], str] = None, ms: Union[List[int], int] = None, ts: Union[List[int], int] = None, par_names: List[str] = None, levels: List[float] = None, show_mean: bool = False, show_kde_max: bool = False, show_kde_max_1d: bool = False, size: tuple = None, rotation: int = 0, refvals: Union[List[dict], dict] = None, kde: pyabc.transition.base.Transition = None, kde_1d: pyabc.transition.base.Transition = None)[source]

Plot credible intervals over time.

Parameters
  • histories (Union[List[History], History]) – The histories to extract data from.

  • labels (Union[List[str], str], optional) – Labels for the histories. If None, they are just numbered.

  • ms (Union[List[int], int], optional (default = 0)) – List of the ids of the models to plot for. Default is model id 0 for all histories.

  • ts (Union[List[int], int], optional (default = all)) – The time points to plot for, same length as histories. If None, the last times are taken.

  • par_names (List[str], optional) – The parameter to plot for. If None, then all parameters are used. Assumes all histories have these parameters.

  • levels (List[float], optional (default = [0.95])) – Confidence intervals to compute.

  • show_kde_max, show_kde_max_1d (show_mean,) – As in plot_credible_intervals.

  • size (tuple of float) – Size of the plot.

  • refvals (Union[List[dict], dict], optional (default = None)) – A dictionary of reference parameter values to plot for each of par_names, for each history. Same length as histories.

  • kde (Transition, optional (default = MultivariateNormalTransition)) – The KDE to use for show_kde_max.

  • kde_1d (Transition, optional (default = MultivariateNormalTransition)) – The KDE to use for show_kde_max_1d.

pyabc.visualization.plot_data_callback(history: pyabc.storage.history.History, f_plot: Callable = None, f_plot_aggregated: Callable = None, t: int = None, ax=None, **kwargs)[source]

Plot the summary statistics from the history using callback functions to plot single statistics or aggregated values.

Parameters
  • history (History) – The history object to use.

  • f_plot (Callable, optional) – Function to plot a single summary statistic. Takes the parameters (sum_stat, weight, ax, **kwargs).

  • f_plot_aggregated (Callable) – Function to plot aggregated values on summary statistics. Takes the parameters (sum_stats, weights, ax, **kwargs).

  • t (int, optional) – Time point to extract data from the history for.

  • ax (maplotlib.axes.Axes) – Axis object for the plot. This object is not touched directly and can thus be also e.g. a list of axis objects.

  • arguments are passed on to the plotting functions. (Additional) –

Returns

ax

Return type

Axis of the generated plot.

pyabc.visualization.plot_data_default(obs_data: dict, sim_data: dict, keys: Union[List[str], str] = None)[source]

Plot summary statistic data.

Parameters
  • obs_data (dict) – A dictionary for the summary statistic of the observed data, where keys represent the summary statistic name and values represent the data itself. The values can be represented as pandas dataframe, 1d numpy array, or 2d numpy array.

  • sim_data (dict) – A dictionary for the summary statistic of the simulated data, where keys represent the summary statistic name and values represent the data itself. The values can be represented as pandas dataframe, 1d numpy array, or 2d numpy array.

  • key (Union[List[str], str], optional) – Specific summary statistic keys to be used. If None, then all entries are used.

Returns

arr_ax

Return type

Axes of the generated plot.

pyabc.visualization.plot_effective_sample_sizes(histories: Union[List, pyabc.storage.history.History], labels: Union[List, str] = None, rotation: int = 0, title: str = 'Effective sample size', relative: bool = False, colors: List = None, size: tuple = None, ax: matplotlib.axes._axes.Axes = None)[source]

Plot effective sample sizes over all iterations.

Parameters
  • histories (Union[List, History]) – The histories to plot from. History ids must be set correctly.

  • labels (Union[List ,str], optional) – Labels corresponding to the histories. If None are provided, indices are used as labels.

  • rotation (int, optional (default = 0)) – Rotation to apply to the plot’s x tick labels. For longer labels, a tilting of 45 or even 90 can be preferable.

  • title (str, optional (default = "Total required samples")) – Title for the plot.

  • relative (bool, optional (default = False)) – Whether to show relative sizes (to 1) or w.r.t. the real number of particles.

  • colors (List, optional) – Colors to use for the lines. If None, then the matplotlib default values are used.

  • size (tuple of float, optional) – The size of the plot in inches.

  • ax (matplotlib.axes.Axes, optional) – The axis object to use. A new one is created if None.

Returns

ax

Return type

Axis of the generated plot.

pyabc.visualization.plot_epsilons(histories: Union[List, pyabc.storage.history.History], labels: Union[List, str] = None, colors: List = None, scale: str = None, title: str = 'Epsilon values', size: tuple = None, ax: matplotlib.axes._axes.Axes = None)[source]

Plot epsilon trajectory.

Parameters
  • histories (Union[List, History]) – The histories to plot from. History ids must be set correctly.

  • labels (Union[List ,str], optional) – Labels corresponding to the histories. If None are provided, indices are used as labels.

  • colors (List, optional) – Colors to use for the lines. If None, then the matplotlib default values are used.

  • scale (str, optional (default='lin')) – Scaling to apply to the y axis. Must be one of ‘lin’, ‘log’, ‘log10’.

  • title (str, optional (default = "Epsilon values")) – Title for the plot.

  • size (tuple of float, optional) – The size of the plot in inches.

  • ax (matplotlib.axes.Axes, optional) – The axis object to use. A new one is created if None.

Returns

ax

Return type

Axis of the generated plot.

pyabc.visualization.plot_histogram_1d(history: pyabc.storage.history.History, x: str, m: int = 0, t: int = None, xmin=None, xmax=None, ax=None, size=None, refval=None, refval_color='C1', **kwargs)[source]

Plot 1d histogram of parameter samples.

Parameters
  • history (History) – History to extract data from.

  • x (str) – Id of the parameter to plot for.

  • m (int, optional (default = 0)) – Id of the model to plot for.

  • t (int, optional (default = None, i.e. the last time)) – Time point to plot for.

  • xmax (xmin,) – Bounds for x. Both must be specified for bounds to be applied.

  • ax (matplotlib.axes.Axes) – Axis object for the plot. If None is passed, a new figure is created.

  • size (2-Tuple of float, optional) – Size of the plot in inches.

  • refval (dict, optional (default = None)) – A reference value for x, to be highlighted in the plot.

  • refval_color (str, optional) – Color to use for the reference value.

Returns

ax

Return type

Axis of the generated plot.

pyabc.visualization.plot_histogram_1d_lowlevel(df: pandas.core.frame.DataFrame, w: pandas.core.frame.DataFrame, x: str, xmin=None, xmax=None, ax=None, size=None, refval=None, refval_color='C1', **kwargs)[source]

Lowlevel interface for plot_histogram_1d (see there for the remaining parameters).

Parameters
  • df (pd.DataFrame) – Contains the parameters. Must have a column ‘x’.

  • w (pd.DataFrame) – Parameter weights.

pyabc.visualization.plot_histogram_2d(history: pyabc.storage.history.History, x: str, y: str, m: int = 0, t: int = None, xmin=None, xmax=None, ymin=None, ymax=None, ax=None, size=None, refval=None, refval_color='C1', **kwargs)[source]

Plot 2d histogram of parameter pair samples.

Parameters
  • history (History) – History to extract data from.

  • y (x,) – Ids of the parameters to plot for.

  • m (int, optional (default = 0)) – Id of the model to plot for.

  • t (int, optional (default = None, i.e. the last time)) – Time point to plot for.

  • xmax, ymin, ymax (xmin,) – Bounds for x and y. All must be specified for bounds to be applied.

  • ax (matplotlib.axes.Axes) – Axis object for the plot. If None is passed, a new figure is created.

  • size (2-Tuple of float, optional) – Size of the plot in inches.

  • refval (dict, optional (default = None)) – Reference values for x and y, to be highlighted in the plot.

  • refval_color (str, optional) – Color to use for the reference value.

Returns

ax

Return type

Axis of the generated plot.

pyabc.visualization.plot_histogram_2d_lowlevel(df: pandas.core.frame.DataFrame, w: pandas.core.frame.DataFrame, x, y, xmin=None, xmax=None, ymin=None, ymax=None, ax=None, size=None, refval=None, refval_color='C1', **kwargs)[source]

Lowlevel interface for plot_histogram_2d (see there for the remaining parameters).

Parameters
  • df (pd.DataFrame) – Contains the parameters. Must have a column ‘x’.

  • w (pd.DataFrame) – Parameter weights.

pyabc.visualization.plot_histogram_matrix(history: pyabc.storage.history.History, m: int = 0, t: int = None, size=None, refval=None, refval_color='C1', **kwargs)[source]

Plot matrix of 1d and 2d histograms over all parameters.

Parameters
  • history (History) – History to extract data from.

  • m (int, optional (default = 0)) – Id of the model to plot for.

  • t (int, optional (default = None, i.e. the last time)) – Time point to plot for.

  • size (2-Tuple of float, optional) – Size of the plot in inches.

  • refval (dict, optional (default = None)) – Reference parameter values, to be highlighted in the plot.

  • refval_color (str, optional) – Color to use for the reference value.

Returns

arr_ax – Axis objects of the generated plots.

Return type

list of matplotlib.axis.Axis

pyabc.visualization.plot_histogram_matrix_lowlevel(df: pandas.core.frame.DataFrame, w: pandas.core.frame.DataFrame, size=None, refval=None, refval_color='C1', **kwargs)[source]

Lowlevel interface for plot_histogram_matrix (see there for the remaining parameters).

Parameters
  • df (pd.DataFrame) – Contains the parameters. Must have a column ‘x’.

  • w (pd.DataFrame) – Parameter weights.

pyabc.visualization.plot_kde_1d(df, w, x, xmin=None, xmax=None, numx=50, ax=None, size=None, title: str = None, refval=None, refval_color='C1', kde=None, **kwargs)[source]

Lowlevel interface for plot_kde_1d_highlevel (see there for the remaining parameters).

Parameters
  • df (pandas.DataFrame) – The rows are the observations, the columns the variables.

  • w (pandas.DataFrame) – The corresponding weights.

Returns

ax – Axis of the plot.

Return type

matplotlib axis

pyabc.visualization.plot_kde_1d_highlevel(history: pyabc.storage.history.History, x: str, m: int = 0, t: int = None, xmin=None, xmax=None, numx=50, ax=None, size=None, title: str = None, refval=None, refval_color='C1', kde=None, **kwargs)[source]

Plot 1d kernel density estimate of parameter samples.

Parameters
  • history (History) – History to extract data from.

  • x (str) – The variable for the x-axis.

  • m (int, optional) – Id of the model to plot for.

  • t (int, optional) – Time point to plot for. Defaults to last time point.

  • xmin (float, optional) – The lower limit in x for the histogram. If left empty, it is set to the minimum of the ovbservations of the variable to be plotted as x.

  • xmax (float, optional) – The upper limit in x for the histogram. If left empty, it is set to the maximum of the ovbservations of the variable to be plotted as x.

  • numx (int, optional) – The number of bins in x direction. Defaults to 50.

  • ax (matplotlib.axes.Axes, optional) – The axis object to use.

  • size (2-Tuple of float, optional) – Size of the plot in inches.

  • title (str, optional) – Title for the plot. Defaults to None.

  • refval (dict, optional) – A reference value for x (as refval[x]: float). If not None, the value will be highlighted in the plot. Default: None.

  • refval_color (str, optional) – Color to use for the reference value.

  • kde (pyabc.Transition, optional) – The kernel density estimator to use for creating a smooth density from the sample. If None, a multivariate normal kde with cross-validated scaling is used.

Returns

ax – axis of the plot

Return type

matplotlib axis

pyabc.visualization.plot_kde_2d(df, w, x, y, xmin=None, xmax=None, ymin=None, ymax=None, numx=50, numy=50, ax=None, size=None, colorbar=True, title: str = None, refval=None, refval_color='C1', kde=None, **kwargs)[source]

Plot a 2d kernel density estimate of parameter samples.

Parameters
  • df (Pandas Dataframe) – The rows are the observations, the columns the variables

  • w (The corresponding weights.) –

  • the other parameters, see plot_kde_2d_highlevel. (For) –

Returns

ax – Axis of the plot.

Return type

matplotlib axis

pyabc.visualization.plot_kde_2d_highlevel(history: pyabc.storage.history.History, x: str, y: str, m: int = 0, t: int = None, xmin: float = None, xmax: float = None, ymin: float = None, ymax: float = None, numx: int = 50, numy: int = 50, ax=None, size=None, colorbar=True, title: str = None, refval=None, refval_color='C1', kde=None, **kwargs)[source]

Plot 2d kernel density estimate of parameter samples.

Parameters
  • history (History) – History to extract data from.

  • x (str) – The variable for the x-axis.

  • y (str) – The variable for the y-axis.

  • m (int, optional) – Id of the model to plot for.

  • t (int, optional) – Time point to plot for. Defaults to last time point.

  • xmin (float, optional) – The lower limit in x for the histogram. If left empty, it is set to the minimum of the ovbservations of the variable to be plotted as x.

  • xmax (float, optional) – The upper limit in x for the histogram. If left empty, it is set to the maximum of the ovbservations of the variable to be plotted as x.

  • ymin (float, optional) – The lower limit in y for the histogram. If left empty, it is set to the minimum of the ovbservations of the variable to be plotted as y.

  • ymax (float, optional) – The upper limit in y for the histogram. If left empty, it is set to the maximum of the ovbservations of the variable to be plotted as y.

  • numx (int, optional) – The number of bins in x direction. Defaults to 50.

  • int, optional (numy) – The number of bins in y direction. Defaults tp 50.

  • ax (matplotlib.axes.Axes, optional) – The axis object to use.

  • size (2-Tuple of float) – Size of the plot in inches.

  • colorbar (bool, optional) – Whether to plot a colorbar. Defaults to True.

  • title (str, optional) – Title for the plot. Defaults to None.

  • refval (dict, optional) – A reference parameter to be shown in the plots. Default: None.

  • refval_color (str, optional) – Color to use for the reference value.

  • kde (pyabc.Transition, optional) – The kernel density estimator to use for creating a smooth density from the sample. If None, a multivariate normal kde with cross-validated scaling is used..

Returns

ax – Axis of the plot.

Return type

matplotlib axis

pyabc.visualization.plot_kde_matrix(df, w, limits=None, colorbar=True, height=2.5, numx=50, numy=50, refval=None, refval_color='C1', kde=None, arr_ax=None)[source]

Plot a KDE matrix for 1- and 2-dim marginals of the parameter samples.

Parameters
  • df (Pandas Dataframe) – The rows are the observations, the columns the variables.

  • w (np.narray) – The corresponding weights.

  • parameters (Other) –

Returns

Array of the generated plots’ axes.

Return type

arr_ax

pyabc.visualization.plot_kde_matrix_highlevel(history, m: int = 0, t: int = None, limits=None, colorbar: bool = True, height: float = 2.5, numx: int = 50, numy: int = 50, refval=None, refval_color='C1', kde=None, arr_ax=None)[source]

Plot a KDE matrix for 1- and 2-dim marginals of the parameter samples.

Parameters
  • history (History) – History to extract data from.

  • m (int, optional) – Id of the model to plot for.

  • t (int, optional) – Time point to plot for. Defaults to last time point.

  • limits (dictionary, optional) – Dictionary of the form {"name": (lower_limit, upper_limit)}.

  • colorbar (bool) – Whether to plot the colorbars or not.

  • height (float, optional) – Height of each subplot in inches. Default: 2.5.

  • numx (int, optional) – The number of bins in x direction. Defaults to 50.

  • numy (int, optional) – The number of bins in y direction. Defaults to 50.

  • refval (dict, optional) – A reference parameter to be shown in the plots (e.g. the underlying ground truth parameter used to simulate the data for testing purposes). Default: None.

  • refval_color (str, optional) – Color to use for the reference value.

  • kde (pyabc.Transition, optional) – The kernel density estimator to use for creating a smooth density from the sample. If None, a multivariate normal kde with cross-validated scaling is used.

  • arr_ax – Array of axes objects to use.

Returns

Array of the generated plots’ axes.

Return type

arr_ax

pyabc.visualization.plot_model_probabilities(history: pyabc.storage.history.History, rotation: int = 0, title: str = 'Model probabilities', ax: matplotlib.axes._axes.Axes = None)[source]

Plot the probabilities of models over time.

Parameters
  • history (History) – The history to extract data from.

  • rotation (int, optional (default = 0)) – Rotation of x axis labels.

  • title (str, optional) – Title of the plot.

  • ax (matplotlib.axes.Axes, optional) – The axis object to use.

pyabc.visualization.plot_sample_numbers(histories: Union[List, pyabc.storage.history.History], labels: Union[List, str] = None, rotation: int = 0, title: str = 'Required samples', size: tuple = None, ax: matplotlib.axes._axes.Axes = None)[source]

Stacked bar plot of required numbers of samples over all iterations.

Parameters
  • histories (Union[List, History]) – The histories to plot from. History ids must be set correctly.

  • labels (Union[List ,str], optional) – Labels corresponding to the histories. If None are provided, indices are used as labels.

  • rotation (int, optional (default = 0)) – Rotation to apply to the plot’s x tick labels. For longer labels, a tilting of 45 or even 90 can be preferable.

  • title (str, optional (default = "Total required samples")) – Title for the plot.

  • size (tuple of float, optional) – The size of the plot in inches.

  • ax (matplotlib.axes.Axes, optional) – The axis object to use.

Returns

ax

Return type

Axis of the generated plot.

pyabc.visualization.plot_sample_numbers_trajectory(histories: Union[List, pyabc.storage.history.History], labels: Union[List, str] = None, rotation: int = 0, title: str = 'Required samples', yscale: str = 'lin', size: tuple = None, ax: matplotlib.axes._axes.Axes = None)[source]

Plot of required sample number over all iterations, i.e. one trajectory per history.

Parameters
  • histories (Union[List, History]) – The histories to plot from. History ids must be set correctly.

  • labels (Union[List ,str], optional) – Labels corresponding to the histories. If None are provided, indices are used as labels.

  • rotation (int, optional (default = 0)) – Rotation to apply to the plot’s x tick labels. For longer labels, a tilting of 45 or even 90 can be preferable.

  • title (str, optional (default = "Required samples")) – Title for the plot.

  • yscale (str, optional (default = 'lin')) – The scale on which to plot the counts. Can be one of ‘lin’, ‘log’ (basis e) or ‘log10’

  • size (tuple of float, optional) – The size of the plot in inches.

  • ax (matplotlib.axes.Axes, optional) – The axis object to use.

Returns

ax

Return type

Axis of the generated plot.

pyabc.visualization.plot_total_sample_numbers(histories: Union[List, pyabc.storage.history.History], labels: Union[List, str] = None, rotation: int = 0, title: str = 'Total required samples', yscale: str = 'lin', size: tuple = None, ax: matplotlib.axes._axes.Axes = None)[source]

Bar plot of total required sample number over all iterations, i.e. a single-colored bar per history, in contrast to plot_sample_numbers, which visually distinguishes iterations.

Parameters
  • histories (Union[List, History]) – The histories to plot from. History ids must be set correctly.

  • labels (Union[List ,str], optional) – Labels corresponding to the histories. If None are provided, indices are used as labels.

  • rotation (int, optional (default = 0)) – Rotation to apply to the plot’s x tick labels. For longer labels, a tilting of 45 or even 90 can be preferable.

  • title (str, optional (default = "Total required samples")) – Title for the plot.

  • yscale (str, optional (default = 'lin')) – The scale on which to plot the counts. Can be one of ‘lin’, ‘log’ (basis e) or ‘log10’

  • size (tuple of float, optional) – The size of the plot in inches.

  • ax (matplotlib.axes.Axes, optional) – The axis object to use.

Returns

ax

Return type

Axis of the generated plot.