Skip to content

Commit cbf580a

Browse files
committed
moving content into tests dir
1 parent 5bff19b commit cbf580a

13 files changed

+332
-0
lines changed

tests/integration_test.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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()

tests/testdata/__init__.py

Whitespace-only changes.

tests/testdata/dummydata.h5

2.21 KB
Binary file not shown.

tests/testdata/gofr.h5

7.86 MB
Binary file not shown.

tests/testdata/gofr_.h5

7.86 MB
Binary file not shown.

tests/testdata/gofr_cut.h5

7.86 MB
Binary file not shown.
7.86 MB
Binary file not shown.
7.86 MB
Binary file not shown.

tests/testdata/make_testdata.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import h5py
2+
import numpy as np
3+
4+
5+
def cutcube(fname_uncut_cube, fname_cut_cube, qmin, qmax):
6+
7+
cube = h5py.File(fname_uncut_cube, "r")["data"]
8+
9+
X, Y, Z = cube.shape
10+
sphere = np.ones((X, Y, Z))
11+
r2_inner = qmin**2
12+
r2_outer = qmax**2
13+
XS, YS, ZS = np.meshgrid(np.arange(X), np.arange(Y), np.arange(Z))
14+
R2 = (XS - X // 2) ** 2 + (YS - Y // 2) ** 2 + (ZS - Z // 2) ** 2
15+
mask = (R2 <= r2_inner) | (R2 >= r2_outer)
16+
sphere[mask] = np.nan
17+
18+
f = h5py.File(fname_cut_cube, "w")
19+
f.create_dataset("data", data=cube * sphere)
20+
f.close()
21+
22+
23+
def fftcube(fname_reci, fname_real):
24+
25+
fftholder = h5py.File(fname_reci, "r")["data"]
26+
27+
fftholder = np.nan_to_num(fftholder)
28+
size = list(fftholder.shape)
29+
fftholder = np.fft.ifftshift(fftholder)
30+
fftholder = np.fft.fftn(fftholder, s=size, norm="ortho")
31+
fftholder = np.fft.fftshift(fftholder)
32+
fftholder = fftholder.real
33+
34+
f = h5py.File(fname_real, "w")
35+
f.create_dataset("data", data=fftholder)
36+
f.close()
37+
38+
39+
def dummydata(fname="dummydata.h5"):
40+
dummydata = np.ones((3, 3, 3))
41+
42+
f = h5py.File(fname, "w")
43+
f.create_dataset("data", data=dummydata)
44+
f.close()
45+
46+
47+
# cutcube("sofq.h5", "sofq_cut_10to40px.h5", 10, 40)
48+
# cutcube("sofq.h5", "sofq_cut_15to35px.h5", 15, 35)
49+
# fftcube("sofq.h5", "gofr.h5")
50+
# fftcube("sofq_cut_10to40px.h5", "gofr_from_sofq_cut_10to40px.h5")
51+
# fftcube("sofq_cut_15to35px.h5", "gofr_from_sofq_cut_15to35px.h5")
52+
dummydata()

tests/testdata/sofq.h5

7.86 MB
Binary file not shown.

0 commit comments

Comments
 (0)