Changeset d23528ee in sasview
- Timestamp:
- Nov 16, 2009 6:47:47 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:
- edd166b
- Parents:
- 255306e
- Location:
- DataLoader
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
DataLoader/invariant.py
r4026380 rd23528ee 3 3 @author: Gervaise B. Alina/UTK 4 4 """ 5 import math 6 import numpy 7 from DataLoader.data_info import Data1D as LoaderData1D 8 from sans.fit.Fitting import Fit 9 INFINITY = 10 10 MIN = 0 11 STEP= 1000 5 12 6 import math7 from DataLoader.data_info import Data1D as LoaderData1D8 13 class InvariantCalculator(object): 9 14 """ … … 18 23 Initialize variables 19 24 @param data: data must be of type DataLoader.Data1D 20 @param contrast: contrast value of type float 21 @param pConst: Porod Constant of type float 22 """ 23 ## Invariant 24 self.q_star = self._get_q_star(data= data) 25 26 def _get_q_star(self, data): 27 """ 28 @param data: data of type Data1D 29 @return invariant value 25 """ 26 #Initialization of variables containing data's info 27 self.data = data 28 #Initialization of the model to use to extrapolate a low q 29 self.model_min = None 30 #Initialization of the model to use to extrapolate a high q 31 self.model_max = None 32 #Initialization of variable that contains invariant extrapolate to low q 33 self.q_star_min = 0 34 #Initialization of variable that contains invariant extrapolate to low q 35 self.q_star_max = 0 36 #Initialization of variables containing invariant's info 37 self.q_star = self._get_qstar(data= data) 38 # Initialization of the invariant total 39 self.q_star_total = self.q_star_min + self.q_star + self.q_star_max 40 41 42 def get_qstar_error(self, data): 43 """ 44 @param data: data of type Data1D 45 @return invariant error 30 46 """ 31 47 if not issubclass(data.__class__, LoaderData1D): … … 33 49 raise ValueError,"Data must be of type DataLoader.Data1D" 34 50 35 # Check whether we have slit smearing information36 51 if data.is_slit_smeared(): 37 return self._get_qstar_unsmeared(data= data) 38 else: 39 return self._get_qstar_smeared(data= data) 40 41 42 52 return self._getQStarUnsmearError(data= data) 53 else: 54 return self._getQStarSmearError(data= data) 55 56 57 def get_qstar_min(self, data, model, npts): 58 """ 59 Set parameters of the model send by the user to value return by the fit 60 engine. Store that model in self.model_min for further use such as plotting. 61 62 Extrapolate data to low q with npts number of point. 63 This function find the parameters that fit a data with a given model. 64 Then create a new data with its q vector between 0 and the previous data 65 first q value. 66 where the step between the points is the distance data.x[1] - data.x[0] 67 The new data has a y defines by model.evalDistrution(new data.x) 68 Finally compute the invariant for this new created data. 69 70 @param data: data of type Data1D 71 @param model: the model uses to extrapolate the data 72 @param npts: the number of points used to extrapolate. npts number 73 of points will be selected from the last of points of q data to the 74 npts th points of q data. 75 76 @return invariant value extrapolated to low q 77 """ 78 if model == None or npts== 0: 79 return 0 80 npts = int(npts) 81 x = data.x[0: npts] 82 qmin = 0 83 qmax = x[len(x)-1] 84 dx = None 85 dy = None 86 dxl = None 87 dxw = None 88 if data.dx !=None: 89 dx = data.dx[0:int(npts)] 90 if data.dy !=None: 91 dxl = data.dy[0:int(npts)] 92 if data.dxl !=None: 93 dx = data.dxl[0:int(npts)] 94 if data.dxw !=None: 95 dxw = data.dxw[0:int(npts)] 96 97 # fit the data with a model to get the appropriate parameters 98 self.model_min = self._fit( data, model, qmin=qmin, qmax= qmax) 99 #Get x between 0 and data.x[0] with step define such as 100 step = math.fabs(x[1]- x[0]) 101 #create new Data1D to compute the invariant 102 new_x = numpy.linspace(start= MIN, 103 stop= x[0], 104 num= npts, 105 endpoint=True 106 ) 107 new_y = model.evalDistribution(new_x) 108 min_data = LoaderData1D(x= new_x,y= new_y) 109 min_data.dxl = dxl 110 min_data.dxw = dxw 111 data.clone_without_data( clone= min_data) 112 113 return self._get_qstar(data= min_data) 114 115 116 def get_qstar_max(self, data, model, npts): 117 """ 118 Set parameters of the model send by the user to value return by the fit 119 engine. Store that model in self.model_max for further use such as plotting. 120 121 Extrapolate data to low q with npts number of point 122 @param data: data of type Data1D 123 @param model: the model uses to extrapolate the data 124 @param npts: the number of points used to extrapolate 125 @return invariant value extrapolated to low q 126 """ 127 if model == None or npts== 0: 128 return 0 129 130 index_max = len(data.x) -1 131 index_min = index_max -int(npts) 132 x = data.x[index_min:index_max] 133 qmin = x[0] 134 qmax = x[len(x)-1] 135 dx = None 136 dy = None 137 dxl = None 138 dxw = None 139 if data.dx !=None: 140 dx = data.dx[qmin:qmax] 141 if data.dy !=None: 142 dxl = data.dy[qmin:qmax] 143 if data.dxl !=None: 144 dx = data.dxl[qmin:qmax] 145 if data.dxw !=None: 146 dxw = data.dxw[0:int(npts)] 147 148 # fit the data with a model to get the appropriate parameters 149 self.model_max = self._fit( data, model, qmin= qmin, qmax= qmax) 150 #Create new Data1D 151 new_x = numpy.linspace(start= data.x[qmax], 152 stop= INFINITY, 153 num= npts, 154 endpoint=True 155 ) 156 new_y = model.evalDistribution(new_x) 157 #create a Data1D to compute the invariant 158 max_data = LoaderData1D(x= new_x,y= new_y) 159 max_data.dxl = dxl 160 max_data.dxw = dxw 161 data.clone_without_data( clone= max_data) 162 163 return self._get_qstar(data= max_data) 164 165 166 167 def get_volume_fraction(self, contrast): 168 """ 169 Compute volume fraction is given by: 170 171 q_star= 2*(pi*contrast)**2* volume( 1- volume) 172 for k = 10^(8)*q_star/(2*(pi*|contrast|)**2) 173 we get 2 values of volume: 174 volume1 = (1- sqrt(1- 4*k))/2 175 volume2 = (1+ sqrt(1- 4*k))/2 176 contrast unit is 1/A^(2)= 10^(16)cm^(2) 177 q_star unit 1/A^(3)*1/cm 178 179 the result returned will be 0<= volume <= 1 180 181 @param contrast: contrast value provides by the user of type float 182 @return: volume fraction 183 @note: volume fraction must have no unit 184 """ 185 if contrast < 0: 186 raise ValueError, "contrast must be greater than zero" 187 if self.q_star ==None: 188 raise RuntimeError, "Q_star is not defined" 189 190 #Get the total invariant with our without extrapolation 191 self.q_star_total = self.q_star + self.q_star_min + self.q_star_max 192 193 if self.q_star_total < 0: 194 raise ValueError, "invariant must be greater than zero" 195 196 #compute intermediate constant 197 k = 1.e-8*self.q_star_total /(2*(math.pi* math.fabs(float(contrast)))**2) 198 #check discriminant value 199 discrim= 1 - 4*k 200 if discrim < 0: 201 raise RuntimeError, "could not compute the volume fraction: negative discriminant" 202 elif discrim ==0: 203 volume = 1/2 204 return volume 205 else: 206 # compute the volume 207 volume1 = 0.5 *(1 - math.sqrt(discrim)) 208 volume2 = 0.5 *(1 + math.sqrt(discrim)) 209 210 if 0<= volume1 and volume1 <= 1: 211 return volume1 212 elif 0<= volume2 and volume2<= 1: 213 return volume2 214 raise RuntimeError, "could not compute the volume fraction: inconsistent results" 215 216 217 def get_surface(self, contrast, porod_const): 218 """ 219 Compute the surface given by: 220 surface = (2*pi *volume(1- volume)*pConst)/ q_star 221 222 @param contrast: contrast value provides by the user of type float 223 @param porod_const: Porod constant 224 @return: specific surface 225 """ 226 # Compute the volume 227 volume = self.get_volume_fraction(contrast) 228 229 #Check whether we have Q star 230 if self.q_star ==None: 231 raise RuntimeError, "Q_star is not defined" 232 233 #Get the total invariant with our without extrapolation 234 self.q_star_total = self.q_star + self.q_star_min + self.q_star_max 235 236 if self.q_star_total == 0: 237 raise ValueError, "invariant must be greater than zero. Q_star=%g" % self.q_star 238 239 return 2*math.pi* volume*(1- volume)*float(porod_const)/self.q_star_total 240 241 242 43 243 def _get_qstar_unsmeared(self, data): 44 244 """ … … 48 248 where n= infinity 49 249 dxi = 1/2*(xi+1 - xi) + (xi - xi-1) 50 dx0 = x 1 - x0250 dx0 = x0+ (x1 - x0)/2 51 251 dxn = xn - xn-1 252 """ 253 if len(data.x)<=1 or len(data.y)<=1 or len(data.x)!=len(data.y): 254 msg= "Length x and y must be equal" 255 msg +=" and greater than 1; got x=%s, y=%s"%(len(data.x), len(data.y)) 256 raise ValueError, msg 257 258 elif len(data.x)==1 and len(data.y)==1: 259 return 0 260 261 else: 262 n = len(data.x)-1 263 #compute the first delta 264 dx0 = data.x[0] + (data.x[1]- data.x[0])/2 265 #compute the last delta 266 dxn= data.x[n]- data.x[n-1] 267 sum = 0 268 sum += data.x[0]* data.x[0]* data.y[0]*dx0 269 sum += data.x[n]* data.x[n]* data.y[n]*dxn 270 if len(data.x)==2: 271 return sum 272 else: 273 #iterate between for element different from the first and the last 274 for i in xrange(1, n-1): 275 dxi = (data.x[i+1] - data.x[i-1])/2 276 sum += data.x[i]*data.x[i]* data.y[i]* dxi 277 return sum 278 279 280 281 def _get_qstar(self, data): 282 """ 283 @param data: data of type Data1D 284 @return invariant value 285 """ 286 if not issubclass(data.__class__, LoaderData1D): 287 #Process only data that inherited from DataLoader.Data_info.Data1D 288 raise ValueError,"Data must be of type DataLoader.Data1D" 289 290 # Check whether we have slit smearing information 291 if data.is_slit_smeared(): 292 return self._get_qstar_smeared(data= data) 293 else: 294 return self._get_qstar_unsmeared(data= data) 295 296 297 def _getQStarUnsmearError(self, data): 298 """ 299 @param data: data of type Data1D 300 Compute invariant uncertainty on y given by 301 q_star = math.sqrt[(x0**2*(dy0)*dx0)**2 + 302 (x1**2 *(dy1)*dx1)**2 + ..+ (xn**2 *(dyn)*dxn)**2 ] 303 where n = infinity 304 dxi = 1/2*(xi+1 - xi) + (xi - xi-1) 305 dx0 = x0+ (x1 - x0)/2 306 dxn = xn - xn-1 307 dyn: error on dy 308 note: if data doesn't contains dy assume dy= math.sqrt(data.y) 52 309 """ 53 310 if len(data.x)<=1 or len(data.y)<=1 or len(data.x)!=len(data.y): … … 60 317 61 318 else: 62 n= len(data.x)-1 319 #Create error for data without dy error 320 if data.dy ==None or data.dy==[]: 321 dy=[math.sqrt(y) for y in data.y] 322 else: 323 dy= data.dy 324 325 n = len(data.x)-1 63 326 #compute the first delta 64 dx0 = data.x[1]- data.x[0]327 dx0 = data.x[0] + (data.x[1]- data.x[0])/2 65 328 #compute the last delta 66 329 dxn= data.x[n]- data.x[n-1] 67 330 sum = 0 68 sum += data.x[0]* data.x[0]* data.y[0]*dx069 sum += data.x[n]* data.x[n]* data.y[n]*dxn331 sum += (data.x[0]* data.x[0]* dy[0]*dx0)**2 332 sum += (data.x[n]* data.x[n]* dy[n]*dxn)**2 70 333 if len(data.x)==2: 71 return sum334 return math.sqrt(sum) 72 335 else: 73 336 #iterate between for element different from the first and the last 74 337 for i in xrange(1, n-1): 75 338 dxi = (data.x[i+1] - data.x[i-1])/2 76 sum += data.x[i]*data.x[i]* data.y[i]* dxi77 return sum339 sum += (data.x[i]*data.x[i]* dy[i]* dxi)**2 340 return math.sqrt(sum) 78 341 79 342 … … 81 344 """ 82 345 @param data: data of type Data1D 83 Compute invariant with slit 346 Compute invariant with slit-smearing info 84 347 q_star= x0*dxl *y0 *dx0 + x1*dxl *y1 *dx1 + ..+ xn*dxl *yn *dxn 85 348 where n= infinity 86 349 dxi = 1/2*(xi+1 - xi) + (xi - xi-1) 87 dx0 = x 1 - x0350 dx0 = x0+ (x1 - x0)/2 88 351 dxn = xn - xn-1 89 352 dxl: slit smearing value 353 """ 354 355 if data.dxl ==None: 356 msg = "Cannot compute slit Smear invariant dxl " 357 msg +="must be a list, got dxl= %s , dxw= %s"%(str(data.dxl), str(data.dxw)) 358 raise ValueError,msg 359 360 if len(data.x)<=1 or len(data.y)<=1 or len(data.x)!=len(data.y)\ 361 or len(data.x)!= len(data.dxl): 362 363 msg = "x, dxl, and y must be have the same length and greater than 1" 364 raise ValueError,msg 365 else: 366 n= len(data.x)-1 367 #compute the first delta 368 dx0= data.x[0] +(data.x[1]- data.x[0])/2 369 #compute the last delta 370 dxn= data.x[n]- data.x[n-1] 371 sum = 0 372 sum += data.x[0]* data.dxl[0]* data.y[0]*dx0 373 sum += data.x[n]* data.dxl[n]* data.y[n]*dxn 374 if len(data.x)==2: 375 return sum 376 else: 377 #iterate between for element different from the first and the last 378 for i in xrange(1, n-1): 379 dxi = (data.x[i+1] - data.x[i-1])/2 380 sum += data.x[i]* data.dxl[i]* data.y[i]* dxi 381 return sum 382 383 def _getQStarSmearError(self, data): 384 """ 385 @param data: data of type Data1D 386 Compute invariant with slit smearing info 387 q_star= x0*dxl *dy0 *dx0 + x1*dxl *dy1 *dx1 + ..+ xn*dxl *dyn *dxn 388 where n= infinity 389 dxi = 1/2*(xi+1 - xi) + (xi - xi-1) 390 dx0 = x0+ (x1 - x0)/2 391 dxn = xn - xn-1 392 dxl: slit smearing value 393 dyn : error on dy 394 note: if data doesn't contains dy assume dy= math.sqrt(data.y) 90 395 """ 91 396 if data.dxl ==None: … … 100 405 raise ValueError,msg 101 406 else: 407 #Create error for data without dy error 408 if data.dy ==None or data.dy==[]: 409 dy= [math.sqrt(y) for y in data.y] 410 else: 411 dy= data.dy 412 102 413 n= len(data.x)-1 103 414 #compute the first delta 104 dx0= data.x[ 1]- data.x[0]415 dx0= data.x[0] +(data.x[1]- data.x[0])/2 105 416 #compute the last delta 106 417 dxn= data.x[n]- data.x[n-1] 107 418 sum = 0 108 sum += data.x[0]* data.dxl[0]* data.y[0]*dx0109 sum += data.x[n]* data.dxl[n]* data.y[n]*dxn419 sum += (data.x[0]* data.dxl[0]* dy[0]*dx0)**2 420 sum += (data.x[n]* data.dxl[n]* dy[n]*dxn)**2 110 421 if len(data.x)==2: 111 return sum422 return math.sqrt(sum) 112 423 else: 113 424 #iterate between for element different from the first and the last 114 425 for i in xrange(1, n-1): 115 426 dxi = (data.x[i+1] - data.x[i-1])/2 116 sum += data.x[i]* data.dxl[i]* data.y[i]* dxi 117 return sum 118 119 def get_volume_fraction(self, contrast): 120 """ 121 Compute volume fraction is given by: 122 123 q_star= 2*(pi*contrast)**2* volume( 1- volume) 124 for k = 10^(8)*q_star/(2*(pi*|contrast|)**2) 125 we get 2 values of volume: 126 volume1 = (1- sqrt(1- 4*k))/2 127 volume2 = (1+ sqrt(1- 4*k))/2 128 contrast unit is 1/A^(2)= 10^(16)cm^(2) 129 q_star unit 1/A^(3)*1/cm 130 131 the result returned will be 0<= volume <= 1 132 427 sum += (data.x[i]* data.dxl[i]* dy[i]* dxi)**2 428 return math.sqrt(sum) 429 430 def _getVolFracError(self, contrast): 431 """ 432 Compute the error on volume fraction uncertainty where 433 uncertainty is delta volume = 1/2 * (4*k* uncertainty on q_star) 434 /(2* math.sqrt(1-k* q_star)) 435 436 for k = 10^(8)*q_star/(2*(pi*|contrast|)**2) 133 437 @param contrast: contrast value provides by the user of type float 134 @return: volume fraction 135 @note: volume fraction must have no unit 136 """ 137 if contrast < 0: 138 raise ValueError, "contrast must be greater than zero" 139 140 if self.q_star ==None: 141 raise RuntimeError, "Q_star is not defined" 142 143 if self.q_star < 0: 144 raise ValueError, "invariant must be greater than zero. Q_star=%g" % self.q_star 145 146 #compute intermediate constant 147 k = 1.e-8*self.q_star /(2*(math.pi* math.fabs(float(contrast)))**2) 438 """ 439 if self.q_star_total == None or self.q_star_err == None: 440 return 441 442 if self.q_star_total < 0: 443 raise ValueError, "invariant must be greater than zero" 444 445 k = 1.e-8*self.q_star_total /(2*(math.pi* math.fabs(float(contrast)))**2) 148 446 #check discriminant value 149 discrim= 1 - 4*k 150 if discrim < 0: 151 raise RuntimeError, "could not compute the volume fraction: negative discriminant" 152 elif discrim ==0: 153 volume = 1/2 154 return volume 155 else: 156 # compute the volume 157 volume1 = 0.5 *(1 - math.sqrt(discrim)) 158 volume2 = 0.5 *(1 + math.sqrt(discrim)) 159 160 if 0<= volume1 and volume1 <= 1: 161 return volume1 162 elif 0<= volume2 and volume2<= 1: 163 return volume2 164 raise RuntimeError, "could not compute the volume fraction: inconsistent results" 165 166 def get_surface(self, contrast, porod_const): 167 """ 168 Compute the surface given by: 169 surface = (2*pi *volume(1- volume)*pConst)/ q_star 170 171 @param contrast: contrast value provides by the user of type float 172 @param porod_const: Porod constant 173 @return: specific surface 174 """ 175 # Compute the volume 176 volume = self.get_volume_fraction(contrast) 177 178 # Check whether we have Q star 179 if self.q_star ==None: 180 raise RuntimeError, "Q_star is not defined" 181 182 if self.q_star ==0: 183 raise ValueError, "invariant must be greater than zero. Q_star=%g" % self.q_star 184 185 return 2*math.pi*volume*(1-volume)*float(porod_const)/self.q_star 186 187 447 discrim = 1 - 4*k 448 if 1- k*self.q_star <=0: 449 raise ValueError, "Cannot compute incertainty on volume" 450 451 uncertainty = (0.5 *4*k* self.q_star_err)/(2*math.sqrt(1- k*self.q_star)) 452 return math.fabs(uncertainty) 453 454 455 def _fit(self, data, model, qmin=None, qmax=None): 456 """ 457 perform fit 458 @param data: data to fit 459 @param model: model to fit 460 @return: model with the parameters computed by the fitting engine 461 """ 462 id = 1 463 fitter = Fit('scipy') 464 fitter.set_data(data,id) 465 pars=[] 466 467 for param in model.getParamList() : 468 # TODO: Remove the background from computation before fitting? 469 if param not in model.getDispParamList(): 470 pars.append(param) 471 fitter.set_model(model,id,pars) 472 fitter.select_problem_for_fit(Uid=id,value=1) 473 result = fitter.fit() 474 out = result.pvec 475 # Set the new parameter back to the model 476 if out== None: 477 #The fit engine didn't find parameters , the model is returned in 478 # the same state 479 return model 480 # Assigned new parameters values to the model 481 if out.__class__== numpy.float64: 482 #Only one parameter was fitted 483 model.setParam(pars[0], out) 484 else: 485 for index in xrange(len(pars)): 486 model.setParam(pars[index], out[index]) 487 return model 488 489 490 -
DataLoader/test/utest_invariant.py
r4026380 rd23528ee 3 3 @author Gervaise Alina: unittest imcoplete so far 4 4 """ 5 6 7 5 import unittest 8 6 import numpy, math … … 12 10 13 11 14 class InvariantTest(unittest.TestCase): 12 class TestInvPolySphere(unittest.TestCase): 13 """ 14 Test unsmeared data for invariant computation 15 """ 16 def setUp(self): 17 #Data with no slit smear information 18 data= Loader().load("PolySpheres.txt") 19 self.I = InvariantCalculator( data= data) 20 21 22 def testWrongData(self): 23 """ test receiving Data1D not of type loader""" 24 #compute invariant with smear information 25 from danse.common.plottools.plottables import Data1D 26 data= Data1D(x=[1,2],y=[2,3] ) 27 try: 28 self.assertRaises(ValueError,InvariantCalculator(data)) 29 except ValueError, msg: 30 print "test pass "+ str(msg) 31 else: raise ValueError, "fail to raise exception when expected" 32 33 def testInvariant(self): 34 """ test the invariant value for data without smearing""" 35 self.assertAlmostEquals(self.I.q_star, 7.48959e-5,2) 36 37 def testVolume(self): 38 """ test volume fraction for polysphere""" 39 volume = self.I.get_volume_fraction(contrast=2.6e-6) 40 self.assert_(volume >= 0 and volume <=1) 41 self.assertAlmostEqual(volume ,0.01) 42 43 def testSurface(self): 44 """ test surface for polysphere """ 45 surface = self.I.get_surface(contrast=2.6e-6, porod_const=20) 46 self.assertAlmostEqual(surface,0.01) 47 48 49 class TestInvariantSmear(unittest.TestCase): 50 """ 51 Test smeared data for invariant computation 52 """ 53 def setUp(self): 54 # data with smear info 55 list = Loader().load("latex_smeared.xml") 56 self.data_q_smear = list[0] 57 self.I_q_smear = InvariantCalculator( data= self.data_q_smear) 58 59 self.data_slit_smear = list[1] 60 self.I_slit_smear = InvariantCalculator( data= self.data_slit_smear) 61 62 from sans.models.PowerLawModel import PowerLawModel 63 self.power_law = PowerLawModel() 64 from sans.models.GuinierModel import GuinierModel 65 self.guinier = GuinierModel() 66 67 68 def test_invariant_slit(self): 69 """ test the invariant value for data with slit smear""" 70 self.assertTrue(self.I_slit_smear.q_star>0) 71 self.assertAlmostEquals(self.I_slit_smear.q_star, 4.1539e-4) 72 73 def test_volume_slit_smear(self): 74 """ test volume fraction for data with slit smear""" 75 volume = self.I_slit_smear.get_volume_fraction(contrast=2.6e-6) 76 self.assert_(volume >= 0 and volume <=1) 77 self.assertAlmostEqual(volume ,0.01) 78 79 def test_invariant_slit_low_guinier(self): 80 """ test the invariant with slit smear for data extrapolated low q range using guinier""" 81 q_star_min = self.I_slit_smear.get_qstar_min( data= self.data_slit_smear, 82 model= self.guinier, npts=10) 83 self.assertAlmostEquals(q_star_min, 4.89783e-8) 84 self.assertAlmostEquals(self.I.q_star_min, q_star_min) 85 86 def test_invariant_slit_low_power(self): 87 """ test the invariant with slit smear for data extrapolated low q range using pow_law""" 88 q_star_min = self.I_slit_smear.get_qstar_min( data=self.data_slit_smear, 89 model= self.power_law, npts=10) 90 self.assertFalse(numpy.isfinite(q_star_min)) 91 self.assertEquals(self.I.q_star_min, q_star_min) 92 93 def test_invariant_slit_high_power(self): 94 """ test the invariant with slit smear for data extrapolated high q range using pow_law""" 95 q_star_max = self.I_slit_smear.get_qstar_max( data= self.data_slit_smear, 96 model= self.power_law, npts=10) 97 self.assertFalse(numpy.isfinite(q_star_max)) 98 self.assertEquals(self.I.q_star_max, q_star_max) 99 100 def testSurface(self): 101 """ test volume fraction for data with slit smear""" 102 surface = self.I_q_smear.get_surface(contrast=2.6e-6, porod_const=20) 103 self.assertAlmostEqual(surface,0.01) 104 105 def test_invariant_q(self): 106 """ test the invariant value for data with q smear""" 107 self.assertTrue(self.I_q_smear.q_star>0) 108 self.assertAlmostEquals(self.I_q_smear.q_star, 1.361677e-3) 109 110 def test_invariant_q_low_guinier(self): 111 """ test the invariant with q smear for data extrapolated low q range using guinier""" 112 q_star_min = self.I_slit_smear.get_qstar_min( data= self.data_q_smear, 113 model= self.guinier, npts=10) 114 self.assertAlmostEquals(q_star_min,6.76e-4) 115 self.assertAlmostEquals(self.I.q_star_min, q_star_min) 116 117 def test_invariant_q_low_power(self): 118 """ test the invariant with q smear for data extrapolated low q range using pow_law""" 119 q_star_min = self.I_slit_smear.get_qstar_min( data= self.data_q_smear, 120 model= self.power_law, npts=10) 121 self.assertFalse(numpy.isfinite(q_star_min)) 122 self.assertEquals(self.I.q_star_min, q_star_min) 123 124 def test_invariant_q_high_power(self): 125 """ test the invariant with q smear for data extrapolated high q range using pow_law""" 126 q_star_max = self.I_slit_smear.get_qstar_max( data= self.data_q_smear, 127 model= self.power_law, npts=10) 128 self.assertAlmostEquals(q_star_max,1.2e-4) 129 self.assertEquals(self.I.q_star_max, q_star_max) 130 131 def test_volume_q_smear(self): 132 """ test volume fraction for data with q smear""" 133 volume = self.I_slit_smear.get_volume_fraction(contrast=2.6e-6) 134 self.assert_(volume > 0 and volume <=1) 135 self.assertAlmostEqual(volume ,0.01) 136 137 def testSurface_q_smear(self): 138 """ test surface for data with q smear""" 139 surface = self.I_q_smear.get_surface(contrast=2.6e-6, porod_const=20) 140 self.assertAlmostEqual(surface,0.01) 141 142 143 class ExtrapolationTest(unittest.TestCase): 15 144 16 145 def setUp(self): 17 146 #Data with no slit smear information 18 data0 = Loader().load("PolySpheres.txt") 19 self.I0 = InvariantCalculator( data=data0) 147 self.data= Loader().load("PolySpheres.txt") 148 from sans.models.PowerLawModel import PowerLawModel 149 self.power_law = PowerLawModel() 150 from sans.models.GuinierModel import GuinierModel 151 self.guinier = GuinierModel() 152 self.I = InvariantCalculator( data= self.data) 153 20 154 21 # data with smear info 22 list = Loader().load("latex_smeared.xml") 23 data1= list[0] 24 self.I1= InvariantCalculator( data= data1) 155 def testInvariant(self): 156 """ test the invariant value for data extrapolated""" 157 self.assertAlmostEquals(self.I.q_star, 7.48959e-5) 25 158 26 data2= list[1] 27 self.I2= InvariantCalculator( data= data2) 28 29 def test_invariant(self): 30 """ test the invariant value for data with no slit smear""" 31 self.assertAlmostEquals(self.I0.q_star, 7.48959e-5) 32 self.assertTrue(self.I1.q_star>0) 33 self.assertTrue(self.I2.q_star>0) 159 def testInvariantLowGuinier(self): 160 """ test the invariant value for data extrapolated low range using guinier""" 161 q_star_min = self.I.get_qstar_min( data= self.data, model= self.guinier, npts=10) 162 self.assertAlmostEquals(q_star_min, 4.89783e-8) 163 self.assertAlmostEquals(self.I.q_star_min, q_star_min) 164 165 def testInvariantLowPower(self): 166 """ test the invariant value for data extrapolated low range using pow_law""" 167 q_star_min = self.I.get_qstar_min( data= self.data, model= self.power_law, npts=10) 168 self.assertFalse(numpy.isfinite(q_star_min)) 169 self.assertEquals(self.I.q_star_min, q_star_min) 170 171 def testInvariantHigh(self): 172 """ test the invariant value for data extrapolated high range""" 173 q_star_max = self.I.get_qstar_max( self.data, model=self.power_law, npts=10) 174 self.assertAlmostEquals(q_star_max, 4.066202e-6) 175 self.assertAlmostEquals(self.I0.q_star_max, q_star_max) 34 176 35 def test_computation(self): 36 """ 37 Test identity smearing 38 """ 39 vol = self.I0.get_volume_fraction(contrast=2.6e-6) 40 surface = self.I0.get_surface(contrast=2.6e-6, porod_const=20) 177 def testInvariantTotal(self): 178 """ test the total invariant value for data extrapolated""" 179 self.assertAlmostEquals(self.I.q_star_total, 7.88981e-5) 41 180 42 # TODO: Need to test output values 43 #self.assertAlmostEquals(vol, 0) 44 #self.assertAlmostEquals(surface, 0) 45 vol = self.I1.get_volume_fraction(contrast=5.3e-6) 46 surface = self.I1.get_surface(contrast=5.3e-6, porod_const=20) 47 48 # TODO: Need to test output values 49 #self.assertAlmostEquals(vol, 0) 50 #self.assertAlmostEquals(surface, 0) 51 52 vol = self.I2.get_volume_fraction(contrast=5.3e-6) 53 surface = self.I2.get_surface(contrast=5.3e-6, porod_const=20) 54 55 # TODO: Need to test output values 56 self.assertAlmostEquals(vol, 0) 57 self.assertAlmostEquals(surface, 0) 58 59 60 181 def testVolume(self): 182 """ test volume fraction for data extrapolated""" 183 volume = self.I.get_volume_fraction(contrast=2.6e-6) 184 self.assert_(volume > 0 and volume <=1) 185 self.assertAlmostEqual(volume ,0.01) 186 187 def testSurface(self): 188 """ test surface for data extrapolated""" 189 surface = self.I.get_surface(contrast=2.6e-6, porod_const=20) 190 self.assertAlmostEqual(surface,0.01) 191 61 192 if __name__ == '__main__': 62 193 unittest.main()
Note: See TracChangeset
for help on using the changeset viewer.