Changeset f629e346 in sasview for sansmodels/src/sans/models/test
- Timestamp:
- Mar 25, 2008 4:19:00 PM (17 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:
- 6b8399b
- Parents:
- 3db3895
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansmodels/src/sans/models/test/utest_nonshape.py
r829eee9 rf629e346 5 5 6 6 import unittest, time, math 7 from scipy.special import erf,gamma 7 from scipy.special import erf,gammaln 8 8 9 9 # Disable "missing docstring" complaint … … 24 24 """ 25 25 def _func(self, a, b, x): 26 return math.exp(a+b*x**2)27 28 def setUp(self): 29 from GuinierModel import GuinierModel26 return a*math.exp(-(b*x)**2/3.0) 27 28 def setUp(self): 29 from sans.models.GuinierModel import GuinierModel 30 30 self.model= GuinierModel() 31 31 32 32 def test1D(self): 33 self.model.setParam(' A', 2.0)34 self.model.setParam(' B', 1.0)35 36 self.assertEqual(self.model.run(0.0), math.exp(2.0))37 self.assertEqual(self.model.run(2.0), math.exp(2.0+1.0*(2.0)**2))38 39 def test2D(self): 40 self.model.setParam(' A', 2.0)41 self.model.setParam(' B', 1.0)33 self.model.setParam('scale', 2.0) 34 self.model.setParam('rg', 1.0) 35 36 self.assertEqual(self.model.run(0.0), 2.0) 37 self.assertEqual(self.model.run(2.0), 2.0*math.exp(-(1.0*2.0)**2/3.0)) 38 39 def test2D(self): 40 self.model.setParam('scale', 2.0) 41 self.model.setParam('rg', 1.0) 42 42 43 43 value = self._func(2.0, 1.0, 1.0)*self._func(2.0, 1.0, 2.0) 44 self.assertEqual(self.model.runXY([0.0,0.0]), math.exp(2.0)*math.exp(2.0))45 self.assertEqual(self.model.runXY([1.0,2.0]), value) 46 47 def test2Dphi(self): 48 self.model.setParam(' A', 2.0)49 self.model.setParam(' B', 1.0)44 self.assertEqual(self.model.runXY([0.0,0.0]), 2.0*2.0) 45 self.assertEqual(self.model.runXY([1.0,2.0]), value) 46 47 def test2Dphi(self): 48 self.model.setParam('scale', 2.0) 49 self.model.setParam('rg', 1.0) 50 50 51 51 x = 1.0 … … 64 64 Unit tests for Porod function 65 65 66 F(x) = exp[ [C]/Q**4 ]66 F(x) = C/Q**4 67 67 68 68 The model has one parameter: C 69 69 """ 70 70 def _func(self, c, x): 71 return math.exp(c/(x*x*x*x))72 73 def setUp(self): 74 from PorodModel import PorodModel71 return c/(x**4) 72 73 def setUp(self): 74 from sans.models.PorodModel import PorodModel 75 75 self.model= PorodModel() 76 self.model.setParam(' c', 2.0)76 self.model.setParam('scale', 2.0) 77 77 78 78 def test1D(self): … … 109 109 110 110 def setUp(self): 111 from DebyeModel import DebyeModel111 from sans.models.DebyeModel import DebyeModel 112 112 self.model= DebyeModel() 113 113 self.model.setParam('Rg', 50.0) 114 114 self.model.setParam('scale',1.0) 115 self.model.setParam('b kd',0.001)115 self.model.setParam('background',0.001) 116 116 117 117 def test1D(self): … … 119 119 self.assertEqual(self.model.run(2.0), value) 120 120 121 # user enter zero as a value of x122 self.assertEqual(self.model.run(0.0),False)123 121 # User enter zero as a value of x 122 # An exceptio is raised 123 self.assertRaises(ZeroDivisionError, self.model.run, 0.0) 124 124 125 125 def test2D(self): … … 148 148 bkd = incoherent background 149 149 """ 150 def _func(self,scale,L,bkd,x): 151 return scale/( 1 + (x*L)**2 ) + bkd 152 def setUp(self): 153 from LorentzModel import LorentzModel 150 def _func(self, I0 , L, bgd, qval): 151 return I0/(1.0 + (qval*L)*(qval*L)) + bgd 152 153 def setUp(self): 154 from sans.models.LorentzModel import LorentzModel 154 155 self.model= LorentzModel() 155 156 156 157 def test1D(self): 157 158 self.model.setParam('scale', 100.0) 158 self.model.setParam('L ', 50.0)159 self.model.setParam('b kd', 1.0)159 self.model.setParam('Length', 50.0) 160 self.model.setParam('background', 1.0) 160 161 161 162 self.assertEqual(self.model.run(0.0), 101.0) … … 164 165 def test2D(self): 165 166 self.model.setParam('scale', 100.0) 166 self.model.setParam('L ', 50.0)167 self.model.setParam('b kd', 1.0)167 self.model.setParam('Length', 50.0) 168 self.model.setParam('background', 1.0) 168 169 169 170 value = self._func(100.0, 50.0, 1.0, 1.0)*self._func(100.0, 50.0, 1.0, 2.0) … … 172 173 def test2Dphi(self): 173 174 self.model.setParam('scale', 100.0) 174 self.model.setParam('L ', 50.0)175 self.model.setParam('b kd', 1.0)175 self.model.setParam('Length', 50.0) 176 self.model.setParam('background', 1.0) 176 177 177 178 x = 1.0 … … 194 195 bkd = incoherent background 195 196 """ 196 def _func(self, a, b,c, x):197 return a/(( 1 + ( x * b )**2 ))**2 + c198 199 def setUp(self): 200 from DABModel import DABModel197 def _func(self, Izero, range, incoh, qval): 198 return Izero/pow((1.0 + (qval*range)*(qval*range)),2) + incoh 199 200 def setUp(self): 201 from sans.models.DABModel import DABModel 201 202 self.model= DABModel() 202 203 203 204 def test1D(self): 204 205 self.model.setParam('scale', 10.0) 205 self.model.setParam('L ', 40.0)206 self.model.setParam('b kd', 0.0)206 self.model.setParam('Length', 40.0) 207 self.model.setParam('background', 0.0) 207 208 208 209 self.assertEqual(self.model.run(0.0), 10.0) … … 211 212 def test2D(self): 212 213 self.model.setParam('scale', 10.0) 213 self.model.setParam('L ', 40.0)214 self.model.setParam('b kd', 0.0)214 self.model.setParam('Length', 40.0) 215 self.model.setParam('background', 0.0) 215 216 216 217 value = self._func(10.0, 40.0, 0.0, 1.0)*self._func(10.0, 40.0, 0.0, 2.0) … … 219 220 def test2Dphi(self): 220 221 self.model.setParam('scale', 10.0) 221 self.model.setParam('L ', 40.0)222 self.model.setParam('b kd', 0.0)222 self.model.setParam('Length', 40.0) 223 self.model.setParam('background', 0.0) 223 224 224 225 x = 1.0 … … 241 242 bkd = incoherent background 242 243 """ 243 def _func(self, a, m,c, x): 244 return a*(x )**m + c 245 246 def setUp(self): 247 from PowerLawModel import PowerLawModel 244 def _func(self, a, m, bgd, qval): 245 return a*math.pow(qval,-m) + bgd 246 247 248 def setUp(self): 249 from sans.models.PowerLawModel import PowerLawModel 248 250 self.model= PowerLawModel() 249 251 … … 251 253 self.model.setParam('scale', math.exp(-6)) 252 254 self.model.setParam('m', 4.0) 253 self.model.setParam('bkd', 1.0) 255 self.model.setParam('background', 1.0) 256 257 #self.assertEqual(self.model.run(0.0), 1.0) 258 self.assertEqual(self.model.run(2.0), self._func(math.exp(-6), 4.0, 1.0, 2.0)) 259 260 def testlimit(self): 261 self.model.setParam('scale', math.exp(-6)) 262 self.model.setParam('m', -4.0) 263 self.model.setParam('background', 1.0) 254 264 255 265 self.assertEqual(self.model.run(0.0), 1.0) 256 self.assertEqual(self.model.run(2.0), self._func(math.exp(-6), 4.0, 1.0, 2.0))257 266 258 267 def test2D(self): 259 268 self.model.setParam('scale', math.exp(-6)) 260 269 self.model.setParam('m', 4.0) 261 self.model.setParam('b kd', 1.0)270 self.model.setParam('background', 1.0) 262 271 263 272 value = self._func(math.exp(-6), 4.0, 1.0, 1.0)\ … … 269 278 self.model.setParam('scale', math.exp(-6)) 270 279 self.model.setParam('m', 4.0) 271 self.model.setParam('b kd', 1.0)280 self.model.setParam('background', 1.0) 272 281 273 282 x = 1.0 … … 292 301 bkd = incoherent background 293 302 """ 294 def _func(self, a,c1,c2,b, x): 295 return 1/( a + c1*(x)**(2)+ c2*(x)**(4)) + b 296 297 def setUp(self): 298 from TeubnerStreyModel import TeubnerStreyModel 303 def _func(self, scale, c1, c2, bck, q): 304 305 q2 = q*q; 306 q4 = q2*q2; 307 308 return 1.0/(scale + c1*q2+c2*q4) + bck 309 310 def setUp(self): 311 from sans.models.TeubnerStreyModel import TeubnerStreyModel 299 312 self.model= TeubnerStreyModel() 300 313 … … 304 317 self.model.setParam('c2', 5000.0) 305 318 self.model.setParam('scale', 0.1) 306 self.model.setParam('b kd', 0.1)319 self.model.setParam('background', 0.1) 307 320 #self.assertEqual(1/(math.sqrt(4)), math.pow(4,-1/2)) 308 self.assertEqual(self.model.TeubnerStreyLengths(),False )321 #self.assertEqual(self.model.TeubnerStreyLengths(),False ) 309 322 310 323 self.assertEqual(self.model.run(0.0), 10.1) … … 315 328 self.model.setParam('c2', 5000.0) 316 329 self.model.setParam('scale', 0.1) 317 self.model.setParam('b kd', 0.1)330 self.model.setParam('background', 0.1) 318 331 value = self._func(0.1,-30.0,5000.0,0.1, 1.0)\ 319 332 *self._func(0.1,-30.0,5000.0,0.1, 2.0) … … 325 338 self.model.setParam('c2', 5000.0) 326 339 self.model.setParam('scale', 0.1) 327 self.model.setParam('b kd', 0.1)340 self.model.setParam('background', 0.1) 328 341 329 342 x = 1.0 … … 358 371 359 372 def setUp(self): 360 from BEPolyelectrolyte import BEPolyelectrolyte373 from sans.models.BEPolyelectrolyte import BEPolyelectrolyte 361 374 self.model= BEPolyelectrolyte() 362 375 363 def test1D(self): 364 365 self.model.setParam('K', 10.0) 366 self.model.setParam('Lb', 7.1) 367 self.model.setParam('H', 12) 368 self.model.setParam('B', 10) 369 self.model.setParam('Cs',0.0) 370 self.model.setParam('alpha', 0.05) 371 self.model.setParam('C', 0.7) 372 self.model.setParam('bkd', 0.001) 373 K2 = 4 * math.pi * 7.1 * (2*0.0 + 0.05*0.7) 374 Ca = 0.7 * 6.022136 * math.exp(-4) 375 r02 =1/(0.05 * math.pow(Ca,0.5)*(10/math.pow((48* math.pi *7.1),0.5))) 376 377 378 self.assertEqual(self.model.run(0.0), self._func( 10.0, 7.1, 12,\ 379 10.0, 0.0,0.05,0.7,0.001, r02, K2, 0.0)) 380 self.assertEqual(self.model.run(2.0), self._func( 10.0, 7.1, 12,\ 381 10.0, 0.0,0.05,0.7,0.001, r02, K2, 2.0)) 382 383 def test2D(self): 384 self.model.setParam('K', 10.0) 385 self.model.setParam('Lb', 7.1) 386 self.model.setParam('H', 12) 387 self.model.setParam('B', 10) 388 self.model.setParam('Cs',0.0) 389 self.model.setParam('alpha', 0.05) 390 self.model.setParam('C', 0.7) 391 self.model.setParam('bkd', 0.001) 392 K2 = 4 * math.pi * 7.1 * (2*0.0 + 0.05*0.7) 393 Ca = 0.7 * 6.022136 * math.exp(-4) 394 r02 =1/(0.05 * math.pow(Ca,0.5)*(10/math.pow((48* math.pi *7.1),0.5))) 395 396 value = self._func(10.0, 7.1, 12, 10.0, 0.0,0.05,0.7,0.001, r02, K2, 1.0)\ 397 *self._func(10.0, 7.1, 12,10.0, 0.0,0.05,0.7,0.001, r02, K2, 2.0) 398 399 self.assertAlmostEquals(self.model.runXY([1.0,2.0]), value) 400 401 def test2Dphi(self): 402 self.model.setParam('K', 10.0) 403 self.model.setParam('Lb', 7.1) 404 self.model.setParam('H', 12) 405 self.model.setParam('B', 10) 406 self.model.setParam('Cs',0.0) 407 self.model.setParam('alpha', 0.05) 408 self.model.setParam('C', 0.7) 409 self.model.setParam('bkd', 0.001) 410 K2 = 4 * math.pi * 7.1 * (2*0.0 + 0.05*0.7) 411 Ca = 0.7 * 6.022136 * math.exp(-4) 412 r02 =1/(0.05 * math.pow(Ca,0.5)*(10/math.pow((48* math.pi *7.1),0.5))) 413 414 x = 1.0 415 y = 2.0 416 r = math.sqrt(x**2 + y**2) 417 phi = math.atan2(y, x) 418 419 value = self._func(10.0, 7.1, 12, 10.0, 0.0,0.05,0.7,0.001, r02, K2, x)\ 420 *self._func(10.0, 7.1, 12, 10.0, 0.0,0.05,0.7,0.001, r02, K2, y) 421 self.assertAlmostEquals(self.model.run([r, phi]), value,1) 376 self.K = 10.0 377 self.Lb = 6.5 378 self.h = 11 379 self.b = 13 380 self.Cs = 0.1 381 self.alpha = 0.05 382 self.C = .7 383 self.Bkd =0.01 384 385 self.model.setParam('K', self.K) 386 self.model.setParam('Lb', self.Lb) 387 self.model.setParam('H', self.h) 388 self.model.setParam('B', self.b) 389 self.model.setParam('Cs',self.Cs) 390 self.model.setParam('alpha', self.alpha) 391 self.model.setParam('C', self.C) 392 self.model.setParam('background', self.Bkd) 393 394 def _func(self, q): 395 396 Ca = self.C *6.022136e-4 397 Csa = self.Cs * 6.022136e-4 398 k2= 4*math.pi*self.Lb*(2*self.Cs+self.alpha*Ca) 399 400 r02 = 1./self.alpha / Ca**0.5*( self.b / (48*math.pi*self.Lb)**0.5 ) 401 q2 = q**2 402 Sq = self.K*1./(4*math.pi*self.Lb*self.alpha**2) * (q2 + k2) / (1+(r02**2) * (q2+k2) * (q2- (12*self.h*Ca/self.b**2)) ) + self.Bkd 403 return Sq 404 405 def test1D(self): 406 407 408 q = 0.001 409 410 self.assertEqual(self.model.run(q), self._func(q)) 411 412 def test2D(self): 413 self.assertAlmostEquals(self.model.runXY([1.0,2.0]), self._func(1.0)*self._func(2.0), 8) 414 415 def test2Dphi(self): 416 417 x = 1.0 418 y = 2.0 419 r = math.sqrt(x**2 + y**2) 420 phi = math.atan2(y, x) 421 422 self.assertAlmostEquals(self.model.run([r, phi]), self._func(x)*self._func(y), 8) 422 423 423 424 class TestFractalModel(unittest.TestCase): … … 434 435 bkd = background 435 436 """ 436 def _func(self,p,s, bkd, x): 437 return p*s + bkd 438 439 def setUp(self): 440 from FractalModel import FractalModel 437 def setUp(self): 438 from sans.models.FractalModel import FractalModel 441 439 self.model= FractalModel() 442 440 443 441 def test1D(self): 444 445 self.model.setParam('scale', 0.05) 446 self.model.setParam('Radius',5) 447 self.model.setParam('Fdim',2) 448 self.model.setParam('L', 100) 449 self.model.setParam('SDLB',2*math.exp(-6)) 450 self.model.setParam('SDLS', 6.35*math.exp(-6)) 451 self.model.setParam('bkd', 0.0) 452 453 s= 1 + (math.sin((2 - 1) * math.atan(2.0*100))* 2 * gamma(2-1))\ 454 /( math.pow( (2.0*5),2)*( 1 + 1/math.pow(((2.0**2)*(100**2)),(2-1)/2))) 455 v= (4/3)*math.pi *math.pow(5, 3) 456 f= ( math.sin(2.0*5)-(2.0*5)*math.cos(2.0*5) )/(3*math.pow(2.0*5, 3)) 457 p= 0.05 *(v**2)*(((2*math.exp(-6))-(6.35*math.exp(-6)))**2)*(f**2) 442 r0 = 5.0 443 sldp = 2.0e-6 444 sldm = 6.35e-6 445 phi = 0.05 446 Df = 2 447 corr = 100.0 448 bck = 1.0 449 450 x = 0.001 451 452 self.model.setParam('scale', phi) 453 self.model.setParam('Radius', r0) 454 self.model.setParam('fractal_dim',Df) 455 self.model.setParam('corr_length', corr) 456 self.model.setParam('block_sld', sldp) 457 self.model.setParam('solvent_sld', sldm) 458 self.model.setParam('background', bck) 459 460 pq = 1.0e8*phi*4.0/3.0*math.pi*r0*r0*r0*(sldp-sldm)*(sldp-sldm)*math.pow((3*(math.sin(x*r0) - x*r0*math.cos(x*r0))/math.pow((x*r0),3)),2); 461 462 sq = Df*math.exp(gammaln(Df-1.0))*math.sin((Df-1.0)*math.atan(x*corr)); 463 sq /= math.pow((x*r0),Df) * math.pow((1.0 + 1.0/(x*corr)/(x*corr)),((Df-1.0)/2.0)); 464 sq += 1.0; 458 465 459 self.assertEqual(self.model._scatterRanDom(2.0),p ) 460 self.assertEqual(self.model._Block(2.0),s ) 461 self.assertEqual(self.model.run(2.0),self._func(p,s ,0.0,2.0)) 462 463 class TestUnifiedPowerLaw(unittest.TestCase): 464 """ 465 Unit tests for Unified PowerLaw function 466 467 F(x) = bkd + sum(G[i]*exp(-x**2 *Rg[i]**2 /3)) \ 468 + [B[i]( erf(x*Rg[i]/math.sqrt(6)))** (3*p[i])]/x**p[i] ) 469 470 """ 471 472 def setUp(self): 473 from UnifiedPowerLaw import UnifiedPowerLawModel 474 self.model= UnifiedPowerLawModel() 475 476 def _func(self,level,B,Rg,G,Pow,bkd, x): 477 return bkd + (B * math.pow(erf(x *Rg/math.sqrt(6)),3 *Pow))/math.pow(x,Pow) 478 479 def _Sum (self,level,x): 480 self.sum = 0 481 for i in xrange(level): 482 self.sum =self.sum +self.model.getParam('G'+str(i+1))*math.exp(-(x**2)*\ 483 (self.model.getParam('Rg'+str(i+1))**2)/3) 484 return self.sum 485 486 def test1D(self): 487 488 489 self.model.setParam('bkd', 0.0) 490 #setValueParam(self,level,Bvalue,Rgvalue,Gvalue,Pvalue) 491 self.model.setValueParam(1,2,1,2,5) 492 self.model.setValueParam(2,3,12,8,9) 493 self.model.setValueParam(3,0,2,3,2) 494 self.model.setValueParam(4,1,4,1,-1) 495 self.model.setValueParam(5,1,4,1,-2) 496 497 self.assertEqual(self.model.getValue('P',1),5) 498 self.assertEqual(self.model.getValue('Rg',2),12) 499 500 value1 = self._func(1,2,1,2,5,0.0,1.0)+self._Sum(5,1.0) 501 value2 = self._func(2,3,12,8,9,0.0,1.0)+self._Sum(5,1.0) 502 value3 = self._func(3,0,2,3,2,0.0,1.0)+self._Sum(5,1.0) 503 value4 = self._func(4,1,4,1,-1,0.0,1.0)+self._Sum(5,1.0) 504 value5 = self._func(5,1,4,1,-2,0.0,1.0)+self._Sum(5,1.0) 505 self.assertEqual(self.model.run(1,1.0), value1) 506 self.assertEqual(self.model.run(2,1.0), value2) 507 self.assertEqual(self.model.run(3,1.0), value3) 508 self.assertEqual(self.model.run(4,1.0), value4) 509 self.assertEqual(self.model.run(5,1.0), value5) 510 511 512 466 self.assertAlmostEqual(self.model._scatterRanDom(x),pq, 8 ) 467 468 self.assertEqual(self.model._Block(x),sq ) 469 470 self.assertEqual(self.model.run(x), pq*sq+bck) 513 471 514 472 if __name__ == '__main__':
Note: See TracChangeset
for help on using the changeset viewer.