Skip to content

Commit 181c8dd

Browse files
Refactor tests for compatibility with logp dispatch and RandomVariables
1 parent b69fd7f commit 181c8dd

21 files changed

+316
-159
lines changed

pymc3/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ def fastfn(outs, mode=None, model=None):
14631463
return model.fastfn(outs, mode)
14641464

14651465

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

14871487

pymc3/tests/models.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def simple_model():
3030
mu = -2.1
3131
tau = 1.3
3232
with Model() as model:
33-
Normal("x", mu, tau=tau, shape=2, testval=aet.ones(2) * 0.1)
33+
Normal("x", mu, tau=tau, size=2, testval=np.ones(2) * 0.1)
3434

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

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

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

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

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

9999

@@ -106,7 +106,7 @@ def mv_simple():
106106
"x",
107107
aet.constant(mu),
108108
tau=aet.constant(tau),
109-
shape=3,
109+
size=3,
110110
testval=floatX_array([0.1, 1.0, 0.8]),
111111
)
112112
H = tau
@@ -123,7 +123,7 @@ def mv_simple_coarse():
123123
"x",
124124
aet.constant(mu),
125125
tau=aet.constant(tau),
126-
shape=3,
126+
size=3,
127127
testval=floatX_array([0.1, 1.0, 0.8]),
128128
)
129129
H = tau
@@ -140,7 +140,7 @@ def mv_simple_very_coarse():
140140
"x",
141141
aet.constant(mu),
142142
tau=aet.constant(tau),
143-
shape=3,
143+
size=3,
144144
testval=floatX_array([0.1, 1.0, 0.8]),
145145
)
146146
H = tau
@@ -153,7 +153,7 @@ def mv_simple_discrete():
153153
n = 5
154154
p = floatX_array([0.15, 0.85])
155155
with pm.Model() as model:
156-
pm.Multinomial("x", n, aet.constant(p), shape=d, testval=np.array([1, 4]))
156+
pm.Multinomial("x", n, aet.constant(p), size=d, testval=np.array([1, 4]))
157157
mu = n * p
158158
# covariance matrix
159159
C = np.zeros((d, d))
@@ -186,28 +186,28 @@ def mv_prior_simple():
186186
std_post = (K - np.dot(v.T, v)).diagonal() ** 0.5
187187

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

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

194194

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

200200

201201
def exponential_beta(n=2):
202202
with pm.Model() as model:
203-
pm.Beta("x", 3, 1, shape=n, transform=None)
204-
pm.Exponential("y", 1, shape=n, transform=None)
203+
pm.Beta("x", 3, 1, size=n, transform=None)
204+
pm.Exponential("y", 1, size=n, transform=None)
205205
return model.test_point, model, None
206206

207207

208208
def beta_bernoulli(n=2):
209209
with pm.Model() as model:
210-
pm.Beta("x", 3, 1, shape=n, transform=None)
210+
pm.Beta("x", 3, 1, size=n, transform=None)
211211
pm.Bernoulli("y", 0.5)
212212
return model.test_point, model, None
213213

pymc3/tests/test_coords.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import numpy as np
2+
import pytest
23

34
import pymc3 as pm
45

56

7+
@pytest.mark.xfail("Arviz incompatibilities")
68
def test_coords():
79
chains = 2
810
n_features = 3

pymc3/tests/test_data_container.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import pymc3 as pm
2222

23-
from pymc3.aesaraf import floatX
23+
from pymc3.distributions import logpt
2424
from pymc3.tests.helpers import SeededTest
2525

2626

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

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

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

152153
np.testing.assert_allclose(
153-
v.logp({"v": [5.0]}),
154+
logpt(v, 5.0).eval(),
154155
-0.91893853,
155156
rtol=1e-5,
156157
)
157158

158159
shared_var.set_value(10.0)
159160

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

183+
@pytest.mark.xfail(reason="Depends on ModelGraph")
182184
def test_model_to_graphviz_for_model_with_data_container(self):
183185
with pm.Model() as model:
184186
x = pm.Data("x", [1.0, 2.0, 3.0])

pymc3/tests/test_dist_math.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def logp(self, value):
132132
)
133133

134134

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

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

152153

154+
@pytest.mark.xfail(reason="MvNormal not implemented")
153155
class TestMvNormalLogp:
154156
def test_logp(self):
155157
np.random.seed(42)

pymc3/tests/test_distribution_defaults.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from pymc3.distributions import Categorical, Continuous, DiscreteUniform
1919
from pymc3.model import Model
2020

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

2224
class DistTest(Continuous):
2325
def __init__(self, a, b, *args, **kwargs):

0 commit comments

Comments
 (0)