Debian Codegraph @

csb/test/cases/statistics/mixtures.py

raw
1 from numpy import array, linspace
2
3 from csb import test
4 from csb.bio.io.wwpdb import LegacyStructureParser
5 from csb.statistics import mixtures
6
7
8 @test.functional
9 class TestMixtures(test.Case):
10
11 w_ref_segments = array([
12 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
13 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
15 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
16 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1,
17 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
19 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
20 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
21 2, 2, 2, 2])
22
23 w_ref_conformers = array([2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1])
24
25 def _ake_ensemble_coords(self):
26
27 pdbfile = self.config.getTestFile('ake-xray-ensemble-ca.pdb')
28 ensemble = LegacyStructureParser(pdbfile).parse_models()
29 X = array([model.get_coordinates(['CA'], True) for model in ensemble])
30
31 self.assertEqual(X.shape, (16, 211, 3))
32
33 self._ake_ensemble_coords = lambda: X
34
35 return X
36
37 def testSegmentMixture(self):
38
39 self._testMixture(mixtures.SegmentMixture, self.w_ref_segments)
40
41 def testConformerMixture(self):
42
43 self._testMixture(mixtures.ConformerMixture, self.w_ref_conformers, 14./16.)
44
45 def _testMixture(self, cls, w_ref, min_overlap=0.9, repeats=5):
46
47 X = self._ake_ensemble_coords()
48 K = len(set(w_ref))
49
50 # non-randomized heuristic with BIC
51 m = cls.new(X)
52 overlap = m.overlap(w_ref)
53
54 self.assertTrue(overlap >= min_overlap, 'mixture not reproduced with heuristic')
55
56 # annealing (randomized initialization)
57 m = cls(X, K, False)
58 for _ in range(repeats):
59 m.randomize_scales()
60 m.anneal(linspace(2.0, 0.1, 10))
61
62 overlap = m.overlap(w_ref)
63 if overlap >= min_overlap:
64 break
65 else:
66 self.assertTrue(False, 'mixture not reproduced with annealing')
67
68
69 if __name__ == '__main__':
70
71 test.Console()
72
73 # vi:expandtab:smarttab