Changeset eca05c8 in sasview for pr_inversion/test
- Timestamp:
- Apr 30, 2008 4:29:36 PM (17 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 34ae302
- Parents:
- 47f695c9
- Location:
- pr_inversion/test
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pr_inversion/test/utest_invertor.py
r9e8dc22 reca05c8 8 8 9 9 10 import unittest, math, numpy 10 import unittest, math, numpy, pylab 11 11 from sans.pr.invertor import Invertor 12 12 … … 21 21 self.x_in = numpy.ones(self.ntest) 22 22 for i in range(self.ntest): 23 self.x_in[i] = 1.0* i23 self.x_in[i] = 1.0*(i+1) 24 24 25 25 … … 31 31 self.invertor.d_max = value 32 32 self.assertEqual(self.invertor.d_max, value) 33 34 def testset_alpha(self): 35 """ 36 Set and read alpha 37 """ 38 value = 15.0 39 self.invertor.alpha = value 40 self.assertEqual(self.invertor.alpha, value) 33 41 34 42 def testset_x_1(self): … … 113 121 self.assertEqual(self.invertor.test_no_data, None) 114 122 123 def test_inversion(self): 124 """ 125 Test an inversion for which we know the answer 126 """ 127 x, y, err = load("sphere_80.txt") 128 129 # Choose the right d_max... 130 self.invertor.d_max = 160.0 131 # Set a small alpha 132 self.invertor.alpha = 1e-7 133 # Set data 134 self.invertor.x = x 135 self.invertor.y = y 136 self.invertor.err = err 137 # Perform inversion 138 out, cov = self.invertor.invert(10) 139 # This is a very specific case 140 # We should make sure it always passes 141 self.assertTrue(self.invertor.chi2/len(x)<200.00) 142 143 # Check the computed P(r) with the theory 144 # for shpere of radius 80 145 x = pylab.arange(0.01, self.invertor.d_max, self.invertor.d_max/51.0) 146 y = numpy.zeros(len(x)) 147 dy = numpy.zeros(len(x)) 148 y_true = numpy.zeros(len(x)) 149 150 sum = 0.0 151 sum_true = 0.0 152 for i in range(len(x)): 153 #y[i] = self.invertor.pr(out, x[i]) 154 (y[i], dy[i]) = self.invertor.pr_err(out, cov, x[i]) 155 sum += y[i] 156 if x[i]<80.0: 157 y_true[i] = pr_theory(x[i], 80.0) 158 else: 159 y_true[i] = 0 160 sum_true += y_true[i] 161 162 y = y/sum*self.invertor.d_max/len(x) 163 dy = dy/sum*self.invertor.d_max/len(x) 164 y_true = y_true/sum_true*self.invertor.d_max/len(x) 165 166 chi2 = 0.0 167 for i in range(len(x)): 168 res = (y[i]-y_true[i])/dy[i] 169 chi2 += res*res 170 171 try: 172 self.assertTrue(chi2/51.0<10.0) 173 except: 174 print "chi2 =", chi2/51.0 175 raise 176 177 def test_q_zero(self): 178 """ 179 Test error condition where a point has q=0 180 """ 181 x, y, err = load("sphere_80.txt") 182 x[0] = 0.0 183 184 # Choose the right d_max... 185 self.invertor.d_max = 160.0 186 # Set a small alpha 187 self.invertor.alpha = 1e-7 188 # Set data 189 def doit(): 190 self.invertor.x = x 191 self.assertRaises(ValueError, doit) 192 193 194 def test_q_neg(self): 195 """ 196 Test error condition where a point has q<0 197 """ 198 x, y, err = load("sphere_80.txt") 199 x[0] = -0.2 200 201 # Choose the right d_max... 202 self.invertor.d_max = 160.0 203 # Set a small alpha 204 self.invertor.alpha = 1e-7 205 # Set data 206 self.invertor.x = x 207 self.invertor.y = y 208 self.invertor.err = err 209 # Perform inversion 210 out, cov = self.invertor.invert(4) 211 212 try: 213 self.assertTrue(self.invertor.chi2>0) 214 except: 215 print "Chi2 =", self.invertor.chi2 216 raise 217 218 def test_Iq_zero(self): 219 """ 220 Test error condition where a point has q<0 221 """ 222 x, y, err = load("sphere_80.txt") 223 y[0] = 0.0 224 225 # Choose the right d_max... 226 self.invertor.d_max = 160.0 227 # Set a small alpha 228 self.invertor.alpha = 1e-7 229 # Set data 230 self.invertor.x = x 231 self.invertor.y = y 232 self.invertor.err = err 233 # Perform inversion 234 out, cov = self.invertor.invert(4) 235 236 try: 237 self.assertTrue(self.invertor.chi2>0) 238 except: 239 print "Chi2 =", self.invertor.chi2 240 raise 241 242 243 244 def pr_theory(r, R): 245 """ 246 P(r) for a sphere 247 """ 248 if r<=2*R: 249 return 12.0* ((0.5*r/R)**2) * ((1.0-0.5*r/R)**2) * ( 2.0 + 0.5*r/R ) 250 else: 251 return 0.0 252 253 def load(path = "sphere_60_q0_2.txt"): 254 import numpy, math, sys 255 # Read the data from the data file 256 data_x = numpy.zeros(0) 257 data_y = numpy.zeros(0) 258 data_err = numpy.zeros(0) 259 if not path == None: 260 input_f = open(path,'r') 261 buff = input_f.read() 262 lines = buff.split('\n') 263 for line in lines: 264 try: 265 toks = line.split() 266 x = float(toks[0]) 267 y = float(toks[1]) 268 data_x = numpy.append(data_x, x) 269 data_y = numpy.append(data_y, y) 270 # Set the error of the first point to 5% 271 # to make theory look like data 272 scale = 0.1/math.sqrt(data_x[0]) 273 data_err = numpy.append(data_err, scale*math.sqrt(y)) 274 except: 275 pass 276 277 return data_x, data_y, data_err 278 115 279 if __name__ == '__main__': 116 280 unittest.main()
Note: See TracChangeset
for help on using the changeset viewer.