arviz-devs / arviz

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

Broadcast Input Array Error #1571

Open fthames opened 3 years ago

fthames commented 3 years ago

I ran this code, which worked perfectly yesterday.

fit_pz_hlm=pz_model_hlm.sampling(data=data_zip1_hlm, warmup=1000, iter=1000, 
                                 chains=4, thin=4, control=dict(max_treedepth=18
                                                                , adapt_delta=0.999))
az_fit_pz_hlm=az.from_pystan(posterior=fit_pz_hlm, posterior_predictive=['y_rep'], 
                             observed_data=['y'],
                           log_likelihood='log_lik')
az.to_netcdf(az_fit_pz_hlm, 'fit_pz_hlm')

Today, I consistently receive this error message: ValueError: could not broadcast input array from shape (250,) into shape (0,). The Traceback is below.

I believe that several packages in python were updated this morning. Thoughts?

thanks in advance.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-32-b19021399361> in <module>
      2                                  chains=4, thin=4, control=dict(max_treedepth=18
      3                                                                 , adapt_delta=0.999))
----> 4 az_fit_pz_hlm=az.from_pystan(posterior=fit_pz_hlm, posterior_predictive=['y_rep'], 
      5                              observed_data=['y'],
      6                            log_likelihood='log_lik')

/usr/local/lib/python3.8/dist-packages/arviz/data/io_pystan.py in from_pystan(posterior, posterior_predictive, predictions, prior, prior_predictive, observed_data, constant_data, predictions_constant_data, log_likelihood, coords, dims, posterior_model, prior_model, save_warmup)
    942         ).to_inference_data()
    943     else:
--> 944         return PyStanConverter(
    945             posterior=posterior,
    946             posterior_predictive=posterior_predictive,

/usr/local/lib/python3.8/dist-packages/arviz/data/io_pystan.py in to_inference_data(self)
    266             save_warmup=self.save_warmup,
    267             **{
--> 268                 "posterior": self.posterior_to_xarray(),
    269                 "sample_stats": self.sample_stats_to_xarray(),
    270                 "log_likelihood": self.log_likelihood_to_xarray(),

/usr/local/lib/python3.8/dist-packages/arviz/data/base.py in wrapped(cls, *args, **kwargs)
     44                 if all([getattr(cls, prop_i) is None for prop_i in prop]):
     45                     return None
---> 46             return func(cls, *args, **kwargs)
     47 
     48         return wrapped

/usr/local/lib/python3.8/dist-packages/arviz/data/io_pystan.py in posterior_to_xarray(self)
     84         ignore = posterior_predictive + predictions + log_likelihood + ["lp__"]
     85 
---> 86         data, data_warmup = get_draws(posterior, ignore=ignore, warmup=self.save_warmup)
     87         attrs = get_attrs(posterior)
     88         return (

/usr/local/lib/python3.8/dist-packages/arviz/data/io_pystan.py in get_draws(fit, variables, ignore, warmup)
    615             for key, loc in keys_locs:
    616                 ary_slice = tuple(axes + loc)
--> 617                 ary[ary_slice] = pyholder.chains[key][-ndraw:]
    618                 if warmup:
    619                     ary_warmup[ary_slice] = pyholder.chains[key][:ndraw_warmup]

ValueError: could not broadcast input array from shape (250,) into shape (0,)
ahartikainen commented 3 years ago

Hmm, does that work with PyStan? What happens if you add iter=1001

fthames commented 3 years ago

Hmm, does that work with PyStan? What happens if you add iter=1001

Of course. I should have looked at the code again. Changing the iter fixed it. Thanks for your help.

ahartikainen commented 3 years ago

No problem, still a bug we need to fix in arviz.

madhucharan commented 3 years ago

@ahartikainen , would you guide me on how to proceed in solving this bug.

ahartikainen commented 3 years ago

We need to figure out the components here

ary[ary_slice] = pyholder.chains[key][-ndraw:]
ary_slice

or

[-ndraw:]

fails.

We probably need to add 0 size check somewhere there.

madhucharan commented 3 years ago

Is there a test to check this function already or we need to add them to check how it is working?

ahartikainen commented 3 years ago

I don't think there is a test for this.

Not sure if we need to add a test for this. It depends how we implement this.

tomicapretto commented 1 year ago

Is this still an issue?

ahartikainen commented 1 year ago

Probably, maybe we need to add a fix for this?