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

Add pickle test and revert theano var names (hopefully temporarily) #4117

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pymc3/distributions/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ def _str_repr(self, name=None, dist=None, formatting='plain'):
return "{var_name} ~ {distr_name}({params})".format(var_name=name,
distr_name=dist._distr_name_for_repr(), params=param_string)

def __str__(self, **kwargs):
return self._str_repr(formatting="plain", **kwargs)
# TODO: Enable this one we figure out pickle
# def __str__(self, **kwargs):
# return self._str_repr(formatting="plain", **kwargs)

def _repr_latex_(self, **kwargs):
"""Magic method name for IPython to use for LaTeX formatting."""
Expand Down
20 changes: 12 additions & 8 deletions pymc3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ def _str_repr(self, name=None, dist=None, formatting="plain"):
def _repr_latex_(self, **kwargs):
return self._str_repr(formatting="latex", **kwargs)

def __str__(self, **kwargs):
return self._str_repr(formatting="plain", **kwargs)
# TODO: Enable this one we figure out pickle
# def __str__(self, **kwargs):
# return self._str_repr(formatting="plain", **kwargs)

__latex__ = _repr_latex_

Expand Down Expand Up @@ -1423,8 +1424,9 @@ def _str_repr(self, formatting="plain", **kwargs):
for n, d in zip(names, distrs)]
return "\n".join(rv_reprs)

def __str__(self, **kwargs):
return self._str_repr(formatting="plain", **kwargs)
# TODO: Enable this one we figure out pickle
# def __str__(self, **kwargs):
# return self._str_repr(formatting="plain", **kwargs)

def _repr_latex_(self, **kwargs):
return self._str_repr(formatting="latex", **kwargs)
Expand Down Expand Up @@ -1934,10 +1936,12 @@ def Deterministic(name, var, model=None, dims=None):

# simply assigning var.__str__ is not enough, since str() will default to the class-
# defined __str__ anyway; see https://stackoverflow.com/a/5918210/1692028
old_type = type(var)
new_type = type(old_type.__name__ + '_pymc3_Deterministic', (old_type,),
{'__str__': functools.partial(_repr_deterministic_rv, var, formatting='plain')})
var.__class__ = new_type

# TODO: Fix enable this once we figure out pickle
# old_type = type(var)
# new_type = type(old_type.__name__ + '_pymc3_Deterministic', (old_type,),
# {'__str__': functools.partial(_repr_deterministic_rv, var, formatting='plain')})
# var.__class__ = new_type

return var

Expand Down
2 changes: 2 additions & 0 deletions pymc3/tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,8 @@ def test_bound():
BoundPoissonPositionalArgs = Bound(Poisson, upper=6)("x", 2.0)


@pytest.mark.xfail(reason=("Currently failing due to pm.Deterministic issue. "
"See https://github.com/pymc-devs/pymc3/pull/4117 "))
class TestStrAndLatexRepr:
def setup_class(self):
# True parameter values
Expand Down
25 changes: 25 additions & 0 deletions pymc3/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import theano
import theano.tensor as tt
import numpy as np
import pickle
import pandas as pd
import numpy.testing as npt
import unittest
Expand Down Expand Up @@ -421,3 +422,27 @@ def test_tempered_logp_dlogp():

npt.assert_allclose(func_nograd(x), func(x)[0])
npt.assert_allclose(func_temp_nograd(x), func_temp(x)[0])


def test_model_pickle(tmpdir):
"""Tests that PyMC3 models are pickleable"""
with pm.Model() as model:
x = pm.Normal('x')
pm.Normal('y', observed=1)

file_path = tmpdir.join("model.p")
with open(file_path, 'wb') as buff:
pickle.dump(model, buff)


def test_model_pickle_deterministic(tmpdir):
"""Tests that PyMC3 models with deterministics are pickleable"""
with pm.Model() as model:
x = pm.Normal('x')
z = pm.Normal("z")
pm.Deterministic("w", x/z)
pm.Normal('y', observed=1)

file_path = tmpdir.join("model.p")
with open(file_path, 'wb') as buff:
pickle.dump(model, buff)
11 changes: 6 additions & 5 deletions pymc3/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import arviz
from numpy import asscalar, ndarray

from theano.tensor import TensorVariable
# TODO: Reimplement after pickle fix
# from theano.tensor import TensorVariable


LATEX_ESCAPE_RE = re.compile(r"(%|_|\$|#|&)", re.MULTILINE)
Expand Down Expand Up @@ -161,10 +162,10 @@ def get_var_name(var):
string representations to our pymc3.PyMC3Variables, yet we want to use the
plain name as e.g. keys in dicts.
"""
if isinstance(var, TensorVariable):
return super(TensorVariable, var).__str__()
else:
return str(var)
# if isinstance(var, TensorVariable):
# return super(TensorVariable, var).__str__()
# else:
return str(var)


def update_start_vals(a, b, model):
Expand Down