Skip to content

Commit 63fc4b3

Browse files
committed
TST: Use on*_(stop,continue) and logging tools in tests
1 parent eabc46f commit 63fc4b3

File tree

11 files changed

+122
-111
lines changed

11 files changed

+122
-111
lines changed

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include common.py setup_build.py
22
recursive-include scikits *.pyx *.pxd *.pyf *.f
33

44
include CONTRIBUTING.md README.md LICENSE.txt
5-
include tox.ini
5+
include tox.ini pytest.ini
66
include pyproject.toml
77

88
prune ci_support

pytest.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
log_cli = 1
3+
log_cli_level = INFO
4+
log_cli_format = %(asctime)s %(levelname)s %(message)s
5+
log_cli_date_format = %H:%M:%S

scikits/odes/tests/test_dae.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
from numpy.testing import TestCase, run_module_suite
1313
from scipy.integrate import ode as Iode
14-
from scikits.odes import ode,dae
15-
from scikits.odes.sundials.common_defs import DTYPE
14+
from .. import ode, dae
15+
from ..sundials.common_defs import DTYPE
1616

1717
class TestDae(TestCase):
1818
"""

scikits/odes/tests/test_dop.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from numpy.testing import (
1313
assert_, TestCase, run_module_suite, assert_array_almost_equal,
1414
assert_raises, assert_allclose, assert_array_equal, assert_equal)
15-
from scikits.odes import ode
16-
from scikits.odes.dopri5 import StatusEnumDOP
15+
from .. import ode
16+
from ..dopri5 import StatusEnumDOP
1717

1818

1919
class SimpleOscillator():

scikits/odes/tests/test_get_info.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from __future__ import print_function
22
import numpy as np
33
import unittest
4-
from scikits.odes import ode
4+
from .. import ode
5+
from ..sundials import log_error_handler
6+
7+
COMMON_ARGS = {
8+
"old_api": False,
9+
"err_handler": log_error_handler
10+
}
511

612

713
xs = np.linspace(1, 10, 10)
@@ -20,7 +26,7 @@ def rhs(x, y, ydot):
2026

2127
class GetInfoTest(unittest.TestCase):
2228
def setUp(self):
23-
self.ode = ode('cvode', rhs, old_api=False)
29+
self.ode = ode('cvode', rhs, **COMMON_ARGS)
2430
self.solution = self.ode.solve(xs, np.array([1]))
2531

2632
def test_we_integrated_correctly(self):
@@ -47,7 +53,7 @@ def test_ode_exposes_num_rhs_evals(self):
4753

4854
class GetInfoTestSpils(unittest.TestCase):
4955
def setUp(self):
50-
self.ode = ode('cvode', rhs, linsolver="spgmr", old_api=False)
56+
self.ode = ode('cvode', rhs, linsolver="spgmr", **COMMON_ARGS)
5157
self.solution = self.ode.solve(xs, np.array([1]))
5258

5359
def test_ode_exposes_num_njtimes_evals(self):

scikits/odes/tests/test_odeint.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
assert_, TestCase, run_module_suite, assert_array_almost_equal,
2020
assert_raises, assert_allclose, assert_array_equal, assert_equal)
2121

22-
from scikits.odes.odeint import odeint
23-
from scikits.odes.sundials.common_defs import DTYPE
22+
from ..odeint import odeint
23+
from ..sundials import log_error_handler
24+
from ..sundials.common_defs import DTYPE
2425

2526
TEST_LAPACK = DTYPE == np.double
2627

scikits/odes/tests/test_on_funcs.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212

1313
from numpy.testing import TestCase, run_module_suite
1414

15-
from scikits.odes import ode
16-
from scikits.odes.sundials.cvode import StatusEnum
17-
from scikits.odes.sundials.common_defs import DTYPE
15+
from .. import ode
16+
from ..sundials.cvode import StatusEnum
17+
from ..sundials.common_defs import DTYPE
18+
from ..sundials import log_error_handler, ontstop_stop, onroot_stop
19+
20+
COMMON_ARGS = {
21+
"old_api": False,
22+
"err_handler": log_error_handler
23+
}
1824

1925
#data
2026
g = 9.81 # gravitational constant
@@ -63,12 +69,6 @@ def onroot_va(t, y, solver):
6369

6470
return 0
6571

66-
def onroot_vb(t, y, solver):
67-
"""
68-
onroot function to stop solver when root is found
69-
"""
70-
return 1
71-
7272
def onroot_vc(t, y, solver):
7373
"""
7474
onroot function to reset the solver back at the start, but keep the current
@@ -103,12 +103,6 @@ def ontstop_va(t, y, solver):
103103

104104
return 0
105105

