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

Indexing problem when using SQLite backend #790

Closed
DeliciousHair opened this issue Jul 23, 2015 · 3 comments
Closed

Indexing problem when using SQLite backend #790

DeliciousHair opened this issue Jul 23, 2015 · 3 comments
Assignees

Comments

@DeliciousHair
Copy link
Contributor

Hello!

Not quite sure how to describe this, so I'll just go with an example to demonstrate, specifically the intro model from the tutorial page.


import numpy as np
from pymc3 import *
from scipy import optimize

np.random.seed(123)
alpha, sigma = 1, 1
beta = [1, 2.5]
size = 100

X1 = np.linspace(0, 1, size)
X2 = np.linspace(0,.2, size)
Y = alpha + beta[0]*X1 + beta[1]*X2 + np.random.randn(size)*sigma

basic_model = Model()

with basic_model:
    alpha = Normal('alpha', mu=0, sd=10)
    beta = Normal('beta', mu=0, sd=10, shape=2)
    sigma = HalfNormal('sigma', sd=1)
    mu = alpha + beta[0]*X1 + beta[1]*X2

    Y_obs = Normal('Y_obs', mu=mu, sd=sigma, observed=Y)

    start = find_MAP(fmin=optimize.fmin_powell)
    step = NUTS(scaling=start)
    backend = backends.SQLite('demo_sampling.sqlite')
    trace = sample(500, step, start=start, trace=backend)

This works fine. The problem arises when I try to use the traceplot method with some indexing:


In [17]: traceplot(trace[500::5])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in ()
----> 1 traceplot(trace[500::5])

/usr/local/lib/python2.7/dist-packages/pymc3/backends/base.pyc in __getitem__(self, idx)
    159     def __getitem__(self, idx):
    160         if isinstance(idx, slice):
--> 161             return self._slice(idx)
    162 
    163         try:

/usr/local/lib/python2.7/dist-packages/pymc3/backends/base.pyc in _slice(self, idx)
    215         """Return a new MultiTrace object sliced according to `idx`."""
    216         new_traces = [trace._slice(idx) for trace in self._traces.values()]
--> 217         return MultiTrace(new_traces)
    218 
    219     def point(self, idx, chain=None):

/usr/local/lib/python2.7/dist-packages/pymc3/backends/base.pyc in __init__(self, traces)
    140         self._traces = {}
    141         for trace in traces:
--> 142             if trace.chain in self._traces:
    143                 raise ValueError("Chains are not unique.")
    144             self._traces[trace.chain] = trace

AttributeError: 'NoneType' object has no attribute 'chain'

Clearly, there is something wrong with how the traces are being read --traces not being passed to MultiTraces or something perhaps? But I cannot seem to figure this out as calling like this when using the default RAM backend works fine. Any ideas where I might want to look?

@twiecki
Copy link
Member

twiecki commented Jul 23, 2015

Thanks for reporting. @kyleam any ideas?

kyleam added a commit that referenced this issue Jul 23, 2015
Return a NDArray slice instead of warning when an out-of-memory backend
is sliced.

Fixes #790
kyleam added a commit that referenced this issue Jul 23, 2015
Out-of-memory backends give a warning when the user tries to slice them,
so the result may be a list of Nones.

re: #790
@kyleam
Copy link
Contributor

kyleam commented Jul 23, 2015 via email

@DeliciousHair
Copy link
Contributor Author

Thanks @kyleam, that makes perfect sense! Digging through backends/sqlite.py I see this is indeed mentioned, but I would also mention that I did not receive the warning message you alluded to. Will check out your PR to see how this fix works :-)

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