Package test_suite :: Package unit_tests :: Package _data_store :: Module test_diff_tensor
[hide private]
[frames] | no frames]

Source Code for Module test_suite.unit_tests._data_store.test_diff_tensor

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2007-2013 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 math import cos, pi, sin 
 24  from numpy import array, dot, float64, transpose, zeros 
 25  from unittest import TestCase 
 26   
 27  # relax module imports. 
 28  from data_store.diff_tensor import DiffTensorData 
 29  from lib.geometry.rotations import two_vect_to_R 
 30  from lib.errors import RelaxError 
 31   
 32   
33 -class Test_diff_tensor(TestCase):
34 """Unit tests for the data.diff_tensor relax module.""" 35
36 - def calc_spheroid_objects(self, tm, Da, theta, phi):
37 """Function for calculating the spheroidal diffusion tensor objects.""" 38 39 # The parameter values. 40 Diso = 1/(6*tm) 41 Dpar = Diso + 2.0/3.0 * Da 42 Dper = Diso - 1.0/3.0 * Da 43 Dratio = Dpar / Dper 44 45 # Vectors. 46 Dpar_unit = array([sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta)]) 47 48 # Matrices. 49 if Dpar > Dper: 50 axis = array([0, 0, 1], float64) 51 tensor_diag = array([[ Dper, 0.0, 0.0], 52 [ 0.0, Dper, 0.0], 53 [ 0.0, 0.0, Dpar]]) 54 else: 55 axis = array([1, 0, 0], float64) 56 tensor_diag = array([[ Dpar, 0.0, 0.0], 57 [ 0.0, Dper, 0.0], 58 [ 0.0, 0.0, Dper]]) 59 60 # The rotation. 61 rotation = zeros((3, 3), float64) 62 two_vect_to_R(Dpar_unit, axis, rotation) 63 64 # The diffusion tensor. 65 tensor = dot(rotation, dot(tensor_diag, transpose(rotation))) 66 67 # Return the objects. 68 return Diso, Dpar, Dper, Dratio, Dpar_unit, tensor_diag, rotation, tensor
69 70
71 - def setUp(self):
72 """Set 'self.diff_data' to an empty instance of the DiffTensorData class.""" 73 74 self.diff_data = DiffTensorData()
75 76
77 - def test_append_spheroid_sim(self):
78 """Test the appending of Monte Carlo simulation spheroidal diffusion tensor parameters. 79 80 The following parameters will be appended to empty lists: 81 - tm: 8 ns 82 - Da: -1e7 83 - theta: 150 degrees 84 - phi: 30 degrees 85 """ 86 87 # The MC sim parameter values. 88 tm = 8e-9 89 Da = -1e7 90 theta = (150 / 360.0) * 2.0 * pi 91 phi = (30 / 360.0) * 2.0 * pi 92 93 # Set the diffusion type. 94 self.diff_data.set(param='type', value='spheroid') 95 self.diff_data.set(param='spheroid_type', value='oblate') 96 97 # Set the number of MC sims. 98 self.diff_data.set_sim_num(1) 99 100 # Set the initial values. 101 self.diff_data.set(param='tm', value=tm, category='sim', sim_index=0) 102 self.diff_data.set(param='Da', value=Da, category='sim', sim_index=0) 103 self.diff_data.set(param='theta', value=theta, category='sim', sim_index=0) 104 self.diff_data.set(param='phi', value=phi, category='sim', sim_index=0) 105 106 # Test the set values. 107 self.assertEqual(self.diff_data.type, 'spheroid') 108 self.assertEqual(self.diff_data.tm_sim[0], tm) 109 self.assertEqual(self.diff_data.Da_sim[0], Da) 110 self.assertEqual(self.diff_data.theta_sim[0], theta) 111 self.assertEqual(self.diff_data.phi_sim[0], phi) 112 113 # Calculate the diffusion tensor objects. 114 Diso, Dpar, Dper, Dratio, Dpar_unit, tensor_diag, rotation, tensor = self.calc_spheroid_objects(tm, Da, theta, phi) 115 116 # Test the automatically created values. 117 self.assertEqual(self.diff_data.Diso_sim[0], Diso) 118 self.assertEqual(self.diff_data.Dpar_sim[0], Dpar) 119 self.assertEqual(self.diff_data.Dper_sim[0], Dper) 120 self.assertEqual(self.diff_data.Dratio_sim[0], Dratio) 121 122 # Test the vectors. 123 self.assertEqual(self.diff_data.Dpar_unit_sim[0].tostring(), Dpar_unit.tostring()) 124 125 # Test the matrices. 126 self.assertEqual(self.diff_data.tensor_diag_sim[0].tostring(), tensor_diag.tostring()) 127 self.assertEqual(self.diff_data.rotation_sim[0].tostring(), rotation.tostring()) 128 self.assertEqual(self.diff_data.tensor_sim[0].tostring(), tensor.tostring())
129 130
131 - def test_display(self):
132 """Test that the contents of the diffusion tensor object can be displayed.""" 133 134 print(self.diff_data)
135 136
137 - def test_set_Diso(self):
138 """Test that the Diso parameter cannot be set.""" 139 140 # Assert that a RelaxError occurs when Diso is set (to the tm value of 10 ns). 141 self.assertRaises(RelaxError, setattr, self.diff_data, 'Diso', 1/(6*1e-8)) 142 143 # Make sure that the Diso parameter has not been set. 144 self.assert_(not hasattr(self.diff_data, 'Diso'))
145 146
147 - def test_set_spheroid_errors(self):
148 """Test the setting of spheroidal diffusion tensor parameter errors. 149 150 The following parameter errors will be set: 151 - tm: 1 ns 152 - Da: 1e3 153 - theta: 3 degrees 154 - phi: 5 degrees 155 """ 156 157 # The parameter errors. 158 tm = 1e-8 159 Da = 1e3 160 theta = (3 / 360.0) * 2.0 * pi 161 phi = (5 / 360.0) * 2.0 * pi 162 163 # Set the diffusion type. 164 self.diff_data.set(param='type', value='spheroid') 165 self.diff_data.set(param='spheroid_type', value='prolate') 166 167 # Set the diffusion parameter errors. 168 self.diff_data.set(param='tm', value=tm, category='err') 169 self.diff_data.set(param='Da', value=Da, category='err') 170 self.diff_data.set(param='theta', value=theta, category='err') 171 self.diff_data.set(param='phi', value=phi, category='err') 172 173 # Test the set values. 174 self.assertEqual(self.diff_data.type, 'spheroid') 175 self.assertEqual(self.diff_data.tm_err, tm) 176 self.assertEqual(self.diff_data.Da_err, Da) 177 self.assertEqual(self.diff_data.theta_err, theta) 178 self.assertEqual(self.diff_data.phi_err, phi) 179 180 # Calculate the diffusion tensor objects. 181 Diso, Dpar, Dper, Dratio, Dpar_unit, tensor_diag, rotation, tensor = self.calc_spheroid_objects(tm, Da, theta, phi) 182 183 # Test the automatically created values. 184 self.assertEqual(self.diff_data.Diso_err, Diso) 185 self.assertEqual(self.diff_data.Dpar_err, Dpar) 186 self.assertEqual(self.diff_data.Dper_err, Dper) 187 self.assertEqual(self.diff_data.Dratio_err, Dratio) 188 189 # Test the vectors. 190 self.assertEqual(self.diff_data.Dpar_unit_err.tostring(), Dpar_unit.tostring())
191 192
193 - def test_set_spheroid_params(self):
194 """Test the setting of spheroidal diffusion tensor parameters. 195 196 The following parameters will be set: 197 - tm: 20 ns 198 - Da: 2e6 199 - theta: 60 degrees 200 - phi: 290 degrees 201 """ 202 203 # The parameter values. 204 tm = 2e-8 205 Da = 2e6 206 theta = (60 / 360.0) * 2.0 * pi 207 phi = (290 / 360.0) * 2.0 * pi 208 209 # Set the diffusion type. 210 self.diff_data.set(param='type', value='spheroid') 211 self.diff_data.set(param='spheroid_type', value='prolate') 212 213 # Set the diffusion parameters. 214 self.diff_data.set(param='tm', value=tm) 215 self.diff_data.set(param='Da', value=Da) 216 self.diff_data.set(param='theta', value=theta) 217 self.diff_data.set(param='phi', value=phi) 218 219 # Test the set values. 220 self.assertEqual(self.diff_data.type, 'spheroid') 221 self.assertEqual(self.diff_data.tm, tm) 222 self.assertEqual(self.diff_data.Da, Da) 223 self.assertEqual(self.diff_data.theta, theta) 224 self.assertEqual(self.diff_data.phi, phi) 225 226 # Calculate the diffusion tensor objects. 227 Diso, Dpar, Dper, Dratio, Dpar_unit, tensor_diag, rotation, tensor = self.calc_spheroid_objects(tm, Da, theta, phi) 228 229 # Test the automatically created values. 230 self.assertEqual(self.diff_data.Diso, Diso) 231 self.assertEqual(self.diff_data.Dpar, Dpar) 232 self.assertEqual(self.diff_data.Dper, Dper) 233 self.assertEqual(self.diff_data.Dratio, Dratio) 234 235 # Test the vectors. 236 self.assertEqual(self.diff_data.Dpar_unit.tostring(), Dpar_unit.tostring()) 237 238 # Test the matrices. 239 self.assertEqual(self.diff_data.tensor_diag.tostring(), tensor_diag.tostring()) 240 self.assertEqual(self.diff_data.rotation.tostring(), rotation.tostring()) 241 self.assertEqual(self.diff_data.tensor.tostring(), tensor.tostring())
242 243
244 - def test_set_spheroid_sim(self):
245 """Test the setting of Monte Carlo simulation spheroidal diffusion tensor parameters. 246 247 Firstly the following parameters will be appended to empty lists: 248 - tm: 2 ns 249 - Da: 1e5 250 - theta: 0 degrees 251 - phi: 360 degrees 252 253 These MC sim values will then be explicity overwritten by setting the first elements of the 254 lists to: 255 - tm: 0.5 ns 256 - Da: 3e5 257 - theta: 2 degrees 258 - phi: 0 degrees 259 """ 260 261 # Set the diffusion type. 262 self.diff_data.set(param='type', value='spheroid') 263 self.diff_data.set(param='spheroid_type', value='prolate') 264 265 # Set the number of MC sims. 266 self.diff_data.set_sim_num(1) 267 268 # Set the initial values. 269 self.diff_data.set(param='tm', value=2e-9, category='sim', sim_index=0) 270 self.diff_data.set(param='Da', value=1e5, category='sim', sim_index=0) 271 self.diff_data.set(param='theta', value=0.0, category='sim', sim_index=0) 272 self.diff_data.set(param='phi', value=2.0 * pi, category='sim', sim_index=0) 273 274 # The new MC sim parameter values. 275 tm = 0.5e-9 276 Da = 3e5 277 theta = (2 / 360.0) * 2.0 * pi 278 phi = 0.0 279 280 # Set the MC sim parameter values (overwriting the initial values). 281 self.diff_data.set(param='tm', value=tm, category='sim', sim_index=0) 282 self.diff_data.set(param='Da', value=Da, category='sim', sim_index=0) 283 self.diff_data.set(param='theta', value=theta, category='sim', sim_index=0) 284 self.diff_data.set(param='phi', value=phi, category='sim', sim_index=0) 285 286 # Test the set values. 287 self.assertEqual(self.diff_data.type, 'spheroid') 288 self.assertEqual(self.diff_data.tm_sim[0], tm) 289 self.assertEqual(self.diff_data.Da_sim[0], Da) 290 self.assertEqual(self.diff_data.theta_sim[0], theta) 291 self.assertEqual(self.diff_data.phi_sim[0], phi) 292 293 # Calculate the diffusion tensor objects. 294 Diso, Dpar, Dper, Dratio, Dpar_unit, tensor_diag, rotation, tensor = self.calc_spheroid_objects(tm, Da, theta, phi) 295 296 # Test the automatically created values. 297 self.assertEqual(self.diff_data.Diso_sim[0], Diso) 298 self.assertEqual(self.diff_data.Dpar_sim[0], Dpar) 299 self.assertEqual(self.diff_data.Dper_sim[0], Dper) 300 self.assertEqual(self.diff_data.Dratio_sim[0], Dratio) 301 302 # Test the vectors. 303 self.assertEqual(self.diff_data.Dpar_unit_sim[0].tostring(), Dpar_unit.tostring()) 304 305 # Test the matrices. 306 self.assertEqual(self.diff_data.tensor_diag_sim[0].tostring(), tensor_diag.tostring()) 307 self.assertEqual(self.diff_data.rotation_sim[0].tostring(), rotation.tostring()) 308 self.assertEqual(self.diff_data.tensor_sim[0].tostring(), tensor.tostring())
309 310
311 - def test_set_tm(self):
312 """Test the setting of the tm parameter. 313 314 The tm parameter will be set to 10 ns. The setting of tm should automatically create the 315 Diso parameter. 316 """ 317 318 # Set the diffusion type. 319 self.diff_data.set(param='type', value='sphere') 320 321 # Set the tm value to 10 ns. 322 self.diff_data.set(param='tm', value=1e-8) 323 324 # Test that the tm parameter has been set correctly. 325 self.assert_(hasattr(self.diff_data, 'tm')) 326 self.assertEqual(self.diff_data.tm, 1e-8) 327 328 # Test that the Diso parameter has been set correctly. 329 self.assert_(hasattr(self.diff_data, 'Diso')) 330 self.assertEqual(self.diff_data.Diso, 1/(6*1e-8))
331