Skip to content

Commit 852ae74

Browse files
committed
break out tests into separate files
1 parent 10262a7 commit 852ae74

4 files changed

Lines changed: 289 additions & 284 deletions

File tree

tests/test_generators.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import numpy as np
2+
import pytest
3+
4+
from diffpy.cmipdf import PDFGenerator
5+
from diffpy.srreal.pdfcalculator import PDFCalculator
6+
from diffpy.structure import PDFFitStructure
7+
8+
9+
def testGenerator(datafile):
10+
qmax = 27.0
11+
gen = PDFGenerator()
12+
gen.set_scattering_type("N")
13+
assert "N" == gen.get_scattering_type()
14+
gen.set_qmax(qmax)
15+
assert qmax == pytest.approx(gen.get_qmax())
16+
17+
structure = PDFFitStructure()
18+
ciffile = datafile("ni.cif")
19+
cif_path = str(ciffile)
20+
structure.read(cif_path)
21+
for i in range(4):
22+
structure[i].Bisoequiv = 1
23+
gen.set_structure(structure)
24+
25+
calc = gen._calc
26+
# Test parameters
27+
for par in gen.iterate_over_parameters(recurse=False):
28+
pname = par.name
29+
defval = calc._getDoubleAttr(pname)
30+
assert defval == par.getValue()
31+
# Test setting values
32+
par.set_value(1.0)
33+
assert 1.0 == par.getValue()
34+
par.set_value(defval)
35+
assert defval == par.getValue()
36+
37+
r = np.arange(0, 10, 0.1)
38+
y = gen(r)
39+
40+
# Now create a reference PDF. Since the calculator is testing its
41+
# output, we just have to make sure we can calculate from the
42+
# PDFGenerator interface.
43+
44+
calc = PDFCalculator()
45+
calc.rstep = r[1] - r[0]
46+
calc.rmin = r[0]
47+
calc.rmax = r[-1] + 0.5 * calc.rstep
48+
calc.qmax = qmax
49+
calc.setScatteringFactorTableByType("N")
50+
calc.eval(structure)
51+
yref = calc.pdf
52+
53+
diff = y - yref
54+
res = np.dot(diff, diff)
55+
assert 0 == pytest.approx(res)
56+
return
57+
58+
59+
def test_set_qmin():
60+
"""Verify qmin is propagated to the calculator object."""
61+
gen = PDFGenerator()
62+
assert 0 == gen.get_qmin()
63+
assert 0 == gen._calc.qmin
64+
gen.set_qmin(0.93)
65+
assert 0.93 == gen.get_qmin()
66+
assert 0.93 == gen._calc.qmin
67+
return

tests/test_parser.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/env python
2+
##############################################################################
3+
#
4+
# (c) 2025 Simon Billinge.
5+
# All rights reserved.
6+
#
7+
# File coded by: Caden Myers, Simon Billinge, and members of the Billinge
8+
# group.
9+
#
10+
# See GitHub contributions for a more detailed list of contributors.
11+
# https://github.com/diffpy/diffpy.cmipdf/graphs/contributors
12+
#
13+
# See LICENSE.rst for license information.
14+
#
15+
##############################################################################
16+
"""Tests for pdf package."""
17+
18+
19+
import numpy as np
20+
import pytest
21+
22+
from diffpy.srfit.fitbase import ProfileParser
23+
24+
# ----------------------------------------------------------------------------
25+
26+
27+
# The tests in this file are for ProfileParser which belongs to diffpy.srfit,
28+
# but since it is used here, we will test it here on test data with modern
29+
# diffpy format.
30+
def testParser1(datafile):
31+
filename = datafile("ni-q27r100-neutron.gr")
32+
parser = ProfileParser()
33+
parser.parse_file(filename)
34+
35+
meta = parser._meta
36+
37+
assert str(filename) == meta["filename"]
38+
assert 1 == meta["nbanks"]
39+
assert "N" == meta["stype"]
40+
assert 27 == meta["qmax"]
41+
assert 300 == meta.get("temperature")
42+
assert meta.get("qdamp") is None
43+
assert meta.get("qbroad") is None
44+
assert meta.get("spdiameter") is None
45+
assert meta.get("scale") is None
46+
assert meta.get("doping") is None
47+
48+
x, y, dx, dy = parser.get_data()
49+
assert dx.tolist() == len(x) * [0]
50+
assert dy.tolist() == len(x) * [0]
51+
52+
testx = np.linspace(0.01, 100, 10000)
53+
diff = testx - x
54+
res = np.dot(diff, diff)
55+
assert 0 == pytest.approx(res)
56+
57+
testy = np.array(
58+
[
59+
1.144,
60+
2.258,
61+
3.312,
62+
4.279,
63+
5.135,
64+
5.862,
65+
6.445,
66+
6.875,
67+
7.150,
68+
7.272,
69+
]
70+
)
71+
diff = testy - y[:10]
72+
res = np.dot(diff, diff)
73+
assert 0 == pytest.approx(res)
74+
75+
return
76+
77+
78+
def testParser2(datafile):
79+
data = datafile("si-q27r60-xray.gr")
80+
parser = ProfileParser()
81+
parser.parse_file(data)
82+
83+
meta = parser._meta
84+
85+
assert str(data) == meta["filename"]
86+
assert 1 == meta["nbanks"]
87+
assert "X" == meta["stype"]
88+
assert 27 == meta["qmax"]
89+
assert 300 == meta.get("temperature")
90+
assert meta.get("qdamp") is None
91+
assert meta.get("qbroad") is None
92+
assert meta.get("spdiameter") is None
93+
assert meta.get("scale") is None
94+
assert meta.get("doping") is None
95+
96+
x, y, dx, dy = parser.get_data()
97+
testx = np.linspace(0.01, 60, 5999, endpoint=False)
98+
diff = testx - x
99+
res = np.dot(diff, diff)
100+
assert 0 == pytest.approx(res)
101+
102+
testy = np.array(
103+
[
104+
0.1105784,
105+
0.2199684,
106+
0.3270088,
107+
0.4305913,
108+
0.5296853,
109+
0.6233606,
110+
0.7108060,
111+
0.7913456,
112+
0.8644501,
113+
0.9297440,
114+
]
115+
)
116+
diff = testy - y[:10]
117+
res = np.dot(diff, diff)
118+
assert 0 == pytest.approx(res)
119+
120+
testdy = np.array(
121+
[
122+
0.001802192,
123+
0.003521449,
124+
0.005079115,
125+
0.006404892,
126+
0.007440527,
127+
0.008142955,
128+
0.008486813,
129+
0.008466340,
130+
0.008096858,
131+
0.007416456,
132+
]
133+
)
134+
diff = testdy - dy[:10]
135+
res = np.dot(diff, diff)
136+
assert 0 == pytest.approx(res)
137+
138+
assert dx.tolist() == [0] * len(dx)
139+
return

0 commit comments

Comments
 (0)