Repository: arviz-devs/arviz

Issue:
Trace plot from cmdstan results in IndexError: arrays used as indices must be of integer (or boolean) type
I can't plot the trace plot from cmdstan
```python
ID = 3
posterior_glob =  "./samplesID_%d-[1-9].csv"%ID
obs_data_path = "./DataID_%d.R"%ID
cmdstan_data = az.from_cmdstan(posterior = posterior_glob, observed_data=obs_data_path)

pars = ["R0","kappa"]
az.plot_trace(cmdstan_data,var_names=pars)
```
which results in 
```python
/home/XXX/arviz/plots/kdeplot.py:326: RuntimeWarning: invalid value encountered in true_divide
  def _fast_kde_2d(x, y, gridsize=(128, 128), circular=False):

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-145-9641094159ee> in <module>
----> 1 az.plot_trace(cmdstan_data,var_names=pars)

~/anaconda3/lib/python3.6/site-packages/arviz/plots/traceplot.py in plot_trace(data, var_names, coords, divergences, figsize, textsize, lines, compact, combined, legend, plot_kwargs, fill_kwargs, rug_kwargs, hist_kwargs, trace_kwargs, max_plots)

IndexError: arrays used as indices must be of integer (or boolean) type
```
I can get density_plot with no problems, but trace plot does not work. It shows the first subplot for R0, but then displays the error.

**Additional context**
Stable version 0.5.1 installed via pip-command



Discussion:
With dev version from github, I have another error
```python
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-5-9641094159ee> in <module>
----> 1 az.plot_trace(cmdstan_data,var_names=pars)

~/anaconda3/lib/python3.6/site-packages/arviz/plots/traceplot.py in plot_trace(data, var_names, coords, divergences, figsize, textsize, rug, lines, compact, combined, legend, plot_kwargs, fill_kwargs, rug_kwargs, hist_kwargs, trace_kwargs, backend, **kwargs)
    111     """
    112     if backend is None or backend.lower() in ("mpl", "matplotlib"):
--> 113         from .backends.matplotlib.mpl_traceplot import _plot_trace_mpl
    114 
    115         axes = _plot_trace_mpl(

ModuleNotFoundError: No module named 'arviz.plots.backends.matplotlib'
```

Dev version is currently a bit wip.

What does your R0 and kappa look (e.g. `data.posterior.R0`)

What does `data.posterior.draw` and `data.posterior.chain` say?
I can't say what is going on, but I suspect that there might be some kind of error in from_cmdstan or your data is ragged (there are some nan in some chains)
I see - good to know. 

My code is here: https://gist.github.com/aakhmetz/d726e9bc362b399ed6494f4144d65141
I would say nothing special about samples-file. It is a usual format for cmdstan I think.
I just updated my gist with the output from the stable version of arviz. Thank you for checking in advance.

```cmdstan_data.posterior.draw``` gives:
```python
<xarray.DataArray 'draw' (draw: 1250)>
array([   0,    1,    2, ..., 1247, 1248, 1249])
Coordinates:
  * draw     (draw) int64 0 1 2 3 4 5 6 7 ... 1243 1244 1245 1246 1247 1248 1249
```

```cmdstan_data.posterior.chain``` gives:
```python
<xarray.DataArray 'chain' (chain: 1)>
array([0])
Coordinates:
  * chain    (chain) int64 0
```
Yes, there is an error in `from_cmdstan` where part of the sample_stats items are `float` (they should be `int` or `bool`). 

The quick fix to plot your results is

    az.plot_trace(cmdstan_data,var_names=pars, divergences=False)

Resolving patch:
diff --git a/arviz/data/io_cmdstan.py b/arviz/data/io_cmdstan.py
--- a/arviz/data/io_cmdstan.py
+++ b/arviz/data/io_cmdstan.py
@@ -224,7 +224,7 @@ def sample_stats_to_xarray(self):
                 name = re.sub("__$", "", key_)
                 name = "diverging" if name == "divergent" else name
                 rename_dict[key] = ".".join((name, *end))
-                sampler_params[j][key] = s_params[key].astype(dtypes.get(key))
+                sampler_params[j][key] = s_params[key].astype(dtypes.get(key_))
             sampler_params[j] = sampler_params[j].rename(columns=rename_dict)
         data = _unpack_dataframes(sampler_params)
         return dict_to_dataset(data, coords=coords, dims=dims)
@@ -300,7 +300,7 @@ def sample_stats_prior_to_xarray(self):
                 name = re.sub("__$", "", key_)
                 name = "diverging" if name == "divergent" else name
                 rename_dict[key] = ".".join((name, *end))
-                sampler_params[j][key] = s_params[key].astype(dtypes.get(key))
+                sampler_params[j][key] = s_params[key].astype(dtypes.get(key_))
             sampler_params[j] = sampler_params[j].rename(columns=rename_dict)
         data = _unpack_dataframes(sampler_params)
         return dict_to_dataset(data, coords=coords, dims=dims)
@@ -654,7 +654,8 @@ def _unpack_dataframes(dfs):
     sample = {}
     for key, cols_locs in col_groups.items():
         ndim = np.array([loc for _, loc in cols_locs]).max(0) + 1
-        sample[key] = utils.full((chains, draws, *ndim), np.nan)
+        dtype = dfs[0][cols_locs[0][0]].dtype
+        sample[key] = utils.full((chains, draws, *ndim), 0, dtype=dtype)
         for col, loc in cols_locs:
             for chain_id, df in enumerate(dfs):
                 draw = df[col].values
diff --git a/arviz/utils.py b/arviz/utils.py
--- a/arviz/utils.py
+++ b/arviz/utils.py
@@ -317,6 +317,6 @@ def expand_dims(x):
 
 
 @conditional_jit(parallel=True)
-def full(shape, x):
+def full(shape, x, dtype=None):
     """Jitting numpy full."""
-    return np.full(shape, x)
+    return np.full(shape, x, dtype=dtype)


Test patch: