source: sasview/sansmodels/test/utest_nonshape.py @ 32ea318

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 32ea318 was 18e250c, checked in by Gervaise Alina <gervyh@…>, 13 years ago

move test to sansmodels top level

  • Property mode set to 100644
File size: 17.9 KB
Line 
1"""
2    Unit tests for non shape based model (Task 8.2.1)
3    These tests are part of the requirements
4"""
5
6import unittest, time, math
7from scipy.special import erf,gammaln
8
9# Disable "missing docstring" complaint
10# pylint: disable-msg=C0111
11# Disable "too many methods" complaint
12# pylint: disable-msg=R0904
13# Disable "could be a function" complaint
14# pylint: disable-msg=R0201
15
16import scipy 
17class TestGuinier(unittest.TestCase):
18    """
19        Unit tests for Guinier function
20       
21        F(x) = exp[ [A] + [B]*Q**2 ]
22       
23        The model has two parameters: A and B
24    """
25    def _func(self, a, b, x):
26        return a*math.exp(-(b*x)**2/3.0)
27   
28    def setUp(self):
29        from sans.models.GuinierModel import GuinierModel
30        self.model= GuinierModel()
31       
32    def test1D(self):
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        self.assertEqual(self.model.runXY(2.0), 2.0*math.exp(-(1.0*2.0)**2/3.0))
39       
40    def test2D(self):
41        self.model.setParam('scale', 2.0)
42        self.model.setParam('rg', 1.0)
43       
44        #value = self._func(2.0, 1.0, 1.0)*self._func(2.0, 1.0, 2.0)
45        value = self._func(2.0, 1.0, math.sqrt(5.0))
46        #self.assertEqual(self.model.runXY([0.0,0.0]), 2.0*2.0)
47        self.assertEqual(self.model.runXY([0.0,0.0]), 2.0)
48        self.assertEqual(self.model.runXY([1.0,2.0]), value)
49       
50    def test2Dphi(self):
51        self.model.setParam('scale', 2.0)
52        self.model.setParam('rg', 1.0)
53       
54        x = 1.0
55        y = 2.0
56        r = math.sqrt(x**2 + y**2)
57        phi = math.atan2(y, x)
58       
59        #value = self._func(2.0, 1.0, x)*self._func(2.0, 1.0, y)
60        value = self._func(2.0, 1.0, r)
61       
62        #self.assertEqual(self.model.run([r, phi]), value)
63        self.assertAlmostEquals(self.model.run([r, phi]), value,1)
64       
65       
66class TestPorod(unittest.TestCase):
67    """
68        Unit tests for Porod function
69       
70        F(x) = C/Q**4
71       
72        The model has one parameter: C
73    """
74    def _func(self, c, x):
75        return c/(x**4)
76   
77    def setUp(self):
78        from sans.models.PorodModel import PorodModel
79        self.model= PorodModel()
80        self.model.setParam('scale', 2.0)       
81       
82    def test1D(self):
83        value = self._func(2.0, 3.0)
84        self.assertEqual(self.model.run(3.0), value)
85        self.assertEqual(self.model.runXY(3.0), value)
86       
87    def test2D(self):
88        #value = self._func(2.0, 1.0)*self._func(2.0, 2.0)
89        value = self._func(2.0, math.sqrt(5.0))
90        self.assertEqual(self.model.runXY([1.0,2.0]), value)
91       
92    def test2Dphi(self):
93        x = 1.0
94        y = 2.0
95        r = math.sqrt(x**2 + y**2)
96        phi = math.atan2(y, x)
97       
98        #value = self._func(2.0, 1.0)*self._func(2.0, 2.0)
99        value = self._func(2.0, r)
100        self.assertAlmostEquals(self.model.run([r, phi]), value,1)
101       
102class TestDebye(unittest.TestCase):
103    """
104        Unit tests for Debye function
105       
106        F(x) = 2( exp(-x)+x -1 )/x**2
107       
108        The model has three parameters:
109            Rg     =  radius of gyration
110            scale  =  scale factor
111            bkd    =  Constant background
112    """
113    def _func(self, Rg, scale, bkg, x):
114        y = (Rg * x)**2
115        return scale * (2*(math.exp(-y) + y -1)/y**2) + bkg
116   
117    def setUp(self):
118        from sans.models.DebyeModel import DebyeModel
119        self.model= DebyeModel()
120        self.model.setParam('rg', 50.0)   
121        self.model.setParam('scale',1.0) 
122        self.model.setParam('background',0.001)   
123       
124    def test1D(self):
125        value = self._func(50.0, 1.0, 0.001, 2.0)
126        self.assertEqual(self.model.run(2.0), value)
127        self.assertEqual(self.model.runXY(2.0), value)
128
129        # User enter zero as a value of x, y= 1
130        self.assertAlmostEqual(self.model.run(0.0), 1.00, 2)
131       
132    def test1D_clone(self):
133        value = self._func(50.0, 1.0, 10.0, 2.0)
134        self.model.setParam('background', 10.0)
135        clone = self.model.clone()
136        self.assertEqual(clone.run(2.0), value)
137        self.assertEqual(clone.runXY(2.0), value)
138       
139        # User enter zero as a value of x
140        # An exceptio is raised: No more exception
141        #self.assertRaises(ZeroDivisionError, clone.run, 0.0)
142       
143    def test2D(self):
144        #value = self._func(50.0, 1.0, 0.001, 1.0)*self._func(50.0, 1.0, 0.001, 2.0)
145        value = self._func(50.0, 1.0, 0.001, math.sqrt(5.0))
146        self.assertEqual(self.model.runXY([1.0,2.0]), value)
147       
148    def test2Dphi(self):
149        x = 1.0
150        y = 2.0
151        r = math.sqrt(x**2 + y**2)
152        phi = math.atan2(y, x)
153       
154        value = self._func(50.0, 1.0, 0.001, 1.0)*self._func(50.0, 1.0, 0.001, 2.0)
155        self.assertAlmostEquals(self.model.run([r, phi]), value,1)
156       
157       
158class TestLorentz(unittest.TestCase):
159    """
160        Unit tests for Lorentz function
161       
162         F(x) = scale/( 1 + (x*L)^2 ) + bkd
163       
164        The model has three parameters:
165            L     =  screen Length
166            scale  =  scale factor
167            bkd    =  incoherent background
168    """
169    def _func(self, I0 , L, bgd, qval):
170        return I0/(1.0 + (qval*L)*(qval*L)) + bgd
171   
172    def setUp(self):
173        from sans.models.LorentzModel import LorentzModel
174        self.model= LorentzModel()
175       
176    def test1D(self):
177        self.model.setParam('scale', 100.0)
178        self.model.setParam('Length', 50.0)
179        self.model.setParam('background', 1.0)
180       
181        self.assertEqual(self.model.run(0.0), 101.0)
182        self.assertEqual(self.model.run(2.0), self._func(100.0, 50.0, 1.0, 2.0))
183        self.assertEqual(self.model.runXY(2.0), self._func(100.0, 50.0, 1.0, 2.0))
184       
185    def test2D(self):
186        self.model.setParam('scale', 100.0)
187        self.model.setParam('Length', 50.0)
188        self.model.setParam('background', 1.0)
189       
190        #value = self._func(100.0, 50.0, 1.0, 1.0)*self._func(100.0, 50.0, 1.0, 2.0)   
191        value = self._func(100.0, 50.0, 1.0, math.sqrt(5.0)) 
192        self.assertEqual(self.model.runXY([1.0,2.0]), value)
193       
194    def test2Dphi(self):
195        self.model.setParam('scale', 100.0)
196        self.model.setParam('Length', 50.0)
197        self.model.setParam('background', 1.0)
198       
199        x = 1.0
200        y = 2.0
201        r = math.sqrt(x**2 + y**2)
202        phi = math.atan2(y, x)
203       
204        value = self._func(100.0, 50.0, 1.0, x)*self._func(100.0, 50.0, 1.0, y)
205        self.assertAlmostEquals(self.model.run([r, phi]), value,1)
206       
207class TestDAB(unittest.TestCase):
208    """
209        Unit tests for DAB function
210       
211         F(x) = scale/( 1 + (x*L)^2 )^(2) + bkd
212       
213        The model has three parameters:
214            L      =  Correlation Length
215            scale  =  scale factor
216            bkd    =  incoherent background
217    """
218    def _func(self, Izero, range, incoh, qval):
219        return Izero* pow(range,3)/pow((1.0 + (qval*range)*(qval*range)),2) + incoh
220   
221    def setUp(self):
222        from sans.models.DABModel import DABModel
223        self.model= DABModel()
224        self.scale = 10.0
225        self.length = 40.0
226        self.back = 1.0
227       
228        self.model.setParam('scale', self.scale)
229        self.model.setParam('length', self.length)
230        self.model.setParam('background', self.back)
231       
232    def test1D(self):
233
234        self.assertEqual(self.model.run(2.0), self._func(self.scale, self.length, self.back, 2.0))
235        self.assertEqual(self.model.runXY(2.0), self._func(self.scale, self.length, self.back, 2.0))
236       
237    def test2D(self):
238        #value = self._func(self.scale, self.length, self.back, 1.0)*self._func(self.scale, self.length, self.back, 2.0)   
239        value = self._func(self.scale, self.length, self.back, math.sqrt(5.0))
240        self.assertEqual(self.model.runXY([1.0,2.0]), value)
241       
242    def test2Dphi(self):
243        x = 1.0
244        y = 2.0
245        r = math.sqrt(x**2 + y**2)
246        phi = math.atan2(y, x)
247       
248        value = self._func(self.scale, self.length, self.back, x)*self._func(self.scale, self.length, self.back, y)
249        self.assertAlmostEquals(self.model.run([x, y]), value,1)
250       
251class TestPowerLaw(unittest.TestCase):
252    """
253        Unit tests for PowerLaw function
254
255        F(x) = scale* (x)^(m) + bkd
256       
257        The model has three parameters:
258            m     =  power
259            scale  =  scale factor
260            bkd    =  incoherent background
261    """
262    def _func(self, a, m, bgd, qval):
263        return a*math.pow(qval,-m) + bgd
264   
265   
266    def setUp(self):
267        from sans.models.PowerLawModel import PowerLawModel
268        self.model= PowerLawModel()
269       
270    def test1D(self):
271        self.model.setParam('scale', math.exp(-6))
272        self.model.setParam('m', 4.0)
273        self.model.setParam('background', 1.0)
274       
275        #self.assertEqual(self.model.run(0.0), 1.0)
276        self.assertEqual(self.model.run(2.0), self._func(math.exp(-6), 4.0, 1.0, 2.0))
277        self.assertEqual(self.model.runXY(2.0), self._func(math.exp(-6), 4.0, 1.0, 2.0))
278   
279    def testlimit(self):
280        self.model.setParam('scale', math.exp(-6))
281        self.model.setParam('m', -4.0)
282        self.model.setParam('background', 1.0)
283       
284        self.assertEqual(self.model.run(0.0), 1.0)
285       
286    def test2D(self):
287        self.model.setParam('scale', math.exp(-6))
288        self.model.setParam('m', 4.0)
289        self.model.setParam('background', 1.0)
290       
291        #value = self._func(math.exp(-6), 4.0, 1.0, 1.0)\
292        #*self._func(math.exp(-6), 4.0, 1.0, 2.0)   
293        value = self._func(math.exp(-6), 4.0, 1.0, math.sqrt(5.0))
294       
295        self.assertEqual(self.model.runXY([1.0,2.0]), value)
296       
297    def test2Dphi(self):
298        self.model.setParam('scale', math.exp(-6))
299        self.model.setParam('m', 4.0)
300        self.model.setParam('background', 1.0)
301       
302        x = 1.0
303        y = 2.0
304        r = math.sqrt(x**2 + y**2)
305        phi = math.atan2(y, x)
306       
307        value = self._func(math.exp(-6), 4.0, 1.0, x)\
308        *self._func(math.exp(-6), 4.0, 1.0, y)
309        self.assertAlmostEquals(self.model.run([r, phi]), value,1)
310               
311class TestTeubnerStrey(unittest.TestCase):
312    """
313        Unit tests for PowerLaw function
314
315        F(x) = 1/( scale + c1*(x)^(2)+  c2*(x)^(4)) + bkd
316       
317        The model has Four parameters:
318            scale  =  scale factor
319            c1     =  constant
320            c2     =  constant
321            bkd    =  incoherent background
322    """
323    def _func(self, scale, c1, c2, bck, q):
324       
325        q2 = q*q;
326        q4 = q2*q2;
327   
328        return 1.0/(scale + c1*q2+c2*q4) + bck
329   
330    def setUp(self):
331        from sans.models.TeubnerStreyModel import TeubnerStreyModel
332        self.model= TeubnerStreyModel()
333       
334    def test1D(self):
335       
336        self.model.setParam('c1', -30.0) 
337        self.model.setParam('c2', 5000.0) 
338        self.model.setParam('scale', 0.1)
339        self.model.setParam('background', 0.1)
340        #self.assertEqual(1/(math.sqrt(4)), math.pow(4,-1/2))
341        #self.assertEqual(self.model.TeubnerStreyLengths(),False )
342       
343        self.assertEqual(self.model.run(0.0), 10.1)
344        self.assertEqual(self.model.run(2.0), self._func(0.1,-30.0,5000.0,0.1,2.0))
345        self.assertEqual(self.model.runXY(2.0), self._func(0.1,-30.0,5000.0,0.1,2.0))
346       
347    def test2D(self):
348        self.model.setParam('c1', -30.0) 
349        self.model.setParam('c2', 5000.0) 
350        self.model.setParam('scale', 0.1)
351        self.model.setParam('background', 0.1)
352        #value = self._func(0.1,-30.0,5000.0,0.1, 1.0)\
353        #*self._func(0.1,-30.0,5000.0,0.1, 2.0)   
354        value = self._func(0.1,-30.0,5000.0,0.1, math.sqrt(5.0))
355       
356        self.assertEqual(self.model.runXY([1.0,2.0]), value)
357       
358    def test2Dphi(self):
359        self.model.setParam('c1', -30.0) 
360        self.model.setParam('c2', 5000.0) 
361        self.model.setParam('scale', 0.1)
362        self.model.setParam('background', 0.1)
363       
364        x = 1.0
365        y = 2.0
366        r = math.sqrt(x**2 + y**2)
367        phi = math.atan2(y, x)
368       
369        #value = self._func(0.1,-30.0,5000.0,0.1, x)\
370        #*self._func(0.1,-30.0,5000.0,0.1, y)
371        value = self._func(0.1,-30.0,5000.0,0.1, r)
372        self.assertAlmostEquals(self.model.run([r, phi]), value,1)
373       
374class TestBEPolyelectrolyte(unittest.TestCase):
375    """
376        Unit tests for  BEPolyelectrolyte function
377       
378        F(x) = K*1/(4*pi()*Lb*(alpha)^(2)*(q^(2)+k2)/(1+(r02)^(2))*(q^(2)+k2)\
379                       *(q^(2)-(12*h*C/b^(2)))
380       
381        The model has Eight parameters:
382            K        =  Constrast factor of the polymer
383            Lb       =  Bjerrum length
384            H        =  virial parameter
385            B        =  monomer length
386            Cs       =  Concentration of monovalent salt
387            alpha    =  ionazation degree
388            C        = polymer molar concentration
389            bkd      = background
390    """
391    def _func(self, K, Lb, H, B, Cs, alpha, C, bkd, r02, k2,  x):
392        return (K /( (4*math.pi *Lb*(alpha**2)*(x**2 +k2)) *( (1 +(r02**2))  \
393                    *((x**2) + k2)*((x**2) -(12 * H * C/(B**2))) )))+ bkd
394   
395    def setUp(self):
396        from sans.models.BEPolyelectrolyte import BEPolyelectrolyte
397        self.model= BEPolyelectrolyte()
398       
399        self.K = 10.0
400        self.Lb = 6.5
401        self.h = 11
402        self.b = 13
403        self.Cs = 0.1
404        self.alpha = 0.05
405        self.= .7
406        self.Bkd =0.01
407       
408        self.model.setParam('K', self.K) 
409        self.model.setParam('Lb', self.Lb) 
410        self.model.setParam('H', self.h)
411        self.model.setParam('B', self.b)
412        self.model.setParam('Cs',self.Cs) 
413        self.model.setParam('alpha', self.alpha) 
414        self.model.setParam('C', self.C)
415        self.model.setParam('background', self.Bkd)
416       
417    def _func(self, q):
418       
419        Ca = self.C *6.022136e-4         
420        Csa = self.Cs * 6.022136e-4       
421        k2= 4*math.pi*self.Lb*(2*self.Cs+self.alpha*Ca)   
422
423        r02 = 1./self.alpha / Ca**0.5*( self.b / (48*math.pi*self.Lb)**0.5 )
424        q2 = q**2   
425        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
426        return Sq
427       
428    def test1D(self):
429       
430       
431        q = 0.001
432   
433        self.assertEqual(self.model.run(q), self._func(q))
434        self.assertEqual(self.model.runXY(q), self._func(q))
435         
436    def test2D(self):
437        #self.assertAlmostEquals(self.model.runXY([1.0,2.0]), self._func(1.0)*self._func(2.0), 8)
438        self.assertAlmostEquals(self.model.runXY([1.0,2.0]), self._func(math.sqrt(1.0+2.0**2)), 8)
439       
440    def test2Dphi(self):
441
442        x = 1.0
443        y = 2.0
444        r = math.sqrt(x**2 + y**2)
445        phi = math.atan2(y, x)
446       
447        self.assertAlmostEquals(self.model.run([r, phi]), self._func(r), 8)
448       
449class TestFractalModel(unittest.TestCase):
450    """
451        Unit tests for  Number Density Fractal function   
452        F(x)= P(x)*S(x) + bkd
453        The model has Seven parameters:
454            scale   =  Volume fraction
455            Radius  =  Block radius
456            Fdim    =  Fractal dimension
457            L       =  correlation Length
458            SDLB    =  SDL block
459            SDLS    =  SDL solvent
460            bkd     =  background
461    """
462    def setUp(self):
463        from sans.models.FractalModel import FractalModel
464        self.model= FractalModel()
465        self.r0 = 5.0
466        self.sldp = 2.0e-6
467        self.sldm = 6.35e-6
468        self.phi = 0.05
469        self.Df = 2
470        self.corr = 100.0
471        self.bck = 1.0
472       
473        self.model.setParam('scale', self.phi) 
474        self.model.setParam('radius', self.r0) 
475        self.model.setParam('fractal_dim',self.Df)
476        self.model.setParam('cor_length', self.corr)
477        self.model.setParam('sldBlock', self.sldp) 
478        self.model.setParam('sldSolv', self.sldm) 
479        self.model.setParam('background', self.bck)
480       
481    def _func(self, x):
482        r0 = self.r0
483        sldp = self.sldp
484        sldm = self.sldm
485        phi = self.phi
486        Df = self.Df
487        corr = self.corr
488        bck = self.bck
489       
490        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);
491       
492        sq = Df*math.exp(gammaln(Df-1.0))*math.sin((Df-1.0)*math.atan(x*corr));
493        sq /= math.pow((x*r0),Df) * math.pow((1.0 + 1.0/(x*corr)/(x*corr)),((Df-1.0)/2.0));
494        sq += 1.0;
495       
496        #self.assertAlmostEqual(self.model._scatterRanDom(x), pq, 8 )
497        #self.assertEqual(self.model._Block(x),sq )
498       
499        return sq*pq+bck
500   
501    def test1D(self):       
502        x = 0.001
503       
504        iq = self._func(x)
505        self.assertEqual(self.model.run(x), iq)
506        self.assertEqual(self.model.runXY(x), iq)
507   
508    def test2D(self):
509        x = 1.0
510        y = 2.0
511        r = math.sqrt(x**2 + y**2)
512        phi = math.atan2(y, x)
513        iq_x = self._func(x)
514        iq_y = self._func(y)
515       
516        #self.assertEqual(self.model.run([r, phi]), iq_x*iq_y)
517        self.assertEqual(self.model.run([r, phi]), self.model.run(r))
518        #self.assertEqual(self.model.runXY([x,y]), iq_x*iq_y)
519        self.assertEqual(self.model.runXY([x,y]), self.model.run(r))
520       
521if __name__ == '__main__':
522    unittest.main()
Note: See TracBrowser for help on using the repository browser.