106-
def ontstop_vb(t, y, solver):
107-
"""
108-
ontstop function to stop solver when tstop is reached
109-
"""
110-
return 1
111-
112106
def ontstop_vc(t, y, solver):
113107
"""
114108
ontstop function to reset the solver back at the start, but keep the current
@@ -132,7 +126,7 @@ def test_cvode_rootfn_noroot(self):
132126
#test calling sequence. End is reached before root is found
133127
tspan = np.arange(0, t_end1 + 1, 1.0, DTYPE)
134128
solver = ode('cvode', rhs_fn, nr_rootfns=1, rootfn=root_fn,
135-
old_api=False)
129+
**COMMON_ARGS)
136130
soln = solver.solve(tspan, y0)
137131
assert soln.flag==StatusEnum.SUCCESS, "ERROR: Error occurred"
138132
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -143,7 +137,7 @@ def test_cvode_rootfn(self):
143137
#test root finding and stopping: End is reached at a root
144138
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
145139
solver = ode('cvode', rhs_fn, nr_rootfns=1, rootfn=root_fn,
146-
old_api=False)
140+
**COMMON_ARGS)
147141
soln = solver.solve(tspan, y0)
148142
assert soln.flag==StatusEnum.ROOT_RETURN, "ERROR: Root not found!"
149143
assert allclose([soln.roots.t[0], soln.roots.y[0,0], soln.roots.y[0,1]],
@@ -155,7 +149,7 @@ def test_cvode_rootfnacc(self):
155149
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
156150
solver = ode('cvode', rhs_fn, nr_rootfns=1, rootfn=root_fn,
157151
onroot=onroot_va,
158-
old_api=False)
152+
**COMMON_ARGS)
159153
soln = solver.solve(tspan, y0)
160154
assert soln.flag==StatusEnum.SUCCESS, "ERROR: Error occurred"
161155
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -170,8 +164,8 @@ def test_cvode_rootfn_stop(self):
170164
#test root finding and stopping: End is reached at a root with a function
171165
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
172166
solver = ode('cvode', rhs_fn, nr_rootfns=1, rootfn=root_fn,
173-
onroot=onroot_vb,
174-
old_api=False)
167+
onroot=onroot_stop,
168+
**COMMON_ARGS)
175169
soln = solver.solve(tspan, y0)
176170
assert soln.flag==StatusEnum.ROOT_RETURN, "ERROR: Root not found!"
177171
assert allclose([soln.roots.t[-1], soln.roots.y[-1,0], soln.roots.y[-1,1]],
@@ -183,7 +177,7 @@ def test_cvode_rootfn_test(self):
183177
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
184178
solver = ode('cvode', rhs_fn, nr_rootfns=1, rootfn=root_fn,
185179
onroot=onroot_vc,
186-
old_api=False)
180+
**COMMON_ARGS)
187181
soln = solver.solve(tspan, y0)
188182
assert soln.flag==StatusEnum.ROOT_RETURN, "ERROR: Not sufficient root found"
189183
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -199,7 +193,7 @@ def test_cvode_rootfn_two(self):
199193
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
200194
solver = ode('cvode', rhs_fn, nr_rootfns=2, rootfn=root_fn2,
201195
onroot=onroot_vc,
202-
old_api=False)
196+
**COMMON_ARGS)
203197
soln = solver.solve(tspan, y0)
204198
assert soln.flag==StatusEnum.ROOT_RETURN, "ERROR: Not sufficient root found"
205199
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -215,7 +209,7 @@ def test_cvode_rootfn_end(self):
215209
tspan = np.arange(0, 30 + 1, 1.0, DTYPE)
216210
solver = ode('cvode', rhs_fn, nr_rootfns=1, rootfn=root_fn3,
217211
onroot=onroot_vc,
218-
old_api=False)
212+
**COMMON_ARGS)
219213
soln = solver.solve(tspan, y0)
220214
assert soln.flag==StatusEnum.ROOT_RETURN, "ERROR: Not sufficient root found"
221215
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -232,7 +226,7 @@ def test_cvode_tstopfn_notstop(self):
232226
n = 0
233227
tspan = np.arange(0, t_end1 + 1, 1.0, DTYPE)
234228
solver = ode('cvode', rhs_fn, tstop=T1+1, ontstop=ontstop_va,
235-
old_api=False)
229+
**COMMON_ARGS)
236230

237231
soln = solver.solve(tspan, y0)
238232
assert soln.flag==StatusEnum.SUCCESS, "ERROR: Error occurred"
@@ -246,7 +240,7 @@ def test_cvode_tstopfn(self):
246240
n = 0
247241
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
248242
solver = ode('cvode', rhs_fn, tstop=T1,
249-
old_api=False)
243+
**COMMON_ARGS)
250244
soln = solver.solve(tspan, y0)
251245
assert soln.flag==StatusEnum.TSTOP_RETURN, "ERROR: Tstop not found!"
252246
assert allclose([soln.tstop.t[0], soln.tstop.y[0,0], soln.tstop.y[0,1]],
@@ -262,7 +256,7 @@ def test_cvode_tstopfnacc(self):
262256
n = 0
263257
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
264258
solver = ode('cvode', rhs_fn, tstop=T1, ontstop=ontstop_va,
265-
old_api=False)
259+
**COMMON_ARGS)
266260
soln = solver.solve(tspan, y0)
267261
assert soln.flag==StatusEnum.SUCCESS, "ERROR: Error occurred"
268262
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -278,8 +272,8 @@ def test_cvode_tstopfn_stop(self):
278272
global n
279273
n = 0
280274
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
281-
solver = ode('cvode', rhs_fn, tstop=T1, ontstop=ontstop_vb,
282-
old_api=False)
275+
solver = ode('cvode', rhs_fn, tstop=T1, ontstop=ontstop_stop,
276+
**COMMON_ARGS)
283277

284278
soln = solver.solve(tspan, y0)
285279
assert soln.flag==StatusEnum.TSTOP_RETURN, "ERROR: Error occurred"
@@ -299,7 +293,7 @@ def test_cvode_tstopfn_test(self):
299293
n = 0
300294
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
301295
solver = ode('cvode', rhs_fn, tstop=T1, ontstop=ontstop_vc,
302-
old_api=False)
296+
**COMMON_ARGS)
303297

304298
soln = solver.solve(tspan, y0)
305299
assert soln.flag==StatusEnum.TSTOP_RETURN, "ERROR: Error occurred"

scikits/odes/tests/test_on_funcs_ida.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212

1313
from numpy.testing import TestCase, run_module_suite
1414

15-
from scikits.odes import dae
16-
from scikits.odes.sundials.ida import StatusEnumIDA
17-
from scikits.odes.sundials.common_defs import DTYPE
15+
from .. import dae
16+
from ..sundials import log_error_handler, ontstop_stop, onroot_stop
17+
from ..sundials.ida import StatusEnumIDA
18+
from ..sundials.common_defs import DTYPE
19+
20+
COMMON_ARGS = {
21+
"old_api": False,
22+
"err_handler": log_error_handler
23+
}
1824

1925
#data
2026
g = 9.81 # gravitational constant
@@ -65,12 +71,6 @@ def onroot_va(t, y, ydot, solver):
6571

6672
return 0
6773

68-
def onroot_vb(t, y, ydot, solver):
69-
"""
70-
onroot function to stop solver when root is found
71-
"""
72-
return 1
73-
7474
def onroot_vc(t, y, ydot, solver):
7575
"""
7676
onroot function to reset the solver back at the start, but keep the current
@@ -105,12 +105,6 @@ def ontstop_va(t, y, ydot, solver):
105105

