1 | """ |
---|
2 | Unit tests for specific models |
---|
3 | @author: Gervaise Alina / UTK |
---|
4 | """ |
---|
5 | |
---|
6 | import unittest, numpy,math |
---|
7 | |
---|
8 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
9 | from sans.models.SphereModel import SphereModel |
---|
10 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
11 | |
---|
12 | class TestDisperser(unittest.TestCase): |
---|
13 | """ Unit tests for sphere model * SquareWellStructure""" |
---|
14 | model1= SphereModel() |
---|
15 | model2= SquareWellStructure() |
---|
16 | model3= MultiplicationModel(model1, model2) |
---|
17 | details={} |
---|
18 | details['scale'] = ['', None, None] |
---|
19 | details['radius'] = ['A', None, None] |
---|
20 | details['contrast'] = ['A-2', None, None] |
---|
21 | details['background'] = ['cm-1', None, None] |
---|
22 | details['volfraction'] = ['', None, None] |
---|
23 | details['welldepth'] = ['kT', None, None] |
---|
24 | details['wellwidth'] = ['', None, None] |
---|
25 | |
---|
26 | ## fittable parameters |
---|
27 | fixed=[] |
---|
28 | fixed=['radius.width'] |
---|
29 | def testMultiplicationModel(self): |
---|
30 | """ Test Multiplication sphere with SquareWellStructure""" |
---|
31 | ## test details dictionary |
---|
32 | self.assertEqual(self.model3.details, self.details) |
---|
33 | |
---|
34 | ## test parameters list |
---|
35 | list3= self.model3.getParamList() |
---|
36 | for item in self.model1.getParamList(): |
---|
37 | self.assert_(item in list3) |
---|
38 | for item in self.model2.getParamList(): |
---|
39 | self.assert_(item in list3) |
---|
40 | |
---|
41 | ## test set value for parameters and get paramaters |
---|
42 | self.model3.setParam("scale", 15) |
---|
43 | self.assertEqual(self.model3.getParam("scale"), 15) |
---|
44 | self.model3.setParam("radius", 20) |
---|
45 | self.assertEqual(self.model3.getParam("radius"), 20) |
---|
46 | self.model3.setParam("radius.width", 15) |
---|
47 | self.assertEqual(self.model3.getParam("radius.width"), 15) |
---|
48 | |
---|
49 | ## Dispersity |
---|
50 | list3= self.model3.getDispParamList() |
---|
51 | self.assertEqual(list3, ['radius.npts', 'radius.nsigmas', 'radius.width']) |
---|
52 | |
---|
53 | from sans.models.dispersion_models import ArrayDispersion |
---|
54 | disp_th = ArrayDispersion() |
---|
55 | |
---|
56 | values_th = numpy.zeros(100) |
---|
57 | weights = numpy.zeros(100) |
---|
58 | for i in range(100): |
---|
59 | values_th[i]=(math.pi/99.0*i) |
---|
60 | weights[i]=(1.0) |
---|
61 | |
---|
62 | disp_th.set_weights(values_th, weights) |
---|
63 | |
---|
64 | self.model3.set_dispersion('radius', disp_th) |
---|
65 | |
---|
66 | val_1d = self.model3.run(math.sqrt(0.0002)) |
---|
67 | val_2d = self.model3.runXY([0.01,0.01]) |
---|
68 | |
---|
69 | self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02) |
---|
70 | model4= self.model3.clone() |
---|
71 | self.assertEqual(model4.getParam("radius"), 20) |
---|
72 | |
---|
73 | if __name__ == '__main__': |
---|
74 | unittest.main() |
---|