|
6 | 6 | import sys
|
7 | 7 | import subprocess
|
8 | 8 | import ihm.reader
|
| 9 | +import pickle |
| 10 | +import IMP |
| 11 | + |
9 | 12 |
|
10 | 13 | TOPDIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '..'))
|
11 | 14 |
|
| 15 | + |
12 | 16 | class Tests(unittest.TestCase):
|
13 | 17 | def test_simple(self):
|
14 | 18 | """Test model building"""
|
15 | 19 | os.chdir(os.path.join(TOPDIR, 'production_scripts'))
|
16 | 20 | p = subprocess.check_call(["python", "sample.py", "--test"])
|
17 | 21 | # todo: assert outputs, run analysis
|
18 | 22 |
|
| 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 | + |
19 | 54 | def test_mmcif(self):
|
20 | 55 | """Test generation of mmCIF output"""
|
21 | 56 | os.chdir(os.path.join(TOPDIR, 'production_scripts'))
|
|
0 commit comments