source: sasview/DataLoader/test/utest_manipulations.py @ 9198b83

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 9198b83 was 9198b83, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Added basic arithmetic for data class, with unit tests.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1"""
2    Unit tests for data manipulations
3"""
4
5import unittest
6import numpy, math
7from DataLoader.loader import  Loader
8from DataLoader.data_info import Data1D
9 
10import os.path
11
12class data_info_tests(unittest.TestCase):
13   
14    def setUp(self):
15        self.data = Loader().load("cansas1d.xml")
16       
17    def test_clone1D(self):
18        """
19            Test basic cloning
20        """
21        clone = self.data.clone_without_data()
22       
23        for i in range(len(self.data.detector)):
24            self.assertEqual(self.data.detector[i].distance, clone.detector[i].distance)
25           
26
27class manip_tests(unittest.TestCase):
28   
29    def setUp(self):
30        # Create two data sets to play with
31        x_0 = numpy.ones(5)
32        for i in range(5):
33            x_0[i] = x_0[i]*(i+1.0)
34           
35        y_0 = 2.0*numpy.ones(5)
36        dy_0 = 0.5*numpy.ones(5)
37        self.data = Data1D(x_0, y_0, dy=dy_0)
38       
39        x = self.data.x
40        y = numpy.ones(5)
41        dy = numpy.ones(5)
42        self.data2 = Data1D(x, y, dy=dy)
43       
44       
45    def test_load(self):
46        """
47            Test whether the test file was loaded properly
48        """
49        # There should be 5 entries in the file
50        self.assertEqual(len(self.data.x), 5)
51       
52        for i in range(5):
53            # The x values should be from 1 to 5
54            self.assertEqual(self.data.x[i], float(i+1))
55       
56            # All y-error values should be 0.5
57            self.assertEqual(self.data.dy[i], 0.5)   
58           
59            # All y values should be 2.0
60            self.assertEqual(self.data.y[i], 2.0)   
61       
62    def test_add(self):
63        result = self.data2+self.data
64        for i in range(5):
65            self.assertEqual(result.y[i], 3.0)
66            self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0))
67       
68    def test_sub(self):
69        result = self.data2-self.data
70        for i in range(5):
71            self.assertEqual(result.y[i], -1.0)
72            self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0))
73       
74    def test_mul(self):
75        result = self.data2*self.data
76        for i in range(5):
77            self.assertEqual(result.y[i], 2.0)
78            self.assertEqual(result.dy[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2))
79       
80    def test_div(self):
81        result = self.data2/self.data
82        for i in range(5):
83            self.assertEqual(result.y[i], 0.5)
84            self.assertEqual(result.dy[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2))
85       
86    def test_radd(self):
87        result = self.data+3.0
88        for i in range(5):
89            self.assertEqual(result.y[i], 5.0)
90            self.assertEqual(result.dy[i], 0.5)
91           
92        result = 3.0+self.data
93        for i in range(5):
94            self.assertEqual(result.y[i], 5.0)
95            self.assertEqual(result.dy[i], 0.5)
96           
97    def test_rsub(self):
98        result = self.data-3.0
99        for i in range(5):
100            self.assertEqual(result.y[i], -1.0)
101            self.assertEqual(result.dy[i], 0.5)
102           
103        result = 3.0-self.data
104        for i in range(5):
105            self.assertEqual(result.y[i], 1.0)
106            self.assertEqual(result.dy[i], 0.5)
107           
108    def test_rmul(self):
109        result = self.data*3.0
110        for i in range(5):
111            self.assertEqual(result.y[i], 6.0)
112            self.assertEqual(result.dy[i], 1.5)
113           
114        result = 3.0*self.data
115        for i in range(5):
116            self.assertEqual(result.y[i], 6.0)
117            self.assertEqual(result.dy[i], 1.5)
118           
119    def test_rdiv(self):
120        result = self.data/4.0
121        for i in range(5):
122            self.assertEqual(result.y[i], 0.5)
123            self.assertEqual(result.dy[i], 0.125)
124           
125        result = 6.0/self.data
126        for i in range(5):
127            self.assertEqual(result.y[i], 3.0)
128            self.assertEqual(result.dy[i], 6.0*0.5/4.0)
129           
130
131if __name__ == '__main__':
132    unittest.main()
133   
Note: See TracBrowser for help on using the repository browser.