1 | """ |
---|
2 | Unit tests for the ascii (n-column) reader |
---|
3 | """ |
---|
4 | import warnings |
---|
5 | warnings.simplefilter("ignore") |
---|
6 | |
---|
7 | import unittest |
---|
8 | from sans.dataloader.loader import Loader |
---|
9 | |
---|
10 | import os.path |
---|
11 | |
---|
12 | class abs_reader(unittest.TestCase): |
---|
13 | |
---|
14 | def setUp(self): |
---|
15 | self.loader = Loader() |
---|
16 | |
---|
17 | def test_checkdata(self): |
---|
18 | """ |
---|
19 | Test .ABS file loaded as ascii |
---|
20 | """ |
---|
21 | f = self.loader.load("ascii_test_1.txt") |
---|
22 | # The length of the data is 10 |
---|
23 | self.assertEqual(len(f.x), 10) |
---|
24 | self.assertEqual(f.x[0],0.002618) |
---|
25 | self.assertEqual(f.x[9],0.0497) |
---|
26 | self.assertEqual(f.x_unit, '1/A') |
---|
27 | self.assertEqual(f.y_unit, '1/cm') |
---|
28 | |
---|
29 | self.assertEqual(f.meta_data['loader'],"ASCII") |
---|
30 | |
---|
31 | def test_truncated_1(self): |
---|
32 | """ |
---|
33 | Test an ascii file with header and a |
---|
34 | comment line in the middle of the data section. |
---|
35 | The business rule says that we should stop |
---|
36 | reading at the first comment once the data |
---|
37 | section has started (and treat the comment |
---|
38 | as though it were the start of a footer). |
---|
39 | """ |
---|
40 | # Test .ABS file loaded as ascii |
---|
41 | f = self.loader.load("ascii_test_2.txt") |
---|
42 | # The length of the data is 10 |
---|
43 | self.assertEqual(len(f.x), 5) |
---|
44 | self.assertEqual(f.x[0],0.002618) |
---|
45 | self.assertEqual(f.x[4],0.02356) |
---|
46 | |
---|
47 | def test_truncated_2(self): |
---|
48 | """ |
---|
49 | Test a 6-col ascii file with header and a |
---|
50 | line with only 2 columns in the middle of the data section. |
---|
51 | The business rule says that we should stop |
---|
52 | reading at the first inconsitent line. |
---|
53 | """ |
---|
54 | # Test .ABS file loaded as ascii |
---|
55 | f = self.loader.load("ascii_test_3.txt") |
---|
56 | # The length of the data is 5 |
---|
57 | self.assertEqual(len(f.x), 5) |
---|
58 | self.assertEqual(f.x[0],0.002618) |
---|
59 | self.assertEqual(f.x[4],0.02356) |
---|
60 | |
---|
61 | def test_truncated_3(self): |
---|
62 | """ |
---|
63 | Test a 6-col ascii file with complex header and |
---|
64 | many lines with 2 or 2 columns in the middle of the data section. |
---|
65 | The business rule says that we should stop |
---|
66 | reading at the last line of header. |
---|
67 | """ |
---|
68 | # Test .ABS file loaded as ascii |
---|
69 | f = self.loader.load("ascii_test_4.abs") |
---|
70 | # The length of the data is 5 |
---|
71 | self.assertEqual(len(f.x), 5) |
---|
72 | self.assertEqual(f.x[0],0.012654) |
---|
73 | self.assertEqual(f.x[4],0.02654) |
---|
74 | |
---|
75 | def test_truncated_4(self): |
---|
76 | """ |
---|
77 | Test mix of 6-col and 2-col. |
---|
78 | Only the last 5 2-col lines should be read. |
---|
79 | """ |
---|
80 | # Test .ABS file loaded as ascii |
---|
81 | f = self.loader.load("ascii_test_5.txt") |
---|
82 | # The length of the data is 5 |
---|
83 | self.assertEqual(len(f.x), 5) |
---|
84 | self.assertEqual(f.x[0],0.02879) |
---|
85 | self.assertEqual(f.x[4],0.0497) |
---|
86 | |
---|
87 | def test_truncated_5(self): |
---|
88 | """ |
---|
89 | Test a 6-col ascii file with complex header where one of them has a letter and |
---|
90 | many lines with 2 or 2 columns in the middle of the data section. |
---|
91 | Only last four lines should be read. |
---|
92 | """ |
---|
93 | # Test .ABS file loaded as ascii |
---|
94 | f = self.loader.load("ascii_test_6.txt") |
---|
95 | # The length of the data is 5 |
---|
96 | self.assertEqual(len(f.x), 4) |
---|
97 | self.assertEqual(f.x[0],0.013534) |
---|
98 | self.assertEqual(f.x[3],0.022254) |
---|
99 | |
---|
100 | if __name__ == '__main__': |
---|
101 | unittest.main() |
---|
102 | |
---|