Repository: arviz-devs/arviz

Issue:
from_pystan puts log likelihood and posterior predictions in posterior data
## Short Description

I would like to convert a posterior sample from a PyStan `stan.fit.Fit` object to an `az.InferenceData` object, but the log likelihood and posterior prediction variables end up in the `posterior` section. (They also appear in their respective sections of the `InferenceData` object). I was wondering if there is a way to have them not be in the `posterior` section. This would make using them with other ArviZ functions much easier because then they will *not* be included in tables or plots of the *posterior distributions* such as `az.plot_trace(data)`.

## Code Example or link

The example is from the PyStan section of the *Creating InferenceData* guide: https://arviz-devs.github.io/arviz/getting_started/CreatingInferenceData.html#from-pystan

> Note that there are some changes to reflect changes in the PyStan API (such as importing `stan` instead of `pystan`.

```python
import arviz as az
import numpy as np
import stan

schools_code = """
data {
    int<lower=0> J;
    real y[J];
    real<lower=0> sigma[J];
}

parameters {
    real mu;
    real<lower=0> tau;
    real theta_tilde[J];
}

transformed parameters {
    real theta[J];
    for (j in 1:J)
        theta[j] = mu + tau * theta_tilde[j];
}

model {
    mu ~ normal(0, 5);
    tau ~ cauchy(0, 5);
    theta_tilde ~ normal(0, 1);
    y ~ normal(theta, sigma);
}

generated quantities {
    vector[J] log_lik;
    vector[J] y_hat;
    for (j in 1:J) {
        log_lik[j] = normal_lpdf(y[j] | theta[j], sigma[j]);
        y_hat[j] = normal_rng(theta[j], sigma[j]);
    }
}
"""

eight_school_data = {
    "J": 8,
    "y": np.array([28.0, 8.0, -3.0, 7.0, -1.0, 1.0, 18.0, 12.0]),
    "sigma": np.array([15.0, 10.0, 16.0, 11.0, 9.0, 11.0, 10.0, 18.0]),
}

stan_model = stan.build(schools_code, data=eight_school_data)
fit = stan_model.sample()

stan_data = az.from_pystan(
    posterior=fit,
    posterior_predictive="y_hat",
    posterior_model= stan_model,
    observed_data=["y"],
    log_likelihood={"y": "log_lik"},
    coords={"school": np.arange(eight_school_data["J"])},
    dims={
        "theta": ["school"],
        "y": ["school"],
        "log_lik": ["school"],
        "y_hat": ["school"],
        "theta_tilde": ["school"],
    },
)

stan_data
#> Inference data with groups:
#>         > posterior
#>         > posterior_predictive
#>         > log_likelihood
#>         > sample_stats

stan_data.posterior
#> <xarray.Dataset>
#> Dimensions:      (chain: 4, draw: 1000, school: 8)
#> Coordinates:
#>   * chain        (chain) int64 0 1 2 3
#>   * draw         (draw) int64 0 1 2 3 4 5 6 7 ... 993 994 995 996 997 998 999
#>   * school       (school) int64 0 1 2 3 4 5 6 7
#> Data variables:
#>     mu           (chain, draw) float64 5.583 13.74 -1.643 ... 7.135 1.818 2.276
#>     tau          (chain, draw) float64 3.375 2.563 2.734 ... 1.585 0.2655 8.359
#>     theta_tilde  (chain, draw, school) float64 0.9058 0.2812 ... 1.286 -0.5058
#>     theta        (chain, draw, school) float64 8.64 6.531 6.381 ... 13.03 -1.952
#>     log_lik      (chain, draw, school) float64 -4.46 -3.232 ... -3.345 -4.11
#>     y_hat        (chain, draw, school) float64 -15.09 7.846 ... 19.34 8.089
#> Attributes:
#>     created_at:                 2022-01-13T22:30:54.799215
#>     arviz_version:              0.11.4
#>     inference_library:          stan
#>     inference_library_version:  3.3.0
#>     num_chains:                 4
#>     num_samples:                1000
#>     num_thin:                   1
#>     num_warmup:                 1000
#>     save_warmup:                0
#>     model_name:                 models/6kxc6kz6
#>     program_code:               \ndata {\n    int<lower=0> J;\n    real y[J];...
#>     random_seed:                None
```

PyStan version: 3.3.0
ArviZ version: 0.11.4

## Relevant documentation or public examples

- Guide on creating InferenceData objects from PyStan: https://arviz-devs.github.io/arviz/getting_started/CreatingInferenceData.html#from-pystan
- documentation for `from_pystan()`: https://arviz-devs.github.io/arviz/api/generated/arviz.from_pystan.html#arviz-from-pystan


Discussion:

Resolving patch:
diff --git a/arviz/data/io_pystan.py b/arviz/data/io_pystan.py
--- a/arviz/data/io_pystan.py
+++ b/arviz/data/io_pystan.py
@@ -809,6 +809,8 @@ def get_draws_stan3(fit, model=None, variables=None, ignore=None, dtypes=None):
     data = OrderedDict()
 
     for var in variables:
+        if var in ignore:
+            continue
         if var in data:
             continue
         dtype = dtypes.get(var)


Test patch:
diff --git a/arviz/tests/external_tests/test_data_pystan.py b/arviz/tests/external_tests/test_data_pystan.py
--- a/arviz/tests/external_tests/test_data_pystan.py
+++ b/arviz/tests/external_tests/test_data_pystan.py
@@ -153,7 +153,7 @@ def test_inference_data(self, data, eight_schools_params):
         inference_data5 = self.get_inference_data5(data)
         # inference_data 1
         test_dict = {
-            "posterior": ["theta"],
+            "posterior": ["theta", "~log_lik"],
             "posterior_predictive": ["y_hat"],
             "predictions": ["y_hat"],
             "observed_data": ["y"],