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

Source Code for Module test_suite.unit_tests._data.test_diff_tensor

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