arviz-devs / arviz

Exploratory analysis of Bayesian models with Python
https://python.arviz.org
Apache License 2.0
1.6k stars 401 forks source link

Trace plot from cmdstan results in IndexError: arrays used as indices must be of integer (or boolean) type #901

Closed aakhmetz closed 4 years ago

aakhmetz commented 4 years ago

I can't plot the trace plot from cmdstan

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

/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

aakhmetz commented 4 years ago

With dev version from github, I have another error

---------------------------------------------------------------------------
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'
ahartikainen commented 4 years ago

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)

aakhmetz commented 4 years ago

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.

aakhmetz commented 4 years ago

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:

<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:

<xarray.DataArray 'chain' (chain: 1)>
array([0])
Coordinates:
  * chain    (chain) int64 0
ahartikainen commented 4 years ago

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)
aakhmetz commented 4 years ago

Thanks!