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) 2006 Gary Thompson                                            # 
  4  # Copyright (C) 2007-2008,2010 Edward d'Auvergne                              # 
  5  #                                                                             # 
  6  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  7  #                                                                             # 
  8  # This program is free software: you can redistribute it and/or modify        # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation, either version 3 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # This program is distributed in the hope that it will be useful,             # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Python module imports. 
 24  from numpy import array, float64, zeros 
 25  from unittest import TestCase 
 26   
 27  # relax module imports. 
 28  from target_functions.chi2 import chi2, dchi2, dchi2_element, d2chi2, d2chi2_element 
 29   
 30   
31 -class Test_chi2(TestCase):
32 """Unit tests for the target_functions.chi2 relax module.""" 33
34 - def setUp(self):
35 """Create a number of objects for the calculation and testing of the chi-squared equations.""" 36 37 # Some test data. 38 self.data = array([1.0, 1.5, 2.0, 2.5, 3.0], float64) 39 40 # Some 'back calculated' data. 41 self.back_calc = array([0.9, 1.45, 2.0, 2.55, 3.1], float64) 42 43 # A 'back calculated' gradient. 44 self.back_calc_grad = array([[ 0.1, 0.2, 0.3, 0.2, 0.1], 45 [-0.2, -0.1, 0.0, 0.1, 0.2]], float64) 46 47 # A 'back calculated' Hessian. 48 self.back_calc_hess = array([[[0.01, 0.005, 0.0, 0.005, 0.01], 49 [0.05, 0.01, 0.0, 0.01, 0.05]], 50 [[0.001, 0.0005, 0.0, 0.0005, 0.001], 51 [0.005, 0.001, 0.0, 0.001, 0.005]]], 52 float64) 53 54 # Some errors. 55 self.errors = array([0.1, 0.1, 0.1, 0.1, 0.1], float64)
56 57
58 - def tearDown(self):
59 """Delete all the data structures.""" 60 61 del self.data 62 del self.back_calc 63 del self.back_calc_grad 64 del self.back_calc_hess 65 del self.errors
66 67
68 - def test_chi2(self):
69 """Unit test for the value returned by the chi2 function. 70 71 The chi-squared value is 2.5 for the following data:: 72 73 data = | 1.0 1.5 2.0 2.5 3.0 |, 74 75 back_calc = | 0.9 1.45 2.0 2.55 3.1 |, 76 77 errors = | 0.1 0.1 0.1 0.1 0.1 |. 78 """ 79 80 # Get the chi-squared value. 81 val = chi2(self.data, self.back_calc, self.errors) 82 83 # Assert that the value must be 2.5. 84 self.assertEqual(val, 2.5) 85 86 # Delete the value. 87 del val
88 89
90 - def test_dchi2(self):
91 """Unit test for the chi-squared gradient created by the dchi2 function. 92 93 The chi-squared gradient is [0, 10] for the following data:: 94 95 data = | 1.0 1.5 2.0 2.5 3.0 |, 96 97 back_calc = | 0.9 1.45 2.0 2.55 3.1 |, 98 99 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 | 100 | -0.2 -0.1 0.0 0.1 0.2 |, 101 102 errors = | 0.1 0.1 0.1 0.1 0.1 |. 103 """ 104 105 # Calculate the gradient elements. 106 grad = zeros(2, float64) 107 dchi2(grad, 2, self.data, self.back_calc, self.back_calc_grad, self.errors) 108 109 # Assert, to a precision of 13 decimal places, that the gradient is [0, 10]. 110 self.assertAlmostEqual(grad[0], 0.0, places=13) 111 self.assertAlmostEqual(grad[1], 10.0, places=13) 112 113 # Delete the gradient data. 114 del grad
115 116
117 - def test_dchi2_element(self):
118 """Unit test for the chi-squared gradient created by the dchi2_element function. 119 120 The chi-squared gradient is [0, 10] for the following data:: 121 122 data = | 1.0 1.5 2.0 2.5 3.0 |, 123 124 back_calc = | 0.9 1.45 2.0 2.55 3.1 |, 125 126 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 | 127 | -0.2 -0.1 0.0 0.1 0.2 |, 128 129 errors = | 0.1 0.1 0.1 0.1 0.1 |. 130 """ 131 132 # Calculate the gradient elements. 133 grad0 = dchi2_element(self.data, self.back_calc, self.back_calc_grad[0], self.errors) 134 grad1 = dchi2_element(self.data, self.back_calc, self.back_calc_grad[1], self.errors) 135 136 # Assert, to a precision of 13 decimal places, that the gradient is [0, 10]. 137 self.assertAlmostEqual(grad0, 0.0, places=13) 138 self.assertAlmostEqual(grad1, 10.0, places=13) 139 140 # Delete the gradient data. 141 del grad0, grad1
142 143
144 - def test_d2chi2(self):
145 """Unit test for the chi-squared Hessian created by the d2chi2 function. 146 147 For the data:: 148 149 data = | 1.0 1.5 2.0 2.5 3.0 |, 150 151 back_calc = | 0.9 1.45 2.0 2.55 3.1 |, 152 153 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 | 154 |-0.2 -0.1 0.0 0.1 0.2 |, 155 156 back_calc_hess[0] = | 0.01 0.005 0.0 0.005 0.01 | 157 | 0.05 0.01 0.0 0.01 0.05 |, 158 159 back_calc_hess[1] = | 0.001 0.0005 0.0 0.0005 0.001 | 160 | 0.005 0.001 0.0 0.001 0.005 |, 161 162 errors = | 0.1 0.1 0.1 0.1 0.1 |, 163 164 the chi-squared hessian is:: 165 166 Hessian = | 38.0 0.0 | 167 | 0.0 20.0 |. 168 """ 169 170 # Calculate the Hessian. 171 hess = zeros((2, 2), float64) 172 d2chi2(hess, 2, self.data, self.back_calc, self.back_calc_grad, self.back_calc_hess, self.errors) 173 174 # Assert, to a precision of 13 decimal places, that the Hessian is [[38.0, 0], [0, 20]]. 175 self.assertAlmostEqual(hess[0, 0], 38.0, places=13) 176 self.assertAlmostEqual(hess[0, 1], 0.0, places=13) 177 self.assertAlmostEqual(hess[1, 0], 0.0, places=13) 178 self.assertAlmostEqual(hess[1, 1], 20.0, places=13) 179 180 # Delete the Hessian data. 181 del hess
182 183
184 - def test_d2chi2_element(self):
185 """Unit test for the chi-squared Hessian created by the d2chi2_element function. 186 187 For the data:: 188 189 data = | 1.0 1.5 2.0 2.5 3.0 |, 190 191 back_calc = | 0.9 1.45 2.0 2.55 3.1 |, 192 193 back_calc_grad = | 0.1 0.2 0.3 0.2 0.1 | 194 |-0.2 -0.1 0.0 0.1 0.2 |, 195 196 back_calc_hess[0] = | 0.01 0.005 0.0 0.005 0.01 | 197 | 0.05 0.01 0.0 0.01 0.05 |, 198 199 back_calc_hess[1] = | 0.001 0.0005 0.0 0.0005 0.001 | 200 | 0.005 0.001 0.0 0.001 0.005 |, 201 202 errors = | 0.1 0.1 0.1 0.1 0.1 |, 203 204 the chi-squared hessian is:: 205 206 Hessian = | 38.0 0.0 | 207 | 0.0 20.0 |. 208 """ 209 210 # Calculate the Hessian elements. 211 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) 212 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) 213 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) 214 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) 215 216 # Assert, to a precision of 13 decimal places, that the Hessian is [[38.0, 0], [0, 20]]. 217 self.assertAlmostEqual(hess00, 38.0, places=13) 218 self.assertAlmostEqual(hess01, 0.0, places=13) 219 self.assertAlmostEqual(hess10, 0.0, places=13) 220 self.assertAlmostEqual(hess11, 20.0, places=13) 221 222 # Delete the Hessian data. 223 del hess00, hess01, hess10, hess11
224