pymc-devs / pymc2

THIS IS THE **OLD** PYMC PROJECT (VERSION 2). PLEASE USE PYMC INSTEAD:
http://pymc-devs.github.com/pymc/
Other
879 stars 228 forks source link

Cannot call dir() on an MCMC object #80

Closed monkpit closed 8 years ago

monkpit commented 8 years ago

Windows 7 running Python 3.5 32-bit and pymc 2.3.5.

Steps to recreate:

>>> import pymc
>>> m = pymc.MCMC()
>>> dir(m)

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()

Expected output: a normal python dir listing of the attributes of the MCMC object.

Edit:

Expected output same as or similar to m.__dir__():

>>> m.__dir__()

['containing_classes', '__str__', '__reduce_ex__', '__subclasshook__', '__ne__', '_get_dic',
'_calc_bpic', 'replace', 'commit', 'deterministics', 'pymc', 'draw', 'tune', '__lt__', 'variables',
'__setattr__', '_assign_database_backend', '__init__', 'verbose', 'containers', '_Sampler__status',
'__hash__', 'change_methods', 'remove_step_method', 'logp', 'iprompt', '_state', 'icontinue',
'register', '__dict__', 'draw_from_prior', '__name__', 'potentials', 'assimilate', '__ge__',
'_get_generations', '__loader__', '__reduce__', 'remember', 'moral_neighbors', '_sum_deviance',
'restore_sm_state', '__le__', 'DIC', '_halt', 'summary', 'generations', 'write_csv',
'_variables_to_tally', '__sizeof__', 'seed', '_finalize', 'trace', '_iter', 'assign_step_methods', 'pause',
'restore_sampler_state', '__class__', '_dict_container', 'OCValue', 'get_node', '__new__', 'status',
'__package__', 'db', '__doc__', '__module__', '_calc_dic', 'observed_stochastics', '_value',
'__delattr__', '_current_iter', '_funs_to_tally', 'use_step_method', 'coparents', 'isample', 'halt',
'__getattribute__', 'stats', 'nodes', '__eq__', '__gt__', '__weakref__', '_get_deviance', '_csv_str',
'tally', 'markov_blanket', 'value', '_get_value', '_loop', 'save_state', '_get_bpic', '_sm_assigned',
'_db_args', '__repr__', '__spec__', 'step_method_dict', '__dir__', 'deviance', '__format__',
'__builtins__', 'stochastics', '_get_logp', '__slotnames__', 'sample', 'get_state', 'BPIC']
fonnesbeck commented 8 years ago

That's interesting. I'll look into it. Thanks for reporting.

fonnesbeck commented 8 years ago

I notice that this only happens when you create a model with no variables, which we should probably forbid. For some reason there is an integer attribute that results, which causes the issue.

monkpit commented 8 years ago

I initially tried this with a model that was not empty (while following along with Bayesian Methods for Hackers) and got the same result, the empty model was the simplest way to recreate the issue.

import pymc as pm
count_data = np.loadtxt("data\txtdata.csv")

alpha = 1.0 / count_data.mean()

lambda_1 = pm.Exponential("lambda_1", alpha)
lambda_2 = pm.Exponential("lambda_2", alpha)
tau = pm.DiscreteUniform("tau", lower=0, upper=n_count_data)

@pm.deterministic
def lambda_(tau=tau, lambda_1=lambda_1, lambda_2=lambda_2):
    out = np.zeros(n_count_data)
    out[:tau] = lambda_1
    out[tau:] = lambda_2
    return out

observation = pm.Poisson("obs", lambda_, value=count_data, observed=True)

model = pm.Model([observation, lambda_1, lambda_2, tau])

mcmc = pm.MCMC(model)
mcmc.sample(40000, 10000, 1)

dir(mcmc)

This gets me:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-127-0fc7ab4e9e82> in <module>()
----> 1 dir(mcmc)

TypeError: unorderable types: int() < str()
fonnesbeck commented 8 years ago

Ah, thanks. We don't recommend instantiating a Model directly; it is intended to be subclassed for particular model types. So, the following is what you need:

mcmc = pm.MCMC([observation, lambda_1, lambda_2, tau])

or if you want to avoid listing the parameters explicitly:

mcmc = pm.MCMC(vars())

I will add an informative message to this effect.

monkpit commented 8 years ago

Perfect, when I change my code to follow your example, dir works fine. Thanks.

fonnesbeck commented 8 years ago

This should all be dealt with in the above pull request. Feel free to test.