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

Source Code for Module test_suite.unit_tests._target_functions.test_chi2

  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 numpy import array, float64, zeros 
 24  from unittest import TestCase 
 25   
 26  # relax module imports. 
 27  from target_functions.chi2 import chi2, dchi2, dchi2_element, d2chi2, d2chi2_element 
 28   
 29   
30 -class Test_chi2(TestCase):
31 """Unit tests for the target_functions.chi2 relax module.""" 32
33 - def setUp(self):
34 """Create a number of objects for the calculation and testing of the chi-squared equations.""" 35 36 # Some test data. 37 self.data = array([1.0, 1.5, 2.0, 2.5, 3.0], float64) 38 39 # Some 'back calculated' data. 40 self.back_calc = array([0.9, 1.45, 2.0, 2.55, 3.1], float64) 41 42 # A 'back calculated' gradient. 43 self.back_calc_grad = array([[ 0.1, 0.2, 0.3, 0.2, 0.1], 44 [-0.2, -0.1, 0.0, 0.1, 0.2]], float64) 45 46 # A 'back calculated' Hessian. 47 self.back_calc_hess = array([[[0.01, 0.005, 0.0, 0.005, 0.01], 48 [0.05, 0.01, 0.0, 0.01, 0.05]], 49 [[0.001, 0.0005, 0.0, 0.0005, 0.001], 50 [0.005, 0.001, 0.0, 0.001, 0.005]]], 51 float64) 52 53 # Some errors. 54 self.errors = array([0.1, 0.1, 0.1, 0.1, 0.1], float64)
55 56
57 - def tearDown(self):
58 """Delete all the data structures.""" 59 60 del self.data 61 del self.back_calc 62 del self.back_calc_grad 63 del self.back_calc_hess 64 del self.errors
65 66
67 - def test_chi2(self):
68 """Unit test for the value returned by the chi2 function. 69 70 The chi-squared value is 2.5 for the following data:: 71 72 data = | 1.0 1.5 2.0 2.5 3.0 |, 73 74 back_calc = | 0.9 1.45 2.0 2.55 3.1 |, 75 76 errors = | 0.1 0.1 0.1 0.1 0.1 |. 77 """ 78 79 # Get the chi-squared value. 80 val = chi2(self.data, self.back_calc, self.errors) 81 82 # Assert that the value must be 2.5. 83 self.assertEqual(val, 2.5) 84 85 # Delete the value. 86 del val
87 88
89 - def test_dchi2(self):
90 """Unit test for the chi-squared gradient created by the dchi2 function. 91 92 The chi-squared gradient is [0, 10] for the following data:: 93 94 data = | 1.0 1.5 2.0 2.5 3.0 |, 95 96 back_calc = | 0.9 1.45 2.0 2.55 3.1 |, 97 98 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 | 99 | -0.2 -0.1 0.0 0.1 0.2 |, 100 101 errors = | 0.1 0.1 0.1 0.1 0.1 |. 102 """ 103 104 # Calculate the gradient elements. 105 grad = zeros(2, float64) 106 dchi2(grad, 2, self.data, self.back_calc, self.back_calc_grad, self.errors) 107 108 # Assert, to a precision of 13 decimal places, that the gradient is [0, 10]. 109 self.assertAlmostEqual(grad[0], 0.0, places=13) 110 self.assertAlmostEqual(grad[1], 10.0, places=13) 111 112 # Delete the gradient data. 113 del grad
114 115
116 - def test_dchi2_element(self):
117 """Unit test for the chi-squared gradient created by the dchi2_element function. 118 119 The chi-squared gradient is [0, 10] for the following data:: 120 121 data = | 1.0 1.5 2.0 2.5 3.0 |, 122 123 back_calc = | 0.9 1.45 2.0 2.55 3.1 |, 124 125 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 | 126 | -0.2 -0.1 0.0 0.1 0.2 |, 127 128 errors = | 0.1 0.1 0.1 0.1 0.1 |. 129 """ 130 131 # Calculate the gradient elements. 132 grad0 = dchi2_element(self.data, self.back_calc, self.back_calc_grad[0], self.errors) 133 grad1 = dchi2_element(self.data, self.back_calc, self.back_calc_grad[1], self.errors) 134 135 # Assert, to a precision of 13 decimal places, that the gradient is [0, 10]. 136 self.assertAlmostEqual(grad0, 0.0, places=13) 137 self.assertAlmostEqual(grad1, 10.0, places=13) 138 139 # Delete the gradient data. 140 del grad0, grad1
141 142
143 - def test_d2chi2(self):
144 """Unit test for the chi-squared Hessian created by the d2chi2 function. 145 146 For the data:: 147 148 data = | 1.0 1.5 2.0 2.5 3.0 |, 149 150 back_calc = | 0.9 1.45 2.0 2.55 3.1 |, 151 152 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 | 153 |-0.2 -0.1 0.0 0.1 0.2 |, 154 155 back_calc_hess[0] = | 0.01 0.005 0.0 0.005 0.01 | 156 | 0.05 0.01 0.0 0.01 0.05 |, 157 158 back_calc_hess[1] = | 0.001 0.0005 0.0 0.0005 0.001 | 159 | 0.005 0.001 0.0 0.001 0.005 |, 160 161 errors = | 0.1 0.1 0.1 0.1 0.1 |, 162 163 the chi-squared hessian is:: 164 165 Hessian = | 38.0 0.0 | 166 | 0.0 20.0 |. 167 """ 168 169 # Calculate the Hessian. 170 hess = zeros((2, 2), float64) 171 d2chi2(hess, 2, self.data, self.back_calc, self.back_calc_grad, self.back_calc_hess, self.errors) 172 173 # Assert, to a precision of 13 decimal places, that the Hessian is [[38.0, 0], [0, 20]]. 174 self.assertAlmostEqual(hess[0, 0], 38.0, places=13) 175 self.assertAlmostEqual(hess[0, 1], 0.0, places=13) 176 self.assertAlmostEqual(hess[1, 0], 0.0, places=13) 177 self.assertAlmostEqual(hess[1, 1], 20.0, places=13) 178 179 # Delete the Hessian data. 180 del hess
181 182
183 - def test_d2chi2_element(self):
184 """Unit test for the chi-squared Hessian created by the d2chi2_element function. 185 186 For the data:: 187 188 data = | 1.0 1.5 2.0 2.5 3.0 |, 189 190 back_calc = | 0.9 1.45 2.0 2.55 3.1 |, 191 192 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 | 193 |-0.2 -0.1 0.0 0.1 0.2 |, 194 195 back_calc_hess[0] = | 0.01 0.005 0.0 0.005 0.01 | 196 | 0.05 0.01 0.0 0.01 0.05 |, 197 198 back_calc_hess[1] = | 0.001 0.0005 0.0 0.0005 0.001 | 199 | 0.005 0.001 0.0 0.001 0.005 |, 200 201 errors = | 0.1 0.1 0.1 0.1 0.1 |, 202 203 the chi-squared hessian is:: 204 205 Hessian = | 38.0 0.0 | 206 | 0.0 20.0 |. 207 """ 208 209 # Calculate the Hessian elements. 210 hess00 = d2chi2_element(self.data, self.back_calc, self.back_calc_grad[0], self.back_calc_grad[0], self.back_calc_hess[0, 0], self.errors) 211 hess01 = d2chi2_element(self.data, self.back_calc, self.back_calc_grad[0], self.back_calc_grad[1], self.back_calc_hess[0, 1], self.errors) 212 hess10 = d2chi2_element(self.data, self.back_calc, self.back_calc_grad[1], self.back_calc_grad[0], self.back_calc_hess[1, 0], self.errors) 213 hess11 = d2chi2_element(self.data, self.back_calc, self.back_calc_grad[1], self.back_calc_grad[1], self.back_calc_hess[1, 1], self.errors) 214 215 # Assert, to a precision of 13 decimal places, that the Hessian is [[38.0, 0], [0, 20]]. 216 self.assertAlmostEqual(hess00, 38.0, places=13) 217 self.assertAlmostEqual(hess01, 0.0, places=13) 218 self.assertAlmostEqual(hess10, 0.0, places=13) 219 self.assertAlmostEqual(hess11, 20.0, places=13) 220 221 # Delete the Hessian data. 222 del hess00, hess01, hess10, hess11
223