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]#Non ASCII charater |
---|
20 | #details['contrast'] = ['[1/A^2]', None, None] #Non ASCII charater |
---|
21 | details['background'] = ['[1/cm]', 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 | |
---|
30 | #Radius of two models should not be equal.. |
---|
31 | def test_multplication_radius(self): |
---|
32 | "" "test multiplication model""" |
---|
33 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
34 | from sans.models.CylinderModel import CylinderModel |
---|
35 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
36 | model1 = CylinderModel() |
---|
37 | model1.setParam("radius", 20) |
---|
38 | model1.setParam("length", 400) |
---|
39 | model2 = HardsphereStructure() |
---|
40 | model = MultiplicationModel(model1,model2 ) |
---|
41 | model.setParam("radius", 20) |
---|
42 | self.assertEqual(model.getParam("radius"), 20) |
---|
43 | #self.assertEqual(model.model1.getParam("radius"),3) |
---|
44 | from sans.models.DiamCylFunc import DiamCylFunc |
---|
45 | model4= DiamCylFunc() |
---|
46 | radius= model4.run(0.0) |
---|
47 | self.assertEqual(model.model2.getParam("radius"),73.340133152616076) |
---|
48 | |
---|
49 | def test_multplication_radius1(self): |
---|
50 | "" "test multiplication model""" |
---|
51 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
52 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
53 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
54 | model1 = EllipsoidModel() |
---|
55 | model1.setParam("radius_a", 20) |
---|
56 | model1.setParam("radius_b", 400) |
---|
57 | model2 = HardsphereStructure() |
---|
58 | model = MultiplicationModel(model1,model2 ) |
---|
59 | model.setParam("radius_a", 20) |
---|
60 | self.assertEqual(model.getParam("radius_a"), 20) |
---|
61 | #self.assertEqual(model.model1.getParam("radius"),3) |
---|
62 | from sans.models.DiamEllipFunc import DiamEllipFunc |
---|
63 | model4= DiamEllipFunc() |
---|
64 | radius= model4.run(0.0) |
---|
65 | self.assertEqual(model.model2.getParam("radius"),270.74504487588121) |
---|
66 | |
---|
67 | def test_multiplication(self): |
---|
68 | """ test multiplication model""" |
---|
69 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
70 | from sans.models.SphereModel import SphereModel |
---|
71 | from sans.models.NoStructure import NoStructure |
---|
72 | model1 = MultiplicationModel(SphereModel(),NoStructure()) |
---|
73 | model2 = SphereModel() |
---|
74 | x= 2 |
---|
75 | a = model1.run(x) |
---|
76 | |
---|
77 | b= model2.run(x) |
---|
78 | self.assertEqual(a, b) |
---|
79 | model2.setParam("scale", 10) |
---|
80 | c= model2.run(x) |
---|
81 | self.assertEqual(c, 10*b) |
---|
82 | model1.setParam("scale", 10) |
---|
83 | d= model1.run(x) |
---|
84 | self.assertEqual(d, 10*a) |
---|
85 | self.assertEqual(model1.getParam("scale"), 10) |
---|
86 | |
---|
87 | |
---|
88 | def testMultiplicationModel(self): |
---|
89 | """ Test Multiplication sphere with SquareWellStructure""" |
---|
90 | ## test details dictionary |
---|
91 | #self.assertEqual(self.model3.details, self.details) |
---|
92 | |
---|
93 | ## test parameters list |
---|
94 | list3= self.model3.getParamList() |
---|
95 | for item in self.model1.getParamList(): |
---|
96 | self.assert_(item in list3) |
---|
97 | for item in self.model2.getParamList(): |
---|
98 | self.assert_(item in list3) |
---|
99 | |
---|
100 | ## test set value for parameters and get paramaters |
---|
101 | self.model3.setParam("scale", 15) |
---|
102 | self.assertEqual(self.model3.getParam("scale"), 15) |
---|
103 | self.model3.setParam("radius", 20) |
---|
104 | self.assertEqual(self.model3.getParam("radius"), 20) |
---|
105 | self.model3.setParam("radius.width", 15) |
---|
106 | self.assertEqual(self.model3.getParam("radius.width"), 15) |
---|
107 | |
---|
108 | ## Dispersity |
---|
109 | list3= self.model3.getDispParamList() |
---|
110 | self.assertEqual(list3, ['radius.npts', 'radius.nsigmas', 'radius.width']) |
---|
111 | |
---|
112 | from sans.models.dispersion_models import ArrayDispersion |
---|
113 | disp_th = ArrayDispersion() |
---|
114 | |
---|
115 | values_th = numpy.zeros(100) |
---|
116 | weights = numpy.zeros(100) |
---|
117 | for i in range(100): |
---|
118 | values_th[i]=(math.pi/99.0*i) |
---|
119 | weights[i]=(1.0) |
---|
120 | |
---|
121 | disp_th.set_weights(values_th, weights) |
---|
122 | |
---|
123 | self.model3.set_dispersion('radius', disp_th) |
---|
124 | |
---|
125 | val_1d = self.model3.run(math.sqrt(0.0002)) |
---|
126 | val_2d = self.model3.runXY([0.01,0.01]) |
---|
127 | |
---|
128 | self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02) |
---|
129 | model4= self.model3.clone() |
---|
130 | self.assertEqual(model4.getParam("radius"), 20) |
---|
131 | |
---|
132 | if __name__ == '__main__': |
---|
133 | unittest.main() |
---|