Skip to content

Commit 249cd2f

Browse files
satrio-hwsatrio
and
satrio
authored
CLN: apply black formater (#4143)
Co-authored-by: satrio <[email protected]>
1 parent ffcb803 commit 249cd2f

10 files changed

+397
-487
lines changed

pymc3/tests/test_mixture.py

Lines changed: 190 additions & 219 deletions
Large diffs are not rendered by default.

pymc3/tests/test_ode.py

Lines changed: 67 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,12 @@ def augmented_system(Y, t, p):
4949
# I used CAS to get these derivatives
5050
y0_sensitivity = np.exp(-a * t)
5151
a_sensitivity = (
52-
-(np.exp(t * (a - 1)) - 1 + (a - 1) * (y0 * a - y0 - 1) * t)
53-
* np.exp(-a * t)
54-
/ (a - 1) ** 2
52+
-(np.exp(t * (a - 1)) - 1 + (a - 1) * (y0 * a - y0 - 1) * t) * np.exp(-a * t) / (a - 1) ** 2
5553
)
5654

5755
sensitivity = np.c_[y0_sensitivity, a_sensitivity]
5856

59-
integrated_solutions = odeint(
60-
func=augmented_system, y0=[y0, 1, 0], t=t.ravel(), args=(p,)
61-
)
57+
integrated_solutions = odeint(func=augmented_system, y0=[y0, 1, 0], t=t.ravel(), args=(p,))
6258
simulated_sensitivity = integrated_solutions[:, 1:]
6359

6460
np.testing.assert_allclose(sensitivity, simulated_sensitivity, rtol=1e-5)
@@ -78,14 +74,12 @@ def ode_func(y, t, p):
7874
y = 1.0 / (a - 1) * (np.exp(-t) - np.exp(-a * t))
7975

8076
# Instantiate ODE model
81-
ode_model = DifferentialEquation(
82-
func=ode_func, t0=0, times=t, n_states=1, n_theta=1
83-
)
77+
ode_model = DifferentialEquation(func=ode_func, t0=0, times=t, n_states=1, n_theta=1)
8478

8579
simulated_y, sens = ode_model._simulate([y0], [a])
86-
87-
assert simulated_y.shape == (len(t),1)
88-
assert sens.shape == (len(t), 1, 1+1)
80+
81+
assert simulated_y.shape == (len(t), 1)
82+
assert sens.shape == (len(t), 1, 1 + 1)
8983
np.testing.assert_allclose(y, simulated_y, rtol=1e-5)
9084

9185

@@ -102,9 +96,7 @@ def ode_func_1(y, t, p):
10296

10397
# Instantiate ODE model
10498
# Instantiate ODE model
105-
model1 = DifferentialEquation(
106-
func=ode_func_1, t0=0, times=self.t, n_states=1, n_theta=1
107-
)
99+
model1 = DifferentialEquation(func=ode_func_1, t0=0, times=self.t, n_states=1, n_theta=1)
108100

109101
# Sensitivity initial condition for this model should be 1 by 2
110102
model1_sens_ic = np.array([1, 0])
@@ -117,9 +109,7 @@ def ode_func_2(y, t, p):
117109
return p[0] * np.exp(-p[0] * t) - p[1] * y[0]
118110

119111
# Instantiate ODE model
120-
model2 = DifferentialEquation(
121-
func=ode_func_2, t0=0, times=self.t, n_states=1, n_theta=2
122-
)
112+
model2 = DifferentialEquation(func=ode_func_2, t0=0, times=self.t, n_states=1, n_theta=2)
123113

124114
model2_sens_ic = np.array([1, 0, 0])
125115

@@ -134,14 +124,9 @@ def ode_func_3(y, t, p):
134124
return [ds, di]
135125

136126
# Instantiate ODE model
137-
model3 = DifferentialEquation(
138-
func=ode_func_3, t0=0, times=self.t, n_states=2, n_theta=1
139-
)
127+
model3 = DifferentialEquation(func=ode_func_3, t0=0, times=self.t, n_states=2, n_theta=1)
140128

141-
model3_sens_ic = np.array([
142-
1, 0, 0,
143-
0, 1, 0
144-
])
129+
model3_sens_ic = np.array([1, 0, 0, 0, 1, 0])
145130

146131
np.testing.assert_array_equal(model3_sens_ic, model3._sens_ic)
147132

@@ -154,14 +139,9 @@ def ode_func_4(y, t, p):
154139
return [ds, di]
155140

156141
# Instantiate ODE model
157-
model4 = DifferentialEquation(
158-
func=ode_func_4, t0=0, times=self.t, n_states=2, n_theta=2
159-
)
142+
model4 = DifferentialEquation(func=ode_func_4, t0=0, times=self.t, n_states=2, n_theta=2)
160143

161-
model4_sens_ic = np.array([
162-
1, 0, 0, 0,
163-
0, 1, 0, 0
164-
])
144+
model4_sens_ic = np.array([1, 0, 0, 0, 0, 1, 0, 0])
165145

166146
np.testing.assert_array_equal(model4_sens_ic, model4._sens_ic)
167147

@@ -175,18 +155,12 @@ def ode_func_5(y, t, p):
175155
return [dx, ds, dz]
176156

177157
# Instantiate ODE model
178-
model5 = DifferentialEquation(
179-
func=ode_func_5, t0=0, times=self.t, n_states=3, n_theta=3
180-
)
158+
model5 = DifferentialEquation(func=ode_func_5, t0=0, times=self.t, n_states=3, n_theta=3)
181159

182160
# First three columns are derivatives with respect to ode parameters
183161
# Last three coluimns are derivatives with repsect to initial condition
184162
# So identity matrix should appear in last 3 columns
185-
model5_sens_ic = np.array([
186-
[1, 0, 0, 0, 0, 0],
187-
[0, 1, 0, 0, 0, 0],
188-
[0, 0, 1, 0, 0, 0]
189-
])
163+
model5_sens_ic = np.array([[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0]])
190164

191165
np.testing.assert_array_equal(np.ravel(model5_sens_ic), model5._sens_ic)
192166

@@ -203,25 +177,18 @@ def system_1(y, t, p):
203177
y0 = 0.0
204178
times = np.arange(0.5, 8, 0.5)
205179

206-
yobs = np.array([
207-
0.30, 0.56, 0.51, 0.55, 0.47, 0.42, 0.38, 0.30,
208-
0.26, 0.21, 0.22, 0.13, 0.13, 0.09, 0.09
209-
])[:,np.newaxis]
180+
yobs = np.array(
181+
[0.30, 0.56, 0.51, 0.55, 0.47, 0.42, 0.38, 0.30, 0.26, 0.21, 0.22, 0.13, 0.13, 0.09, 0.09]
182+
)[:, np.newaxis]
210183

211-
ode_model = DifferentialEquation(
212-
func=system_1, t0=0, times=times, n_theta=1, n_states=1
213-
)
184+
ode_model = DifferentialEquation(func=system_1, t0=0, times=times, n_theta=1, n_states=1)
214185

215186
integrated_solution, *_ = ode_model._simulate([y0], [alpha])
216187

217188
assert integrated_solution.shape == yobs.shape
218189

219190
# compare automatic and manual logp values
220-
manual_logp = norm.logpdf(
221-
x=np.ravel(yobs),
222-
loc=np.ravel(integrated_solution),
223-
scale=1
224-
).sum()
191+
manual_logp = norm.logpdf(x=np.ravel(yobs), loc=np.ravel(integrated_solution), scale=1).sum()
225192
with pm.Model() as model_1:
226193
forward = ode_model(theta=[alpha], y0=[y0])
227194
y = pm.Normal("y", mu=forward, sd=1, observed=yobs)
@@ -238,9 +205,7 @@ def system(y, t, p):
238205

239206
times = np.arange(0, 9)
240207

241-
ode_model = DifferentialEquation(
242-
func=system, t0=0, times=times, n_states=1, n_theta=1
243-
)
208+
ode_model = DifferentialEquation(func=system, t0=0, times=times, n_states=1, n_theta=1)
244209

245210
@pytest.mark.xfail(condition=(theano.config.floatX == "float32"), reason="Fails on float32")
246211
def test_too_many_params(self):
@@ -264,21 +229,15 @@ def test_too_few_y0(self):
264229

265230
def test_func_callable(self):
266231
with pytest.raises(ValueError):
267-
DifferentialEquation(
268-
func=1, t0=0, times=self.times, n_states=1, n_theta=1
269-
)
232+
DifferentialEquation(func=1, t0=0, times=self.times, n_states=1, n_theta=1)
270233

271234
def test_number_of_states(self):
272235
with pytest.raises(ValueError):
273-
DifferentialEquation(
274-
func=self.system, t0=0, times=self.times, n_states=0, n_theta=1
275-
)
236+
DifferentialEquation(func=self.system, t0=0, times=self.times, n_states=0, n_theta=1)
276237

277238
def test_number_of_params(self):
278239
with pytest.raises(ValueError):
279-
DifferentialEquation(
280-
func=self.system, t0=0, times=self.times, n_states=1, n_theta=0
281-
)
240+
DifferentialEquation(func=self.system, t0=0, times=self.times, n_states=1, n_theta=0)
282241

283242

284243
class TestDiffEqModel:
@@ -294,7 +253,9 @@ def ode_func(y, t, p):
294253
# Instantiate two Ops
295254
op_1 = DifferentialEquation(func=ode_func, t0=0, times=t, n_states=1, n_theta=1)
296255
op_2 = DifferentialEquation(func=ode_func, t0=0, times=t, n_states=1, n_theta=1)
297-
op_other = DifferentialEquation(func=ode_func, t0=0, times=np.linspace(0, 2, 16), n_states=1, n_theta=1)
256+
op_other = DifferentialEquation(
257+
func=ode_func, t0=0, times=np.linspace(0, 2, 16), n_states=1, n_theta=1
258+
)
298259

299260
assert op_1 == op_2
300261
assert op_1 != op_other
@@ -310,14 +271,11 @@ def system(y, t, p):
310271
[0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5]
311272
)
312273

