Skip to content

Commit 1f91409

Browse files
committed
Check that we can pickle ReplicaExchange
1 parent 349724b commit 1f91409

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,51 @@
66
import sys
77
import subprocess
88
import ihm.reader
9+
import pickle
10+
import IMP
11+
912

1013
TOPDIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..'))
1114

15+
1216
class Tests(unittest.TestCase):
1317
def test_simple(self):
1418
"""Test model building"""
1519
os.chdir(os.path.join(TOPDIR, 'production_scripts'))
1620
p = subprocess.check_call(["python", "sample.py", "--test"])
1721
# todo: assert outputs, run analysis
1822

23+
def test_pickle(self):
24+
"""Test that pickled ReplicaExchange object works"""
25+
# Set up modeling but don't run sampling
26+
os.chdir(os.path.join(TOPDIR, 'production_scripts'))
27+
with open('sample.py') as fh:
28+
contents = fh.read().replace('rex.execute_macro()', '')
29+
sys.argv = [sys.argv[0], '--test']
30+
g = {}
31+
exec(contents, g)
32+
rex = g['rex']
33+
del g
34+
rex.vars['number_of_frames'] = 2
35+
36+
dump = pickle.dumps((rex.model, rex))
37+
38+
# Run the original ReplicaExchange and get the final score
39+
IMP.random_number_generator.seed(99)
40+
rex.execute_macro()
41+
rs = IMP.pmi.tools.get_restraint_set(rex.model)
42+
original_score = rs.evaluate(False)
43+
del rex, rs
44+
45+
# With the same random seed, we should get the exact same trajectory
46+
# with the pickled object
47+
newm, newrex = pickle.loads(dump)
48+
IMP.random_number_generator.seed(99)
49+
newrex.execute_macro()
50+
rs = IMP.pmi.tools.get_restraint_set(newrex.model)
51+
new_score = rs.evaluate(False)
52+
self.assertAlmostEqual(original_score, new_score, delta=1e-4)
53+
1954
def test_mmcif(self):
2055
"""Test generation of mmCIF output"""
2156
os.chdir(os.path.join(TOPDIR, 'production_scripts'))

0 commit comments

Comments
 (0)