1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22   
 23  from numpy import array, transpose 
 24  from unittest import TestCase 
 25   
 26   
 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   
 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           
 44          super(Test_relax_fit, self).__init__(methodName) 
 45   
 46           
 47          if not C_module_exp_fn: 
 48               
 49              status.skipped_tests.append([methodName, 'Relax curve-fitting C module', 'unit']) 
  50   
 51   
 53          """Create a number of objects for the calculation and testing of the relaxation curve-fitting equations.""" 
 54   
 55           
 56          self.scaling_list = [1.0, 1000.0] 
 57   
 58           
 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           
 64          relax_times = [0.0, 1.0, 2.0, 3.0, 4.0] 
 65   
 66           
 67          I = [1000.0, 367.879441171, 135.335283237, 49.7870683679, 18.3156388887] 
 68   
 69           
 70          errors = [10.0, 10.0, 10.0, 10.0, 10.0] 
 71   
 72           
 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   
 77          """Unit test for the value returned by the func_exp() function at the minimum.""" 
 78   
 79           
 80          val = func_exp(self.params) 
 81   
 82           
 83          self.assertAlmostEqual(val, 0.0) 
  84   
 85   
 87          """Unit test for the gradient returned by the dfunc_exp() function at the minimum.""" 
 88   
 89           
 90          grad = dfunc_exp(self.params) 
 91   
 92           
 93          print("The gradient at the minimum is:\n%s" % grad) 
 94   
 95           
 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           
107          I0 = 500.0 
108          R = 2.0 
109          params = [R/self.scaling_list[0], I0/self.scaling_list[1]] 
110   
111           
112          grad = dfunc_exp(params) 
113   
114           
115          print("The gradient at %s is:\n    %s" % (params, grad)) 
116   
117           
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   
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           
129          hess = d2func_exp(self.params) 
130   
131           
132          print("The Hessian at the minimum is:\n%s" % hess) 
133   
134           
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           
148          I0 = 500.0 
149          R = 2.0 
150          params = [R/self.scaling_list[0], I0/self.scaling_list[1]] 
151   
152           
153          hess = d2func_exp(params) 
154   
155           
156          print("The Hessian at %s is:\n%s" % (params, hess)) 
157   
158           
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   
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           
172          matrix = jacobian_exp(self.params) 
173   
174           
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           
182          matrix = array(matrix) 
183          real = transpose(array(real)) 
184   
185           
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           
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   
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           
202          matrix = jacobian_chi2_exp(self.params) 
203   
204           
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           
212          matrix = array(matrix) 
213          real = transpose(array(real)) 
214   
215           
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           
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           
232          I0 = 500.0 
233          R = 2.0 
234          params = [R/self.scaling_list[0], I0/self.scaling_list[1]] 
235   
236           
237          matrix = jacobian_chi2_exp(params) 
238   
239           
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           
247          matrix = array(matrix) 
248          real = transpose(array(real)) 
249   
250           
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           
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           
267          I0 = 500.0 
268          R = 2.0 
269          params = [R/self.scaling_list[0], I0/self.scaling_list[1]] 
270   
271           
272          matrix = jacobian_exp(params) 
273   
274           
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           
282          matrix = array(matrix) 
283          real = transpose(array(real)) 
284   
285           
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           
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