313-
yobs = np.array([
314-
0.31, 0.57, 0.51, 0.55, 0.47, 0.42, 0.38, 0.3,
315-
0.26, 0.22, 0.22, 0.14, 0.14, 0.09, 0.1
316-
])[:,np.newaxis]
274+
yobs = np.array(
275+
[0.31, 0.57, 0.51, 0.55, 0.47, 0.42, 0.38, 0.3, 0.26, 0.22, 0.22, 0.14, 0.14, 0.09, 0.1]
276+
)[:, np.newaxis]
317277

318-
ode_model = DifferentialEquation(
319-
func=system, t0=0, times=times, n_states=1, n_theta=1
320-
)
278+
ode_model = DifferentialEquation(func=system, t0=0, times=times, n_states=1, n_theta=1)
321279

322280
with pm.Model() as model:
323281
alpha = pm.HalfCauchy("alpha", 1)
@@ -341,14 +299,11 @@ def system(y, t, p):
341299
[0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5]
342300
)
343301

344-
yobs = np.array([
345-
0.31, 0.57, 0.51, 0.55, 0.47, 0.42, 0.38, 0.3,
346-
0.26, 0.22, 0.22, 0.14, 0.14, 0.09, 0.1
347-
])[:,np.newaxis]
302+
yobs = np.array(
303+
[0.31, 0.57, 0.51, 0.55, 0.47, 0.42, 0.38, 0.3, 0.26, 0.22, 0.22, 0.14, 0.14, 0.09, 0.1]
304+
)[:, np.newaxis]
348305

