|
1 |
| -# Copyright 2020 The PyMC Developers |
| 1 | +# Copyright 2021 The PyMC Developers |
2 | 2 | #
|
3 | 3 | # Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | # you may not use this file except in compliance with the License.
|
|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -"""PyMC3 Plotting. |
| 15 | +"""Alias for the `plots` submodule from ArviZ. |
16 | 16 |
|
17 |
| -Plots are delegated to the `ArviZ <https://arviz-devs.github.io/arviz/>`_ library, a general purpose library for |
18 |
| -exploratory analysis of Bayesian models. For more details, see https://arviz-devs.github.io/arviz/. |
19 |
| -
|
20 |
| -Only `plot_posterior_predictive_glm` is kept in the PyMC code base for now, but it will move to ArviZ once the latter adds features for regression plots. |
| 17 | +Plots are delegated to the ArviZ library, a general purpose library for |
| 18 | +"exploratory analysis of Bayesian models." |
| 19 | +See https://arviz-devs.github.io/arviz/ for details on plots. |
21 | 20 | """
|
22 | 21 | import functools
|
23 | 22 | import sys
|
24 | 23 | import warnings
|
25 | 24 |
|
26 | 25 | import arviz as az
|
27 | 26 |
|
| 27 | +# Makes this module as identical to arviz.plots as possible |
| 28 | +for attr in az.plots.__all__: |
| 29 | + obj = getattr(az.plots, attr) |
| 30 | + if not attr.startswith("__"): |
| 31 | + setattr(sys.modules[__name__], attr, obj) |
| 32 | + |
| 33 | + |
| 34 | +def map_args(func, alias: str): |
| 35 | + @functools.wraps(func) |
| 36 | + def wrapped(*args, **kwargs): |
| 37 | + if "varnames" in kwargs: |
| 38 | + raise DeprecationWarning( |
| 39 | + f"The `varnames` kwarg was renamed to `var_names`.", stacklevel=2 |
| 40 | + ) |
| 41 | + original = func.__name__ |
| 42 | + warnings.warn( |
| 43 | + f"The function `{alias}` from PyMC3 is just an alias for `{original}` from ArviZ. " |
| 44 | + f"Please switch to `pymc3.{original}` or `arviz.{original}`.", |
| 45 | + DeprecationWarning, |
| 46 | + stacklevel=2, |
| 47 | + ) |
| 48 | + return func(*args, **kwargs) |
| 49 | + |
| 50 | + return wrapped |
| 51 | + |
| 52 | + |
| 53 | +# Always show the DeprecationWarnings |
| 54 | +warnings.filterwarnings("once", category=DeprecationWarning, module="pymc3.plots") |
| 55 | + |
| 56 | + |
| 57 | +# Aliases of ArviZ functions |
| 58 | +autocorrplot = map_args(az.plot_autocorr, alias="autocorrplot") |
| 59 | +forestplot = map_args(az.plot_forest, alias="forestplot") |
| 60 | +kdeplot = map_args(az.plot_kde, alias="kdeplot") |
| 61 | +energyplot = map_args(az.plot_energy, alias="energyplot") |
| 62 | +densityplot = map_args(az.plot_density, alias="densityplot") |
| 63 | +pairplot = map_args(az.plot_pair, alias="pairplot") |
| 64 | +traceplot = map_args(az.plot_trace, alias="traceplot") |
| 65 | + |
| 66 | + |
| 67 | +# Customized with kwarg reformatting |
| 68 | +@functools.wraps(az.plot_compare) |
| 69 | +def compareplot(*args, **kwargs): |
| 70 | + warnings.warn( |
| 71 | + f"The function `compareplot` from PyMC3 is an alias for `plot_compare` from ArviZ. " |
| 72 | + "It also applies some kwarg replacements. Nevertheless, please switch " |
| 73 | + f"to `pymc3.plot_compare` or `arviz.plot_compare`.", |
| 74 | + DeprecationWarning, |
| 75 | + stacklevel=2, |
| 76 | + ) |
| 77 | + if "comp_df" in kwargs: |
| 78 | + comp_df = kwargs["comp_df"].copy() |
| 79 | + else: |
| 80 | + args = list(args) |
| 81 | + comp_df = args[0].copy() |
| 82 | + if "WAIC" in comp_df.columns: |
| 83 | + comp_df = comp_df.rename( |
| 84 | + index=str, |
| 85 | + columns={ |
| 86 | + "WAIC": "waic", |
| 87 | + "pWAIC": "p_waic", |
| 88 | + "dWAIC": "d_waic", |
| 89 | + "SE": "se", |
| 90 | + "dSE": "dse", |
| 91 | + "var_warn": "warning", |
| 92 | + }, |
| 93 | + ) |
| 94 | + elif "LOO" in comp_df.columns: |
| 95 | + comp_df = comp_df.rename( |
| 96 | + index=str, |
| 97 | + columns={ |
| 98 | + "LOO": "loo", |
| 99 | + "pLOO": "p_loo", |
| 100 | + "dLOO": "d_loo", |
| 101 | + "SE": "se", |
| 102 | + "dSE": "dse", |
| 103 | + "shape_warn": "warning", |
| 104 | + }, |
| 105 | + ) |
| 106 | + if "comp_df" in kwargs: |
| 107 | + kwargs["comp_df"] = comp_df |
| 108 | + else: |
| 109 | + args[0] = comp_df |
| 110 | + return az.plot_compare(*args, **kwargs) |
| 111 | + |
| 112 | + |
28 | 113 | from pymc3.plots.posteriorplot import plot_posterior_predictive_glm
|
29 | 114 |
|
30 |
| -__all__ = ["plot_posterior_predictive_glm"] |
| 115 | +__all__ = tuple(az.plots.__all__) + ( |
| 116 | + "autocorrplot", |
| 117 | + "compareplot", |
| 118 | + "forestplot", |
| 119 | + "kdeplot", |
| 120 | + "plot_posterior", |
| 121 | + "traceplot", |
| 122 | + "energyplot", |
| 123 | + "densityplot", |
| 124 | + "pairplot", |
| 125 | + "plot_posterior_predictive_glm", |
| 126 | +) |
0 commit comments