How to fix 'TypeError: hasattr (): attribute name must be string' error?

I have the following code:

import pymc as pm
from matplotlib import pyplot as plt
from pymc.Matplot import plot as mcplot
import numpy as np
from matplotlib import rc

res = [18.752, 12.450, 11.832]

v = pm.Uniform('v', 0, 20)

errors = pm.Uniform('errors', 0, 100, size = 3)

taus = 1/(errors ** 2)

mydist = pm.Normal('mydist', mu = v, tau = taus, value = res, observed = True)

model=pm.Model([mydist, errors, taus, v, res])
mcmc=pm.MCMC(model) # This is line 19 where the TypeError originates
mcmc.sample(20000,10000)

mcplot(mcmc.trace('mydist'))

      

For some reason this doesn't work, I get "TypeError: hasattr (): attribute name must be string" with the following traceback:

 Traceback (most recent call last):

  File "<ipython-input-49-759ebaf4321c>", line 1, in <module>
runfile('C:/Users/Paul/.spyder2-py3/temp.py', wdir='C:/Users/Paul/.spyder2-py3')

  File "C:\Users\Paul\Miniconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

  File "C:/Users/Paul/.spyder2-py3/temp.py", line 19, in <module>
mcmc=pm.MCMC(model)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\MCMC.py", line 82, in __init__
**kwds)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Model.py", line 197, in __init__
Model.__init__(self, input, name, verbose)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Model.py", line 99, in __init__
ObjectContainer.__init__(self, input)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Container.py", line 606, in __init__
conservative_update(self, input_to_file)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Container.py", line 549, in conservative_update
if not hasattr(obj, k):

TypeError: hasattr(): attribute name must be string

      

How do I get it to work and output "mydist"?

Edit: I accidentally encountered the error by mistake.

Edit2: everything must be because res has no name, because it is an array, but I don't know how to assign a name to it, so it will do the job.

+3


source to share


2 answers


I have to admit that I am not familiar with pymc, but changing it to the following at least got the application up and running:

mydist = pm.Normal('mydist', mu = v, tau = taus, value = res, observed = False)

mcmc=pm.MCMC([mydist, errors, taus, v, res])

      

It looks like you are wrapping everything in a Model which is an extension ObjectContainer

, but since you passed a list to it, MCMC file_items

in Container.py tried to assign an index 4

in the list to something uses replace

, but since it Model

is ObjectContainer

, it is assigned a key 4

in it __dict__

causing a strange TypeError

which you received. Removing the wrapper Model

caused MCMC

it to be used correctly ListContainer

.



Now, there is probably a bug in Model.py on line 543 where the observable stochastics are not stored in the database - the expression for object in self.stochastics | self.deterministics:

but I suspect it should include it self.observable_stochastics

too - so I needed to change observable

to False

, or the last line would throw KeyError

.

I am not familiar enough with pymc to determine if this is indeed a bug or desired behavior, so I leave that to you to issue an issue about it.

+1


source


You just need to define res

as a numpy array:

res = np.array([18.752, 12.450, 11.832])

      



Then you will get an error here mcmc.trace('mydist')

because mydist

is the observable data and therefore not selected. You probably want to build other variables ...

0


source







All Articles