349-
ode_model = DifferentialEquation(
350-
func=system, t0=0, times=times, n_states=1, n_theta=2
351-
)
306+
ode_model = DifferentialEquation(func=system, t0=0, times=times, n_states=1, n_theta=2)
352307

353308
with pm.Model() as model:
354309
alpha = pm.HalfCauchy("alpha", 1)
@@ -375,24 +330,24 @@ def system(y, t, p):
375330

376331
times = np.array([0.0, 0.8, 1.6, 2.4, 3.2, 4.0, 4.8, 5.6, 6.4, 7.2, 8.0])
377332

378-
yobs = np.array([
379-
[1.02, 0.02],
380-
[0.86, 0.12],
381-
[0.43, 0.37],
382-
[0.14, 0.42],
383-
[0.05, 0.43],
384-
[0.03, 0.14],
385-
[0.02, 0.08],
386-
[0.02, 0.04],
387-
[0.02, 0.01],
388-
[0.02, 0.01],
389-
[0.02, 0.01],
390-
])
391-
392-
ode_model = DifferentialEquation(
393-
func=system, t0=0, times=times, n_states=2, n_theta=1
333+
yobs = np.array(
334+
[
335+
[1.02, 0.02],
336+
[0.86, 0.12],
337+
[0.43, 0.37],
338+
[0.14, 0.42],
339+
[0.05, 0.43],
340+
[0.03, 0.14],
341+
[0.02, 0.08],
342+
[0.02, 0.04],
343+
[0.02, 0.01],
344+
[0.02, 0.01],
345+
[0.02, 0.01],
346+
]
394347
)
395348

349+
ode_model = DifferentialEquation(func=system, t0=0, times=times, n_states=2, n_theta=1)
350+
396351
with pm.Model() as model:
397352
R = pm.Lognormal("R", 1, 5)
398353
sigma = pm.HalfCauchy("sigma", 1, shape=2)
@@ -413,25 +368,25 @@ def system(y, t, p):
413368
return [ds, di]
414369

415370
times = np.array([0.0, 0.8, 1.6, 2.4, 3.2, 4.0, 4.8, 5.6, 6.4, 7.2, 8.0])
416-
417-
yobs = np.array([
418-
[1.02, 0.02],
419-
[0.86, 0.12],
420-
[0.43, 0.37],
421-
[0.14, 0.42],
422-
[0.05, 0.43],
423-
[0.03, 0.14],
424-
[0.02, 0.08],
425-
[0.02, 0.04],
426-
[0.02, 0.01],
427-
[0.02, 0.01],
428-
[0.02, 0.01],
429-
])
430-
431-
ode_model = DifferentialEquation(
432-
func=system, t0=0, times=times, n_states=2, n_theta=2
371+
372+
yobs = np.array(
373+
[
374+
[1.02, 0.02],
375+
[0.86, 0.12],
376+
[0.43, 0.37],
377+
[0.14, 0.42],
378+
[0.05, 0.43],
379+
[0.03, 0.14],
380+
[0.02, 0.08],
381+
[0.02, 0.04],
382+
[0.02, 0.01],
383+
[0.02, 0.01],
384+
[0.02, 0.01],
385+
]
433386
)
434387

388+
ode_model = DifferentialEquation(func=system, t0=0, times=times, n_states=2, n_theta=2)
389+
435390
with pm.Model() as model:
436391
beta = pm.HalfCauchy("beta", 1)
437392
gamma = pm.HalfCauchy("gamma", 1)

pymc3/tests/test_quadpotential.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ def test_random_dense():
126126
quadpotential.QuadPotentialFullInv(inv),
127127
]
128128
if quadpotential.chol_available:
129-
pot = quadpotential.QuadPotential_Sparse(
130-
scipy.sparse.csc_matrix(cov)
131-
)
129+
pot = quadpotential.QuadPotential_Sparse(scipy.sparse.csc_matrix(cov))
132130
pots.append(pot)
133131
for pot in pots:
134132
cov_ = np.cov(np.array([pot.random() for _ in range(1000)]).T)
@@ -219,9 +217,7 @@ def test_full_adapt_sample_p(seed=4566):
219217
def test_full_adapt_update_window(seed=1123):
220218
np.random.seed(seed)
221219
init_cov = np.array([[1.0, 0.02], [0.02, 0.8]])
222-
pot = quadpotential.QuadPotentialFullAdapt(
223-
2, np.zeros(2), init_cov, 1, update_window=50
224-
)
220+
pot = quadpotential.QuadPotentialFullAdapt(2, np.zeros(2), init_cov, 1, update_window=50)
225221
assert np.allclose(pot._cov, init_cov)
226222
for i in range(49):
227223
pot.update(np.random.randn(2), None, True)
@@ -276,16 +272,12 @@ def test_full_adapt_sampling(seed=289586):
276272
with pymc3.Model() as model:
277273
pymc3.MvNormal("a", mu=np.zeros(len(L)), chol=L, shape=len(L))
278274

279-
pot = quadpotential.QuadPotentialFullAdapt(
280-
model.ndim, np.zeros(model.ndim)
281-
)
275+
pot = quadpotential.QuadPotentialFullAdapt(model.ndim, np.zeros(model.ndim))
282276
step = pymc3.NUTS(model=model, potential=pot)
283-
pymc3.sample(
284-
draws=10, tune=1000, random_seed=seed, step=step, cores=1, chains=1
285-
)
277+
pymc3.sample(draws=10, tune=1000, random_seed=seed, step=step, cores=1, chains=1)
278+
286279

287-
288280
def test_issue_3965():
289281
with pymc3.Model():
290-
pymc3.Normal('n')
291-
pymc3.sample(100, tune=300, chains=1, init='advi+adapt_diag_grad')
282+
pymc3.Normal("n")
283+
pymc3.sample(100, tune=300, chains=1, init="advi+adapt_diag_grad")

0 commit comments

Comments
 (0)