|
3 | 3 | """Unit tests for diffpy.utils.parsers.loaddata
|
4 | 4 | """
|
5 | 5 |
|
6 |
| -import unittest |
7 |
| - |
8 |
| -import numpy |
| 6 | +import numpy as np |
9 | 7 | import pytest
|
10 | 8 |
|
11 | 9 | from diffpy.utils.parsers.loaddata import loadData
|
12 | 10 |
|
13 | 11 |
|
14 |
| -############################################################################## |
15 |
| -class TestLoadData(unittest.TestCase): |
16 |
| - @pytest.fixture(autouse=True) |
17 |
| - def prepare_fixture(self, datafile): |
18 |
| - self.datafile = datafile |
19 |
| - |
20 |
| - def test_loadData_default(self): |
21 |
| - """check loadData() with default options""" |
22 |
| - loaddata01 = self.datafile("loaddata01.txt") |
23 |
| - d2c = numpy.array([[3, 31], [4, 32], [5, 33]]) |
24 |
| - self.assertRaises(IOError, loadData, "doesnotexist") |
25 |
| - # the default minrows=10 makes it read from the third line |
26 |
| - d = loadData(loaddata01) |
27 |
| - self.assertTrue(numpy.array_equal(d2c, d)) |
28 |
| - # the usecols=(0, 1) would make it read from the third line |
29 |
| - d = loadData(loaddata01, minrows=1, usecols=(0, 1)) |
30 |
| - self.assertTrue(numpy.array_equal(d2c, d)) |
31 |
| - # check the effect of usecols effect |
32 |
| - d = loadData(loaddata01, usecols=(0,)) |
33 |
| - self.assertTrue(numpy.array_equal(d2c[:, 0], d)) |
34 |
| - d = loadData(loaddata01, usecols=(1,)) |
35 |
| - self.assertTrue(numpy.array_equal(d2c[:, 1], d)) |
36 |
| - return |
37 |
| - |
38 |
| - def test_loadData_1column(self): |
39 |
| - """check loading of one-column data.""" |
40 |
| - loaddata01 = self.datafile("loaddata01.txt") |
41 |
| - d1c = numpy.arange(1, 6) |
42 |
| - d = loadData(loaddata01, usecols=[0], minrows=1) |
43 |
| - self.assertTrue(numpy.array_equal(d1c, d)) |
44 |
| - d = loadData(loaddata01, usecols=[0], minrows=2) |
45 |
| - self.assertTrue(numpy.array_equal(d1c, d)) |
46 |
| - d = loadData(loaddata01, usecols=[0], minrows=3) |
47 |
| - self.assertFalse(numpy.array_equal(d1c, d)) |
48 |
| - return |
49 |
| - |
50 |
| - def test_loadData_headers(self): |
51 |
| - """check loadData() with headers options enabled""" |
52 |
| - loaddatawithheaders = self.datafile("loaddatawithheaders.txt") |
53 |
| - hignore = ["# ", "// ", "["] # ignore lines beginning with these strings |
54 |
| - delimiter = ": " # what our data should be separated by |
55 |
| - hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore) |
56 |
| - # only fourteen lines of data are formatted properly |
57 |
| - assert len(hdata) == 14 |
58 |
| - # check the following are floats |
59 |
| - vfloats = ["wavelength", "qmaxinst", "qmin", "qmax", "bgscale"] |
60 |
| - for name in vfloats: |
61 |
| - assert isinstance(hdata.get(name), float) |
62 |
| - # check the following are NOT floats |
63 |
| - vnfloats = ["composition", "rmax", "rmin", "rstep", "rpoly"] |
64 |
| - for name in vnfloats: |
65 |
| - assert not isinstance(hdata.get(name), float) |
66 |
| - |
67 |
| - |
68 |
| -# End of class TestRoutines |
69 |
| - |
70 |
| -if __name__ == "__main__": |
71 |
| - unittest.main() |
72 |
| - |
73 |
| -# End of file |
| 12 | +def test_loadData_default(datafile): |
| 13 | + """check loadData() with default options""" |
| 14 | + loaddata01 = datafile("loaddata01.txt") |
| 15 | + d2c = np.array([[3, 31], [4, 32], [5, 33]]) |
| 16 | + |
| 17 | + with pytest.raises(IOError) as err: |
| 18 | + loadData("doesnotexist.txt") |
| 19 | + assert ( |
| 20 | + str(err.value) |
| 21 | + == "File doesnotexist.txt cannot be found. Please rerun the program specifying a valid filename." |
| 22 | + ) |
| 23 | + |
| 24 | + # The default minrows=10 makes it read from the third line |
| 25 | + d = loadData(loaddata01) |
| 26 | + assert np.array_equal(d2c, d) |
| 27 | + |
| 28 | + # The usecols=(0, 1) would make it read from the third line |
| 29 | + d = loadData(loaddata01, minrows=1, usecols=(0, 1)) |
| 30 | + assert np.array_equal(d2c, d) |
| 31 | + |
| 32 | + # Check the effect of usecols effect |
| 33 | + d = loadData(loaddata01, usecols=(0,)) |
| 34 | + assert np.array_equal(d2c[:, 0], d) |
| 35 | + |
| 36 | + d = loadData(loaddata01, usecols=(1,)) |
| 37 | + assert np.array_equal(d2c[:, 1], d) |
| 38 | + |
| 39 | + |
| 40 | +def test_loadData_1column(datafile): |
| 41 | + """check loading of one-column data.""" |
| 42 | + loaddata01 = datafile("loaddata01.txt") |
| 43 | + d1c = np.arange(1, 6) |
| 44 | + |
| 45 | + # Assertions using pytest's assert |
| 46 | + d = loadData(loaddata01, usecols=[0], minrows=1) |
| 47 | + assert np.array_equal(d1c, d) |
| 48 | + |
| 49 | + d = loadData(loaddata01, usecols=[0], minrows=2) |
| 50 | + assert np.array_equal(d1c, d) |
| 51 | + |
| 52 | + d = loadData(loaddata01, usecols=[0], minrows=3) |
| 53 | + assert not np.array_equal(d1c, d) |
| 54 | + |
| 55 | + |
| 56 | +def test_loadData_headers(datafile): |
| 57 | + """check loadData() with headers options enabled""" |
| 58 | + expected = { |
| 59 | + "wavelength": 0.1, |
| 60 | + "dataformat": "Qnm", |
| 61 | + "inputfile": "darkSub_rh20_C_01.chi", |
| 62 | + "mode": "xray", |
| 63 | + "bgscale": 1.2998929285, |
| 64 | + "composition": "0.800.20", |
| 65 | + "outputtype": "gr", |
| 66 | + "qmaxinst": 25.0, |
| 67 | + "qmin": 0.1, |
| 68 | + "qmax": 25.0, |
| 69 | + "rmax": "100.0r", |
| 70 | + "rmin": "0.0r", |
| 71 | + "rstep": "0.01r", |
| 72 | + "rpoly": "0.9r", |
| 73 | + } |
| 74 | + |
| 75 | + loaddatawithheaders = datafile("loaddatawithheaders.txt") |
| 76 | + hignore = ["# ", "// ", "["] # ignore lines beginning with these strings |
| 77 | + delimiter = ": " # what our data should be separated by |
| 78 | + |
| 79 | + # Load data with headers |
| 80 | + hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore) |
| 81 | + assert hdata == expected |
0 commit comments