[959eb01] | 1 | """ |
---|
| 2 | Unit tests for the ascii (n-column) reader |
---|
| 3 | """ |
---|
[f53d684] | 4 | |
---|
| 5 | import os.path |
---|
[959eb01] | 6 | import warnings |
---|
[cc62607] | 7 | import math |
---|
[959eb01] | 8 | warnings.simplefilter("ignore") |
---|
| 9 | |
---|
| 10 | import unittest |
---|
[b09095a] | 11 | from sas.sascalc.dataloader.loader import Loader |
---|
[959eb01] | 12 | |
---|
[b09095a] | 13 | |
---|
[f53d684] | 14 | def find(filename): |
---|
| 15 | return os.path.join(os.path.dirname(__file__), filename) |
---|
| 16 | |
---|
| 17 | |
---|
[b09095a] | 18 | class ABSReaderTests(unittest.TestCase): |
---|
[959eb01] | 19 | |
---|
| 20 | def setUp(self): |
---|
| 21 | self.loader = Loader() |
---|
[f53d684] | 22 | self.f1_list = self.loader.load(find("ascii_test_1.txt")) |
---|
[b09095a] | 23 | self.f1 = self.f1_list[0] |
---|
[f53d684] | 24 | self.f2_list = self.loader.load(find("ascii_test_2.txt")) |
---|
[b09095a] | 25 | self.f2 = self.f2_list[0] |
---|
[f53d684] | 26 | self.f3_list = self.loader.load(find("ascii_test_3.txt")) |
---|
[b09095a] | 27 | self.f3 = self.f3_list[0] |
---|
[f53d684] | 28 | self.f4_list = self.loader.load(find("ascii_test_4.abs")) |
---|
[b09095a] | 29 | self.f4 = self.f4_list[0] |
---|
[f53d684] | 30 | self.f5_list = self.loader.load(find("ascii_test_5.txt")) |
---|
[b09095a] | 31 | self.f5 = self.f5_list[0] |
---|
[ad92c5a] | 32 | |
---|
[959eb01] | 33 | def test_checkdata(self): |
---|
| 34 | """ |
---|
| 35 | Test .ABS file loaded as ascii |
---|
| 36 | """ |
---|
| 37 | # The length of the data is 10 |
---|
[fe15198] | 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) |
---|
[b09095a] | 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) |
---|
[a78a02f] | 46 | self.assertTrue(self.f1.x_unit == 'A^{-1}') |
---|
| 47 | self.assertTrue(self.f1.y_unit == 'cm^{-1}') |
---|
[959eb01] | 48 | |
---|
[b09095a] | 49 | self.assertEqual(self.f1.meta_data['loader'],"ASCII") |
---|
[ad92c5a] | 50 | |
---|
[959eb01] | 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 | """ |
---|
[b09095a] | 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) |
---|
[ad92c5a] | 64 | |
---|
[959eb01] | 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 |
---|
[b09095a] | 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) |
---|
[ad92c5a] | 76 | |
---|
[959eb01] | 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 |
---|
[b09095a] | 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) |
---|
[ad92c5a] | 88 | |
---|
[959eb01] | 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 |
---|
[b09095a] | 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) |
---|
[ad92c5a] | 98 | |
---|
[959eb01] | 99 | def test_truncated_5(self): |
---|
| 100 | """ |
---|
[b09095a] | 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. |
---|
[959eb01] | 104 | """ |
---|
| 105 | # Test .ABS file loaded as ascii |
---|
| 106 | f = None |
---|
| 107 | try: |
---|
[f53d684] | 108 | f = self.loader.load(find("ascii_test_6.txt")) |
---|
[959eb01] | 109 | # The length of the data is 5 |
---|
| 110 | except: |
---|
| 111 | self.assertEqual(f, None) |
---|
[ad92c5a] | 112 | |
---|
[cc62607] | 113 | def test_nan_values(self): |
---|
| 114 | """ |
---|
| 115 | Test loading an ascii data file with nan values saved in x, y, or dy. |
---|
| 116 | """ |
---|
[db5196d] | 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])) |
---|
[cc62607] | 130 | |
---|
| 131 | |
---|
[959eb01] | 132 | if __name__ == '__main__': |
---|
| 133 | unittest.main() |
---|
| 134 | |
---|