source: sasview/test/sasguiframe/test/utest_manipulations.py @ 9a5097c

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.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 9a5097c was 9a5097c, checked in by andyfaff, 7 years ago

MAINT: import numpy as np

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