Changeset 472b11c in sasview
- Timestamp:
- Dec 17, 2009 4:18:31 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:
- 6311713
- Parents:
- 9b6497bb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Invariant/invariant.py
r9b6497bb r472b11c 115 115 & (self.data.x <= self._qmax_unsmeared) 116 116 117 def fit(self ):117 def fit(self, power =None): 118 118 """ 119 119 Fit data for y = ax + b return a and b 120 121 """ 122 120 @param power = a fixed, otherwise None 121 """ 122 power = power 123 123 fx = numpy.zeros(len(self.data.x)) 124 124 sigma = numpy.zeros(len(self.data.x)) … … 133 133 if self.smearer is not None: 134 134 fx = self.smearer(fx, self._first_unsmeared_bin,self._last_unsmeared_bin) 135 136 A = numpy.vstack([ self.data.x[self.idx]/sigma2, 137 numpy.ones(len(self.data.x[self.idx]))/sigma2]).T 138 139 a, b = numpy.linalg.lstsq(A, fx)[0] 140 return a, b 135 136 ##power is given only for function = power_law 137 if power != None: 138 a = -math.fabs(power) 139 b = (scipy.sum(fx) - a*scipy.sum(self.data.x[self.idx]/sigma2))/scipy.sum(numpy.ones(len(sigma2))/sigma2) 140 return a, b 141 else: 142 A = numpy.vstack([ self.data.x[self.idx]/sigma2, 143 numpy.ones(len(self.data.x[self.idx]))/sigma2]).T 144 145 a, b = numpy.linalg.lstsq(A, fx)[0] 146 return a, b 141 147 142 148 class InvariantCalculator(object): … … 214 220 for power_law will be the power value 215 221 """ 216 if function.__name__ == "guinier": 217 fit_x = numpy.array([x * x for x in self._data.x]) 218 222 fit_x = numpy.array([math.log(x) for x in self._data.x]) 223 if function.__name__ == "guinier": 219 224 qmin = qmin**2 220 225 qmax = qmax**2 … … 224 229 225 230 elif function.__name__ == "power_law": 226 if power is None: 227 fit_x = numpy.array([math.log(x) for x in self._data.x]) 228 229 qmin = math.log(qmin) 230 qmax = math.log(qmax) 231 qmin = math.log(qmin) 232 qmax = math.log(qmax) 231 233 fit_y = numpy.array([math.log(y) for y in self._data.y]) 232 234 fit_dy = numpy.array([y for y in self._data.y]) … … 235 237 else: 236 238 raise ValueError("Unknown function used to fit %s"%function.__name__) 237 238 if function.__name__ == "power_law" and power != None: 239 b = math.fabs(power) 240 fit_y = numpy.array([math.log(y) for y in self._data.y]) 241 fit_dy = numpy.array([y for y in self._data.y]) 242 fit_dy = numpy.array([dy for dy in self._data.dy])/fit_dy 243 sigma2 = fit_dy*fit_dy 244 a = scipy.sum(fit_y/sigma2) - scipy.sum(fit_x/sigma2*b)/scipy.sum(sigma2) 245 else: 246 fit_data = LoaderData1D(x=fit_x, y=fit_y, dy=fit_dy) 247 fit_data.dxl = self._data.dxl 248 fit_data.dxw = self._data.dxw 249 functor = FitFunctor(data=fit_data) 250 functor.set_fit_range(qmin=qmin, qmax=qmax) 251 b, a = functor.fit() 252 239 240 241 #else: 242 fit_data = LoaderData1D(x=fit_x, y=fit_y, dy=fit_dy) 243 fit_data.dxl = self._data.dxl 244 fit_data.dxw = self._data.dxw 245 functor = FitFunctor(data=fit_data) 246 functor.set_fit_range(qmin=qmin, qmax=qmax) 247 b, a = functor.fit(power=power) 248 253 249 254 250 if function.__name__ == "guinier": … … 261 257 262 258 if function.__name__ == "power_law": 263 if power == None: 264 b = -1 * b 259 b = -1 * b 265 260 if b <= 0: 266 261 raise ValueError("Power_law fit expected posive power, but got %s"%power) 267 262 # a is the scale of the guinier function 268 263 a = math.exp(a) 269 264 270 265 return a, b 271 266
Note: See TracChangeset
for help on using the changeset viewer.