source: sasview/test/sasdataloader/test/utest_ascii.py @ 3a473ef

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 3a473ef was 3a473ef, checked in by Piotr Rozyczko <rozyczko@…>, 14 months ago

Manual update of test/ directory with changes on master - SASVIEW-996

  • Property mode set to 100644
File size: 4.7 KB
Line 
1"""
2    Unit tests for the ascii (n-column) reader
3"""
4
5import os.path
6import warnings
7import math
8warnings.simplefilter("ignore")
9
10import unittest
11from sas.sascalc.dataloader.loader import Loader
12
13
14def find(filename):
15    return os.path.join(os.path.dirname(__file__), filename)
16
17
18class ABSReaderTests(unittest.TestCase):
19   
20    def setUp(self):
21        self.loader = Loader()
22        self.f1_list = self.loader.load(find("ascii_test_1.txt"))
23        self.f1 = self.f1_list[0]
24        self.f2_list = self.loader.load(find("ascii_test_2.txt"))
25        self.f2 = self.f2_list[0]
26        self.f3_list = self.loader.load(find("ascii_test_3.txt"))
27        self.f3 = self.f3_list[0]
28        self.f4_list = self.loader.load(find("ascii_test_4.abs"))
29        self.f4 = self.f4_list[0]
30        self.f5_list = self.loader.load(find("ascii_test_5.txt"))
31        self.f5 = self.f5_list[0]
32
33    def test_checkdata(self):
34        """
35            Test .ABS file loaded as ascii
36        """
37        # The length of the data is 10
38        self.assertEqual(len(self.f1_list), 1)
39        self.assertEqual(len(self.f2_list), 1)
40        self.assertEqual(len(self.f3_list), 1)
41        self.assertEqual(len(self.f4_list), 1)
42        self.assertEqual(len(self.f5_list), 1)
43        self.assertEqual(len(self.f1.x), 10)
44        self.assertEqual(self.f1.x[0],0.002618)
45        self.assertEqual(self.f1.x[9],0.0497)
46        self.assertTrue(self.f1.x_unit == 'A^{-1}')
47        self.assertTrue(self.f1.y_unit == 'cm^{-1}')
48       
49        self.assertEqual(self.f1.meta_data['loader'],"ASCII")
50
51    def test_truncated_1(self):
52        """
53            Test an ascii file with header and a
54            comment line in the middle of the data section.
55            The business rule says that we should stop
56            reading at the first comment once the data
57            section has started (and treat the comment
58            as though it were the start of a footer).
59        """
60        # The length of the data is 5
61        self.assertEqual(len(self.f2.x), 5)
62        self.assertEqual(self.f2.x[0],0.002618)
63        self.assertEqual(self.f2.x[4],0.02356)
64
65    def test_truncated_2(self):
66        """
67            Test a 6-col ascii file with header and a
68            line with only 2 columns in the middle of the data section.
69            The business rule says that we should stop
70            reading at the first inconsitent line.
71        """
72        # The length of the data is 5
73        self.assertEqual(len(self.f3.x), 5)
74        self.assertEqual(self.f3.x[0],0.002618)
75        self.assertEqual(self.f3.x[4],0.02356)
76
77    def test_truncated_3(self):
78        """
79            Test a 6-col ascii file with complex header and
80            many lines with 2 or 2 columns in the middle of the data section.
81            The business rule says that we should stop
82            reading at the last line of header.
83        """
84        # The length of the data is 5
85        self.assertEqual(len(self.f4.x), 5)
86        self.assertEqual(self.f4.x[0],0.012654)
87        self.assertEqual(self.f4.x[4],0.02654)
88
89    def test_truncated_4(self):
90        """
91            Test mix of 6-col and 2-col.
92            Only the last 5 2-col lines should be read.
93        """
94        # The length of the data is 5
95        self.assertEqual(len(self.f5.x), 5)
96        self.assertEqual(self.f5.x[0],0.02879)
97        self.assertEqual(self.f5.x[4],0.0497)
98
99    def test_truncated_5(self):
100        """
101            Test a 6-col ascii file with complex header where one of them has a
102            letter and many lines with 2 or 2 columns in the middle of the data
103            section. Will be rejected because fewer than 5 lines.
104        """
105        # Test .ABS file loaded as ascii
106        f = None
107        try:
108            f = self.loader.load(find("ascii_test_6.txt"))
109        # The length of the data is 5
110        except:
111            self.assertEqual(f, None)
112
113    def test_nan_values(self):
114        """
115        Test loading an ascii data file with nan values saved in x, y, or dy.
116        """
117        f_1d = self.loader.load(find("nans_in_1d_data.dat"))[0]
118        f_2d = self.loader.load(find("nans_in_2d_data.DAT"))[0]
119        for i in range(0, len(f_1d.x) - 1):
120            self.assertFalse(math.isnan(f_1d.x[i]))
121            self.assertFalse(math.isnan(f_1d.y[i]))
122            self.assertFalse(math.isnan(f_1d.dy[i]))
123        f_2d.data = f_2d.data.flatten()
124        f_2d.qx_data = f_2d.qx_data.flatten()
125        f_2d.qy_data = f_2d.qy_data.flatten()
126        for i in range(0, len(f_2d.data) - 1):
127            self.assertFalse(math.isnan(f_2d.data[i]))
128            self.assertFalse(math.isnan(f_2d.qx_data[i]))
129            self.assertFalse(math.isnan(f_2d.qy_data[i]))
130
131
132if __name__ == '__main__':
133    unittest.main()
134   
Note: See TracBrowser for help on using the repository browser.