Skip to content

Commit d4da374

Browse files
authored
Merge pull request #10 from 8bitsam/cookie-src
Cookiecutting - src directory
2 parents d4dfba5 + b67b7c0 commit d4da374

20 files changed

+500
-74
lines changed

src/diffpy/__init__.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
#!/usr/bin/env python
22
##############################################################################
33
#
4-
# diffpy by DANSE Diffraction group
5-
# Simon J. L. Billinge
6-
# (c) 2008 Trustees of the Columbia University
7-
# in the City of New York. All rights reserved.
4+
# (c) 2024 The Trustees of Columbia University in the City of New York.
5+
# All rights reserved.
86
#
9-
# File coded by: Pavol Juhas
7+
# File coded by: Billinge Group members and community contributors.
108
#
11-
# See AUTHORS.txt for a list of people who contributed.
12-
# See LICENSENOTICE.txt for license information.
9+
# See GitHub contributions for a more detailed list of contributors.
10+
# https://github.com/diffpy/fourigui/graphs/contributors
11+
#
12+
# See LICENSE.rst for license information.
1313
#
1414
##############################################################################
1515

16-
"""diffpy - tools for structure analysis by diffraction.
17-
18-
Blank namespace package.
19-
"""
16+
"""Blank namespace package for module diffpy."""
2017

2118

2219
from pkgutil import extend_path
2320

2421
__path__ = extend_path(__path__, __name__)
2522

26-
2723
# End of file

src/diffpy/fourigui/__init__.py

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
#!/usr/bin/env python
2-
##############################################################################
3-
#
4-
# diffpy.fourigui by DANSE Diffraction group
5-
# Simon J. L. Billinge
6-
# (c) 2008 Trustees of the Columbia University
7-
# in the City of New York. All rights reserved.
8-
#
9-
# File coded by: Sani Harouna-Mayer
10-
#
11-
# See AUTHORS.txt for a list of people who contributed.
12-
# See LICENSENOTICE.txt for license information.
13-
#
14-
##############################################################################
15-
# TODO add description
16-
"""\
17-
'{{cookiecutter.short_description}}'
18-
"""
1+
"""Tool for visualizing 3D diffraction and PDF Images"""
2+
3+
# from . import fourigui
4+
from .version import __version__
5+
6+
# silence the pyflakes syntax checker
7+
assert __version__ or True
8+
9+
# End of file

src/diffpy/fourigui/tests/__init__.py

Whitespace-only changes.

src/diffpy/fourigui/tests/conftest.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import json
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
7+
@pytest.fixture
8+
def user_filesystem(tmp_path):
9+
base_dir = Path(tmp_path)
10+
home_dir = base_dir / "home_dir"
11+
home_dir.mkdir(parents=True, exist_ok=True)
12+
cwd_dir = base_dir / "cwd_dir"
13+
cwd_dir.mkdir(parents=True, exist_ok=True)
14+
15+
home_config_data = {"username": "home_username", "email": "[email protected]"}
16+
with open(home_dir / "diffpyconfig.json", "w") as f:
17+
json.dump(home_config_data, f)
18+
19+
yield tmp_path

