Coverage for tests/test_rmsd.py: 91%

81 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-07-11 14:46 +0200

1import unittest 

2 

3import os 

4import yaml 

5import numpy as np 

6import tempfile 

7from pathlib import Path 

8import MPP.run as run_module 

9 

10 

11config_dir = "/data/evaluation/MPP/stochastic_MPP_Felix/data_production/sm/config/" 

12root = "/data/evaluation/MPP/stochastic_MPP_Felix/data_production/sm/results/" 

13 

14SYSTEMS = [ 

15 "HP35", 

16 "PDZ3_7", 

17 "aSyn_rdc_200ns", 

18 "HP35_stoch", 

19] 

20SETUPS = [ 

21 "t", 

22 "kl", 

23 "t_js", 

24 "kl_js", 

25 "gpcca", 

26] 

27 

28with open(f"{config_dir}lumpings.yaml") as f: 

29 lumpings = yaml.safe_load(f) 

30 

31 

32def get_d(system, setup, rmsd=False): 

33 d = run_module.Data( 

34 f"/data/evaluation/MPP/stochastic_MPP_Felix/data_production/sm/config/{system}.yaml" 

35 ) 

36 d.setup_mpp( 

37 lumpings[setup]["kernel similarity"], 

38 lumpings[setup]["feature kernel"], 

39 ) 

40 if setup == "gpcca": 40 ↛ 41line 40 didn't jump to line 41 because the condition on line 40 was never true

41 d.perform_gpcca("ref", f"{root}{system}/{setup}/Z.npy") 

42 else: 

43 d.perform_mpp(f"{root}{system}/{setup}/Z.npy") 

44 if rmsd: 44 ↛ 45line 44 didn't jump to line 45 because the condition on line 44 was never true

45 d.mpp.load_rmsd(f"{root}{system}/{setup}/rmsd.npy") 

46 return d 

47 

48 

49class TestRMSD_HP35(unittest.TestCase): 

50 def setUp(self): 

51 self.d = get_d("HP35", "kl") 

52 self.mpp = self.d.mpp 

53 

54 def compare_text_files(self, file1, file2): 

55 """Compares two text files, ignoring the first line (timestamp).""" 

56 with open(file1, "r") as f1, open(file2, "r") as f2: 

57 lines1 = f1.readlines()[1:] # skip first line 

58 lines2 = f2.readlines()[1:] 

59 self.assertEqual( 

60 lines1, 

61 lines2, 

62 f"Mismatch in file content (excluding header): {file1} vs {file2}", 

63 ) 

64 

65 def test_draw_random_indices(self): 

66 np.random.seed(123456) 

67 drawn_frames = self.mpp.draw_random_frames_indices() 

68 

69 with tempfile.TemporaryDirectory() as tmpdir: 

70 tmpdir = Path(tmpdir) / "random_indices" 

71 np.random.seed(123456) 

72 self.mpp.draw_random_frames_indices(tmpdir) 

73 

74 drawn_frames_from_file = np.zeros(drawn_frames.shape, dtype=int) 

75 for s, i in enumerate(drawn_frames): 

76 drawn_frames_from_file[s] = np.loadtxt(tmpdir / f"{s + 1:02d}.ndx") 

77 

78 np.testing.assert_allclose(drawn_frames, drawn_frames_from_file) 

79 

80 def test_draw_random_frames(self): 

81 expected_path = ( 

82 Path(__file__).parent / "data" / "HP35" / "expected_output" / "t" / "pdbs" 

83 ) 

84 with tempfile.TemporaryDirectory() as tmpdir: 

85 tmpdir = Path(tmpdir) 

86 np.random.seed(123456) 

87 self.mpp.draw_random_frames(tmpdir) 

88 

89 for expected_file in expected_path.glob("*.pdb"): 

90 actual_file = tmpdir / expected_file.name 

91 self.assertTrue(actual_file.exists(), f"Missing file: {actual_file}") 

92 # self.compare_text_files(expected_file, actual_file) 

93 

94 

95class TestRMSD_aSyn(unittest.TestCase): 

96 def setUp(self): 

97 self.d = get_d("aSyn_rdc_200ns", "t") 

98 self.mpp = self.d.mpp 

99 

100 def compare_text_files(self, file1, file2): 

101 """Compares two text files, ignoring the first line (timestamp).""" 

102 with open(file1, "r") as f1, open(file2, "r") as f2: 

103 lines1 = f1.readlines() 

104 lines2 = f2.readlines() 

105 self.assertEqual( 

106 lines1, 

107 lines2, 

108 f"Mismatch in file content (excluding header): {file1} vs {file2}", 

109 ) 

110 

111 def test_write_least_moving_residues(self): 

112 expected_output = ( 

113 Path(__file__).parent 

114 / "data" 

115 / "aSyn" 

116 / "expected_output" 

117 / "t" 

118 / "least_moving_residues.ndx" 

119 ) 

120 with tempfile.TemporaryDirectory() as tmpdir: 

121 tmpfile = Path(tmpdir) / "least_moving_residues.ndx" 

122 self.mpp.write_least_moving_residues( 

123 self.d.d["source"] + self.d.d["contact index file"], 

124 tmpfile, 

125 ) 

126 self.compare_text_files(tmpfile, expected_output) 

127 

128 

129class TestRMSD_PDZ3(unittest.TestCase): 

130 def setUp(self): 

131 self.d1 = get_d("PDZ3_7", "kl") 

132 self.d2 = get_d("PDZ3_7", "kl") 

133 

134 def test_rmsd_property(self): 

135 expected_output = ( 

136 Path(__file__).parent 

137 / "data" 

138 / "PDZ3" 

139 / "expected_output" 

140 / "kl" 

141 / "rmsd.npy" 

142 ) 

143 self.d2.mpp.load_rmsd(expected_output) 

144 with tempfile.TemporaryDirectory() as tmpdir: 

145 rmsd_file = Path(tmpdir) / "rmsd.npy" 

146 self.d1.mpp.save_rmsd(rmsd_file) 

147 

148 np.testing.assert_allclose(self.d2.mpp.rmsd, self.d1.mpp.rmsd, rtol=1e-5) 

149 

150 def test_rmsd_sharpness(self): 

151 expected_output = ( 

152 Path(__file__).parent 

153 / "data" 

154 / "PDZ3" 

155 / "expected_output" 

156 / "kl" 

157 / "rmsd.npy" 

158 ) 

159 self.d1.mpp.load_rmsd(expected_output) 

160 np.testing.assert_allclose(self.d1.mpp.rmsd_sharpness(), 2.1352340796266334)