Package test_suite :: Package system_tests :: Module bmrb
[hide private]
[frames] | no frames]

Source Code for Module test_suite.system_tests.bmrb

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Python module imports. 
 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  # relax module imports. 
 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 # Execute the base class method. 48 super(Bmrb, self).__init__(methodName) 49 50 # Missing module. 51 if not dep_check.bmrblib_module: 52 # Store in the status object. 53 status.skipped_tests.append([methodName, 'Bmrblib', self._skip_type])
54 55
56 - def setUp(self):
57 """Common set up for these system tests.""" 58 59 # Create a temporary file name. 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 # Print out. 67 print("\n\nComparing data pipe contents:") 68 69 # Blacklists (data that is not restored, and relaxation data which has been reordered and will be checked in data_ri_comp()). 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 # The data pipes. 77 old_pipe = ds[old_pipe_name] 78 new_pipe = ds[new_pipe_name] 79 80 # The molecule data structure. 81 self.assertEqual(len(old_pipe.mol), len(new_pipe.mol)) 82 for i in range(len(old_pipe.mol)): 83 # Check the attributes. 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 # The residue data structure. 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 # Check the attributes. 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 # The spin data structure. 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 # Skip deselected spins. 98 if not old_pipe.mol[i].res[j].spin[k].select: 99 continue 100 101 # Swap the spin indices for the N, H, NE1 and HE1 spins of residue 9. 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 # Check the containers. 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 # The diffusion tensor. 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 # The global data structures. 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 # Print a new line. 129 print('') 130 131 # Check the attributes. 132 names = dir(cont_old) 133 for name in names: 134 # Skip special names. 135 if search('^__', name): 136 continue 137 138 # Skip the data store methods. 139 if name in list(cont_old.__class__.__dict__.keys()): 140 continue 141 142 # Simulation data. 143 if search('_sim$', name): 144 continue 145 146 # Blacklisting. 147 if name in blacklist: 148 continue 149 150 # Print out. 151 print("%s object: '%s'" % (label, name)) 152 153 # Get the objects. 154 obj_old = getattr(cont_old, name) 155 obj_new = getattr(cont_new, name) 156 157 # Functions and methods. 158 if inspect.isfunction(obj_old) or inspect.ismethod(obj_old): 159 continue 160 161 # Does it exist? 162 self.assert_(hasattr(cont_new, name)) 163 164 # Compare lists. 165 if (isinstance(obj_old, list) or isinstance(obj_old, ndarray)): 166 self.assertEqual(len(obj_old), len(obj_new)) 167 168 # List of lists (or rank-2 array). 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 # Standard list. 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 # Compare floats. 186 elif isinstance(obj_old, float): 187 self.assertAlmostEqual(obj_old, obj_new, prec) 188 189 # Compare ints and strings. 190 else: 191 self.assertEqual(obj_old, obj_new)
192 193
194 - def data_ri_comp_pipe(self, cont_old=None, cont_new=None):
195 """Compare the contents of the two pipe data containers.""" 196 197 # Check that the new container has relaxation data. 198 for name in ['frq', 'ri_ids', 'ri_type']: 199 self.assert_(hasattr(cont_new, name)) 200 201 # Check the IDs. 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 # Check the frequencies and types. 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
214 - def data_ri_comp_spin(self, cont_old=None, cont_new=None):
215 """Compare the contents of the two spin data containers.""" 216 217 # Check that the new container has relaxation data. 218 for name in ['ri_data', 'ri_data_err']: 219 self.assert_(hasattr(cont_new, name)) 220 221 # Check the IDs. 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 # Check the data and errors. 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 # Set the NMR-STAR version. 238 ds.version = '3.0' 239 240 # Execute the script. 241 self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'bmrb_rw.py') 242 243 # Test the data. 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 # Set the NMR-STAR version. 251 ds.version = '3.1' 252 253 # Execute the script. 254 self.script_exec(status.install_path + sep+'test_suite'+sep+'system_tests'+sep+'scripts'+sep+'bmrb_rw.py') 255 256 # Test the data. 257 self.data_check(version='3.1')
258