From bff15eb7e71729664b677111df8cdf2764a13c44 Mon Sep 17 00:00:00 2001 From: Michael Osthege Date: Sun, 14 Mar 2021 17:24:30 +0100 Subject: [PATCH] Revive ArviZ aliases, but raise DeprecationWarning on old wrappers Also see #4536 where the wrappers were brought back for v3. Closes #4528 --- RELEASE-NOTES.md | 2 ++ docs/source/api/plots.rst | 7 +++-- docs/source/api/stats.rst | 9 +++++-- pymc3/__init__.py | 1 + pymc3/plots/__init__.py | 56 ++++++++++++++++++++++++++++++++++----- pymc3/stats/__init__.py | 31 ++++++++++++++++++++++ 6 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 pymc3/stats/__init__.py diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 7baffaabdd6..7c378ce8ee0 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -3,6 +3,8 @@ ## PyMC3 vNext (4.0.0) ### Breaking Changes - ⚠ Theano-PyMC has been replaced with Aesara, so all external references to `theano`, `tt`, and `pymc3.theanof` need to be replaced with `aesara`, `aet`, and `pymc3.aesaraf` (see [4471](https://github.com/pymc-devs/pymc3/pull/4471)). +- ArviZ `plots` and `stats` *wrappers* were removed. The functions are now just available by their original names (see [#4549](https://github.com/pymc-devs/pymc3/pull/4471) and `3.11.2` release notes). +- ... ### New Features - The `CAR` distribution has been added to allow for use of conditional autoregressions which often are used in spatial and network models. diff --git a/docs/source/api/plots.rst b/docs/source/api/plots.rst index 0dec109b7f1..1e2b2e0139a 100644 --- a/docs/source/api/plots.rst +++ b/docs/source/api/plots.rst @@ -7,8 +7,7 @@ Plots Plots are delegated to the `ArviZ `_. library, a general purpose library for -"exploratory analysis of Bayesian models." -Refer to its documentation to use the plotting functions directly. +"exploratory analysis of Bayesian models". -.. automodule:: pymc3.plots.posteriorplot - :members: +Functions from the `arviz.plots` module are available through ``pymc3.`` or ``pymc3.plots.``, +but for their API documentation please refer to the :ref:`ArviZ documentation `. diff --git a/docs/source/api/stats.rst b/docs/source/api/stats.rst index bf85bb596dc..4cd23c3446f 100644 --- a/docs/source/api/stats.rst +++ b/docs/source/api/stats.rst @@ -1,8 +1,13 @@ ***** Stats ***** + +.. currentmodule:: pymc3.stats + Statistics and diagnostics are delegated to the `ArviZ `_. library, a general purpose library for -"exploratory analysis of Bayesian models." -Refer to its documentation to use the diagnostics functions directly. +"exploratory analysis of Bayesian models". + +Functions from the `arviz.stats` module are available through ``pymc3.`` or ``pymc3.stats.``, +but for their API documentation please refer to the :ref:`ArviZ documentation `. diff --git a/pymc3/__init__.py b/pymc3/__init__.py index 8f33feef09d..480db8ac775 100644 --- a/pymc3/__init__.py +++ b/pymc3/__init__.py @@ -62,6 +62,7 @@ def __set_compiler_flags(): from pymc3.plots import * from pymc3.sampling import * from pymc3.smc import * +from pymc3.stats import * from pymc3.step_methods import * from pymc3.tests import test from pymc3.tuning import * diff --git a/pymc3/plots/__init__.py b/pymc3/plots/__init__.py index 5923114cee3..04fb73db361 100644 --- a/pymc3/plots/__init__.py +++ b/pymc3/plots/__init__.py @@ -1,4 +1,4 @@ -# Copyright 2020 The PyMC Developers +# Copyright 2021 The PyMC Developers # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,12 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""PyMC3 Plotting. +"""Alias for the `plots` submodule from ArviZ. -Plots are delegated to the `ArviZ `_ library, a general purpose library for -exploratory analysis of Bayesian models. For more details, see https://arviz-devs.github.io/arviz/. - -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. +Plots are delegated to the ArviZ library, a general purpose library for +"exploratory analysis of Bayesian models." +See https://arviz-devs.github.io/arviz/ for details on plots. """ import functools import sys @@ -25,6 +24,49 @@ import arviz as az +# Makes this module as identical to arviz.plots as possible +for attr in az.plots.__all__: + obj = getattr(az.plots, attr) + if not attr.startswith("__"): + setattr(sys.modules[__name__], attr, obj) + + +def alias_deprecation(func, alias: str): + original = func.__name__ + + @functools.wraps(func) + def wrapped(*args, **kwargs): + raise DeprecationWarning( + f"The function `{alias}` from PyMC3 was an alias for `{original}` from ArviZ. " + "It was removed in PyMC3 4.0. " + f"Switch to `pymc3.{original}` or `arviz.{original}`." + ) + + return wrapped + + +# Aliases of ArviZ functions +autocorrplot = alias_deprecation(az.plot_autocorr, alias="autocorrplot") +forestplot = alias_deprecation(az.plot_forest, alias="forestplot") +kdeplot = alias_deprecation(az.plot_kde, alias="kdeplot") +energyplot = alias_deprecation(az.plot_energy, alias="energyplot") +densityplot = alias_deprecation(az.plot_density, alias="densityplot") +pairplot = alias_deprecation(az.plot_pair, alias="pairplot") +traceplot = alias_deprecation(az.plot_trace, alias="traceplot") +compareplot = alias_deprecation(az.plot_compare, alias="compareplot") + + from pymc3.plots.posteriorplot import plot_posterior_predictive_glm -__all__ = ["plot_posterior_predictive_glm"] +__all__ = tuple(az.plots.__all__) + ( + "autocorrplot", + "compareplot", + "forestplot", + "kdeplot", + "plot_posterior", + "traceplot", + "energyplot", + "densityplot", + "pairplot", + "plot_posterior_predictive_glm", +) diff --git a/pymc3/stats/__init__.py b/pymc3/stats/__init__.py new file mode 100644 index 00000000000..0880ca8b52d --- /dev/null +++ b/pymc3/stats/__init__.py @@ -0,0 +1,31 @@ +# Copyright 2021 The PyMC Developers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Alias for the `stats` submodule from ArviZ. + +Diagnostics and auxiliary statistical functions are delegated to the ArviZ library, a general +purpose library for "exploratory analysis of Bayesian models." +See https://arviz-devs.github.io/arviz/ for details. +""" +import sys + +import arviz as az + +for attr in az.stats.__all__: + obj = getattr(az.stats, attr) + if not attr.startswith("__"): + setattr(sys.modules[__name__], attr, obj) + + +__all__ = tuple(az.stats.__all__)