Changeset b00b487 in sasview for pr_inversion
- Timestamp:
- Jun 13, 2008 1:08:35 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:
- b659551
- Parents:
- 96f13a9
- Location:
- pr_inversion
- Files:
-
- 6 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
pr_inversion/invertor.py
re39640f rb00b487 92 92 return self.set_y(value) 93 93 elif name=='err': 94 return self.set_err(value) 94 value2 = abs(value) 95 return self.set_err(value2) 95 96 elif name=='d_max': 96 97 return self.set_dmax(value) … … 319 320 @param nfunc: number of base functions to use. 320 321 @param nr: number of r points to evaluate the 2nd derivative at for the reg. term. 322 323 If the result does not allow us to compute the covariance matrix, 324 a matrix filled with zeros will be returned. 325 321 326 """ 322 327 import math 323 328 from scipy.linalg.basic import lstsq 329 330 if self.is_valid()<0: 331 raise RuntimeError, "Invertor: invalid data; incompatible data lengths." 324 332 325 333 self.nfunc = nfunc … … 328 336 npts = len(self.x) 329 337 nq = nr 330 sqrt_alpha = math.sqrt(self.alpha) 338 sqrt_alpha = math.sqrt(math.fabs(self.alpha)) 339 if sqrt_alpha<0.0: 340 nq = 0 331 341 332 342 a = numpy.zeros([npts+nq, nfunc]) 333 343 b = numpy.zeros(npts+nq) 334 err = numpy.zeros( nfunc)344 err = numpy.zeros([nfunc, nfunc]) 335 345 336 346 for j in range(nfunc): … … 350 360 351 361 c, chi2, rank, n = lstsq(a, b) 362 # Sanity check 363 try: 364 float(chi2) 365 except: 366 chi2 = -1.0 352 367 self.chi2 = chi2 353 368 … … 358 373 inv_cov[i][j] = 0.0 359 374 for k in range(npts+nr): 360 if self._accept_q(self.x[i]):361 inv_cov[i][j]= at[i][k]*a[k][j]375 #if self._accept_q(self.x[i]): 376 inv_cov[i][j] += at[i][k]*a[k][j] 362 377 363 378 # Compute the reg term size for the output … … 378 393 379 394 try: 380 err = math.fabs(chi2/(npts-nfunc))* inv_cov 395 cov = numpy.linalg.pinv(inv_cov) 396 err = math.fabs(chi2/float(npts-nfunc)) * cov 381 397 except: 382 print "Error estimating uncertainties" 398 # We were not able to estimate the errors, 399 # returns an empty covariance matrix 400 print "lstsq:", sys.exc_value 401 print chi2 402 pass 383 403 384 404 # Keep a copy of the last output -
pr_inversion/test/test_output.txt
rf71287f4 rb00b487 2 2 #nfunc=10 3 3 #alpha=0.0007 4 #chi2= 16654.14 #chi2=836.797 5 5 #elapsed=0 6 #alpha_estimate= 2.234317 #C_0= 1254.55002508+-12.08894644468 #C_1= 386.59602793+-62.48519614589 #C_2= 5.10066421813+-235.8458692410 #C_3= 48.454288318+-1217.9468767611 #C_4=0. 4174373785+-213577.44997212 #C_5= 17.3551976035+-2673.3627435213 #C_6=-0. 228001082457+-692.66175993614 #C_7= 1.95934734382+-332.13475429115 #C_8=-0. 885447495+-200.86205248716 #C_9= 0.401866788245+-137.0646806096 #alpha_estimate=4.58991e-006 7 #C_0=217.876478953+-0.0394946442786 8 #C_1=0.30164617344+-0.407412922728 9 #C_2=7.15694574584+-2.73726481828 10 #C_3=-0.79690812244+-5.7183647083 11 #C_4=0.948333847403+-16.2086432642 12 #C_5=-0.751612279429+-28.2108893047 13 #C_6=-0.0796650783501+-52.0568306543 14 #C_7=-0.518109559543+-88.6068891309 15 #C_8=-0.129910831984+-141.7178308 16 #C_9=-0.258134624641+-215.767430098 17 17 <r> <Pr> <dPr> 18 18 0 0 0 19 1.6 2 36.371 235.67820 3.2 9 43.103 930.74421 4.8 2 113.12 2049.8222 6.4 3 734.86 3535.6123 8 57 92.64 5311.3724 9.6 8 267.09 7284.0425 11.2 11 135.7 9348.126 12.8 14 373.5 11389.727 14.4 1 7953.5 13291.428 16 2 1847.7 14936.229 17.6 2 6027.5 16212.730 19.2 3 0464.2 17019.131 20.8 3 5129.9 17267.832 22.4 39997.6 16889.233 24 45041.7 15836.134 25.6 5 0238.2 14088.735 27.2 55565.1 11662.936 28.8 61002 8635.9237 30.4 66530.4 5281.7338 32 72133.5 3272.6439 33.6 77796 6067.2640 35.2 83503.6 10814.741 36.8 89243.2 16028.242 38.4 95001.9 21350.243 40 1 00767 26561.244 41.6 1 06527 3146845 43.2 1 12267 35887.346 44.8 1 17975 39646.447 46.4 1 23636 42586.248 48 1 29233 44565.849 49.6 1 34751 45467.650 51.2 1 40171 45201.551 52.8 1 45473 43709.852 54.4 150638 40971.153 56 155645 37005.554 57.6 160473 31882.955 59.2 165101 25742.156 60.8 169508 18857.357 62.4 173675 11970.158 64 177583 8265.1559 65.6 181215 12750.960 67.2 184556 21038.761 68.8 187595 30104.662 70.4 190321 39131.563 72 192727 47705.764 73.6 194808 55513.265 75.2 196563 62282.766 76.8 197991 67775.367 78.4 199093 71784.568 80 199873 74140.469 81.6 200335 74714.170 83.2 200484 73422.471 84.8 200325 70232.572 86.4 199862 6516673 88 199101 58304.974 89.6 198044 49801.375 91.2 196695 39903.576 92.8 195056 29050.877 94.4 193129 18346.578 96 190915 12397.779 97.6 188414 18724.880 99.2 185629 30770.181 100.8 182559 4383482 102.4 179206 56683.483 104 175572 68726.984 105.6 171661 79534.185 107.2 167475 88745.886 108.8 163019 96054.487 110.4 158298 10120388 112 153320 10398789 113.6 148091 10426290 115.2 142620 10194791 116.8 136919 97026.992 118.4 130998 89560.493 120 124872 79680.994 121.6 118558 67605.795 123.2 112075 53655.896 124.8 105448 38339.497 126.4 98705.1 2282198 128 91877.8 13090.499 129.6 85003.8 22520.4100 131.2 78125.5 39345.4101 132.8 71289.5 56946.4102 134.4 64546.5 73969.2103 136 57949.9 89741.1104 137.6 51554.5 103751105 139.2 45414.9 115564106 140.8 39583.1 124808107 142.4 34106.6 131176108 144 2 9025.7 134426109 145.6 2 4371.3 134390110 147.2 20162.8 130981111 148.8 16 406 124194112 150.4 1 3091.6 114114113 152 1 0194.5 100912114 153.6 7673.45 84850.4115 155.2 5471.36 66272.7116 156.8 3516.48 45601.3117 158.4 1724.23 23326.719 1.6 22.981 20.5137 20 3.2 91.986 78.6612 21 4.8 207.187 164.741 22 6.4 368.819 264.129 23 8 577.117 359.427 24 9.6 832.224 433.162 25 11.2 1134.11 470.89 26 12.8 1482.46 464.814 27 14.4 1876.63 419.086 28 16 2315.57 360.693 29 17.6 2797.81 353.245 30 19.2 3321.46 448.357 31 20.8 3884.25 611.481 32 22.4 4483.6 786.464 33 24 5116.72 934.559 34 25.6 5780.7 1030.78 35 27.2 6472.66 1062.21 36 28.8 7189.82 1030.28 37 30.4 7929.59 955.681 38 32 8689.7 883.97 39 33.6 9468.17 878.871 40 35.2 10263.4 977.553 41 36.8 11073.9 1152 42 38.4 11898.7 1342.59 43 40 12737 1498.3 44 41.6 13587.7 1587.08 45 43.2 14450.2 1596.95 46 44.8 15323.5 1537.99 47 46.4 16206.5 1445.58 48 48 17098 1379.39 49 49.6 17996.3 1401.86 50 51.2 18899.8 1532.06 51 52.8 19806.5 1728.86 52 54.4 20714.3 1927.37 53 56 21620.8 2073.39 54 57.6 22524 2135.21 55 59.2 23421.6 2106.97 56 60.8 24311.7 2011.28 57 62.4 25192.3 1900.75 58 64 26061.8 1848.67 59 65.6 26918.7 1912.83 60 67.2 27761.7 2089.96 61 68.8 28589.6 2319.48 62 70.4 29401.3 2527.71 63 72 30195.6 2658.76 64 73.6 30971.4 2685.17 65 75.2 31727.2 2611.68 66 76.8 32461.5 2478.05 67 78.4 33172.7 2357.19 68 80 33858.8 2333.81 69 81.6 34517.8 2453.43 70 83.2 35147.6 2684.61 71 84.8 35745.9 2945.13 72 86.4 36310.5 3152.18 73 88 36839.5 3249.59 74 89.6 37330.9 3217.04 75 91.2 37783.1 3074.61 76 92.8 38194.6 2886.09 77 94.4 38564.3 2752.58 78 96 38891.1 2773.03 79 97.6 39174.1 2973.02 80 99.2 39412.6 3281.62 81 100.8 39605.8 3587.59 82 102.4 39752.6 3795.95 83 104 39851.9 3850.56 84 105.6 39902.4 3741.32 85 107.2 39902.5 3510.03 86 108.8 39850.1 3255.58 87 110.4 39743.2 3120.25 88 112 39579.6 3218.61 89 113.6 39356.7 3539.26 90 115.2 39072.3 3954.92 91 116.8 38724.1 4317.73 92 118.4 38310.3 4517.08 93 120 37829 4494.86 94 121.6 37278.9 4251.44 95 123.2 36659 3856.58 96 124.8 35968.4 3465.78 97 126.4 35206.6 3306.22 98 128 34373 3535.97 99 129.6 33466.9 4074.35 100 131.2 32487.5 4693.64 101 132.8 31433.4 5186.14 102 134.4 30303.1 5411.6 103 136 29094.1 5295.75 104 137.6 27803.8 4830.98 105 139.2 26428.9 4092.02 106 140.8 24965.9 3287.78 107 142.4 23411.1 2858.06 108 144 21761.3 3246.41 109 145.6 20013.4 4237.67 110 147.2 18165.4 5345.59 111 148.8 16216.4 6256.27 112 150.4 14166.9 6786.26 113 152 12019 6829.04 114 153.6 9776.61 6338.91 115 155.2 7445.34 5326.31 116 156.8 5032.57 3854 117 158.4 2547.22 2030.4 -
pr_inversion/test/utest_invertor.py
rffca8f2 rb00b487 224 224 self.invertor.d_max = 160.0 225 225 # Set a small alpha 226 self.invertor.alpha = .00 07226 self.invertor.alpha = .005 227 227 # Set data 228 228 self.invertor.x = x … … 399 399 self.assertEqual(self.invertor.d_max, 160.0) 400 400 self.assertEqual(self.invertor.alpha, 0.0007) 401 self.assertEqual(self.invertor.chi2, 16654.1)402 self.assertAlmostEqual(self.invertor.pr(self.invertor.out, 10.0), 8948.22689927, 4)401 self.assertEqual(self.invertor.chi2, 836.797) 402 self.assertAlmostEqual(self.invertor.pr(self.invertor.out, 10.0), 903.31577041, 4) 403 403 404 404 def test_qmin(self): … … 416 416 self.invertor.q_max = None 417 417 self.assertEqual(self.invertor.q_max, None) 418 419 class TestErrorConditions(unittest.TestCase): 420 421 def setUp(self): 422 self.invertor = Invertor() 423 self.invertor.d_max = 100.0 424 425 # Test array 426 self.ntest = 5 427 self.x_in = numpy.ones(self.ntest) 428 for i in range(self.ntest): 429 self.x_in[i] = 1.0*(i+1) 430 431 def test_negative_errs(self): 432 """ 433 Test an inversion for which we know the answer 434 """ 435 x, y, err = load("data_error_1.txt") 436 437 # Choose the right d_max... 438 self.invertor.d_max = 160.0 439 # Set a small alpha 440 self.invertor.alpha = .0007 441 # Set data 442 self.invertor.x = x 443 self.invertor.y = y 444 self.invertor.err = err 445 # Perform inversion 446 447 out, cov = self.invertor.lstsq(10) 448 449 def test_zero_errs(self): 450 """ 451 Have zero as an error should raise an exception 452 """ 453 x, y, err = load("data_error_2.txt") 454 455 # Set data 456 self.invertor.x = x 457 self.invertor.y = y 458 self.invertor.err = err 459 # Perform inversion 460 self.assertRaises(ValueError, self.invertor.invert, 10) 461 462 463 def test_invalid(self): 464 """ 465 Test an inversion for which we know the answer 466 """ 467 x, y, err = load("data_error_1.txt") 468 469 # Set data 470 self.invertor.x = x 471 self.invertor.y = y 472 err = numpy.zeros(len(x)-1) 473 self.invertor.err = err 474 # Perform inversion 475 self.assertRaises(RuntimeError, self.invertor.invert, 10) 476 477 478 def test_zero_q(self): 479 """ 480 One of the q-values is zero. 481 An exception should be raised. 482 """ 483 x, y, err = load("data_error_3.txt") 484 485 # Set data 486 self.assertRaises(ValueError, self.invertor.__setattr__, 'x', x) 487 488 def test_zero_Iq(self): 489 """ 490 One of the I(q) points has a value of zero 491 Should not complain or crash. 492 """ 493 x, y, err = load("data_error_4.txt") 494 495 # Set data 496 self.invertor.x = x 497 self.invertor.y = y 498 self.invertor.err = err 499 # Perform inversion 500 out, cov = self.invertor.lstsq(10) 501 502 def test_negative_q(self): 503 """ 504 One q value is negative. 505 Makes not sense, but should not complain or crash. 506 """ 507 x, y, err = load("data_error_5.txt") 508 509 # Set data 510 self.invertor.x = x 511 self.invertor.y = y 512 self.invertor.err = err 513 # Perform inversion 514 out, cov = self.invertor.lstsq(10) 515 516 def test_negative_Iq(self): 517 """ 518 One I(q) value is negative. 519 Makes not sense, but should not complain or crash. 520 """ 521 x, y, err = load("data_error_6.txt") 522 523 # Set data 524 self.invertor.x = x 525 self.invertor.y = y 526 self.invertor.err = err 527 # Perform inversion 528 out, cov = self.invertor.lstsq(10) 529 530 def test_nodata(self): 531 """ 532 No data was loaded. Should not complain. 533 """ 534 out, cov = self.invertor.lstsq(10) 535 418 536 419 537 … … 433 551 data_y = numpy.zeros(0) 434 552 data_err = numpy.zeros(0) 553 scale = None 435 554 if not path == None: 436 555 input_f = open(path,'r') … … 442 561 x = float(toks[0]) 443 562 y = float(toks[1]) 563 if len(toks)>2: 564 err = float(toks[2]) 565 else: 566 if scale==None: 567 scale = 0.15*math.sqrt(y) 568 err = scale*math.sqrt(y) 444 569 data_x = numpy.append(data_x, x) 445 570 data_y = numpy.append(data_y, y) 446 # Set the error of the first point to 5% 447 # to make theory look like data 448 scale = 0.1/math.sqrt(data_x[0]) 449 data_err = numpy.append(data_err, scale*math.sqrt(y)) 571 data_err = numpy.append(data_err, err) 450 572 except: 451 573 pass
Note: See TracChangeset
for help on using the changeset viewer.