source: sasview/test/sasguiframe/test/utest_manipulations.py @ b699768

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 b699768 was b699768, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 8 years ago

Initial commit of the refactored SasCalc? module.

  • Property mode set to 100644
File size: 12.6 KB
Line 
1"""
2    Unit tests for data manipulations
3"""
4#TODO: what happens if you add a Data1D to a Data2D?
5
6import unittest
7import numpy, math
8from sas.sascalc.dataloader.loader import  Loader
9from sas.guiframe.dataFitting import Data1D, Data2D
10from sas.guiframe.dataFitting import Data1D as Theory1D
11 
12import os.path
13
14class data_info_tests(unittest.TestCase):
15   
16    def setUp(self):
17        data = Loader().load("cansas1d.xml")
18        self.data = data[0]
19       
20    def test_clone1D(self):
21        """
22            Test basic cloning
23        """
24        clone = self.data.clone_without_data()
25       
26        for i in range(len(self.data.detector)):
27            self.assertEqual(self.data.detector[i].distance, clone.detector[i].distance)
28           
29class theory1d_tests(unittest.TestCase):
30   
31    def setUp(self):
32        data = Loader().load("cansas1d.xml")
33        self.data = data[0]
34       
35    def test_clone_theory1D(self):
36        """
37            Test basic cloning
38        """
39        theory = Theory1D(x=[], y=[], dy=None)
40        theory.clone_without_data(clone=self.data)
41        theory.copy_from_datainfo(data1d=self.data)
42        for i in range(len(self.data.detector)):
43            self.assertEqual(self.data.detector[i].distance, theory.detector[i].distance)
44           
45        for i in range(len(self.data.x)):
46            self.assertEqual(self.data.x[i], theory.x[i])
47            self.assertEqual(self.data.y[i], theory.y[i])
48            self.assertEqual(self.data.dy[i], theory.dy[i])
49
50class manip_tests(unittest.TestCase):
51   
52    def setUp(self):
53        # Create two data sets to play with
54        x_0 = numpy.ones(5)
55        for i in range(5):
56            x_0[i] = x_0[i]*(i+1.0)
57           
58        y_0 = 2.0*numpy.ones(5)
59        dy_0 = 0.5*numpy.ones(5)
60        self.data = Data1D(x_0, y_0, dy=dy_0)
61       
62        x = self.data.x
63        y = numpy.ones(5)
64        dy = numpy.ones(5)
65        self.data2 = Data1D(x, y, dy=dy)
66       
67       
68    def test_load(self):
69        """
70            Test whether the test file was loaded properly
71        """
72        # There should be 5 entries in the file
73        self.assertEqual(len(self.data.x), 5)
74       
75        for i in range(5):
76            # The x values should be from 1 to 5
77            self.assertEqual(self.data.x[i], float(i+1))
78       
79            # All y-error values should be 0.5
80            self.assertEqual(self.data.dy[i], 0.5)   
81           
82            # All y values should be 2.0
83            self.assertEqual(self.data.y[i], 2.0)   
84       
85    def test_add(self):
86        result = self.data2+self.data
87        for i in range(5):
88            self.assertEqual(result.y[i], 3.0)
89            self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0))
90       
91    def test_sub(self):
92        result = self.data2-self.data
93        for i in range(5):
94            self.assertEqual(result.y[i], -1.0)
95            self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0))
96       
97    def test_mul(self):
98        result = self.data2*self.data
99        for i in range(5):
100            self.assertEqual(result.y[i], 2.0)
101            self.assertEqual(result.dy[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2))
102       
103    def test_div(self):
104        result = self.data2/self.data
105        for i in range(5):
106            self.assertEqual(result.y[i], 0.5)
107            self.assertEqual(result.dy[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2))
108       
109    def test_radd(self):
110        result = self.data+3.0
111        for i in range(5):
112            self.assertEqual(result.y[i], 5.0)
113            self.assertEqual(result.dy[i], 0.5)
114           
115        result = 3.0+self.data
116        for i in range(5):
117            self.assertEqual(result.y[i], 5.0)
118            self.assertEqual(result.dy[i], 0.5)
119           
120    def test_rsub(self):
121        result = self.data-3.0
122        for i in range(5):
123            self.assertEqual(result.y[i], -1.0)
124            self.assertEqual(result.dy[i], 0.5)
125           
126        result = 3.0-self.data
127        for i in range(5):
128            self.assertEqual(result.y[i], 1.0)
129            self.assertEqual(result.dy[i], 0.5)
130           
131    def test_rmul(self):
132        result = self.data*3.0
133        for i in range(5):
134            self.assertEqual(result.y[i], 6.0)
135            self.assertEqual(result.dy[i], 1.5)
136           
137        result = 3.0*self.data
138        for i in range(5):
139            self.assertEqual(result.y[i], 6.0)
140            self.assertEqual(result.dy[i], 1.5)
141           
142    def test_rdiv(self):
143        result = self.data/4.0
144        for i in range(5):
145            self.assertEqual(result.y[i], 0.5)
146            self.assertEqual(result.dy[i], 0.125)
147           
148        result = 6.0/self.data
149        for i in range(5):
150            self.assertEqual(result.y[i], 3.0)
151            self.assertEqual(result.dy[i], 6.0*0.5/4.0)
152           
153class manip_2D(unittest.TestCase):
154   
155    def setUp(self):
156        # Create two data sets to play with
157        x_0 = 2.0*numpy.ones(25)
158        dx_0 = 0.5*numpy.ones(25)
159        qx_0 = numpy.arange(25)
160        qy_0 = numpy.arange(25)
161        mask_0 = numpy.zeros(25)
162        dqx_0 = numpy.arange(25)/100
163        dqy_0 = numpy.arange(25)/100
164        q_0 = numpy.sqrt(qx_0 * qx_0 + qy_0 * qy_0)
165        self.data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, 
166                           qy_data=qy_0, q_data=q_0, mask=mask_0, 
167                           dqx_data=dqx_0, dqy_data=dqy_0)
168       
169        y = numpy.ones(25)
170        dy = numpy.ones(25)
171        qx = numpy.arange(25)
172        qy = numpy.arange(25)
173        mask = numpy.zeros(25)
174        q = numpy.sqrt(qx * qx + qy * qy)
175        self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, 
176                            q_data=q, mask=mask)
177       
178       
179    def test_load(self):
180        """
181            Test whether the test file was loaded properly
182        """
183        # There should be 5 entries in the file
184        self.assertEqual(numpy.size(self.data.data), 25)
185       
186        for i in range(25):
187            # All y-error values should be 0.5
188            self.assertEqual(self.data.err_data[i], 0.5)   
189           
190            # All y values should be 2.0
191            self.assertEqual(self.data.data[i], 2.0)   
192       
193    def test_add(self):
194        result = self.data2+self.data
195        for i in range(25):
196            self.assertEqual(result.data[i], 3.0)
197            self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0))
198       
199    def test_sub(self):
200        result = self.data2-self.data
201        for i in range(25):
202                self.assertEqual(result.data[i], -1.0)
203                self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0))
204       
205    def test_mul(self):
206        result = self.data2*self.data
207        for i in range(25):
208            self.assertEqual(result.data[i], 2.0)
209            self.assertEqual(result.err_data[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2))
210       
211    def test_div(self):
212        result = self.data2/self.data
213        for i in range(25):
214            self.assertEqual(result.data[i], 0.5)
215            self.assertEqual(result.err_data[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2))
216       
217    def test_radd(self):
218        result = self.data+3.0
219        for i in range(25):
220            self.assertEqual(result.data[i], 5.0)
221            self.assertEqual(result.err_data[i], 0.5)
222   
223        result = 3.0+self.data
224        for i in range(25):
225            self.assertEqual(result.data[i], 5.0)
226            self.assertEqual(result.err_data[i], 0.5)
227           
228    def test_rsub(self):
229        result = self.data-3.0
230        for i in range(25):
231            self.assertEqual(result.data[i], -1.0)
232            self.assertEqual(result.err_data[i], 0.5)
233   
234        result = 3.0-self.data
235        for i in range(25):
236            self.assertEqual(result.data[i], 1.0)
237            self.assertEqual(result.err_data[i], 0.5)
238           
239    def test_rmul(self):
240        result = self.data*3.0
241        for i in range(25):
242            self.assertEqual(result.data[i], 6.0)
243            self.assertEqual(result.err_data[i], 1.5)
244 
245        result = 3.0*self.data
246        for i in range(25):
247            self.assertEqual(result.data[i], 6.0)
248            self.assertEqual(result.err_data[i], 1.5)
249           
250    def test_rdiv(self):
251        result = self.data/4.0
252        for i in range(25):
253            self.assertEqual(result.data[i], 0.5)
254            self.assertEqual(result.err_data[i], 0.125)
255
256        result = 6.0/self.data
257        for i in range(25):
258            self.assertEqual(result.data[i], 3.0)
259            self.assertEqual(result.err_data[i], 6.0*0.5/4.0)
260   
261class extra_manip_2D(unittest.TestCase):
262   
263    def setUp(self):
264        # Create two data sets to play with
265        x_0 = 2.0*numpy.ones(25)
266        dx_0 = 0.5*numpy.ones(25)
267        qx_0 = numpy.arange(25)
268        qy_0 = numpy.arange(25)
269        mask_0 = numpy.zeros(25)
270        dqx_0 = numpy.arange(25)/100
271        dqy_0 = numpy.arange(25)/100
272        q_0 = numpy.sqrt(qx_0 * qx_0 + qy_0 * qy_0)
273        self.data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, 
274                           qy_data=qy_0, q_data=q_0, mask=mask_0, 
275                           dqx_data=dqx_0, dqy_data=dqy_0)
276       
277        y = numpy.ones(25)
278        dy = numpy.ones(25)
279        qx = numpy.arange(25)
280        qy = numpy.arange(25)
281        mask = numpy.zeros(25)
282        q = numpy.sqrt(qx * qx + qy * qy)
283        self.data2 = Data2D(image=y, err_image=dy, qx_data=qx, qy_data=qy, 
284                            q_data=q, mask=mask)
285       
286       
287    def test_load(self):
288        """
289            Test whether the test file was loaded properly
290        """
291        # There should be 5 entries in the file
292        self.assertEqual(numpy.size(self.data.data), 25)
293       
294        for i in range(25):
295            # All y-error values should be 0.5
296            self.assertEqual(self.data.err_data[i], 0.5)   
297           
298            # All y values should be 2.0
299            self.assertEqual(self.data.data[i], 2.0)   
300       
301    def test_add(self):
302        result = self.data2+self.data
303        for i in range(25):
304            self.assertEqual(result.data[i], 3.0)
305            self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0))
306       
307    def test_sub(self):
308        result = self.data2-self.data
309        for i in range(25):
310                self.assertEqual(result.data[i], -1.0)
311                self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0))
312       
313    def test_mul(self):
314        result = self.data2*self.data
315        for i in range(25):
316            self.assertEqual(result.data[i], 2.0)
317            self.assertEqual(result.err_data[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2))
318       
319    def test_div(self):
320        result = self.data2/self.data
321        for i in range(25):
322            self.assertEqual(result.data[i], 0.5)
323            self.assertEqual(result.err_data[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2))
324       
325    def test_radd(self):
326        result = self.data+3.0
327        for i in range(25):
328            self.assertEqual(result.data[i], 5.0)
329            self.assertEqual(result.err_data[i], 0.5)
330   
331        result = 3.0+self.data
332        for i in range(25):
333            self.assertEqual(result.data[i], 5.0)
334            self.assertEqual(result.err_data[i], 0.5)
335           
336    def test_rsub(self):
337        result = self.data-3.0
338        for i in range(25):
339            self.assertEqual(result.data[i], -1.0)
340            self.assertEqual(result.err_data[i], 0.5)
341   
342        result = 3.0-self.data
343        for i in range(25):
344            self.assertEqual(result.data[i], 1.0)
345            self.assertEqual(result.err_data[i], 0.5)
346           
347    def test_rmul(self):
348        result = self.data*3.0
349        for i in range(25):
350            self.assertEqual(result.data[i], 6.0)
351            self.assertEqual(result.err_data[i], 1.5)
352 
353        result = 3.0*self.data
354        for i in range(25):
355            self.assertEqual(result.data[i], 6.0)
356            self.assertEqual(result.err_data[i], 1.5)
357           
358    def test_rdiv(self):
359        result = self.data/4.0
360        for i in range(25):
361            self.assertEqual(result.data[i], 0.5)
362            self.assertEqual(result.err_data[i], 0.125)
363
364        result = 6.0/self.data
365        for i in range(25):
366            self.assertEqual(result.data[i], 3.0)
367            self.assertEqual(result.err_data[i], 6.0*0.5/4.0)
368
369if __name__ == '__main__':
370    unittest.main()
371   
Note: See TracBrowser for help on using the repository browser.