src/diffpy/fourigui/tests/debug.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python
2+
##############################################################################
3+
#
4+
# (c) 2024 The Trustees of Columbia University in the City of New York.
5+
# All rights reserved.
6+
#
7+
# File coded by: Billinge Group members and community contributors.
8+
#
9+
# See GitHub contributions for a more detailed list of contributors.
10+
# https://github.com/diffpy/fourigui/graphs/contributors
11+
#
12+
# See LICENSE.rst for license information.
13+
#
14+
##############################################################################
15+
16+
"""
17+
Convenience module for debugging the unit tests using
18+
19+
python -m diffpy.fourigui.tests.debug
20+
21+
Exceptions raised by failed tests or other errors are not caught.
22+
"""
23+
24+
25+
if __name__ == "__main__":
26+
import sys
27+
28+
from diffpy.fourigui.tests import testsuite
29+
30+
pattern = sys.argv[1] if len(sys.argv) > 1 else ""
31+
suite = testsuite(pattern)
32+
suite.debug()
33+
34+
35+
# End of file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import unittest
2+
3+
import h5py
4+
import numpy as np
5+
6+
from src.diffpy.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(
17+
"diffpy/tests/testdata/sofq_cut_10to40px.h5"
18+
)["data"]
19+
self.test_sofq_cut_15to35px = h5py.File(
20+
"diffpy/tests/testdata/sofq_cut_15to35px.h5"
21+
)["data"]
22+
self.test_gofr = h5py.File("diffpy/tests/testdata/gofr.h5")["data"]
23+
self.test_gofr_cut_10to40px = h5py.File(
24+
"diffpy/tests/testdata/gofr_from_sofq_cut_10to40px.h5"
25+
)["data"]
26+
self.test_gofr_cut_15to35px = h5py.File(
27+
"diffpy/tests/testdata/gofr_from_sofq_cut_15to35px.h5"
28+
)["data"]
29+
30+
def test_load_cube_testdataset1(self):
31+
# given
32+
self.test_gui.filename_entry.delete(0, "end")
33+
self.test_gui.filename_entry.insert(0, "diffpy/tests/testdata/sofq.h5")
34+
35+
# when
36+
self.test_gui.load_cube()
37+
result = self.test_gui.cube
38+
39+
# then
40+
self.assertTrue(np.allclose(result, self.test_sofq))
41+
42+
def test_load_cube_testdataset2(self):
43+
# given
44+
self.test_gui.filename_entry.delete(0, "end")
45+
self.test_gui.filename_entry.insert(
46+
0, "diffpy/tests/testdata/sofq_cut_10to40px.h5"
47+
)
48+
49+
# when
50+
self.test_gui.load_cube()
51+
result = self.test_gui.cube
52+
53+
# then
54+
self.assertTrue(
55+
np.allclose(
56+
np.nan_to_num(result), np.nan_to_num(self.test_sofq_cut_10to40px)
57+
)
58+
)
59+
60+
def test_load_cube_testdataset3(self):
61+
# given
62+
self.test_gui.filename_entry.delete(0, "end")
63+
self.test_gui.filename_entry.insert(
64+
0, "diffpy/tests/testdata/sofq_cut_15to35px.h5"
65+
)
66+
67+
# when
68+
self.test_gui.load_cube()
69+
result = self.test_gui.cube
70+
71+
# then
72+
self.assertTrue(
73+
np.allclose(
74+
np.nan_to_num(result), np.nan_to_num(self.test_sofq_cut_15to35px)
75+
)
76+
)
77+
78+
def test_fft_testdataset1(self):
79+
# given
80+
self.test_gui.plot_plane = (
81+
lambda *a, **b: ()
82+
) # overwrite plot_plane which requires not initialized attribute im
83+
self.test_gui.cube = self.test_sofq
84+
85+
# when
86+
self.test_gui.fft()
87+
result = self.test_gui.cube
88+
89+
# then
90+
self.assertTrue(np.allclose(result, self.test_gofr))
91+
92+
def test_fft_testdataset2(self):
93+
# given
94+
self.test_gui.plot_plane = (
95+
lambda *a, **b: ()
96+
) # overwrite plot_plane which requires not initialized attribute im
97+
self.test_gui.cube = self.test_sofq_cut_10to40px
98+
99+
# when
100+
self.test_gui.fft()
101+
result = self.test_gui.cube
102+
103+
# then
104+
self.assertTrue(np.allclose(result, self.test_gofr_cut_10to40px))
105+
106+
def test_fft_testdataset3(self):
107+
# given
108+
self.test_gui.plot_plane = (
109+
lambda *a, **b: ()
110+
) # overwrite plot_plane which requires not initialized attribute im
111+
self.test_gui.cube = self.test_sofq_cut_15to35px
112+
113+
# when
114+
self.test_gui.fft()
115+
result = self.test_gui.cube
116+
117+
# then
118+
self.assertTrue(np.allclose(result, self.test_gofr_cut_15to35px))
119+
120+
def test_applycutoff_range1(self):
121+
# given
122+
self.test_gui.plot_plane = lambda *a, **b: ()
123+
self.test_gui.cube = self.test_sofq
124+
self.test_gui.qminentry.insert(0, "10")
125+
self.test_gui.qmaxentry.insert(0, "40")
126+
127+
# when
128+
self.test_gui.applycutoff()
129+
result = self.test_gui.cube
130+
131+
# then
132+
self.assertTrue(
133+
np.allclose(
134+
np.nan_to_num(result), np.nan_to_num(self.test_sofq_cut_10to40px)
135+
)
136+
)
137+
138+
def test_applycutoff_range2(self):
139+
# given
140+
self.test_gui.plot_plane = lambda *a, **b: ()
141+
self.test_gui.cube = self.test_sofq
142+
self.test_gui.qminentry.insert(0, "15")
143+
self.test_gui.qmaxentry.insert(0, "35")
144+
145+
# when
146+
self.test_gui.applycutoff()
147+
result = self.test_gui.cube
148+
149+
# then
150+
self.assertTrue(
151+
np.allclose(
152+
np.nan_to_num(result), np.nan_to_num(self.test_sofq_cut_15to35px)
153+
)
154+
)
155+
156+
157+
if __name__ == "__main__":
158+
unittest.main()

