Skip to content

Commit 3079983

Browse files
Bring back stats and plotting aliases (#4536)
* Restore plots and stats wrappers (see #4528) * Alias stats/plots from ArviZ and add deprecation warnings in old wrappers * Link to ArviZ docs, improve warnings UX * Use intersphinx for docs linking Co-authored-by: Oriol Abril-Pla <[email protected]>
1 parent 03448f7 commit 3079983

File tree

6 files changed

+155
-15
lines changed

6 files changed

+155
-15
lines changed

RELEASE-NOTES.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
# Release Notes
22

3-
## PyMC3 vNext (TBD)
3+
## PyMC3 3.11.2 (TBD)
44
### Breaking Changes
55
+ ...
66

77
### New Features
88
+ `pm.math.cartesian` can now handle inputs that are themselves >1D (see [#4482](https://github.com/pymc-devs/pymc3/pull/4482)).
9-
+ ...
9+
+ Statistics and plotting functions that were removed in `3.11.0` were brought back, albeit with deprecation warnings if an old naming scheme is used (see [#4536](https://github.com/pymc-devs/pymc3/pull/4536)). In order to future proof your code, rename these function calls:
10+
+ `pm.traceplot``pm.plot_trace`
11+
+ `pm.compareplot``pm.plot_compare` (here you might need to rename some columns in the input according to the [`arviz.plot_compare` documentation](https://arviz-devs.github.io/arviz/api/generated/arviz.plot_compare.html))
12+
+ `pm.autocorrplot``pm.plot_autocorr`
13+
+ `pm.forestplot``pm.plot_forest`
14+
+ `pm.kdeplot``pm.plot_kde`
15+
+ `pm.energyplot``pm.plot_energy`
16+
+ `pm.densityplot``pm.plot_density`
17+
+ `pm.pairplot``pm.plot_pair`
1018

1119
### Maintenance
1220
- ⚠ Our memoization mechanism wasn't robust against hash collisions (#4506), sometimes resulting in incorrect values in, for example, posterior predictives. The `pymc3.memoize` module was removed and replaced with `cachetools`. The `hashable` function and `WithMemoization` class were moved to `pymc3.util` (see #4525).

docs/source/api/plots.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ Plots
77
Plots are delegated to the
88
`ArviZ <https://arviz-devs.github.io/arviz/index.html>`_.
99
library, a general purpose library for
10-
"exploratory analysis of Bayesian models."
11-
Refer to its documentation to use the plotting functions directly.
10+
"exploratory analysis of Bayesian models".
1211

13-
.. automodule:: pymc3.plots.posteriorplot
14-
:members:
12+
Functions from the `arviz.plots` module are available through ``pymc3.<function>`` or ``pymc3.plots.<function>``,
13+
but for their API documentation please refer to the :ref:`ArviZ documentation <arviz:plot_api>`.

docs/source/api/stats.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
*****
22
Stats
33
*****
4+
5+
.. currentmodule:: pymc3.stats
6+
47
Statistics and diagnostics are delegated to the
58
`ArviZ <https://arviz-devs.github.io/arviz/index.html>`_.
69
library, a general purpose library for
7-
"exploratory analysis of Bayesian models."
8-
Refer to its documentation to use the diagnostics functions directly.
10+
"exploratory analysis of Bayesian models".
11+
12+
Functions from the `arviz.stats` module are available through ``pymc3.<function>`` or ``pymc3.stats.<function>``,
13+
but for their API documentation please refer to the :ref:`ArviZ documentation <arviz:stats_api>`.

pymc3/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def __set_compiler_flags():
6161
from pymc3.plots import *
6262
from pymc3.sampling import *
6363
from pymc3.smc import *
64+
from pymc3.stats import *
6465
from pymc3.step_methods import *
6566
from pymc3.tests import test
6667
from pymc3.theanof import *

pymc3/plots/__init__.py

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 The PyMC Developers
1+
# Copyright 2021 The PyMC Developers
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,19 +12,115 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""PyMC3 Plotting.
15+
"""Alias for the `plots` submodule from ArviZ.
1616
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.
2120
"""
2221
import functools
2322
import sys
2423
import warnings
2524

2625
import arviz as az
2726

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+
28113
from pymc3.plots.posteriorplot import plot_posterior_predictive_glm
29114

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+
)

pymc3/stats/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2021 The PyMC Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Alias for the `stats` submodule from ArviZ.
16+
17+
Diagnostics and auxiliary statistical functions are delegated to the ArviZ library, a general
18+
purpose library for "exploratory analysis of Bayesian models."
19+
See https://arviz-devs.github.io/arviz/ for details.
20+
"""
21+
import sys
22+
23+
import arviz as az
24+
25+
for attr in az.stats.__all__:
26+
obj = getattr(az.stats, attr)
27+
if not attr.startswith("__"):
28+
setattr(sys.modules[__name__], attr, obj)
29+
30+
31+
__all__ = tuple(az.stats.__all__)

0 commit comments

Comments
 (0)