Changeset 2a11d09 in sasview
- Timestamp:
- Jan 11, 2010 9:11:45 AM (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:
- f9a1279
- Parents:
- 793e52f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Invariant/invariant.py
r793e52f r2a11d09 66 66 """ 67 67 self.data = data 68 x_len = len(self.data.x) -1 69 #fitting range 70 self.qmin = self.data.x[0] 71 if self.qmin == 0: 72 self.qmin = Q_MINIMUM 73 74 self.qmax = self.data.x[x_len] 75 #Unsmeared q range 76 self._qmin_unsmeared = 0 77 self._qmax_unsmeared = self.data.x[x_len] 78 79 #bin for smear data 80 self._first_unsmeared_bin = 0 81 self._last_unsmeared_bin = x_len 82 83 # Identify the bin range for the unsmeared and smeared spaces 84 self.idx = (self.data.x >= self.qmin) & (self.data.x <= self.qmax) 85 self.idx_unsmeared = (self.data.x >= self._qmin_unsmeared) \ 86 & (self.data.x <= self._qmax_unsmeared) 87 68 69 # Set qmin as the lowest non-zero value 70 self.qmin = Q_MINIMUM 71 for q_value in self.data.x: 72 if q_value>0: 73 self.qmin = q_value 74 break 75 self.qmax = max(self.data.x) 76 88 77 #get the smear object of data 89 78 self.smearer = smear_selection( self.data ) 79 # Set the q-range information to allow smearing 80 self.set_fit_range() 90 81 91 82 def set_fit_range(self ,qmin=None, qmax=None): … … 101 92 self._qmax_unsmeared = self.qmax 102 93 103 self._first_unsmeared_bin = 0 104 self._last_unsmeared_bin = len(self.data.x)-1 105 106 if self.smearer!=None: 94 if self.smearer is not None: 107 95 self._first_unsmeared_bin, self._last_unsmeared_bin = self.smearer.get_bin_range(self.qmin, self.qmax) 108 96 self._qmin_unsmeared = self.data.x[self._first_unsmeared_bin] … … 119 107 @param power = a fixed, otherwise None 120 108 """ 121 power = power122 109 fx = numpy.zeros(len(self.data.x)) 123 one = numpy.ones(len(self.data.x)) 124 125 #define dy^2 126 try: 127 sigma = self.data.dy[self.idx_unsmeared ] 128 except: 129 print "The dy data for Invariant calculation should be prepared before get to FitFunctor.fit()..." 130 sigma = one[self.idx_unsmeared ] 131 sigma2 = sigma * sigma 110 111 # Uncertainty 112 if type(self.data.y)==numpy.ndarray and len(self.data.y)==len(self.data.x): 113 sigma = self.data.y 114 else: 115 sigma = numpy.ones(len(self.data.x)) 132 116 133 117 # Compute theory data f(x) 134 fx = self.data.y[self.idx_unsmeared ]/sigma118 fx[self.idx_unsmeared] = self.data.y[self.idx_unsmeared] 135 119 ## Smear theory data 136 120 if self.smearer is not None: 137 121 fx = self.smearer(fx, self._first_unsmeared_bin,self._last_unsmeared_bin) 138 122 123 fx[self.idx_unsmeared] = fx[self.idx_unsmeared]/sigma[self.idx_unsmeared] 124 139 125 ##power is given only for function = power_law 140 126 if power != None: 127 sigma2 = sigma * sigma 141 128 a = -(power) 142 b = (numpy.sum(fx /sigma) - a*numpy.sum(self.data.x[self.idx]/sigma2))/numpy.sum(numpy.ones(len(sigma2))/sigma2)129 b = (numpy.sum(fx[self.idx]/sigma[self.idx]) - a*numpy.sum(self.data.x[self.idx]/sigma2[self.idx]))/numpy.sum(numpy.ones(len(sigma2[self.idx]))/sigma2[self.idx]) 143 130 return a, b 144 131 else: 145 A = numpy.vstack([ self.data.x[self.idx]/sigma ,146 numpy.ones(len(self.data.x[self.idx]))/sigma ]).T132 A = numpy.vstack([ self.data.x[self.idx]/sigma[self.idx], 133 numpy.ones(len(self.data.x[self.idx]))/sigma[self.idx]]).T 147 134 148 a, b = numpy.linalg.lstsq(A, fx )[0]135 a, b = numpy.linalg.lstsq(A, fx[self.idx])[0] 149 136 return a, b 150 137 … … 165 152 Initialize variables 166 153 @param data: data must be of type DataLoader.Data1D 167 @param contrast: contrast value of type float168 @param pConst: Porod Constant of type float154 @param background: Background value. The data will be corrected before processing 155 @param scale: Scaling factor for I(q). The data will be corrected before processing 169 156 """ 170 157 # Background and scale should be private data member if the only way to … … 202 189 #Process only data that inherited from DataLoader.Data_info.Data1D 203 190 raise ValueError,"Data must be of type DataLoader.Data1D" 204 new_data = self._scale * data - self._background 205 try: 206 #All data should pass here. 207 new_data.dy = data.dy 208 new_data.dxl = data.dxl 209 new_data.dxw = data.dxw 210 except: 211 #in case... 212 new_data.dy = numpy.ones(len(data.x)) 213 new_data.dxl = numpy.zeros(len(data.x)) 214 new_data.dxw = numpy.zeros(len(data.x)) 191 192 new_data = (self._scale * data) - self._background 193 194 # Copy data that is not copied by the operations 195 #TODO: fix this in DataLoader 196 new_data.dxl = data.dxl 197 new_data.dxw = data.dxw 215 198 216 199 return new_data … … 410 393 note: if data doesn't contain dy assume dy= math.sqrt(data.y) 411 394 """ 412 if data is None:413 data = self.data414 415 395 if len(data.x) <= 1 or len(data.y) <= 1 or \ 416 396 len(self.data.x) != len(self.data.y): … … 548 528 qmin = self._data.x[0] 549 529 qmax = self._data.x[self._low_extrapolation_npts - 1] 550 try: 551 # fit the data with a model to get the appropriate parameters 552 a, b = self._fit(function=self._low_extrapolation_function, 553 qmin=qmin, 554 qmax=qmax, 555 power=self._low_extrapolation_power) 556 except: 557 #raise 558 return None 530 531 # Extrapolate the low-Q data 532 #TODO: this fit fails. Fix it. 533 a, b = self._fit(function=self._low_extrapolation_function, 534 qmin=qmin, 535 qmax=qmax, 536 power=self._low_extrapolation_power) 559 537 560 538 #q_start point
Note: See TracChangeset
for help on using the changeset viewer.