|
2 | 2 |
|
3 | 3 | from pathlib import Path |
4 | 4 |
|
| 5 | +import numpy as np |
5 | 6 | import pytest |
6 | 7 |
|
7 | 8 | from diffpy.morph.morphapp import ( |
@@ -255,6 +256,68 @@ def test_morphsequence(self, setup_morphsequence): |
255 | 256 | ) |
256 | 257 | assert s_sequence_results == sequence_results |
257 | 258 |
|
| 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 | + |
258 | 321 |
|
259 | 322 | if __name__ == "__main__": |
260 | 323 | TestApp() |
0 commit comments