Package test_suite :: Package unit_tests :: Package _target_functions :: Module test_relax_fit
[hide private]
[frames] | no frames]

Source Code for Module test_suite.unit_tests._target_functions.test_relax_fit

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2014 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 numpy import array, float64, transpose, zeros 
 24  from unittest import TestCase 
 25   
 26  # relax module imports. 
 27  from dep_check import C_module_exp_fn 
 28  from status import Status; status = Status() 
 29  if C_module_exp_fn: 
 30      from target_functions.relax_fit import setup, func_exp, dfunc_exp, d2func_exp, jacobian_exp, jacobian_chi2_exp 
 31   
 32   
33 -class Test_relax_fit(TestCase):
34 """Unit tests for the target_functions.relax_fit relax C module.""" 35
36 - def __init__(self, methodName='runTest'):
37 """Skip the tests if the C modules are non-functional. 38 39 @keyword methodName: The name of the test. 40 @type methodName: str 41 """ 42 43 # Execute the base class method. 44 super(Test_relax_fit, self).__init__(methodName) 45 46 # Missing module. 47 if not C_module_exp_fn: 48 # Store in the status object. 49 status.skipped_tests.append([methodName, 'Relax curve-fitting C module', 'unit'])
50 51
52 - def setUp(self):
53 """Create a number of objects for the calculation and testing of the relaxation curve-fitting equations.""" 54 55 # The parameter scaling. 56 self.scaling_list = [1.0, 1000.0] 57 58 # The parameter values at the minimum. 59 self.I0 = 1000.0 60 self.R = 1.0 61 self.params = [self.R/self.scaling_list[0], self.I0/self.scaling_list[1]] 62 63 # The time points. 64 relax_times = [0.0, 1.0, 2.0, 3.0, 4.0] 65 66 # The intensities for the above I0 and R. 67 I = [1000.0, 367.879441171, 135.335283237, 49.7870683679, 18.3156388887] 68 69 # The intensity errors. 70 errors = [10.0, 10.0, 10.0, 10.0, 10.0] 71 72 # Setup the C module. 73 setup(num_params=2, num_times=len(relax_times), values=I, sd=errors, relax_times=relax_times, scaling_matrix=self.scaling_list)
74 75
76 - def test_func_exp(self):
77 """Unit test for the value returned by the func_exp() function at the minimum.""" 78 79 # Get the chi-squared value. 80 val = func_exp(self.params) 81 82 # Assert that the value must be 0.0. 83 self.assertAlmostEqual(val, 0.0)
84 85
86 - def test_dfunc_exp(self):
87 """Unit test for the gradient returned by the dfunc_exp() function at the minimum.""" 88 89 # Get the chi-squared gradient. 90 grad = dfunc_exp(self.params) 91 92 # Printout. 93 print("The gradient at the minimum is:\n%s" % grad) 94 95 # Assert that the elements must be 0.0. 96 self.assertAlmostEqual(grad[0], 0.0, 6) 97 self.assertAlmostEqual(grad[1], 0.0, 6)
98 99
101 """Unit test for the gradient returned by the dfunc_exp() function at a position away from the minimum. 102 103 This uses the data from test_suite/shared_data/curve_fitting/numeric_gradient/integrate.log. 104 """ 105 106 # The off-minimum parameter values. 107 I0 = 500.0 108 R = 2.0 109 params = [R/self.scaling_list[0], I0/self.scaling_list[1]] 110 111 # Get the chi-squared gradient. 112 grad = dfunc_exp(params) 113 114 # Printout. 115 print("The gradient at %s is:\n %s" % (params, grad)) 116 117 # Check that the gradient matches the numerically derived values. 118 self.assertAlmostEqual(grad[0], 456.36655522098829*self.scaling_list[0], 3) 119 self.assertAlmostEqual(grad[1], -10.8613338920982*self.scaling_list[1], 3)
120 121
122 - def test_d2func_exp(self):
123 """Unit test for the Hessian returned by the d2func_exp() function at the minimum. 124 125 This uses the data from test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.log. 126 """ 127 128 # Get the chi-squared Hessian. 129 hess = d2func_exp(self.params) 130 131 # Printout. 132 print("The Hessian at the minimum is:\n%s" % hess) 133 134 # Check that the Hessian matches the numerically derived values. 135 self.assertAlmostEqual(hess[0][0], 4.72548021e+03*self.scaling_list[0]**2, 3) 136 self.assertAlmostEqual(hess[0][1], -3.61489336e+00*self.scaling_list[0]*self.scaling_list[1], 3) 137 self.assertAlmostEqual(hess[1][0], -3.61489336e+00*self.scaling_list[0]*self.scaling_list[1], 3) 138 self.assertAlmostEqual(hess[1][1], 2.31293027e-02*self.scaling_list[1]**2, 3)
139 140
142 """Unit test for the Hessian returned by the d2func_exp() function at a position away from the minimum. 143 144 This uses the data from test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.log. 145 """ 146 147 # The off-minimum parameter values. 148 I0 = 500.0 149 R = 2.0 150 params = [R/self.scaling_list[0], I0/self.scaling_list[1]] 151 152 # Get the chi-squared Hessian. 153 hess = d2func_exp(params) 154 155 # Printout. 156 print("The Hessian at %s is:\n%s" % (params, hess)) 157 158 # Check that the Hessian matches the numerically derived values. 159 self.assertAlmostEqual(hess[0][0], -4.11964848e+02*self.scaling_list[0]**2, 3) 160 self.assertAlmostEqual(hess[0][1], 7.22678641e-01*self.scaling_list[0]*self.scaling_list[1], 3) 161 self.assertAlmostEqual(hess[1][0], 7.22678641e-01*self.scaling_list[0]*self.scaling_list[1], 3) 162 self.assertAlmostEqual(hess[1][1], 2.03731472e-02*self.scaling_list[1]**2, 3)
163 164
165 - def test_jacobian_exp(self):
166 """Unit test for the Jacobian returned by the jacobian_exp() function at the minimum. 167 168 This uses the data from test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.log. 169 """ 170 171 # Get the exponential curve Jacobian. 172 matrix = jacobian_exp(self.params) 173 174 # The real Jacobian. 175 real = [[ 0.00000000e+00, 1.00000000e+00], 176 [ -3.67879441e+02, 3.67879441e-01], 177 [ -2.70670566e+02, 1.35335283e-01], 178 [ -1.49361205e+02, 4.97870684e-02], 179 [ -7.32625556e+01, 1.83156389e-02]] 180 181 # Numpy conversion. 182 matrix = array(matrix) 183 real = transpose(array(real)) 184 185 # Printouts. 186 print("The Jacobian at the minimum is:\n%s" % matrix) 187 print("The real Jacobian at the minimum is:\n%s" % real) 188 189 # Check that the Jacobian matches the numerically derived values. 190 for i in range(len(matrix)): 191 for j in range(len(matrix[i])): 192 self.assertAlmostEqual(matrix[i, j], real[i, j], 3)
193 194
195 - def test_jacobian_chi2_exp(self):
196 """Unit test for the Jacobian returned by the jacobian_chi2_exp() function at the minimum. 197 198 This uses the data from test_suite/shared_data/curve_fitting/numeric_gradient/jacobian_chi2.log. 199 """ 200 201 # Get the exponential curve Jacobian. 202 matrix = jacobian_chi2_exp(self.params) 203 204 # The real Jacobian. 205 real = [[ 0.00000000e+00, 0.00000000e+00], 206 [ -3.25440806e-09, 3.25446070e-12], 207 [ 2.09660266e-09, -1.04831421e-12], 208 [ 1.07707223e-10, -3.58994022e-14], 209 [ -5.00778448e-11, 1.25201612e-14]] 210 211 # Numpy conversion. 212 matrix = array(matrix) 213 real = transpose(array(real)) 214 215 # Printouts. 216 print("The chi-squared Jacobian at the minimum is:\n%s" % matrix) 217 print("The real chi-squared Jacobian at the minimum is:\n%s" % real) 218 219 # Check that the Jacobian matches the numerically derived values. 220 for i in range(len(matrix)): 221 for j in range(len(matrix[i])): 222 self.assertAlmostEqual(matrix[i, j], real[i, j], 3)
223 224
226 """Unit test for the Jacobian returned by the jacobian_chi2_exp() function at a position away from the minimum. 227 228 This uses the data from test_suite/shared_data/curve_fitting/numeric_gradient/jacobian_chi2.log. 229 """ 230 231 # The off-minimum parameter values. 232 I0 = 500.0 233 R = 2.0 234 params = [R/self.scaling_list[0], I0/self.scaling_list[1]] 235 236 # Get the exponential curve Jacobian. 237 matrix = jacobian_chi2_exp(params) 238 239 # The real Jacobian. 240 real = [[ 0.00000000e+00, -1.00000000e+01], 241 [ 4.06292489e+02, -8.12584978e-01], 242 [ 4.62204173e+01, -4.62204173e-02], 243 [ 3.61013094e+00, -2.40675396e-03], 244 [ 2.43517791e-01, -1.21758895e-04]] 245 246 # Numpy conversion. 247 matrix = array(matrix) 248 real = transpose(array(real)) 249 250 # Printout. 251 print("The chi-squared Jacobian at %s is:\n%s" % (params, matrix)) 252 print("The real chi-squared Jacobian at the minimum is:\n%s" % real) 253 254 # Check that the Jacobian matches the numerically derived values. 255 for i in range(len(matrix)): 256 for j in range(len(matrix[i])): 257 self.assertAlmostEqual(matrix[i, j], real[i, j], 3)
258 259
261 """Unit test for the Jacobian returned by the jacobian_exp() function at a position away from the minimum. 262 263 This uses the data from test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.log. 264 """ 265 266 # The off-minimum parameter values. 267 I0 = 500.0 268 R = 2.0 269 params = [R/self.scaling_list[0], I0/self.scaling_list[1]] 270 271 # Get the exponential curve Jacobian. 272 matrix = jacobian_exp(params) 273 274 # The real Jacobian. 275 real = [[ 0.00000000e+00, 1.00000000e+00], 276 [ -6.76676416e+01, 1.35335283e-01], 277 [ -1.83156389e+01, 1.83156389e-02], 278 [ -3.71812826e+00, 2.47875218e-03], 279 [ -6.70925256e-01, 3.35462628e-04]] 280 281 # Numpy conversion. 282 matrix = array(matrix) 283 real = transpose(array(real)) 284 285 # Printout. 286 print("The Jacobian at %s is:\n%s" % (params, matrix)) 287 print("The real Jacobian at the minimum is:\n%s" % real) 288 289 # Check that the Jacobian matches the numerically derived values. 290 for i in range(len(matrix)): 291 for j in range(len(matrix[i])): 292 self.assertAlmostEqual(matrix[i, j], real[i, j], 3)
293