Changeset b6666d4 in sasview
- Timestamp:
- Nov 24, 2009 1:57:04 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:
- 669aaf9
- Parents:
- 5d91e9c
- Location:
- Invariant
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Invariant/invariant.py
r75047cf rb6666d4 10 10 from DataLoader.qsmearing import smear_selection 11 11 12 INFINITY = 10 13 MINIMUM = 1e-5 14 STEPS = 1000 15 16 class Functor: 12 13 # PLEASE NEVER USE SUCH A DIRTY TRICK. Change your logic instead. 14 #INFINITY = 10 15 16 # The minimum q-value to be used when extrapolating 17 Q_MINIMUM = 1e-5 18 19 # The maximum q-value to be used when extrapolating 20 Q_MAXIMUM = 10 21 22 # Number of steps in the extrapolation 23 INTEGRATION_NSTEPS = 1000 24 25 class FitFunctor: 17 26 """ 18 27 compute f(x) … … 103 112 return self._get_residuals(params) 104 113 114 115 def guinier(x, scale=1, radius=0.1): 116 """ 117 Compute a F(x) = scale* e-((radius*x)**2/3). 118 @param x: a vector of q values or one q value 119 @param scale: the scale value 120 @param radius: the guinier radius value 121 @return F(x) 122 """ 123 124 def power_law(x, scale=1, power=4): 125 """ 126 F(x) = scale* (x)^(-power) 127 when power= 4. the model is porod 128 else power_law 129 The model has three parameters: 130 @param power: power of the function 131 @param scale : scale factor value 132 @param F(x) 133 """ 134 135 105 136 class InvariantCalculator(object): 106 137 """ … … 120 151 @param pConst: Porod Constant of type float 121 152 """ 122 self.data = None 123 self.qstar = None 124 self.background = background 125 self.scale = scale 126 127 def _get_guinier(self, x, scale=1, radius=0.1): 128 """ 129 Compute a F(x) = scale* e-((radius*x)**2/3). 130 @param x: a vector of q values or one q value 131 @param scale: the scale value 132 @param radius: the guinier radius value 133 @return F(x) 134 """ 135 136 def _get_power_law(self, x, scale=1, power=4): 137 """ 138 F(x) = scale* (x)^(-power) 139 when power= 4. the model is porod 140 else power_law 141 The model has three parameters: 142 @param power: power of the function 143 @param scale : scale factor value 144 @param F() 145 """ 146 147 def _get_data(self): 153 # Background and scale should be private data member if the only way to 154 # change them are by instantiating a new object. 155 self._background = background 156 self._scale = scale 157 158 # The data should be private 159 self._data = self._get_data(data) 160 161 # Since there are multiple variants of Q*, you should force the 162 # user to use the get method and keep Q* a private data member 163 self._qstar = None 164 165 # You should keep the error on Q* so you can reuse it without 166 # recomputing the whole thing. 167 self._qstar_err = 0 168 169 # Extrapolation parameters 170 self._low_extrapolation_npts = 4 171 self._low_extrapolation_function = guinier 172 self._low_extrapolation_power = 4 173 174 self._high_extrapolation_npts = 4 175 self._high_extrapolation_function = power_law 176 self._high_extrapolation_power = 4 177 178 def set_extrapolation(self, range, npts=4, function=None, power=4): 179 """ 180 Set the extrapolation parameters for the high or low Q-range. 181 Note that this does not turn extrapolation on or off. 182 """ 183 range = range.lower() 184 if range not in ['high', 'low']: 185 raise ValueError, "Extrapolation range should be 'high' or 'low'" 186 function = function.lower() 187 if function not in ['power_law', 'guinier']: 188 raise ValueError, "Extrapolation function should be 'guinier' or 'power_law'" 189 190 if range=='high': 191 if function != 'power_law': 192 raise ValueError, "Extrapolation only allows a power law at high Q" 193 self._high_extrapolation_npts = npts 194 self._high_extrapolation_power = power 195 else: 196 if function == 'power_law': 197 self._low_extrapolation_function = power_law 198 else: 199 self._low_extrapolation_function = guinier 200 self._low_extrapolation_npts = npts 201 self._low_extrapolation_power = power 202 203 def _get_data(self, data): 148 204 """ 149 205 @note this function must be call before computing any type 150 206 of invariant 151 @return data= self.scale *self.data - self.background 152 """ 207 @return data= self._scale *data - self._background 208 """ 209 if not issubclass(data.__class__, LoaderData1D): 210 #Process only data that inherited from DataLoader.Data_info.Data1D 211 raise ValueError,"Data must be of type DataLoader.Data1D" 212 153 213 154 214 def _fit(self, function, params=[]): … … 166 226 """ 167 227 168 def _get_qstar(self):228 def get_qstar(self, extrapolation=None): 169 229 """ 170 230 Compute the invariant of the local copy of data. 171 231 Implementation: 172 data = self._get_data()173 232 if slit smear: 174 return self._get_qstar_smear(data)233 qstar_0 = self._get_qstar_smear() 175 234 else: 176 return self._get_qstar_unsmear(data) 235 qstar_0 = self._get_qstar_unsmear() 236 237 if extrapolation==low: 238 return qstar_0 + self._get_qstar_low() 239 elif extrapolation==high: 240 return qstar_0 + self._get_qstar_high() 241 elif extrapolation==both: 242 return qstar_0 + self._get_qstar_low() + self._get_qstar_high() 243 177 244 @return q_star: invariant of the data within data's q range 178 245 """ 179 def _get_qstar_unsmear(self, data): 246 247 248 def _get_qstar_unsmear(self): 180 249 """ 181 250 Compute invariant for pinhole data. … … 184 253 q_star = x0**2 *y0 *dx0 +x1**2 *y1 *dx1 185 254 + ..+ xn**2 *yn *dxn 186 where n= infinity 255 256 where n= SOME GOOD DEFAULT 187 257 dxi = 1/2*(xi+1 - xi) + (xi - xi-1) 188 258 dx0 = (x1 - x0)/2 189 259 dxn = xn - xn-1 190 @param data: data of type Data1D 260 191 261 @return q_star: invariant value for pinhole data. 192 262 """ 193 263 194 def _get_qstar_smear(self , data):264 def _get_qstar_smear(self): 195 265 """ 196 266 Compute invariant for slit-smeared data. … … 198 268 q_star = x0*dxl *y0*dx0 + x1*dxl *y1 *dx1 199 269 + ..+ xn*dxl *yn *dxn 200 where n= infinity270 where n= SOME GOOD DEFAULT 201 271 dxi = 1/2*(xi+1 - xi) + (xi - xi-1) 202 272 dx0 = x0+ (x1 - x0)/2 203 273 dxn = xn - xn-1 204 274 dxl: slit smear value 205 @param data: data of type Data1D275 206 276 @return q_star: invariant value for slit smeared data. 207 277 """ 208 278 209 def _get_qstar_unsmear_uncertainty(self , data):279 def _get_qstar_unsmear_uncertainty(self): 210 280 """ 211 281 Compute invariant uncertainty with with pinhole data. … … 213 283 dq_star = math.sqrt[(x0**2*(dy0)*dx0)**2 + 214 284 (x1**2 *(dy1)*dx1)**2 + ..+ (xn**2 *(dyn)*dxn)**2 ] 215 where n = infinity285 where n = SOME GOOD DEFAULT 216 286 dxi = 1/2*(xi+1 - xi) + (xi - xi-1) 217 287 dx0 = x0+ (x1 - x0)/2 … … 222 292 note: if data doesn't contain dy assume dy= math.sqrt(data.y) 223 293 """ 224 def _get_qstar_smear_uncertainty(self, data): 294 295 def _get_qstar_smear_uncertainty(self): 225 296 """ 226 297 Compute invariant uncertainty with slit smeared data. … … 239 310 note: if data doesn't contain dy assume dy= math.sqrt(data.y) 240 311 """ 241 def _get_qstar_total(self):242 """243 Compute the total invariant whether or not it is extrapolated.244 Implementation:245 qstar = self._get_qstar() + self._get_qstar_min()246 + self._get_qstar_max()247 @return q_star: invariant total value248 """249 250 def set_background(self, background=0):251 """252 Set the value of the background253 @param background : the value uses to set the local background254 """255 256 def set_scale(self, scale=1):257 """258 Set the value of the scale259 @param scale: the value to set the scale.260 """261 312 262 313 def get_surface(self,contrast, porod_const): … … 297 348 """ 298 349 299 def get_qstar_min(self, npts=0, power_law=False):350 def _get_qstar_low(self): 300 351 """ 301 352 Compute the invariant for extrapolated data at low q range. 302 353 303 354 Implementation: 304 data = self.get_extra_data_low( npts, power_law)355 data = self.get_extra_data_low() 305 356 return self._get_qstar() 306 357 307 @param npts: data the number of points of the local data to consider308 when fitting and created extrapolated data.309 310 358 @return q_star: the invariant for data extrapolated at low q. 311 359 """ 312 360 313 def get_qstar_max(self, npts=0):361 def _get_qstar_high(self): 314 362 """ 315 363 Compute the invariant for extrapolated data at high q range. 316 364 317 365 Implementation: 318 data = self.get_extra_data_high( npts)366 data = self.get_extra_data_high() 319 367 return self._get_qstar() 320 368 321 @param npts: data the number of points of the local data to consider322 when fitting and created extrapolated data.323 369 @return q_star: the invariant for data extrapolated at high q. 324 370 """ 325 371 326 def get_extra_data_low(self, data, npts, power_law=False): 327 """ 328 This method creates a new_data from the invariant calculator data 329 It takes npts first points of data , 330 fits them with a given model 331 then uses 332 the new parameters resulting from the fit to create a new data. 333 the new data first point is MINIMUM . 334 the last point of the new data is the first point of the local data. 335 the number of q points of this data is STEPS. 336 337 @param power_law: a flag to allow the function used for fitting 338 to switch between a guinier or a power_law model. 339 if set to True: the power_law will be used for fitting 340 else: the guinier will be used. 341 @param data : the data to used to extrapolated 342 assume data is scale and the background is removed 343 @param npts: the number of last points of data to fit. 344 345 @return new_data: a new data of type Data1D 346 """ 347 def get_extra_data_high(self, data, npts): 348 """ 349 This method creates a new_data from the invariant calculator data 350 It takes npts last points of data , 351 fits them with a given model 372 def _get_extra_data_low(self): 373 """ 374 This method creates a new data set from the invariant calculator. 375 376 It will use the extrapolation parameters kept as private data members. 377 378 self._low_extrapolation_npts is the number of data points to use in to fit. 379 self._low_extrapolation_function will be used as the fit function. 380 381 382 383 It takes npts first points of data, fits them with a given model 384 then uses the new parameters resulting from the fit to create a new data set. 385 386 The new data first point is Q_MINIMUM. 387 388 The last point of the new data is the first point of the original data. 389 the number of q points of this data is INTEGRATION_NSTEPS. 390 391 @return: a new data of type Data1D 392 """ 393 394 def _get_extra_data_high(self): 395 """ 396 This method creates a new data from the invariant calculator. 397 398 It takes npts last points of data, fits them with a given model 352 399 (for this function only power_law will be use), then uses 353 the new parameters resulting from the fit to create a new_data. 354 the new_data first point is the last point of data. 355 the last point of the new data is INFINITY. 356 the number of q points of this data is STEPS. 357 @param data : the data to used to extrapolated 358 assume data is scale and the background is removed 359 @param npts: the number of last points of data to fit. 360 361 @return new_data: a new data of type Data1D 362 """ 363 def get_qstar_uncertainty(self): 400 the new parameters resulting from the fit to create a new data set. 401 The first point is the last point of data. 402 The last point of the new data is Q_MAXIMUM. 403 The number of q points of this data is INTEGRATION_NSTEPS. 404 405 406 @return: a new data of type Data1D 407 """ 408 409 def get_qstar_with_error(self): 364 410 """ 365 411 Compute the invariant uncertainty. … … 367 413 smeared. 368 414 @return dq_star: 369 data = self._get_data()370 415 if slit smear: 371 return self._get_qstar _smear_uncertainty(data)416 return self._get_qstar(), self._get_qstar_smear_uncertainty() 372 417 else: 373 return self._get_qstar _unsmear_uncertainty(data)374 """ 375 376 def get_volume_fraction_ uncertainty(self, contrast):418 return self._get_qstar(), self._get_qstar_unsmear_uncertainty() 419 """ 420 421 def get_volume_fraction_with_error(self, contrast): 377 422 """ 378 423 Compute uncertainty on volume value. … … 385 430 dV: the volume uncertainty 386 431 @param contrast: contrast value 387 """ 388 389 def get_surface_uncertainty(self, contrast, porod_const): 432 @return: v, dv 433 """ 434 435 def get_surface_with_error(self, contrast, porod_const): 390 436 """ 391 437 Compute uncertainty of the surface value. … … 402 448 @param contrast: contrast value 403 449 @param porod_const: porod constant value 404 @return dS: the surfaceuncertainty405 """ 406 407 450 @return S, dS: the surface, with its uncertainty 451 """ 452 453
Note: See TracChangeset
for help on using the changeset viewer.