Skip to content

Commit

Permalink
Refactor tests for compatibility with logp dispatch and RandomVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonwillard committed Mar 1, 2021
1 parent b69fd7f commit 181c8dd
Show file tree
Hide file tree
Showing 21 changed files with 316 additions and 159 deletions.
4 changes: 2 additions & 2 deletions pymc3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ def fastfn(outs, mode=None, model=None):
return model.fastfn(outs, mode)


def Point(*args, **kwargs):
def Point(*args, filter_model_vars=True, **kwargs):
"""Build a point. Uses same args as dict() does.
Filters out variables not in the model. All keys are strings.
Expand All @@ -1481,7 +1481,7 @@ def Point(*args, **kwargs):
return {
get_var_name(k): np.array(v)
for k, v in d.items()
if get_var_name(k) in map(get_var_name, model.vars)
if not filter_model_vars or (get_var_name(k) in map(get_var_name, model.vars))
}


Expand Down
28 changes: 14 additions & 14 deletions pymc3/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def simple_model():
mu = -2.1
tau = 1.3
with Model() as model:
Normal("x", mu, tau=tau, shape=2, testval=aet.ones(2) * 0.1)
Normal("x", mu, tau=tau, size=2, testval=np.ones(2) * 0.1)

return model.test_point, model, (mu, tau ** -0.5)

Expand All @@ -39,7 +39,7 @@ def simple_categorical():
p = floatX_array([0.1, 0.2, 0.3, 0.4])
v = floatX_array([0.0, 1.0, 2.0, 3.0])
with Model() as model:
Categorical("x", p, shape=3, testval=[1, 2, 3])
Categorical("x", p, size=3, testval=[1, 2, 3])

mu = np.dot(p, v)
var = np.dot(p, (v - mu) ** 2)
Expand All @@ -50,7 +50,7 @@ def multidimensional_model():
mu = -2.1
tau = 1.3
with Model() as model:
Normal("x", mu, tau=tau, shape=(3, 2), testval=0.1 * aet.ones((3, 2)))
Normal("x", mu, tau=tau, size=(3, 2), testval=0.1 * np.ones((3, 2)))

return model.test_point, model, (mu, tau ** -0.5)

Expand Down Expand Up @@ -93,7 +93,7 @@ def simple_2model_continuous():
with Model() as model:
x = pm.Normal("x", mu, tau=tau, testval=0.1)
pm.Deterministic("logx", aet.log(x))
pm.Beta("y", alpha=1, beta=1, shape=2)
pm.Beta("y", alpha=1, beta=1, size=2)
return model.test_point, model


Expand All @@ -106,7 +106,7 @@ def mv_simple():
"x",
aet.constant(mu),
tau=aet.constant(tau),
shape=3,
size=3,
testval=floatX_array([0.1, 1.0, 0.8]),
)
H = tau
Expand All @@ -123,7 +123,7 @@ def mv_simple_coarse():
"x",
aet.constant(mu),
tau=aet.constant(tau),
shape=3,
size=3,
testval=floatX_array([0.1, 1.0, 0.8]),
)
H = tau
Expand All @@ -140,7 +140,7 @@ def mv_simple_very_coarse():
"x",
aet.constant(mu),
tau=aet.constant(tau),
shape=3,
size=3,
testval=floatX_array([0.1, 1.0, 0.8]),
)
H = tau
Expand All @@ -153,7 +153,7 @@ def mv_simple_discrete():
n = 5
p = floatX_array([0.15, 0.85])
with pm.Model() as model:
pm.Multinomial("x", n, aet.constant(p), shape=d, testval=np.array([1, 4]))
pm.Multinomial("x", n, aet.constant(p), size=d, testval=np.array([1, 4]))
mu = n * p
# covariance matrix
C = np.zeros((d, d))
Expand Down Expand Up @@ -186,28 +186,28 @@ def mv_prior_simple():
std_post = (K - np.dot(v.T, v)).diagonal() ** 0.5

with pm.Model() as model:
x = pm.Flat("x", shape=n)
x_obs = pm.MvNormal("x_obs", observed=obs, mu=x, cov=noise * np.eye(n), shape=n)
x = pm.Flat("x", size=n)
x_obs = pm.MvNormal("x_obs", observed=obs, mu=x, cov=noise * np.eye(n), size=n)

return model.test_point, model, (K, L, mu_post, std_post, noise)


def non_normal(n=2):
with pm.Model() as model:
pm.Beta("x", 3, 3, shape=n, transform=None)
pm.Beta("x", 3, 3, size=n, transform=None)
return model.test_point, model, (np.tile([0.5], n), None)


def exponential_beta(n=2):
with pm.Model() as model:
pm.Beta("x", 3, 1, shape=n, transform=None)
pm.Exponential("y", 1, shape=n, transform=None)
pm.Beta("x", 3, 1, size=n, transform=None)
pm.Exponential("y", 1, size=n, transform=None)
return model.test_point, model, None


def beta_bernoulli(n=2):
with pm.Model() as model:
pm.Beta("x", 3, 1, shape=n, transform=None)
pm.Beta("x", 3, 1, size=n, transform=None)
pm.Bernoulli("y", 0.5)
return model.test_point, model, None

Expand Down
2 changes: 2 additions & 0 deletions pymc3/tests/test_coords.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import numpy as np
import pytest

import pymc3 as pm


@pytest.mark.xfail("Arviz incompatibilities")
def test_coords():
chains = 2
n_features = 3
Expand Down
10 changes: 6 additions & 4 deletions pymc3/tests/test_data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import pymc3 as pm

from pymc3.aesaraf import floatX
from pymc3.distributions import logpt
from pymc3.tests.helpers import SeededTest


Expand All @@ -32,6 +32,7 @@ def test_deterministic(self):
pm.Normal("y", 0, 1, observed=X)
model.logp(model.test_point)

@pytest.mark.xfail(reason="Competence hasn't been updated")
def test_sample(self):
x = np.random.normal(size=100)
y = x + np.random.normal(scale=1e-2, size=100)
Expand Down Expand Up @@ -105,7 +106,7 @@ def test_shared_data_as_index(self):
with pm.Model() as model:
index = pm.Data("index", [2, 0, 1, 0, 2])
y = pm.Data("y", [1.0, 2.0, 3.0, 2.0, 1.0])
alpha = pm.Normal("alpha", 0, 1.5, shape=3)
alpha = pm.Normal("alpha", 0, 1.5, size=3)
pm.Normal("obs", alpha[index], np.sqrt(1e-2), observed=y)

prior_trace = pm.sample_prior_predictive(1000, var_names=["alpha"])
Expand Down Expand Up @@ -150,15 +151,15 @@ def test_shared_scalar_as_rv_input(self):
v = pm.Normal("v", mu=shared_var, shape=1)

np.testing.assert_allclose(
v.logp({"v": [5.0]}),
logpt(v, 5.0).eval(),
-0.91893853,
rtol=1e-5,
)

shared_var.set_value(10.0)

np.testing.assert_allclose(
v.logp({"v": [10.0]}),
logpt(v, 10.0).eval(),
-0.91893853,
rtol=1e-5,
)
Expand All @@ -179,6 +180,7 @@ def test_set_data_to_non_data_container_variables(self):
pm.set_data({"beta": [1.1, 2.2, 3.3]}, model=model)
error.match("defined as `pymc3.Data` inside the model")

@pytest.mark.xfail(reason="Depends on ModelGraph")
def test_model_to_graphviz_for_model_with_data_container(self):
with pm.Model() as model:
x = pm.Data("x", [1.0, 2.0, 3.0])
Expand Down
2 changes: 2 additions & 0 deletions pymc3/tests/test_dist_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def logp(self, value):
)


@pytest.mark.xfail(reason="This test relies on the deprecated Distribution interface")
def test_multinomial_bound():

x = np.array([1, 5])
Expand All @@ -150,6 +151,7 @@ def test_multinomial_bound():
)


@pytest.mark.xfail(reason="MvNormal not implemented")
class TestMvNormalLogp:
def test_logp(self):
np.random.seed(42)
Expand Down
2 changes: 2 additions & 0 deletions pymc3/tests/test_distribution_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from pymc3.distributions import Categorical, Continuous, DiscreteUniform
from pymc3.model import Model

pytestmark = pytest.mark.xfail(reason="This test relies on the deprecated Distribution interface")


class DistTest(Continuous):
def __init__(self, a, b, *args, **kwargs):
Expand Down
Loading

0 comments on commit 181c8dd

Please sign in to comment.