src/diffpy/fourigui/tests/run.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
##############################################################################
3+
#
4+
# (c) 2024 The Trustees of Columbia University in the City of New York.
5+
# All rights reserved.
6+
#
7+
# File coded by: Billinge Group members and community contributors.
8+
#
9+
# See GitHub contributions for a more detailed list of contributors.
10+
# https://github.com/diffpy/fourigui/graphs/contributors
11+
#
12+
# See LICENSE.rst for license information.
13+
#
14+
##############################################################################
15+
"""Convenience module for executing all unit tests with
16+
python -m diffpy.fourigui.tests.run
17+
"""
18+
19+
if __name__ == "__main__":
20+
import sys
21+
22+
# show warnings by default
23+
if not sys.warnoptions:
24+
import os
25+
import warnings
26+
27+
warnings.simplefilter("default")
28+
# also affect subprocesses
29+
os.environ["PYTHONWARNINGS"] = "default"
30+
import diffpy.fourigui.tests.integration_test as integration_test
31+
import diffpy.fourigui.tests.unit_test as unit_test
32+
33+
# produce zero exit code for a successful test
34+
sys.exit(
35+
(not integration_test.main().wasSuccessful())
36+
or (not unit_test.main().wasSuccessful())
37+
)
38+
39+
40+
# Consider upgrading to pytest
41+
# import sys
42+
#
43+
# import pytest
44+
#
45+
# if __name__ == "__main__":
46+
# # show output results from every test function
47+
# args = ["-v"]
48+
# # show the message output for skipped and expected failure tests
49+
# if len(sys.argv) > 1:
50+
# args.extend(sys.argv[1:])
51+
# print("pytest arguments: {}".format(args))
52+
# # call pytest and exit with the return code from pytest
53+
# exit_res = pytest.main(args)
54+
# sys.exit(exit_res)
55+
56+
# End of file

src/diffpy/fourigui/tests/testdata/__init__.py

Whitespace-only changes.
2.21 KB
Binary file not shown.
7.86 MB
Binary file not shown.
7.86 MB
Binary file not shown.
7.86 MB
Binary file not shown.
Binary file not shown.
Binary file not shown.
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()
7.86 MB
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)