Skip to content

Commit a5301bf

Browse files
committed
Move tests to test_morphapp
1 parent 2d4ada8 commit a5301bf

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

tests/test_morphapp.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pathlib import Path
44

5+
import numpy as np
56
import pytest
67

78
from diffpy.morph.morphapp import (
@@ -255,6 +256,68 @@ def test_morphsequence(self, setup_morphsequence):
255256
)
256257
assert s_sequence_results == sequence_results
257258

259+
def test_morphsmear(self, setup_parser, tmp_path):
260+
def gaussian(x, mu, sigma):
261+
return np.exp(-((x - mu) ** 2) / (2 * sigma**2)) / (
262+
sigma * np.sqrt(2 * np.pi)
263+
)
264+
265+
# Generate the test files
266+
x_grid = np.linspace(1, 101, 1001)
267+
# Gaussian with STD 3 (morph)
268+
g2 = gaussian(x_grid, 51, 3)
269+
mf = tmp_path / "morph.txt"
270+
with open(mf, "w") as f:
271+
np.savetxt(f, np.array([x_grid, g2]).T)
272+
# Gaussian with STD 5 (target)
273+
g3 = gaussian(x_grid, 51, 5)
274+
tf = tmp_path / "target.txt"
275+
with open(tf, "w") as f:
276+
np.savetxt(f, np.array([x_grid, g3]).T)
277+
# Gaussian with STD 3 and baseline slope -0.5 (PDF morph)
278+
g2_bl = gaussian(x_grid, 51, 3) / x_grid - 0.5 * x_grid
279+
pmf = tmp_path / "pdf_morph.txt"
280+
with open(pmf, "w") as f:
281+
np.savetxt(f, np.array([x_grid, g2_bl]).T)
282+
# Gaussian with STD 5 with baseline slope -0.5 (PDF target)
283+
g3_bl = gaussian(x_grid, 51, 5) / x_grid - 0.5 * x_grid
284+
ptf = tmp_path / "pdf_target.txt"
285+
with open(ptf, "w") as f:
286+
np.savetxt(f, np.array([x_grid, g3_bl]).T)
287+
288+
# No PDF smear (should not activate baseline slope)
289+
(opts, _) = self.parser.parse_args(
290+
[
291+
"--smear",
292+
"1",
293+
"-n",
294+
]
295+
)
296+
pargs = [mf, tf]
297+
smear_results = single_morph(
298+
self.parser, opts, pargs, stdout_flag=False
299+
)
300+
# Variances add, and 3^2+4^2=5^2
301+
assert pytest.approx(abs(smear_results["smear"])) == 4.0
302+
assert pytest.approx(smear_results["Rw"]) == 0.0
303+
304+
# PDF-specific smear (should activate baseline slope)
305+
(opts, _) = self.parser.parse_args(
306+
[
307+
"--smear",
308+
"100",
309+
"--smear-pdf",
310+
"1",
311+
"-n",
312+
]
313+
)
314+
pargs = [pmf, ptf]
315+
pdf_smear_results = single_morph(
316+
self.parser, opts, pargs, stdout_flag=False
317+
)
318+
assert pytest.approx(abs(pdf_smear_results["smear"])) == 4.0
319+
assert pytest.approx(pdf_smear_results["Rw"]) == 0.0
320+
258321

259322
if __name__ == "__main__":
260323
TestApp()

tests/test_morphio.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,3 @@ def ignore_path(line):
137137
generated = filter(ignore_path, gf)
138138
target = filter(ignore_path, tf)
139139
assert all(x == y for x, y in zip(generated, target))
140-
141-
def test_morphsmear(self, setup, tmp_path):
142-
pass

0 commit comments

Comments
 (0)