From e0e2c54f7dc0a8ce79f899a986c96807da68f656 Mon Sep 17 00:00:00 2001 From: aloctavodia Date: Tue, 21 Apr 2020 18:01:52 -0300 Subject: [PATCH 1/5] improve docs and error handling --- arviz/plots/ppcplot.py | 69 +++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/arviz/plots/ppcplot.py b/arviz/plots/ppcplot.py index ee0e425231..e7488b5c4d 100644 --- a/arviz/plots/ppcplot.py +++ b/arviz/plots/ppcplot.py @@ -3,6 +3,7 @@ import platform import logging import numpy as np +from matplotlib import get_backend from .plot_utils import ( xarray_var_iter, @@ -68,16 +69,15 @@ def plot_ppc( - value = posterior/prior predictive var_name For example, `data_pairs = {'y' : 'y_hat'}` - If None, it will assume that the observed data and the posterior/prior - predictive data have the same variable name. + If None, it will assume that the observed data and the posterior/prior predictive data have + the same variable name. var_names : list List of variables to be plotted. Defaults to all observed variables in the model if None. coords : dict Dictionary mapping dimensions to selected coordinates to be plotted. - Dimensions without a mapping specified will include all coordinates for - that dimension. Defaults to including all coordinates for all - dimensions if None. + Dimensions without a mapping specified will include all coordinates for that dimension. + Defaults to including all coordinates for all dimensions if None. flatten : list List of dimensions to flatten in observed_data. Only flattens across the coordinates specified in the coords argument. Defaults to flattening all of the dimensions. @@ -91,17 +91,23 @@ def plot_ppc( `animation = False` if defaults to a maximum of 5 samples and will set jitter to 0.7 unless defined otherwise. Otherwise it defaults to all provided samples. random_seed : int - Random number generator seed passed to numpy.random.seed to allow - reproducibility of the plot. By default, no seed will be provided - and the plot will change each call if a random sample is specified - by `num_pp_samples`. + Random number generator seed passed to numpy.random.seed to allow reproducibility of the + plot. By default, no seed will be provided and the plot will change each call if a random + sample is specified by `num_pp_samples`. jitter : float If kind is "scatter", jitter will add random uniform noise to the height of the ppc samples and observed data. By default 0. animated : bool Create an animation of one posterior/prior predictive sample per frame. Defaults to False. + Only works with matploblib backend. + To run animations inside a notebook you have to use the nbAgg backend. Try with + `%matplotlib notebook` or `%matplotlib nbAgg`. You can switch back to the default backend + with `%matplotlib inline` or `%matplotlib auto`. + If you experience problems rendering the animation try setting + `animation_kwargs({'blit':False}) or changing the plotting backend (e.g. to TkAgg) + If you run the animations from a script write `ax, ani = az.plot_ppc(.)` animation_kwargs : dict - Keywords passed to `animation.FuncAnimation`. + Keywords passed to `animation.FuncAnimation`. Ignored with matploblib backend. legend : bool Add legend to figure. By default True. ax: numpy array-like of matplotlib axes or bokeh figures, optional @@ -110,8 +116,8 @@ def plot_ppc( backend: str, optional Select plotting backend {"matplotlib","bokeh"}. Default "matplotlib". backend_kwargs: bool, optional - These are kwargs specific to the backend being used. For additional documentation - check the plotting method of the backend. + These are kwargs specific to the backend being used. For additional documentation check the + plotting method of the backend. group : {"prior", "posterior"}, optional Specifies which InferenceData group should be plotted. Defaults to 'posterior'. Other value can be 'prior'. @@ -180,6 +186,10 @@ def plot_ppc( if data_pairs is None: data_pairs = {} + if backend is None: + backend = rcParams["plot.backend"] + backend = backend.lower() + if animation_kwargs is None: animation_kwargs = {} if platform.system() == "Linux": @@ -187,14 +197,29 @@ def plot_ppc( else: animation_kwargs.setdefault("blit", False) - if animated and backend == "bokeh": - raise TypeError("Animation option is only supported with matplotlib backend.") - - if animated and animation_kwargs["blit"] and platform.system() != "Linux": - _log.warning( - "If you experience problems rendering the animation try setting" - "`animation_kwargs({'blit':False}) or changing the plotting backend (e.g. to TkAgg)" - ) + if animated: + if backend == "matplotlib": + try: + shell = get_ipython().__class__.__name__ + if shell == "ZMQInteractiveShell" and get_backend() != "nbAgg": + raise Warning( + "To run animations inside a notebook you have to use the nbAgg backend. " + "Try with `%matplotlib notebook` or `%matplotlib nbAgg`. You can switch " + "back to the default backend with `%matplotlib inline` or " + "`%matplotlib auto`." + ) + except NameError: + pass + + if animation_kwargs["blit"] and platform.system() != "Linux": + _log.warning( + "If you experience problems rendering the animation try setting " + "`animation_kwargs({'blit':False}) or changing the plotting backend " + "(e.g. to TkAgg)" + ) + + elif backend == "bokeh": + raise TypeError("Animation option is only supported with matplotlib backend.") if alpha is None: if animated: @@ -312,10 +337,6 @@ def plot_ppc( show=show, ) - if backend is None: - backend = rcParams["plot.backend"] - backend = backend.lower() - if backend == "bokeh": ppcplot_kwargs.pop("animated") From f0cb77f3a890e3897f0db9f15a97b5c290fc8ab2 Mon Sep 17 00:00:00 2001 From: aloctavodia Date: Tue, 21 Apr 2020 18:16:35 -0300 Subject: [PATCH 2/5] improve docs and error handling --- arviz/plots/ppcplot.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/arviz/plots/ppcplot.py b/arviz/plots/ppcplot.py index e7488b5c4d..1c08b562a0 100644 --- a/arviz/plots/ppcplot.py +++ b/arviz/plots/ppcplot.py @@ -100,12 +100,14 @@ def plot_ppc( animated : bool Create an animation of one posterior/prior predictive sample per frame. Defaults to False. Only works with matploblib backend. - To run animations inside a notebook you have to use the nbAgg backend. Try with - `%matplotlib notebook` or `%matplotlib nbAgg`. You can switch back to the default backend - with `%matplotlib inline` or `%matplotlib auto`. + To run animations inside a notebook you have to use the `nbAgg` matplotlib's backend. + Try with `%matplotlib notebook` or `%matplotlib nbAgg`. You can switch back to the + default matplotlib's backend with `%matplotlib inline` or `%matplotlib auto`. + If switching back and forth between matplotlib's backend, you may need to run twice the cell + with the animation. If you experience problems rendering the animation try setting - `animation_kwargs({'blit':False}) or changing the plotting backend (e.g. to TkAgg) - If you run the animations from a script write `ax, ani = az.plot_ppc(.)` + `animation_kwargs({'blit':False}) or changing the matplotlib's backend (e.g. to TkAgg) + If you run the animation from a script write `ax, ani = az.plot_ppc(.)` animation_kwargs : dict Keywords passed to `animation.FuncAnimation`. Ignored with matploblib backend. legend : bool From 728adcadff35a4f42a566fd052fce117717b7598 Mon Sep 17 00:00:00 2001 From: aloctavodia Date: Mon, 27 Apr 2020 08:34:41 -0300 Subject: [PATCH 3/5] move warnings --- arviz/plots/backends/matplotlib/ppcplot.py | 25 +++++++++++++++++++- arviz/plots/ppcplot.py | 27 ++-------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/arviz/plots/backends/matplotlib/ppcplot.py b/arviz/plots/backends/matplotlib/ppcplot.py index 47d96c2eea..db95cfedab 100644 --- a/arviz/plots/backends/matplotlib/ppcplot.py +++ b/arviz/plots/backends/matplotlib/ppcplot.py @@ -1,5 +1,7 @@ """Matplotib Posterior predictive plot.""" -from matplotlib import animation +import platform +import logging +from matplotlib import animation, get_backend import matplotlib.pyplot as plt import numpy as np @@ -11,6 +13,7 @@ ) from ....numeric_utils import _fast_kde, histogram, get_bins +_log = logging.getLogger(__name__) def plot_ppc( ax, @@ -40,6 +43,26 @@ def plot_ppc( show, ): """Matplotlib ppc plot.""" + if animated: + try: + shell = get_ipython().__class__.__name__ + if shell == "ZMQInteractiveShell" and get_backend() != "nbAgg": + raise Warning( + "To run animations inside a notebook you have to use the nbAgg backend. " + "Try with `%matplotlib notebook` or `%matplotlib nbAgg`. You can switch " + "back to the default backend with `%matplotlib inline` or " + "`%matplotlib auto`." + ) + except NameError: + pass + + if animation_kwargs["blit"] and platform.system() != "Linux": + _log.warning( + "If you experience problems rendering the animation try setting " + "`animation_kwargs({'blit':False}) or changing the plotting backend " + "(e.g. to TkAgg)" + ) + if ax is None: fig, axes = _create_axes_grid( length_plotters, rows, cols, figsize=figsize, backend_kwargs=backend_kwargs diff --git a/arviz/plots/ppcplot.py b/arviz/plots/ppcplot.py index 1c08b562a0..bdfaa989b6 100644 --- a/arviz/plots/ppcplot.py +++ b/arviz/plots/ppcplot.py @@ -3,7 +3,6 @@ import platform import logging import numpy as np -from matplotlib import get_backend from .plot_utils import ( xarray_var_iter, @@ -199,30 +198,6 @@ def plot_ppc( else: animation_kwargs.setdefault("blit", False) - if animated: - if backend == "matplotlib": - try: - shell = get_ipython().__class__.__name__ - if shell == "ZMQInteractiveShell" and get_backend() != "nbAgg": - raise Warning( - "To run animations inside a notebook you have to use the nbAgg backend. " - "Try with `%matplotlib notebook` or `%matplotlib nbAgg`. You can switch " - "back to the default backend with `%matplotlib inline` or " - "`%matplotlib auto`." - ) - except NameError: - pass - - if animation_kwargs["blit"] and platform.system() != "Linux": - _log.warning( - "If you experience problems rendering the animation try setting " - "`animation_kwargs({'blit':False}) or changing the plotting backend " - "(e.g. to TkAgg)" - ) - - elif backend == "bokeh": - raise TypeError("Animation option is only supported with matplotlib backend.") - if alpha is None: if animated: alpha = 1 @@ -340,6 +315,8 @@ def plot_ppc( ) if backend == "bokeh": + if animated: + raise TypeError("Animation option is only supported with matplotlib backend.") ppcplot_kwargs.pop("animated") ppcplot_kwargs.pop("animation_kwargs") From 7d463fe88e37275610b21e9074176fe259a83347 Mon Sep 17 00:00:00 2001 From: aloctavodia Date: Mon, 27 Apr 2020 08:36:09 -0300 Subject: [PATCH 4/5] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11f04a7546..40d8328a4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ * Added a warning specifying log scale is now the default in compare/loo/waic functions ([#1150](https://github.com/arviz-devs/arviz/pull/1150)) * Fixed bug in `plot_posterior` with rcParam "plot.matplotlib.show" = True (#1151) * Set `fill_last` argument of `plot_kde` to False by default (#1158) +* plot_ppc animation: improve docs and error handling (#1162) ### Deprecation From 36965c50079590041fef4107b49710e55084d815 Mon Sep 17 00:00:00 2001 From: aloctavodia Date: Thu, 30 Apr 2020 11:17:25 -0300 Subject: [PATCH 5/5] black --- arviz/plots/backends/matplotlib/ppcplot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/arviz/plots/backends/matplotlib/ppcplot.py b/arviz/plots/backends/matplotlib/ppcplot.py index db95cfedab..7d89447f50 100644 --- a/arviz/plots/backends/matplotlib/ppcplot.py +++ b/arviz/plots/backends/matplotlib/ppcplot.py @@ -15,6 +15,7 @@ _log = logging.getLogger(__name__) + def plot_ppc( ax, length_plotters,