Coda_output function doesn't work.
Hi,
So after going through the utils.coda_output function I noticed that observed nodes are being skipped with a try/except statement. The caught on the except is a TypeError, and I've noticed that when an observed node is used, I doesn't have the attribute .trace() , so an AttributeError is thrown instead. Since no catch is made, the program crashes.
I set the information in the gelman_biossary.py in a function called tester(), which returns locals():
Here are my results...
>>> from pymc.examples import gelman_bioassay
>>> bar = pymc.MCMC(gelman_bioassay.tester(), db="ram")
>>> bar.stochastics
set([<pymc.distributions.Normal 'alpha' at 0x000000001673C780>, <pymc.distributions.Normal 'beta' at 0x000000001673C128>])
>>> bar.sample(100)
[ 0% ]
[****************100%******************] 100 of 100 complete
>>> pymc.utils.coda_output(bar)
Generating CODA output
==================================================
Processing alpha
Processing beta
Processing LD50
Processing deaths
Traceback (most recent call last):
File "<pyshell#174>", line 1, in <module>
pymc.utils.coda_output(bar)
File "C:\Python27\lib\site-packages\pymc\utils.py", line 726, in coda_output
index = _process_trace(trace_file, index_file, v.trace(), vname, index)
AttributeError: 'Binomial' object has no attribute 'trace'
I've made an edit, which will remove all observed nodes from the variables list, before the loop:
def coda_output(pymc_object):
"""Generate output files that are compatible with CODA
:Arguments:
pymc_object : Model or Node
A PyMC object containing MCMC output.
"""
print_()
print_("Generating CODA output")
print_('='*50)
name = pymc_object.__name__
# Open trace file
trace_file = open(name+'_coda.out', 'w')
# Open index file
index_file = open(name+'_coda.ind', 'w')
## observedNodes is a list of any observed node name in the model
observedNodes = [node.__name__ for node in pymc_object.observed_stochastics]
variables = [pymc_object]
if hasattr(pymc_object, 'variables'):
## changed this call to the one below
#variables = pymc_object.variables
variables = [v for v in pymc_object.variables if v.__name__ not in observedNodes]
# Initialize index
index = 1
# Loop over all parameters
for v in variables:
vname = v.__name__
print_("Processing", vname)
try:
index = _process_trace(trace_file, index_file, v.trace(), vname, index)
except TypeError:
pass
# Close files
trace_file.close()
index_file.close()
# Lazy shortcut
coda = coda_output
This works for me. I'm not sure if anyone else has had issues with this, but here's a fix.
Also I believe (haven't tested yet though) that if you change the "except TypeError:" to "except Exception:" it will work as well.
Can you please post your code for tester? I currently cannot replicate this behavior. What version of PyMC are you running?
I am using pymc2.2 64bit. Here is the code for tester,
from pymc import *
from numpy import ones, array
def tester():
# Samples for each dose level
n = 5*ones(4,dtype=int)
# Log-dose
dose=array([-.86,-.3,-.05,.73])
# Logit-linear model parameters
alpha = Normal('alpha', 0, 0.01)
beta = Normal('beta', 0, 0.01)
# Calculate probabilities of death
theta = Lambda('theta', lambda a=alpha, b=beta, d=dose: invlogit(a + b*d))
# Data likelihood
deaths = Binomial('deaths', n=n, p=theta, value=array([0,1,3,5], dtype=float), observed=True)
# Calculate LD50
LD50 = Lambda('LD50', lambda a=alpha, b=beta: -a/b)
return locals()
Can you build a version from the GitHub repository and see if it goes away? The code in tester() does not generate any sort of error when I run coda_output on the model.
I wasn't able to get the cloned zip to build on windows, but I had used the version from here for the 32bit code:
http://pypi.python.org/pypi/pymc/
I got the 64bit from:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyzmq which was updated May 7th, 2012
I have the same problem. I could use the above workaround code after the following preparations:
def print_(*args):
print args
_process_trace = pymc.utils._process_trace
The test code passes in the current master. Closing.