|
| 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