TypeError when using arviz.from_cmdstanpy(fit)

I tried to use arviz to visualize the posterior params. But when I load the data from fit, the error appeared. Does anyone have any idea about what causes such error? Or is there any alternative method to use arviz?

fit = model.sample(data=lg_data_stan, inits=0.1, chains=4, max_treedepth=10, adapt_delta=0.9,
        iter_warmup=1000, save_warmup=False, iter_sampling=1000, thin=2, show_progress=True, output_dir= path + '/output/', 
data = arviz.from_cmdstanpy(fit)

Traceback (most recent call last):

File “”, line 1, in
data = az.from_cmdstanpy(fit2)

File “/Users/eva/anaconda3/lib/python3.8/site-packages/arviz/data/io_cmdstanpy.py”, line 659, in from_cmdstanpy
return CmdStanPyConverter(

File “/Users/eva/anaconda3/lib/python3.8/site-packages/arviz/data/io_cmdstanpy.py”, line 335, in to_inference_data
“posterior”: self.posterior_to_xarray(),

File “/Users/eva/anaconda3/lib/python3.8/site-packages/arviz/data/base.py”, line 46, in wrapped
return func(cls, *args, **kwargs)

File “/Users/eva/anaconda3/lib/python3.8/site-packages/arviz/data/io_cmdstanpy.py”, line 61, in posterior_to_xarray
return self.posterior_to_xarray_pre_v_0_9_68()

File “/Users/eva/anaconda3/lib/python3.8/site-packages/arviz/data/base.py”, line 46, in wrapped
return func(cls, *args, **kwargs)

File “/Users/eva/anaconda3/lib/python3.8/site-packages/arviz/data/io_cmdstanpy.py”, line 402, in posterior_to_xarray_pre_v_0_9_68
data, data_warmup = _unpack_frame(

File “/Users/eva/anaconda3/lib/python3.8/site-packages/arviz/data/io_cmdstanpy.py”, line 539, in _unpack_frame
data = fit.draws(inc_warmup=save_warmup)

TypeError: ‘int’ object is not callable

1 Like

Which versions of ArviZ and CmdStanPy are you using?

Latest cmdstanpy version added many improvements on how metadata for variables and sample stats are handled, so we updated the converter in ArviZ to take advantage of that. We still tried to keep the converter compatible with the older versions (this is why there is a posterior_to_xarray_pre_v_0_9_68 in the error trace) but the converter is only tested in the latest version, so it possible that either some later change broke older version compatibility for latest ArviZ or that the inital manual backward compatibility tests did not cover enough cases.

If possible, I would recommend using ArviZ development version and latest cmdstanpy. Or if it were not possible, to downgrade ArviZ to 0.11.0, 0.10.0 or the release that was tested against your cmdstanpy version.

My CmdStanpy is 0.9.5 and ArviZ is 0.11.2.

I have updated ArviZ to the development version. But the following error appeared.

Traceback (most recent call last):

File “”, line 1, in
data = az.from_cmdstanpy(fit)

File “/Users/eva/anaconda3/lib/python3.8/site-packages/arviz/data/io_cmdstanpy.py”, line 751, in from_cmdstanpy
return CmdStanPyConverter(

File “/Users/eva/anaconda3/lib/python3.8/site-packages/arviz/data/io_cmdstanpy.py”, line 53, in init
if self.log_likelihood is None and “log_lik” in self.posterior.stan_vars_cols:

AttributeError: ‘CmdStanMCMC’ object has no attribute ‘stan_vars_cols’

Any idea about how to deal with it? Or if it is hard to directly use from_cmdstanpy function, is there any method that I can extract data needed from the output file and put it into arviz?

Hi, there is a bug in arviz.

I have a PR there to fix the issue Fix cmdstanpy io by ahartikainen · Pull Request #1598 · arviz-devs/arviz · GitHub

For now, easiest way to go forward is to update cmdstanpy

python -m pip install cmdstanpy -U

I just updated cmdstanpy but when I sample the model using the following code,

fit1 = model.sample(data=lg_data_stan, inits=0.1, chains=2, max_treedepth=10, adapt_delta=0.9,
iter_warmup=200, save_warmup=False, iter_sampling=100, thin=2, show_progress=True, output_dir= path + ‘/output/’,

The following error occurs. But such error never occurs when I did not update cmdstanpy. Any idea?

Traceback (most recent call last):

File “”, line 1, in
fit1 = model.sample(data=lg_data_stan, inits=0.1, chains=2, max_treedepth=10,

File “/Users/eva/anaconda3/lib/python3.8/site-packages/cmdstanpy/model.py”, line 704, in sample
sampler_args = SamplerArgs(

TypeError: init() got an unexpected keyword argument ‘adapt_init_phase’

That error sounds weird.

Have you restarted / closed your current python sessions (if using IDE)?

Thank you so much! The problem has been solved after I restarted Spyder.

But I met a new problem when I want to plot the posterior of the parameters.

data = az.from_cmdstanpy(fit)
az.plot_trace(data, var_names=("itc_k_pr[1,1]", "itc_k_pr[1,2]", "itc_k_pr[5,4]"))

Traceback (most recent call last):

File “/Users/chenwenqi/anaconda3/lib/python3.8/site-packages/arviz/utils.py”, line 59, in _var_names
var_names = _subset_list(var_names, all_vars, filter_items=filter_vars, warn=False)

File “/Users/chenwenqi/anaconda3/lib/python3.8/site-packages/arviz/utils.py”, line 137, in _subset_list
raise KeyError("{} are not present".format(np.array(subset)[~existing_items]))

KeyError: “[‘itc_k_pr[1,1]’ ‘itc_k_pr[1,2]’ ‘itc_k_pr[5,4]’] are not present”

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File “”, line 1, in
az.plot_trace(data, var_names=(“itc_k_pr[1,1]”, “itc_k_pr[1,2]”, “itc_k_pr[5,4]”))

File “/Users/chenwenqi/anaconda3/lib/python3.8/site-packages/arviz/plots/traceplot.py”, line 193, in plot_trace
var_names = _var_names(var_names, coords_data, filter_vars)

File “/Users/chenwenqi/anaconda3/lib/python3.8/site-packages/arviz/utils.py”, line 62, in _var_names
raise KeyError(msg) from err

KeyError: ‘var names: “[‘itc_k_pr[1,1]’ ‘itc_k_pr[1,2]’ ‘itc_k_pr[5,4]’] are not present” in dataset’

It seems I did not input correct names of the parameters. I have also tried to find the names in the output csv files but they also do not work. Is there any way that I could know what name I should input?


You can drop the index from the name. ([...] part)

To access specific coordinates you need to use .sel method. Also, for now indexing starts with 0 on python.

See examples here how InferenceData object works


1 Like

To add on what Ari said, in ArviZ, selection in done with label based indexing by default, not with positional indexing. You can use .sel or the coords argument in plot functions like plot_trace, or use .isel instead to use positional index (which starts at 0).

When creating InferenceData, ArviZ always populates all the coordinate values with the user provided values (if any) or with integer indexes. If you have the development version installed as we suggested earlier, you can use the rcParam data.index_origin to choose if those default indexes start at 0 or at 1. Thus, as indexing is label based, you can use sel and coords with the same indexes used in Stan and you will see those same imdexes when printing the inferencedata.

Useful references:
The label guide in ArviZ docs covers how labeling works (in dev version) and I wrote two blogposts on plot_trace and rcParams: Categories | Oriol unraveled

Thank you both so much! It is a great help!

1 Like