source: sasview/sansmodels/src/sans/models/test/utest_evaldist.py @ 83195f7

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 83195f7 was ecc58e72, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

sansmodels: changed BaseComponent?.evalDistribution to match the C API. Documented the API. Added unit tests to test the output against the existing methods.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1"""
2    Test for the BaseComponent.evalDistribution
3    See method documentation for more details
4"""
5import unittest
6import numpy, math
7from sans.models.SphereModel import SphereModel
8from sans.models.Cos import Cos
9
10
11class TestEvalPythonMethods(unittest.TestCase):
12    """ Testing evalDistribution for pure python models """
13
14    def setUp(self):
15        self.model= Cos()
16       
17    def test_scalar_methods(self):
18        """
19            Simple test comparing the run(), runXY() and
20            evalDistribution methods
21        """
22        q1 = self.model.run(0.001)
23        q2 = self.model.runXY(0.001)
24        qlist3 = numpy.asarray([0.001, 0.002])
25        q3 = self.model.evalDistribution(qlist3)
26        q4 = self.model.run(0.002)
27
28        self.assertEqual(q1, q2)
29        self.assertEqual(q1, q3[0])
30        self.assertEqual(q4, q3[1])
31       
32    def test_XY_methods(self):
33        """
34            Compare to the runXY() method for 2D models.
35           
36                      +--------+--------+--------+
37            qy=0.009  |        |        |        |
38                      +--------+--------+--------+
39            qy-0.006  |        |        |        |
40                      +--------+--------+--------+           
41            qy=0.003  |        |        |        |
42                      +--------+--------+--------+
43                      qx=0.001   0.002   0.003
44           
45        """
46        # These are the expected values for all bins
47        expected = numpy.zeros([3,3])
48        for i in range(3):
49            for j in range(3):
50                q_length = math.sqrt( (0.001*(i+1.0))*(0.001*(i+1.0)) + (0.003*(j+1.0))*(0.003*(j+1.0)) )
51                expected[i][j] = self.model.run(q_length)
52       
53        qx_values = [0.001, 0.002, 0.003]
54        qy_values = [0.003, 0.006, 0.009]
55       
56        qx = numpy.asarray(qx_values)
57        qy = numpy.asarray(qy_values)
58       
59        qx_prime = numpy.reshape(qx, [3,1])
60        qy_prime = numpy.reshape(qy, [1,3])
61       
62        iq = self.model.evalDistribution([qx_prime, qy_prime])
63
64        for i in range(3):
65            for j in range(3):
66                self.assertAlmostEquals(iq[i][j], expected[i][j])
67
68    def test_rectangle_methods(self):
69        """
70            Compare to the runXY() method for 2D models
71            with a non-square matrix.
72            TODO: Doesn't work for C models apparently
73       
74                      +--------+--------+--------+
75            qy-0.006  |        |        |        |
76                      +--------+--------+--------+           
77            qy=0.003  |        |        |        |
78                      +--------+--------+--------+
79                      qx=0.001   0.002   0.003
80           
81        """
82        # These are the expected values for all bins
83        expected = numpy.zeros([3,3])
84        for i in range(3):
85            for j in range(2):
86                q_length = math.sqrt( (0.001*(i+1.0))*(0.001*(i+1.0)) + (0.003*(j+1.0))*(0.003*(j+1.0)) )
87                expected[i][j] = self.model.run(q_length)
88       
89        qx_values = [0.001, 0.002, 0.003]
90        qy_values = [0.003, 0.006]
91       
92        qx = numpy.asarray(qx_values)
93        qy = numpy.asarray(qy_values)
94       
95        qx_prime = numpy.reshape(qx, [3,1])
96        qy_prime = numpy.reshape(qy, [1,2])
97       
98        iq = self.model.evalDistribution([qx_prime, qy_prime])
99       
100        for i in range(3):
101            for j in range(2):
102                self.assertAlmostEquals(iq[i][j], expected[i][j])
103
104
105               
106class TestEvalMethods(unittest.TestCase):
107    """ Testing evalDistribution for C models """
108
109    def setUp(self):
110        self.model= SphereModel()
111       
112    def test_scalar_methods(self):
113        """
114            Simple test comparing the run(), runXY() and
115            evalDistribution methods
116        """
117        q1 = self.model.run(0.001)
118        q2 = self.model.runXY(0.001)
119        q4 = self.model.run(0.002)
120        qlist3 = numpy.asarray([0.001, 0.002])
121        q3 = self.model.evalDistribution(qlist3)
122
123        self.assertEqual(q1, q2)
124        self.assertEqual(q1, q3[0])
125        self.assertEqual(q4, q3[1])
126       
127    def test_XY_methods(self):
128        """
129            Compare to the runXY() method for 2D models.
130           
131                      +--------+--------+--------+
132            qy=0.009  |        |        |        |
133                      +--------+--------+--------+
134            qy-0.006  |        |        |        |
135                      +--------+--------+--------+           
136            qy=0.003  |        |        |        |
137                      +--------+--------+--------+
138                      qx=0.001   0.002   0.003
139           
140        """
141        # These are the expected values for all bins
142        expected = numpy.zeros([3,3])
143        for i in range(3):
144            for j in range(3):
145                q_length = math.sqrt( (0.001*(i+1.0))*(0.001*(i+1.0)) + (0.003*(j+1.0))*(0.003*(j+1.0)) )
146                expected[i][j] = self.model.run(q_length)
147       
148        qx_values = [0.001, 0.002, 0.003]
149        qy_values = [0.003, 0.006, 0.009]
150       
151        qx = numpy.asarray(qx_values)
152        qy = numpy.asarray(qy_values)
153       
154        qx_prime = numpy.reshape(qx, [3,1])
155        qy_prime = numpy.reshape(qy, [1,3])
156       
157        iq = self.model.evalDistribution([qx_prime, qy_prime])
158       
159        for i in range(3):
160            for j in range(3):
161                self.assertAlmostEquals(iq[i][j], expected[i][j])
162               
163
164if __name__ == '__main__':
165    unittest.main()
Note: See TracBrowser for help on using the repository browser.