Repository: arviz-devs/arviz

Issue:
Repeated warning in summary() when only one chain is sampled
Sometimes I sample just one chain (yeah I know, call the Bayesian police on me). When then calling `az.summary()` I get tons of: `arviz.stats.stats_utils - WARNING - Shape validation failed: input_shape: (1, 2000), minimum_shape: (chains=2, draws=4)` one for each RV, which can be a lot. I think that warning could just be dropped, if only one chain is present I would for example not assume an Rhat value to be calculated but I would still like to see the rest of the summary.


Discussion:
Chain on you Thomas! ;-)
I agree it can be very annoying. It should be reported just one.

Resolving patch:
diff --git a/arviz/__init__.py b/arviz/__init__.py
--- a/arviz/__init__.py
+++ b/arviz/__init__.py
@@ -9,8 +9,24 @@
 from matplotlib.pyplot import register_cmap, style
 
 
-# Configure logging before importing arviz internals
-_log = logging.getLogger("arviz")
+class Logger(logging.Logger):
+    """Override Logger to avoid repeated messages."""
+
+    def __init__(self, name, level=logging.NOTSET):
+        super().__init__(name=name, level=level)
+        self.cache = []
+
+    def _log(
+        self, level, msg, *args, **kwargs
+    ):  # pylint: disable=signature-differs, arguments-differ
+        msg_hash = hash(msg)
+        if msg_hash in self.cache:
+            return
+        self.cache.append(msg_hash)
+        super()._log(level, msg, *args, **kwargs)
+
+
+_log = Logger("arviz")
 
 
 from .data import *
@@ -306,4 +322,4 @@ def _mpl_cm(name, colorlist):
 
 
 # clean namespace
-del os, logging, register_cmap, LinearSegmentedColormap
+del os, logging, register_cmap, LinearSegmentedColormap, Logger
diff --git a/arviz/stats/stats.py b/arviz/stats/stats.py
--- a/arviz/stats/stats.py
+++ b/arviz/stats/stats.py
@@ -1,6 +1,5 @@
 # pylint: disable=too-many-lines
 """Statistical functions in ArviZ."""
-import logging
 import warnings
 from collections import OrderedDict
 from copy import deepcopy
@@ -12,6 +11,7 @@
 import xarray as xr
 from scipy.optimize import minimize
 
+from arviz import _log
 from ..data import CoordSpec, DimSpec, InferenceData, convert_to_dataset, convert_to_inference_data
 from ..rcparams import rcParams
 from ..utils import Numba, _numba_var, _var_names, credible_interval_warning, get_coords
@@ -26,7 +26,6 @@
 from .stats_utils import stats_variance_2d as svar
 from .stats_utils import wrap_xarray_ufunc as _wrap_xarray_ufunc
 
-_log = logging.getLogger(__name__)
 
 __all__ = [
     "apply_test_function",
@@ -1132,6 +1131,8 @@ def summary(
            ...: )
 
     """
+    _log.cache = []
+
     if include_circ:
         warnings.warn(
             "include_circ is deprecated and will be ignored. Use circ_var_names instead",
diff --git a/arviz/stats/stats_utils.py b/arviz/stats/stats_utils.py
--- a/arviz/stats/stats_utils.py
+++ b/arviz/stats/stats_utils.py
@@ -1,5 +1,4 @@
 """Stats-utility functions for ArviZ."""
-import logging
 import warnings
 from collections.abc import Sequence
 from copy import copy as _copy
@@ -11,10 +10,10 @@
 from scipy.stats.mstats import mquantiles
 from xarray import apply_ufunc
 
+from arviz import _log
 from ..utils import conditional_jit, conditional_vect, conditional_dask
 from .density_utils import histogram as _histogram
 
-_log = logging.getLogger(__name__)
 
 __all__ = ["autocorr", "autocov", "ELPDData", "make_ufunc", "wrap_xarray_ufunc"]
 


Test patch: