Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

from_pymc3 fails in certain circumstances #692

Closed
canyon289 opened this issue Jun 5, 2019 · 7 comments
Closed

from_pymc3 fails in certain circumstances #692

canyon289 opened this issue Jun 5, 2019 · 7 comments

Comments

@canyon289
Copy link
Member

canyon289 commented Jun 5, 2019

Describe the bug
A clear and concise description of what the bug is.

To Reproduce

import arviz as az
import pymc3 as pm
print(az.__version__)
print(pm.__version__)

with pm.Model() as planetmodel:
    p_water = pm.Uniform("p", 0 ,1)
    w = pm.Binomial("w", p=p_water, n=2, observed=1)
    trace = pm.sample(5000, chains=2)

water_data = az.from_pymc3(trace=trace)

0.4.0
3.7
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [p]
Sampling 2 chains: 100%|███████████████████████████████████████████████████████████████| 11000/11000 [00:03<00:00, 2882.36draws/s]
/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py:56: FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.
chain_likelihoods.append(np.stack(log_like))
Traceback (most recent call last):
File "trace_error.py", line 11, in
water_data = az.from_pymc3(trace=trace)
File "/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py", line 152, in from_pymc3
dims=dims,
File "/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py", line 137, in to_inference_data
"sample_stats": self.sample_stats_to_xarray(),
File "/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/base.py", line 25, in wrapped
return func(cls, *args, **kwargs)
File "/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py", line 78, in sample_stats_to_xarray
log_likelihood, dims = self._extract_log_likelihood()
File "/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/base.py", line 25, in wrapped
return func(cls, *args, **kwargs)
File "/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py", line 56, in _extract_log_likelihood
chain_likelihoods.append(np.stack(log_like))
File "/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/numpy/core/shape_base.py", line 410, in stack
arrays = [asanyarray(arr) for arr in arrays]
File "/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/numpy/core/shape_base.py", line 410, in
arrays = [asanyarray(arr) for arr in arrays]
File "/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py", line 55, in
log_like = (log_likelihood_vals_point(point) for point in self.trace.points([chain]))
File "/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py", line 51, in log_likelihood_vals_point
return np.concatenate(log_like_vals)
ValueError: zero-dimensional arrays cannot be concatenated

Expected behavior
An Inferencedata object should be created
Additional context

@ahartikainen
Copy link
Contributor

In this func

    def log_likelihood_vals_point(point):
            """Compute log likelihood for each observed point."""
            log_like_vals = []
            for var, log_like in cached:
                log_like_val = log_like(point)
                if var.missing_values:
                    log_like_val = log_like_val[~var.observations.mask]
                log_like_vals.append(log_like_val)
            return np.concatenate(log_like_vals)

log_like(point) returns a 0-dim array (np.array(num)), which can't be concatenated.

One option is to wrap that to np.atleast_1d(log_like(point))

@canyon289
Copy link
Member Author

Fix makes sense, but moreso how this started appearing in the scipy tutorial when the env was frozen. Maybe it'll just be a mystery :)

@ahartikainen
Copy link
Contributor

This change comes from pymc, not arviz (?)

@canyon289
Copy link
Member Author

Tutorial has pinned version? No matter, I'll just fix and move on with life. Thanks @ahartikainen

@canyon289
Copy link
Member Author

Came from ArviZ. I'll wrap like you suggested

95c9888

@ahartikainen
Copy link
Contributor

True. Ravel makes 0d -> 1d?

@ColCarroll
Copy link
Member

Closing since it seems fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants