Repository: arviz-devs/arviz

Issue:
plot_density has no filter_vars argument
I think it should have the argument. It may be a little more complicated given the multiple idata input but is should still be feasible.


Discussion:
prahasR: @OriolAbril, I have added the code in `densityplot.py` and `test_plots_matplotlib.py`. Also I am not sure that how do I run tests to check the code. Can you please help me. 
prahasR: @OriolAbril Thankyou for the suggestions. I have applied the changes pls review.
Also I wanted to ask that you suggested that for testing our `filter_vars` argument we should test the behaviour of  `_var_names` instead of  `plot_density` to keep the test short, but we already have a test for this in `test_utils.py` as you have mentioned. Or I misunderstood something here? 
OriolAbril: The test looks good like this, but it should be in `test_utils.py`. 

> but we already have a test for this in test_utils.py as you have mentioned.

we have similar tests but not the same tests, the other tests don't check the behaviour with multiple input datasets, they check the behaviour of filter_like on a single dataset. 
OriolAbril: you have to remove the (now unused) imports in test_matplotlilb for CI to pass: https://dev.azure.com/ArviZ/ArviZ/_build/results?buildId=5507&view=logs&j=7f538728-4589-5563-0e87-d0823eb40420&t=6753be35-af0d-5ca7-e801-8ec60efd9201&l=14
prahasR: Yes I got that, Thanks. I think now there will be no issue. 

Patch:
diff --git a/arviz/plots/densityplot.py b/arviz/plots/densityplot.py
index cc90a63..dd881a8 100644
--- a/arviz/plots/densityplot.py
+++ b/arviz/plots/densityplot.py
@@ -17,6 +17,7 @@ def plot_density(
     group="posterior",
     data_labels=None,
     var_names=None,
+    filter_vars=None,
     transform=None,
     hdi_prob=None,
     point_estimate="auto",
@@ -58,6 +59,11 @@ def plot_density(
         List of variables to plot.  If multiple datasets are supplied and var_names is not None,
         will print the same set of variables for each dataset.  Defaults to None, which results in
         all the variables being plotted.
+    filter_vars: {None, "like", "regex"}, optional, default=None
+        If `None` (default), interpret var_names as the real variables names. If "like",
+        interpret var_names as substrings of the real variables names. If "regex",
+        interpret var_names as regular expressions on the real variables names. A la
+        ``pandas.filter``.
     transform : callable
         Function to transform data (defaults to None i.e. the identity function)
     hdi_prob : float
@@ -186,7 +192,7 @@ def plot_density(
     if labeller is None:
         labeller = BaseLabeller()
 
-    var_names = _var_names(var_names, datasets)
+    var_names = _var_names(var_names, datasets, filter_vars)
     n_data = len(datasets)
 
     if data_labels is None:


Test patch:
diff --git a/arviz/tests/base_tests/test_plots_matplotlib.py b/arviz/tests/base_tests/test_plots_matplotlib.py
index 488d444..a005741 100644
--- a/arviz/tests/base_tests/test_plots_matplotlib.py
+++ b/arviz/tests/base_tests/test_plots_matplotlib.py
@@ -163,6 +163,9 @@ def test_plot_density_bad_kwargs(models):
     with pytest.raises(ValueError):
         plot_density(obj, hdi_prob=2)
 
+    with pytest.raises(ValueError):
+        plot_density(obj, filter_vars="bad_value")
+
 
 @pytest.mark.parametrize(
     "kwargs",
diff --git a/arviz/tests/base_tests/test_utils.py b/arviz/tests/base_tests/test_utils.py
index d6575a2..595bab6 100644
--- a/arviz/tests/base_tests/test_utils.py
+++ b/arviz/tests/base_tests/test_utils.py
@@ -66,6 +66,25 @@ def test_var_names_key_error(data):
         _var_names(("theta", "tau", "bad_var_name"), data)
 
 
+@pytest.mark.parametrize(
+    "var_args",
+    [
+        (["ta"], ["beta1", "beta2", "theta"], "like"),
+        (["~beta"], ["phi", "theta"], "like"),
+        (["beta[0-9]+"], ["beta1", "beta2"], "regex"),
+        (["^p"], ["phi"], "regex"),
+        (["~^t"], ["beta1", "beta2", "phi"], "regex"),
+    ],
+)
+def test_var_names_filter_multiple_input(var_args):
+    samples = np.random.randn(10)
+    data1 = dict_to_dataset({"beta1": samples, "beta2": samples, "phi": samples})
+    data2 = dict_to_dataset({"beta1": samples, "beta2": samples, "theta": samples})
+    data = [data1, data2]
+    var_names, expected, filter_vars = var_args
+    assert _var_names(var_names, data, filter_vars) == expected
+
+
 @pytest.mark.parametrize(
     "var_args",
     [