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