source: sasview/test/sasdataloader/test/utest_ascii.py @ cc62607

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since cc62607 was cc62607, checked in by krzywon, 6 years ago

Add unit tests to show nan values are coerced to 0. refs #1037

  • Property mode set to 100644
File size: 4.2 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 = self.loader.load(find("test_data_nan_values.dat"))[0]
118        for i in range(0, len(f.x) - 1):
119            self.assertFalse(math.isnan(f.x[i]))
120            self.assertFalse(math.isnan(f.y[i]))
121            self.assertFalse(math.isnan(f.dy[i]))
122
123
124if __name__ == '__main__':
125    unittest.main()
126   
Note: See TracBrowser for help on using the repository browser.