forked from InstituteforDiseaseModeling/covasim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_regression.py
97 lines (67 loc) · 2.09 KB
/
test_regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'''
Load an old simulation to test regression.
Saved file originally generated by checking out Covasim v1.7.0 (with this file
not checked in) and running:
import test_regression as tr
tr.make_sim(do_save=True)
'''
import sciris as sc
import covasim as cv
import pytest
pop_size = 500
filename = 'example_regression.sim'
version = '1.7.0'
def make_sim(do_save=False, **kwargs):
# Shared settings
pars = dict(pop_size=pop_size, verbose=0)
pars.update(kwargs)
# Versioning was introduced in 2.0
if cv.check_version('2.0.0', verbose=False) >= 0:
pars.update({'version':version})
sim = cv.Sim(**pars)
sim.run()
if do_save:
print(sim.summary)
sim.save(filename)
return sim
def test_regression():
sc.heading('Testing regression...')
sim1 = cv.load(filename)
sim2 = make_sim()
# Check that they match
cv.diff_sims(sim1, sim2, skip_key_diffs=True, die=True)
# Confirm that non-matching sims don't match
sim3 = make_sim(beta=0.02123)
with pytest.raises(ValueError):
cv.diff_sims(sim1, sim3, skip_key_diffs=True, die=True)
return sim1, sim2
def test_migration():
sc.heading('Testing migration...')
# Create sim and people
base = make_sim()
base.people.version = version
sim = cv.load(filename)
sim.people = base.people
# Create msim
msim = cv.MultiSim(base_sim=sim)
del msim.version # To simulate <2.0.0
msim.init_sims()
# Create scenarios
scens = cv.Scenarios(sim=sim)
del scens.version # To simulate <2.0.0
# Try migrations
new_sim = cv.migrate(sim, die=True)
new_msim = cv.migrate(msim, die=True)
new_scens = cv.migrate(scens, die=True)
# Try something un-migratable
with pytest.raises(TypeError):
cv.migrate('Strings are not migratable', die=True)
return new_sim, new_msim, new_scens
#%% Run as a script
if __name__ == '__main__':
# Start timing and optionally enable interactive plotting
T = sc.tic()
sim1, sim2 = test_regression()
sim, msim, scens = test_migration()
sc.toc(T)
print('Done.')