Changeset 437a9f0 in sasview
- Timestamp:
- Dec 1, 2009 4:21:19 PM (15 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:
- 7a108dd
- Parents:
- c5607fa
- Location:
- Invariant
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Invariant/invariant.py
ref9ed58 r437a9f0 8 8 9 9 from DataLoader.data_info import Data1D as LoaderData1D 10 #from DataLoader.data_info import Data1D as LoaderData1D11 10 from DataLoader.qsmearing import smear_selection 12 11 … … 50 49 compute f(x) 51 50 """ 52 def __init__(self,data , function): 53 """ 54 @param function :the function used for computing residuals 55 @param Data: data used for computing residuals 56 """ 57 self.function = function 51 def __init__(self,data): 52 """ 53 Determine a and b given a linear equation y = ax + b 54 @param Data: data containing x and y such as y = ax + b 55 """ 58 56 self.data = data 59 57 x_len = len(self.data.x) -1 … … 111 109 112 110 """ 113 fx = self.data.y 111 # Compute theory data f(x) 112 fx = numpy.zeros(len(self.data.x)) 113 fx = self.data.y[self.idx_unsmeared ] 114 114 ## Smear theory data 115 115 if self.smearer is not None: 116 fx = self.smearer(fx, self._first_unsmeared_bin, 117 self._last_unsmeared_bin) 118 A = numpy.vstack([ self.data.x, numpy.ones(len(self.data.x))]).T 116 fx = self.smearer(fx, self._first_unsmeared_bin,self._last_unsmeared_bin) 117 118 A = numpy.vstack([ self.data.x[self.idx], 119 numpy.ones(len(self.data.x[self.idx]))]).T 119 120 120 121 a, b = numpy.linalg.lstsq(A, fx)[0] … … 223 224 """ 224 225 if function.__name__ == "guinier": 225 fit_x = self._data.x * self._data.x226 fit_x = numpy.array([x * x for x in self._data.x]) 226 227 qmin = qmin**2 227 228 qmax = qmax**2 228 fit_y = [math.log(y) for y in self._data.y]229 fit_y = numpy.array([math.log(y) for y in self._data.y]) 229 230 elif function.__name__ == "power_law": 230 fit_x = [math.log(x) for x in self._data.x]231 fit_x = numpy.array([math.log(x) for x in self._data.x]) 231 232 qmin = math.log(qmin) 232 233 qmax = math.log(qmax) 233 fit_y = [math.log(y) for y in self._data.y]234 fit_y = numpy.array([math.log(y) for y in self._data.y]) 234 235 else: 235 236 raise ValueError("Unknown function used to fit %s"%function.__name__) … … 239 240 fit_data.dxw = self._data.dxw 240 241 241 functor = FitFunctor(data=fit_data , function= function)242 functor = FitFunctor(data=fit_data) 242 243 functor.set_fit_range(qmin=qmin, qmax=qmax) 243 a, b= functor.fit()244 b, a = functor.fit() 244 245 245 246 if function.__name__ == "guinier": 246 247 # b is the radius value of the guinier function 247 print "b",b248 248 b = math.sqrt(-3 * b) 249 249 if function.__name__ == "power_law": 250 b = -1 * b 250 251 # a is the scale of the guinier function 251 252 a = math.exp(a) 252 return a, math.fabs(b)253 return a, b 253 254 254 255 def _get_qstar(self, data): … … 446 447 n = len(data.x) - 1 447 448 #compute the first delta 448 dx0 = (data.x[1] -data.x[0])/2449 dx0 = (data.x[1] + data.x[0])/2 449 450 #compute the last delta 450 451 dxn= data.x[n] - data.x[n-1] … … 499 500 n = len(data.x) - 1 500 501 #compute the first delta 501 dx0 = (data.x[1] -data.x[0])/2502 dx0 = (data.x[1] + data.x[0])/2 502 503 #compute the last delta 503 504 dxn = data.x[n] - data.x[n-1] … … 570 571 raise RuntimeError, "invariant must be greater than zero" 571 572 572 print "self._qstar",self._qstar573 573 # Compute intermediate constant 574 574 k = 1.e-8 * self._qstar/(2 * (math.pi * math.fabs(float(contrast)))**2) 575 575 #Check discriminant value 576 576 discrim = 1 - 4 * k 577 print "discrim",k,discrim577 578 578 # Compute volume fraction 579 579 if discrim < 0: 580 580 raise RuntimeError, "could not compute the volume fraction: negative discriminant" 581 elif discrim == 0:581 elif discrim == 0: 582 582 return 1/2 583 583 else: 584 volume1 = 0.5 * (1 - math.sqrt(discrim))585 volume2 = 0.5 * (1 + math.sqrt(discrim))584 volume1 = 0.5 * (1 - math.sqrt(discrim)) 585 volume2 = 0.5 * (1 + math.sqrt(discrim)) 586 586 587 587 if 0 <= volume1 and volume1 <= 1: … … 640 640 # Data boundaries for fiiting 641 641 qmin = self._data.x[0] 642 qmax = self._data.x[self._low_extrapolation_npts ]642 qmax = self._data.x[self._low_extrapolation_npts - 1] 643 643 644 644 try: … … 648 648 except: 649 649 raise 650 return None650 #return None 651 651 652 652 #create new Data1D to compute the invariant … … 655 655 num=INTEGRATION_NSTEPS, 656 656 endpoint=True) 657 new_y = self._low_extrapolation_function(x=new_x, 658 scale=a, 659 radius=b) 657 new_y = self._low_extrapolation_function(x=new_x, scale=a, radius=b) 660 658 dxl = None 661 659 dxw = None … … 664 662 dxl = dxl * self._data.dxl[0] 665 663 if self._data.dxw is not None: 666 d wl= numpy.ones(1, INTEGRATION_NSTEPS)667 d wl = dwl * self._data.dwl[0]664 dxw = numpy.ones(1, INTEGRATION_NSTEPS) 665 dxw = dxw * self._data.dxw[0] 668 666 669 667 data_min = LoaderData1D(x=new_x, y=new_y) … … 690 688 # Data boundaries for fiiting 691 689 x_len = len(self._data.x) - 1 692 qmin = self._data.x[x_len - self._high_extrapolation_npts]690 qmin = self._data.x[x_len - (self._high_extrapolation_npts - 1) ] 693 691 qmax = self._data.x[x_len] 694 692 … … 699 697 except: 700 698 raise 701 return None699 #return None 702 700 703 701 #create new Data1D to compute the invariant … … 706 704 num=INTEGRATION_NSTEPS, 707 705 endpoint=True) 708 new_y = self._high_extrapolation_function(x=new_x,709 scale=a,710 power=b)706 707 new_y = self._high_extrapolation_function(x=new_x, scale=a, power=b) 708 711 709 dxl = None 712 710 dxw = None … … 715 713 dxl = dxl * self._data.dxl[0] 716 714 if self._data.dxw is not None: 717 d wl= numpy.ones(1, INTEGRATION_NSTEPS)718 d wl = dwl * self._data.dwl[0]715 dxw = numpy.ones(1, INTEGRATION_NSTEPS) 716 dxw = dxw * self._data.dxw[0] 719 717 720 718 data_max = LoaderData1D(x=new_x, y=new_y) … … 746 744 dV = 0.5 * (4*k* dq_star) /(2* math.sqrt(1-k* q_star)) 747 745 748 for k = 10^( 8)*q_star/(2*(pi*|contrast|)**2)746 for k = 10^(-8)*q_star/(2*(pi*|contrast|)**2) 749 747 750 748 q_star: the invariant value including extrapolated value if existing … … 760 758 raise ValueError, "invariant must be greater than zero" 761 759 762 k = 1.e-8 * self._qstar /(2 *(math.pi* math.fabs(float(contrast)))**2)760 k = 1.e-8 * self._qstar /(2 * (math.pi* math.fabs(float(contrast)))**2) 763 761 #check value inside the sqrt function 764 762 value = 1 - k * self._qstar … … 766 764 raise ValueError, "Cannot compute incertainty on volume" 767 765 # Compute uncertainty 768 uncertainty = (0.5 * 4 * k * self._qstar_err)/(2 * math.sqrt(1 - k * self._qstar))766 uncertainty = (0.5 * 4 * k * self._qstar_err)/(2 * math.sqrt(1 - k * self._qstar)) 769 767 770 768 return volume, math.fabs(uncertainty) -
Invariant/test/utest_use_cases.py
ref9ed58 r437a9f0 93 93 # parameters. For instance, you might not want to allow 'high' and 'guinier'. 94 94 # The power parameter (not shown below) should default to 4. 95 inv.set_extrapolation(range='low', npts= 20, function='guinier')95 inv.set_extrapolation(range='low', npts=10, function='guinier') 96 96 97 97 # The version of the call without error … … 108 108 109 109 # Test results 110 self.assertAlmostEquals(qstar, 7.49e-5 ,2)110 self.assertAlmostEquals(qstar, 7.49e-5) 111 111 self.assertAlmostEquals(v, 0.005648401) 112 112 self.assertAlmostEquals(s, 9.42e+2,2) … … 120 120 121 121 # Set the extrapolation parameters for the high-Q range 122 inv.set_extrapolation(range='high', npts= 20, function='power_law', power=4)122 inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 123 123 124 124 # The version of the call without error … … 146 146 147 147 # Set the extrapolation parameters for the low- and high-Q ranges 148 inv.set_extrapolation(range='low', npts= 5, function='guinier')149 inv.set_extrapolation(range='high', npts= 5, function='power_law', power=4)148 inv.set_extrapolation(range='low', npts=10, function='guinier') 149 inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 150 150 151 151 # The version of the call without error … … 161 161 162 162 # Test results 163 self.assertAlmostEquals(qstar, 7. 90069e-5,2)163 self.assertAlmostEquals(qstar, 7.88981e-5,2) 164 164 self.assertAlmostEquals(v, 0.005952674) 165 165 self.assertAlmostEquals(s, 9.42e+2,2) … … 213 213 inv = invariant.InvariantCalculator(data=self.data_slit_smear) 214 214 # Set the extrapolation parameters for the low-Q range 215 inv.set_extrapolation(range='low', npts= 20, function='guinier')215 inv.set_extrapolation(range='low', npts=10, function='guinier') 216 216 # The version of the call without error 217 217 # At this point, we could still compute Q* without extrapolation by calling … … 237 237 238 238 # Set the extrapolation parameters for the high-Q range 239 inv.set_extrapolation(range='high', npts= 20, function='power_law', power=4)239 inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 240 240 241 241 # The version of the call without error … … 263 263 264 264 # Set the extrapolation parameters for the low- and high-Q ranges 265 inv.set_extrapolation(range='low', npts= 20, function='guinier')266 inv.set_extrapolation(range='high', npts= 20, function='power_law', power=4)265 inv.set_extrapolation(range='low', npts=10, function='guinier') 266 inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 267 267 268 268 # The version of the call without error … … 351 351 inv = invariant.InvariantCalculator(data=self.data_q_smear) 352 352 # Set the extrapolation parameters for the high-Q range 353 inv.set_extrapolation(range='high', npts= 20, function='power_law', power=4)353 inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 354 354 # The version of the call without error 355 355 qstar = inv.get_qstar(extrapolation='high') … … 372 372 inv = invariant.InvariantCalculator(data=self.data_q_smear) 373 373 # Set the extrapolation parameters for the low- and high-Q ranges 374 inv.set_extrapolation(range='low', npts= 20, function='guinier')375 inv.set_extrapolation(range='high', npts= 20, function='power_law', power=4)374 inv.set_extrapolation(range='low', npts=10, function='guinier') 375 inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 376 376 # The version of the call without error 377 377 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' … … 384 384 385 385 # Test results 386 self.assertAlmostEquals(qstar, 0.0021 57677)386 self.assertAlmostEquals(qstar, 0.0021621) 387 387 self.assertAlmostEquals(v, 0.202846825) 388 388 self.assertAlmostEquals(s, 9.42e+2,2)
Note: See TracChangeset
for help on using the changeset viewer.