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