Changeset 7578961 in sasview
- Timestamp:
- Jul 3, 2008 3:33:46 PM (16 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:
- 357b79b
- Parents:
- a916ccc
- Location:
- pr_inversion
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
pr_inversion/invertor.py
rf168d02 r7578961 6 6 import numpy 7 7 import sys 8 import math, time 9 from scipy.linalg.basic import lstsq 8 10 9 11 def help(): … … 331 333 332 334 def lstsq(self, nfunc=5, nr=20): 333 #TODO: do this on the C side334 #335 # To make sure an array is contiguous:336 # blah = numpy.ascontiguousarray(blah_original)337 # ... before passing it to C338 335 """ 339 336 The problem is solved by posing the problem as Ax = b, … … 366 363 367 364 """ 368 #TODO: Allow for background by starting at n=0 (since the base function 369 # is zero for n=0). 370 import math, time 371 from scipy.linalg.basic import lstsq 365 # Note: To make sure an array is contiguous: 366 # blah = numpy.ascontiguousarray(blah_original) 367 # ... before passing it to C 372 368 373 369 if self.is_valid()<0: … … 395 391 t_0 = time.time() 396 392 self._get_matrix(nfunc, nq, a, b) 397 #print "elasped: ", time.time()-t_0398 393 399 394 # Perform the inversion (least square fit) … … 423 418 err = math.fabs(chi2/float(npts-nfunc)) * cov 424 419 except: 425 # We were not able to estimate the errors, 426 # returns an empty covariance matrix 427 print "lstsq:", sys.exc_value 428 print chi2 420 # We were not able to estimate the errors 421 # Return an empty error matrix 429 422 pass 430 423 … … 450 443 return self.out, self.cov 451 444 452 def lstsq_bck(self, nfunc=5, nr=20):453 #TODO: Allow for background by starting at n=0 (since the base function454 # is zero for n=0).455 import math456 from scipy.linalg.basic import lstsq457 458 if self.is_valid()<0:459 raise RuntimeError, "Invertor: invalid data; incompatible data lengths."460 461 self.nfunc = nfunc462 # a -- An M x N matrix.463 # b -- An M x nrhs matrix or M vector.464 npts = len(self.x)465 nq = nr466 sqrt_alpha = math.sqrt(math.fabs(self.alpha))467 if sqrt_alpha<0.0:468 nq = 0469 470 err_0 = numpy.zeros([nfunc, nfunc])471 c_0 = numpy.zeros(nfunc)472 nfunc_0 = nfunc473 nfunc += 1474 475 a = numpy.zeros([npts+nq, nfunc])476 b = numpy.zeros(npts+nq)477 err = numpy.zeros([nfunc, nfunc])478 479 # Construct the a matrix and b vector that represent the problem480 self._get_matrix(nfunc, nq, a, b)481 482 c, chi2, rank, n = lstsq(a, b)483 # Sanity check484 try:485 float(chi2)486 except:487 chi2 = -1.0488 self.chi2 = chi2489 490 inv_cov = numpy.zeros([nfunc,nfunc])491 # Get the covariance matrix, defined as inv_cov = a_transposed * a492 self._get_invcov_matrix(nfunc, nr, a, inv_cov)493 494 # Compute the reg term size for the output495 sum_sig, sum_reg = self._get_reg_size(nfunc, nr, a)496 497 if math.fabs(self.alpha)>0:498 new_alpha = sum_sig/(sum_reg/self.alpha)499 else:500 new_alpha = 0.0501 self.suggested_alpha = new_alpha502 503 try:504 cov = numpy.linalg.pinv(inv_cov)505 err = math.fabs(chi2/float(npts-nfunc)) * cov506 except:507 # We were not able to estimate the errors,508 # returns an empty covariance matrix509 print "lstsq:", sys.exc_value510 print chi2511 pass512 513 # Keep a copy of the last output514 515 print "BACKGROUND =", c[0]516 self.background = c[0]517 518 for i in range(nfunc_0):519 c_0[i] = c[i+1]520 for j in range(nfunc_0):521 err_0[i][j] = err[i+1][j+1]522 523 self.out = c_0524 self.cov = err_0525 526 return c_0, err_0527 528 445 def estimate_numterms(self, isquit_func=None): 529 446 """ 530 447 Returns a reasonable guess for the 531 448 number of terms 449 @param isquit_func: reference to thread function to call to 450 check whether the computation needs to 451 be stopped. 532 452 533 453 @return: number of terms, alpha, message … … 548 468 regularization constant alpha 549 469 470 @param nfunc: number of terms to use in the expansion. 550 471 @return: alpha, message, elapsed 551 472 … … 641 562 file.write("#chi2=%g\n" % self.chi2) 642 563 file.write("#elapsed=%g\n" % self.elapsed) 564 file.write("#qmin=%s\n" % str(self.q_min)) 565 file.write("#qmax=%s\n" % str(self.q_max)) 566 file.write("#slit_height=%g\n" % self.slit_height) 567 file.write("#slit_width=%g\n" % self.slit_width) 568 file.write("#background=%g\n" % self.background) 569 if self.has_bck==True: 570 file.write("#has_bck=1\n") 571 else: 572 file.write("#has_bck=0\n") 643 573 file.write("#alpha_estimate=%g\n" % self.suggested_alpha) 644 574 if not self.out==None: … … 692 622 toks = line.split('=') 693 623 self.suggested_alpha = float(toks[1]) 624 elif line.startswith('#qmin='): 625 toks = line.split('=') 626 try: 627 self.q_min = float(toks[1]) 628 except: 629 self.q_min = None 630 elif line.startswith('#qmax='): 631 toks = line.split('=') 632 try: 633 self.q_max = float(toks[1]) 634 except: 635 self.q_max = None 636 elif line.startswith('#slit_height='): 637 toks = line.split('=') 638 self.slit_height = float(toks[1]) 639 elif line.startswith('#slit_width='): 640 toks = line.split('=') 641 self.slit_width = float(toks[1]) 642 elif line.startswith('#background='): 643 toks = line.split('=') 644 self.background = float(toks[1]) 645 elif line.startswith('#has_bck='): 646 toks = line.split('=') 647 if int(toks[1])==1: 648 self.has_bck=True 649 else: 650 self.has_bck=False 694 651 695 652 # Now read in the parameters -
pr_inversion/num_term.py
rbd30f4a5 r7578961 8 8 self.nterm_min = 10 9 9 self.nterm_max = len(self.invertor.x) 10 if self.nterm_max>50: 11 self.nterm_max=50 10 12 self.isquit_func = None 11 13 … … 48 50 self.err_list = [] 49 51 self.alpha_list = [] 50 for k in range(self.nterm_min, self.nterm_max ):52 for k in range(self.nterm_min, self.nterm_max, 1): 51 53 if self.isquit_func != None: 52 54 self.isquit_func() … … 56 58 osc = inver.oscillations(inver.out) 57 59 err = inver.get_pos_err(inver.out, inver.cov) 58 60 if osc>10.0: 61 break 59 62 self.osc_list.append(osc) 60 63 self.err_list.append(err) -
pr_inversion/test/test_output.txt
r9a23253e r7578961 4 4 #chi2=836.797 5 5 #elapsed=0 6 #qmin=None 7 #qmax=None 8 #slit_height=0 9 #slit_width=0 10 #background=0 11 #has_bck=0 6 12 #alpha_estimate=4.58991e-006 7 13 #C_0=217.876478953+-2522.42314999
Note: See TracChangeset
for help on using the changeset viewer.