106106
return 0
107107

108-
def ontstop_vb(t, y, ydot, solver):
109-
"""
110-
ontstop function to stop solver when tstop is reached
111-
"""
112-
return 1
113-
114108
def ontstop_vc(t, y, ydot, solver):
115109
"""
116110
ontstop function to reset the solver back at the start, but keep the current
@@ -134,7 +128,7 @@ def test_ida_rootfn_noroot(self):
134128
#test calling sequence. End is reached before root is found
135129
tspan = np.arange(0, t_end1 + 1, 1.0, DTYPE)
136130
solver = dae('ida', rhs_fn, nr_rootfns=1, rootfn=root_fn,
137-
old_api=False)
131+
**COMMON_ARGS)
138132
soln = solver.solve(tspan, y0, yp0)
139133
assert soln.flag==StatusEnumIDA.SUCCESS, "ERROR: Error occurred"
140134
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -145,7 +139,7 @@ def test_ida_rootfn(self):
145139
#test root finding and stopping: End is reached at a root
146140
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
147141
solver = dae('ida', rhs_fn, nr_rootfns=1, rootfn=root_fn,
148-
old_api=False)
142+
**COMMON_ARGS)
149143
soln = solver.solve(tspan, y0, yp0)
150144
assert soln.flag==StatusEnumIDA.ROOT_RETURN, "ERROR: Root not found!"
151145
assert allclose([soln.roots.t[0], soln.roots.y[0,0], soln.roots.y[0,1]],
@@ -157,7 +151,7 @@ def test_ida_rootfnacc(self):
157151
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
158152
solver = dae('ida', rhs_fn, nr_rootfns=1, rootfn=root_fn,
159153
onroot=onroot_va,
160-
old_api=False)
154+
**COMMON_ARGS)
161155
soln = solver.solve(tspan, y0, yp0)
162156
assert soln.flag==StatusEnumIDA.SUCCESS, "ERROR: Error occurred"
163157
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -172,8 +166,8 @@ def test_ida_rootfn_stop(self):
172166
#test root finding and stopping: End is reached at a root with a function
173167
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
174168
solver = dae('ida', rhs_fn, nr_rootfns=1, rootfn=root_fn,
175-
onroot=onroot_vb,
176-
old_api=False)
169+
onroot=onroot_stop,
170+
**COMMON_ARGS)
177171
soln = solver.solve(tspan, y0, yp0)
178172
assert soln.flag==StatusEnumIDA.ROOT_RETURN, "ERROR: Root not found!"
179173
assert allclose([soln.roots.t[-1], soln.roots.y[-1,0], soln.roots.y[-1,1]],
@@ -185,7 +179,7 @@ def test_ida_rootfn_test(self):
185179
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
186180
solver = dae('ida', rhs_fn, nr_rootfns=1, rootfn=root_fn,
187181
onroot=onroot_vc,
188-
old_api=False)
182+
**COMMON_ARGS)
189183
soln = solver.solve(tspan, y0, yp0)
190184
assert soln.flag==StatusEnumIDA.ROOT_RETURN, "ERROR: Not sufficient root found"
191185
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -201,7 +195,7 @@ def test_ida_rootfn_two(self):
201195
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
202196
solver = dae('ida', rhs_fn, nr_rootfns=2, rootfn=root_fn2,
203197
onroot=onroot_vc,
204-
old_api=False)
198+
**COMMON_ARGS)
205199
soln = solver.solve(tspan, y0, yp0)
206200
assert soln.flag==StatusEnumIDA.ROOT_RETURN, "ERROR: Not sufficient root found"
207201
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -217,7 +211,7 @@ def test_ida_rootfn_end(self):
217211
tspan = np.arange(0, 30 + 1, 1.0, DTYPE)
218212
solver = dae('ida', rhs_fn, nr_rootfns=1, rootfn=root_fn3,
219213
onroot=onroot_vc,
220-
old_api=False)
214+
**COMMON_ARGS)
221215
soln = solver.solve(tspan, y0, yp0)
222216
assert soln.flag==StatusEnumIDA.ROOT_RETURN, "ERROR: Not sufficient root found"
223217
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -234,7 +228,7 @@ def test_ida_tstopfn_notstop(self):
234228
n = 0
235229
tspan = np.arange(0, t_end1 + 1, 1.0, DTYPE)
236230
solver = dae('ida', rhs_fn, tstop=T1+1, ontstop=ontstop_va,
237-
old_api=False)
231+
**COMMON_ARGS)
238232
soln = solver.solve(tspan, y0, yp0)
239233
assert soln.flag==StatusEnumIDA.SUCCESS, "ERROR: Error occurred"
240234
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -247,7 +241,7 @@ def test_ida_tstopfn(self):
247241
n = 0
248242
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
249243
solver = dae('ida', rhs_fn, tstop=T1,
250-
old_api=False)
244+
**COMMON_ARGS)
251245
soln = solver.solve(tspan, y0, yp0)
252246
assert soln.flag==StatusEnumIDA.TSTOP_RETURN, "ERROR: Tstop not found!"
253247
assert allclose([soln.tstop.t[0], soln.tstop.y[0,0], soln.tstop.y[0,1]],
@@ -263,7 +257,7 @@ def test_ida_tstopfnacc(self):
263257
n = 0
264258
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
265259
solver = dae('ida', rhs_fn, tstop=T1, ontstop=ontstop_va,
266-
old_api=False)
260+
**COMMON_ARGS)
267261
soln = solver.solve(tspan, y0, yp0)
268262
assert soln.flag==StatusEnumIDA.SUCCESS, "ERROR: Error occurred"
269263
assert allclose([soln.values.t[-1], soln.values.y[-1,0], soln.values.y[-1,1]],
@@ -279,8 +273,8 @@ def test_ida_tstopfn_stop(self):
279273
global n
280274
n = 0
281275
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
282-
solver = dae('ida', rhs_fn, tstop=T1, ontstop=ontstop_vb,
283-
old_api=False)
276+
solver = dae('ida', rhs_fn, tstop=T1, ontstop=ontstop_stop,
277+
**COMMON_ARGS)
284278

285279
soln = solver.solve(tspan, y0, yp0)
286280
assert soln.flag==StatusEnumIDA.TSTOP_RETURN, "ERROR: Error occurred"
@@ -300,7 +294,7 @@ def test_ida_tstopfn_test(self):
300294
n = 0
301295
tspan = np.arange(0, t_end2 + 1, 1.0, DTYPE)
302296
solver = dae('ida', rhs_fn, tstop=T1, ontstop=ontstop_vc,
303-
old_api=False)
297+
**COMMON_ARGS)
304298

305299
soln = solver.solve(tspan, y0, yp0)
306300
assert soln.flag==StatusEnumIDA.TSTOP_RETURN, "ERROR: Error occurred"

0 commit comments

Comments
 (0)