Skip to content

Commit b98a895

Browse files
Alias stats/plots from ArviZ and add deprecation warnings in old wrappers
1 parent 04f2612 commit b98a895

File tree

3 files changed

+45
-85
lines changed

3 files changed

+45
-85
lines changed

RELEASE-NOTES.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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 (see [#4536](https://github.com/pymc-devs/pymc3/pull/4536)).
1010

1111
### Maintenance
1212
- ⚠ 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).

pymc3/plots/__init__.py

+34-36
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,61 @@
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
1717
Plots are delegated to the ArviZ library, a general purpose library for
18-
"exploratory analysis of Bayesian models." See https://arviz-devs.github.io/arviz/
19-
for details on plots.
18+
"exploratory analysis of Bayesian models."
19+
See https://arviz-devs.github.io/arviz/ for details on plots.
2020
"""
2121
import functools
2222
import sys
2323
import warnings
2424

2525
import arviz as az
2626

27+
# Makes this module as identical to arviz.plots as possible
28+
for attr in dir(az.plots):
29+
obj = getattr(az.plots, attr)
30+
if not attr.startswith("__"):
31+
setattr(sys.modules[__name__], attr, obj)
2732

28-
def map_args(func):
29-
swaps = [("varnames", "var_names")]
3033

34+
def map_args(func, alias: str):
3135
@functools.wraps(func)
3236
def wrapped(*args, **kwargs):
33-
for (old, new) in swaps:
34-
if old in kwargs and new not in kwargs:
35-
warnings.warn(
36-
f"Keyword argument `{old}` renamed to `{new}`, and will be removed in pymc3 3.8"
37-
)
38-
kwargs[new] = kwargs.pop(old)
39-
return func(*args, **kwargs)
37+
if "varnames" in kwargs:
38+
raise DeprecationWarning(f"The `varnames` kwarg was renamed to `var_names`.")
39+
original = func.__name__
40+
warnings.warn(
41+
f"The function `{alias}` from PyMC3 is just an alias for `{original}` from ArviZ. "
42+
f"Please switch to `pymc3.{original}` or `arviz.{original}`.",
43+
DeprecationWarning,
44+
)
45+
return func(*args, **kwargs)
4046

4147
return wrapped
4248

4349

44-
# pymc3 custom plots: override these names for custom behavior
45-
autocorrplot = map_args(az.plot_autocorr)
46-
forestplot = map_args(az.plot_forest)
47-
kdeplot = map_args(az.plot_kde)
48-
plot_posterior = map_args(az.plot_posterior)
49-
energyplot = map_args(az.plot_energy)
50-
densityplot = map_args(az.plot_density)
51-
pairplot = map_args(az.plot_pair)
52-
53-
# Use compact traceplot by default
54-
@map_args
55-
@functools.wraps(az.plot_trace)
56-
def traceplot(*args, **kwargs):
57-
try:
58-
kwargs.setdefault("compact", True)
59-
return az.plot_trace(*args, **kwargs)
60-
except TypeError:
61-
kwargs.pop("compact")
62-
return az.plot_trace(*args, **kwargs)
50+
# Aliases of ArviZ functions
51+
autocorrplot = map_args(az.plot_autocorr, alias="autocorrplot")
52+
forestplot = map_args(az.plot_forest, alias="forestplot")
53+
kdeplot = map_args(az.plot_kde, alias="kdeplot")
54+
plot_posterior = map_args(az.plot_posterior, alias="plot_posterior")
55+
energyplot = map_args(az.plot_energy, alias="energyplot")
56+
densityplot = map_args(az.plot_density, alias="densityplot")
57+
pairplot = map_args(az.plot_pair, alias="pairplot")
58+
traceplot = map_args(az.plot_trace, alias="traceplot")
6359

6460

65-
# addition arg mapping for compare plot
61+
# Customized with kwarg reformatting
6662
@functools.wraps(az.plot_compare)
6763
def compareplot(*args, **kwargs):
64+
warnings.warn(
65+
f"The function `compareplot` from PyMC3 is an alias for `plot_compare` from ArviZ. "
66+
"It also applies some kwarg replacements. Nevertheless, please switch "
67+
f"to `pymc3.plot_compare` or `arviz.plot_compare`.",
68+
DeprecationWarning,
69+
)
6870
if "comp_df" in kwargs:
6971
comp_df = kwargs["comp_df"].copy()
7072
else:
@@ -103,10 +105,6 @@ def compareplot(*args, **kwargs):
103105

104106
from pymc3.plots.posteriorplot import plot_posterior_predictive_glm
105107

106-
# Access to arviz plots: base plots provided by arviz
107-
for plot in az.plots.__all__:
108-
setattr(sys.modules[__name__], plot, map_args(getattr(az.plots, plot)))
109-
110108
__all__ = tuple(az.plots.__all__) + (
111109
"autocorrplot",
112110
"compareplot",

pymc3/stats/__init__.py

+9-47
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,20 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Statistical utility functions for PyMC3
15+
"""Alias for the `stats` submodule from ArviZ.
1616
1717
Diagnostics and auxiliary statistical functions are delegated to the ArviZ library, a general
18-
purpose library for "exploratory analysis of Bayesian models." See
19-
https://arviz-devs.github.io/arviz/ for details.
18+
purpose library for "exploratory analysis of Bayesian models."
19+
See https://arviz-devs.github.io/arviz/ for details.
2020
"""
21-
import functools
22-
import warnings
21+
import sys
2322

2423
import arviz as az
2524

25+
for attr in dir(az.stats):
26+
obj = getattr(az.stats, attr)
27+
if not attr.startswith("__"):
28+
setattr(sys.modules[__name__], attr, obj)
2629

27-
def map_args(func):
28-
swaps = [("varnames", "var_names")]
2930

30-
@functools.wraps(func)
31-
def wrapped(*args, **kwargs):
32-
for (old, new) in swaps:
33-
if old in kwargs and new not in kwargs:
34-
warnings.warn(
35-
"Keyword argument `{old}` renamed to `{new}`, and will be removed in "
36-
"pymc3 3.9".format(old=old, new=new)
37-
)
38-
kwargs[new] = kwargs.pop(old)
39-
return func(*args, **kwargs)
40-
41-
return wrapped
42-
43-
44-
bfmi = map_args(az.bfmi)
45-
compare = map_args(az.compare)
46-
ess = map_args(az.ess)
47-
geweke = map_args(az.geweke)
48-
hpd = map_args(az.hpd)
49-
loo = map_args(az.loo)
50-
mcse = map_args(az.mcse)
51-
r2_score = map_args(az.r2_score)
52-
rhat = map_args(az.rhat)
53-
summary = map_args(az.summary)
54-
waic = map_args(az.waic)
55-
56-
57-
__all__ = [
58-
"bfmi",
59-
"compare",
60-
"ess",
61-
"geweke",
62-
"hpd",
63-
"loo",
64-
"mcse",
65-
"r2_score",
66-
"rhat",
67-
"summary",
68-
"waic",
69-
]
31+
__all__ = tuple(az.stats.__all__)

0 commit comments

Comments
 (0)