|
| 1 | +import unittest |
| 2 | + |
| 3 | +import h5py |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from ..fourigui.fourigui import Gui |
| 7 | + |
| 8 | + |
| 9 | +class TestGui(unittest.TestCase): |
| 10 | + def setUp(self): |
| 11 | + # set up gui |
| 12 | + self.test_gui = Gui() |
| 13 | + |
| 14 | + # set up test data |
| 15 | + self.test_sofq = h5py.File("diffpy/tests/testdata/sofq.h5")["data"] |
| 16 | + self.test_sofq_cut_10to40px = h5py.File("diffpy/tests/testdata/sofq_cut_10to40px.h5")["data"] |
| 17 | + self.test_sofq_cut_15to35px = h5py.File("diffpy/tests/testdata/sofq_cut_15to35px.h5")["data"] |
| 18 | + self.test_gofr = h5py.File("diffpy/tests/testdata/gofr.h5")["data"] |
| 19 | + self.test_gofr_cut_10to40px = h5py.File("diffpy/tests/testdata/gofr_from_sofq_cut_10to40px.h5")["data"] |
| 20 | + self.test_gofr_cut_15to35px = h5py.File("diffpy/tests/testdata/gofr_from_sofq_cut_15to35px.h5")["data"] |
| 21 | + |
| 22 | + def test_load_cube_testdataset1(self): |
| 23 | + # given |
| 24 | + self.test_gui.filename_entry.delete(0, "end") |
| 25 | + self.test_gui.filename_entry.insert(0, "diffpy/tests/testdata/sofq.h5") |
| 26 | + |
| 27 | + # when |
| 28 | + self.test_gui.load_cube() |
| 29 | + result = self.test_gui.cube |
| 30 | + |
| 31 | + # then |
| 32 | + self.assertTrue(np.allclose(result, self.test_sofq)) |
| 33 | + |
| 34 | + def test_load_cube_testdataset2(self): |
| 35 | + # given |
| 36 | + self.test_gui.filename_entry.delete(0, "end") |
| 37 | + self.test_gui.filename_entry.insert(0, "diffpy/tests/testdata/sofq_cut_10to40px.h5") |
| 38 | + |
| 39 | + # when |
| 40 | + self.test_gui.load_cube() |
| 41 | + result = self.test_gui.cube |
| 42 | + |
| 43 | + # then |
| 44 | + self.assertTrue(np.allclose(np.nan_to_num(result), np.nan_to_num(self.test_sofq_cut_10to40px))) |
| 45 | + |
| 46 | + def test_load_cube_testdataset3(self): |
| 47 | + # given |
| 48 | + self.test_gui.filename_entry.delete(0, "end") |
| 49 | + self.test_gui.filename_entry.insert(0, "diffpy/tests/testdata/sofq_cut_15to35px.h5") |
| 50 | + |
| 51 | + # when |
| 52 | + self.test_gui.load_cube() |
| 53 | + result = self.test_gui.cube |
| 54 | + |
| 55 | + # then |
| 56 | + self.assertTrue(np.allclose(np.nan_to_num(result), np.nan_to_num(self.test_sofq_cut_15to35px))) |
| 57 | + |
| 58 | + def test_fft_testdataset1(self): |
| 59 | + # given |
| 60 | + self.test_gui.plot_plane = ( |
| 61 | + lambda *a, **b: () |
| 62 | + ) # overwrite plot_plane which requires not initialized attribute im |
| 63 | + self.test_gui.cube = self.test_sofq |
| 64 | + |
| 65 | + # when |
| 66 | + self.test_gui.fft() |
| 67 | + result = self.test_gui.cube |
| 68 | + |
| 69 | + # then |
| 70 | + self.assertTrue(np.allclose(result, self.test_gofr)) |
| 71 | + |
| 72 | + def test_fft_testdataset2(self): |
| 73 | + # given |
| 74 | + self.test_gui.plot_plane = ( |
| 75 | + lambda *a, **b: () |
| 76 | + ) # overwrite plot_plane which requires not initialized attribute im |
| 77 | + self.test_gui.cube = self.test_sofq_cut_10to40px |
| 78 | + |
| 79 | + # when |
| 80 | + self.test_gui.fft() |
| 81 | + result = self.test_gui.cube |
| 82 | + |
| 83 | + # then |
| 84 | + self.assertTrue(np.allclose(result, self.test_gofr_cut_10to40px)) |
| 85 | + |
| 86 | + def test_fft_testdataset3(self): |
| 87 | + # given |
| 88 | + self.test_gui.plot_plane = ( |
| 89 | + lambda *a, **b: () |
| 90 | + ) # overwrite plot_plane which requires not initialized attribute im |
| 91 | + self.test_gui.cube = self.test_sofq_cut_15to35px |
| 92 | + |
| 93 | + # when |
| 94 | + self.test_gui.fft() |
| 95 | + result = self.test_gui.cube |
| 96 | + |
| 97 | + # then |
| 98 | + self.assertTrue(np.allclose(result, self.test_gofr_cut_15to35px)) |
| 99 | + |
| 100 | + def test_applycutoff_range1(self): |
| 101 | + # given |
| 102 | + self.test_gui.plot_plane = lambda *a, **b: () |
| 103 | + self.test_gui.cube = self.test_sofq |
| 104 | + self.test_gui.qminentry.insert(0, "10") |
| 105 | + self.test_gui.qmaxentry.insert(0, "40") |
| 106 | + |
| 107 | + # when |
| 108 | + self.test_gui.applycutoff() |
| 109 | + result = self.test_gui.cube |
| 110 | + |
| 111 | + # then |
| 112 | + self.assertTrue(np.allclose(np.nan_to_num(result), np.nan_to_num(self.test_sofq_cut_10to40px))) |
| 113 | + |
| 114 | + def test_applycutoff_range2(self): |
| 115 | + # given |
| 116 | + self.test_gui.plot_plane = lambda *a, **b: () |
| 117 | + self.test_gui.cube = self.test_sofq |
| 118 | + self.test_gui.qminentry.insert(0, "15") |
| 119 | + self.test_gui.qmaxentry.insert(0, "35") |
| 120 | + |
| 121 | + # when |
| 122 | + self.test_gui.applycutoff() |
| 123 | + result = self.test_gui.cube |
| 124 | + |
| 125 | + # then |
| 126 | + self.assertTrue(np.allclose(np.nan_to_num(result), np.nan_to_num(self.test_sofq_cut_15to35px))) |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + unittest.main() |
0 commit comments