1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 from copy import deepcopy
24 import inspect
25 from numpy import ndarray
26 from os import sep
27 from re import search
28 from tempfile import mktemp
29
30
31 from data import Relax_data_store; ds = Relax_data_store()
32 import dep_check
33 from status import Status; status = Status()
34 from test_suite.system_tests.base_classes import SystemTestCase
35
36
37 -class Bmrb(SystemTestCase):
38 """TestCase class for functional tests of the reading and writing of BMRB STAR formatted files."""
39
40 - def __init__(self, methodName='runTest'):
41 """Skip the tests if bmrblib is not installed.
42
43 @keyword methodName: The name of the test.
44 @type methodName: str
45 """
46
47
48 super(Bmrb, self).__init__(methodName)
49
50
51 if not dep_check.bmrblib_module:
52
53 status.skipped_tests.append([methodName, 'Bmrblib', self._skip_type])
54
55
57 """Common set up for these system tests."""
58
59
60 ds.tmpfile = mktemp()
61
62
63 - def data_check(self, old_pipe_name='results', new_pipe_name='new', version=None):
64 """Check that all data has been successfully restored from the BMRB files."""
65
66
67 print("\n\nComparing data pipe contents:")
68
69
70 blacklist_spin = ['attached_proton', 'fixed', 'nucleus', 'proton_type', 'relax_sim_data', 'select', 'xh_vect'] + ['r_err', 'csa_err'] + ['chi2_sim', 'f_count', 'g_count', 'h_count', 'iter', 'warning'] + ['frq', 'frq_labels', 'noe_r1_table', 'remap_table', 'ri_labels', 'relax_data', 'relax_error'] + ['_spin_index']
71 if version == '3.0':
72 blacklist_spin = blacklist_spin + ['r', 'local_tm', 'local_tm_err']
73 blacklist_diff = []
74 blacklist_global = ['diff_tensor', 'exp_info', 'hybrid_pipes', 'mol', 'interatomic', 'sim_number', 'sim_state'] + ['ri_ids', 'frq', 'ri_type'] + ['result_files']
75
76
77 old_pipe = ds[old_pipe_name]
78 new_pipe = ds[new_pipe_name]
79
80
81 self.assertEqual(len(old_pipe.mol), len(new_pipe.mol))
82 for i in range(len(old_pipe.mol)):
83
84 self.assertEqual(old_pipe.mol[i].name, old_pipe.mol[i].name)
85 self.assertEqual(old_pipe.mol[i].type, old_pipe.mol[i].type)
86
87
88 self.assertEqual(len(old_pipe.mol[i].res), len(new_pipe.mol[i].res))
89 for j in range(len(old_pipe.mol[i].res)):
90
91 self.assertEqual(old_pipe.mol[i].res[j].name, old_pipe.mol[i].res[j].name)
92 self.assertEqual(old_pipe.mol[i].res[j].num, old_pipe.mol[i].res[j].num)
93
94
95 self.assertEqual(len(old_pipe.mol[i].res[j].spin), len(new_pipe.mol[i].res[j].spin))
96 for k in range(len(old_pipe.mol[i].res[j].spin)):
97
98 if not old_pipe.mol[i].res[j].spin[k].select:
99 continue
100
101
102 k_new = k
103 if i == 0 and j == 0:
104 if k == 1:
105 k_new = 2
106 elif k == 2:
107 k_new = 1
108
109
110 self.data_cont_comp(label='Spin', cont_old=old_pipe.mol[i].res[j].spin[k], cont_new=new_pipe.mol[i].res[j].spin[k_new], blacklist=blacklist_spin)
111 if hasattr(old_pipe.mol[i].res[j].spin[k], 'ri_labels'):
112 self.data_ri_comp_spin(cont_old=old_pipe.mol[i].res[j].spin[k], cont_new=new_pipe.mol[i].res[j].spin[k_new])
113
114
115 if version != '3.0':
116 self.assert_(hasattr(new_pipe, 'diff_tensor'))
117 self.data_cont_comp(label='Diff tensor', cont_old=old_pipe.diff_tensor, cont_new=new_pipe.diff_tensor, prec=4, blacklist=blacklist_diff)
118
119
120 self.data_cont_comp(label='Global', cont_old=old_pipe, cont_new=new_pipe, blacklist=blacklist_global)
121 if hasattr(old_pipe, 'ri_ids'):
122 self.data_ri_comp_pipe(cont_old=old_pipe, cont_new=new_pipe)
123
124
125 - def data_cont_comp(self, label=None, cont_old=None, cont_new=None, prec=7, blacklist=[]):
126 """Compare the contents of the two data containers."""
127
128
129 print('')
130
131
132 names = dir(cont_old)
133 for name in names:
134
135 if search('^__', name):
136 continue
137
138
139 if name in list(cont_old.__class__.__dict__.keys()):
140 continue
141
142
143 if search('_sim$', name):
144 continue
145
146
147 if name in blacklist:
148 continue
149
150
151 print("%s object: '%s'" % (label, name))
152
153
154 obj_old = getattr(cont_old, name)
155 obj_new = getattr(cont_new, name)
156
157
158 if inspect.isfunction(obj_old) or inspect.ismethod(obj_old):
159 continue
160
161
162 self.assert_(hasattr(cont_new, name))
163
164
165 if (isinstance(obj_old, list) or isinstance(obj_old, ndarray)):
166 self.assertEqual(len(obj_old), len(obj_new))
167
168
169 if (isinstance(obj_old[0], list) or isinstance(obj_old[0], ndarray)):
170 for i in range(len(obj_old)):
171 for j in range(len(obj_old[i])):
172 if isinstance(obj_old[i][j], float):
173 self.assertAlmostEqual(obj_old[i][j], obj_new[i][j], prec)
174 else:
175 self.assertEqual(obj_old[i][j], obj_new[i][j])
176
177
178 else:
179 for i in range(len(obj_old)):
180 if isinstance(obj_old[i], float):
181 self.assertAlmostEqual(obj_old[i], obj_new[i], prec)
182 else:
183 self.assertEqual(obj_old[i], obj_new[i])
184
185
186 elif isinstance(obj_old, float):
187 self.assertAlmostEqual(obj_old, obj_new, prec)
188
189
190 else:
191 self.assertEqual(obj_old, obj_new)
192
193
195 """Compare the contents of the two pipe data containers."""
196
197
198 for name in ['frq', 'ri_ids', 'ri_type']:
199 self.assert_(hasattr(cont_new, name))
200
201
202 old_ids = deepcopy(cont_old.ri_ids)
203 new_ids = deepcopy(cont_new.ri_ids)
204 old_ids.sort()
205 new_ids.sort()
206 self.assertEqual(old_ids, new_ids)
207
208
209 for ri_id in old_ids:
210 self.assertEqual(cont_old.frq[ri_id], cont_new.frq[ri_id])
211 self.assertEqual(cont_old.ri_type[ri_id], cont_new.ri_type[ri_id])
212
213
215 """Compare the contents of the two spin data containers."""
216
217
218 for name in ['ri_data', 'ri_data_err']:
219 self.assert_(hasattr(cont_new, name))
220
221
222 old_ids = cont_old.ri_data.keys()
223 new_ids = cont_new.ri_data.keys()
224 old_ids.sort()
225 new_ids.sort()
226 self.assertEqual(old_ids, new_ids)
227
228
229 for ri_id in old_ids:
230 self.assertEqual(cont_old.ri_data[ri_id], cont_new.ri_data[ri_id])
231 self.assertEqual(cont_old.ri_data_err[ri_id], cont_new.ri_data_err[ri_id])
232
233
235 """Write and then read a BRMB STAR formatted file containing model-free results."""
236
237
238 ds.version = '3.0'
239
240
241 self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'bmrb_rw.py')
242
243
244 self.data_check(version='3.0')
245
246
248 """Write and then read a BRMB STAR formatted file containing model-free results."""
249
250
251 ds.version = '3.1'
252
253
254 self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'bmrb_rw.py')
255
256
257 self.data_check(version='3.1')
258