Repository: arviz-devs/arviz

Issue:
from_cmdstanpy uses posterior in place of prior when both provided
**Describe the bug**
I'm seeing some odd behavior on a branch of arviz when using `from_cmdstanpy`. Basically, if I fit a model twice using cmdstanpy & construct the az.InferenceData object using both the prior & the posterior samples, I see that the posterior draws are in both the prior & posterior groups. 

In other words, say I construct my arviz InferenceData object as follows:

```py
idata = az.from_cmdstanpy(posterior_fit, 
    prior = prior_fit, 
    coords = dict(school=["A", "B", "C", "D", "E", "F", "G", "H"]), 
    dims = dict(
        theta=["school"],
        y=["school"],
        log_lik=["school"],
        y_hat=["school"],
        theta_tilde=["school"],
    ),
    posterior_predictive = ["y_hat"],
    prior_predictive = ['y_hat'],
    log_likelihood = ["log_lik"])
```

The draws for the prior & posterior are identical.

```py
all(idata.prior == idata.posterior)
> True
```

Notably, this is not the case for the predicted quantities:

```py
all(idata.prior_predictive == idata.posterior_predictive)
> False
```

**To Reproduce**
I have set up a reproducible example here: https://gist.github.com/jburos/b8839b7919455d6a0019d7ce33394c2f

**Expected behavior**
The prior draws should be saved in the prior group.

**Additional context**

I had installed `arviz` from the `cmdstanpy` branch, due to previous bugs. I was on commit d95e9763b19611fcdeefa7b9aecb2746d5534fc9, according to my Pipfile.lock.

My system:

```py
In [3]: sys.version
Out[3]: '3.8.7 (default, Feb  3 2021, 06:31:03) \n[Clang 12.0.0 (clang-1200.0.32.29)]'
In [5]: arviz.__version__
Out[5]: '0.11.2'




Discussion:
Thanks for the info. There were couple of typos.

https://github.com/arviz-devs/arviz/pull/1595
Linking to a [comment on the PR](https://github.com/arviz-devs/arviz/pull/1595#issuecomment-790933424), i'm still seeing this bug on a fit from the `main` branch. I'm on cmdstanpy v 0.9.68.

There is one more typo that needs to be resolved. 
Thanks for double checking!

Resolving patch:
diff --git a/arviz/data/io_cmdstanpy.py b/arviz/data/io_cmdstanpy.py
--- a/arviz/data/io_cmdstanpy.py
+++ b/arviz/data/io_cmdstanpy.py
@@ -50,8 +50,16 @@ def __init__(
 
         self.save_warmup = rcParams["data.save_warmup"] if save_warmup is None else save_warmup
 
-        if self.log_likelihood is None and "log_lik" in self.posterior.stan_vars_cols:
-            self.log_likelihood = ["log_lik"]
+        if hasattr(self.posterior, "stan_vars_cols"):
+            if self.log_likelihood is None and "log_lik" in self.posterior.stan_vars_cols:
+                self.log_likelihood = ["log_lik"]
+        else:
+            if (
+                self.log_likelihood is None
+                and self.posterior is not None
+                and any(name.split("[")[0] == "log_lik" for name in self.posterior.column_names)
+            ):
+                self.log_likelihood = ["log_lik"]
 
         import cmdstanpy  # pylint: disable=import-error
 
@@ -309,7 +317,7 @@ def prior_to_xarray(self):
                 except ValueError:
                     pass
             data, data_warmup = _unpack_fit(
-                self.posterior,
+                self.prior,
                 items,
                 self.save_warmup,
             )
@@ -483,7 +491,6 @@ def posterior_to_xarray_pre_v_0_9_68(self):
             ),
         )
 
-    @requires("posterior")
     def sample_stats_to_xarray_pre_v_0_9_68(self, fit):
         """Extract sample_stats from fit."""
         dtypes = {"divergent__": bool, "n_leapfrog__": np.int64, "treedepth__": np.int64}


Test patch: