1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
From: Stuart Prescott <stuart@debian.org>
Date: Thu, 26 Mar 2026 19:04:47 +1100
Subject: Fix tests to cope with round-off error
Forwarded: not-needed
Applied-Upstream: https://github.com/pkienzle/periodictable/commit/538018a26513267ec1a7aa71b48042071aa53fdd
(superseded by alternate solution upstream)
---
test/test_xsf.py | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)
diff --git a/test/test_xsf.py b/test/test_xsf.py
index 1ef5195..ad5c7f4 100644
--- a/test/test_xsf.py
+++ b/test/test_xsf.py
@@ -1,5 +1,6 @@
import numpy as np
from numpy import pi, isnan
+from numpy.testing import assert_almost_equal
from periodictable import formula
from periodictable import Cu,Mo,Ni,Fe,Si,H,D,O
@@ -14,8 +15,8 @@ def test_xsf():
# Check scalar scattering factor lookup
f1,f2 = Ni.xray.scattering_factors(energy=xray_energy(Cu.K_alpha))
- assert abs(f1-25.0229)<0.0001
- assert abs(f2-0.5249)<0.0001
+ assert_almost_equal(f1, 25.0229, 4)
+ assert_almost_equal(f2, 0.5249, 4)
# Check array scattering factor lookup
f1,f2 = Ni.xray.scattering_factors(wavelength=Cu.K_alpha)
@@ -25,10 +26,10 @@ def test_xsf():
# Check that we can lookup sld by wavelength and energy
Fe_rho,Fe_mu = Fe.xray.sld(wavelength=Cu.K_alpha)
- assert abs(Fe_rho-59.45) < 0.01
+ assert_almost_equal(Fe_rho, 59.45, 2)
Si_rho,Si_mu = Si.xray.sld(energy=8.050)
- assert abs(Si_rho-20.0705) < 0.0001
- assert abs(Si_mu-0.4572) < 0.0001
+ assert_almost_equal(Si_rho, 20.0705, 4)
+ assert_almost_equal(Si_mu, 0.4572, 4)
# Check that wavelength is the default
Fe_rho_default,Fe_mu_default = Fe.xray.sld(wavelength=Cu.K_alpha)
@@ -64,16 +65,16 @@ def test_xsf():
# Cross check against mo
rho,mu = xray_sld({Si:1},density=Si.density,wavelength=1.54)
rhoSi,muSi = Si.xray.sld(wavelength=1.54)
- assert abs(rho - rhoSi) < 1e-14
- assert abs(mu - muSi) < 1e-14
+ assert_almost_equal(rho, rhoSi, 14)
+ assert_almost_equal(mu, muSi, 14)
# Check that xray_sld works as expected
atoms = formula('SiO2').atoms
rho,mu = xray_sld(atoms,density=2.2,energy=xray_energy(Cu.K_alpha))
- assert abs(rho-18.87)<0.1
+ assert_almost_equal(rho, 18.87, 2)
atoms = formula('B4C').atoms
rho,mu = xray_sld(atoms,density=2.52,energy=xray_energy(Cu.K_alpha))
- assert abs(rho-20.17)<0.1
+ assert_almost_equal(rho, 20.17, 2)
F = formula('', density=0)
rho,mu = xray_sld('', density=0, wavelength=Cu.K_alpha)
@@ -83,13 +84,14 @@ def test_xsf():
D2O_density = (2*D.mass + O.mass)/(2*H.mass + O.mass)
rho,mu = xray_sld('D2O',natural_density=1,wavelength=1.54)
rho2,mu2 = xray_sld('D2O',density=D2O_density,wavelength=1.54)
- assert abs(rho-rho2)<1e-14 and abs(mu-mu2)<1e-14
+ assert_almost_equal(rho, rho2, 14)
+ assert_almost_equal(mu, mu2, 14)
# Check f0 calculation for scalar, vector, array and empty
Q1,Q2 = 4*pi/Cu.K_alpha, 4*pi/Mo.K_alpha
f0 = Ni.xray.f0(Q=Q1)
- assert abs(f0-10.11303) < 0.00001
+ assert_almost_equal(f0, 10.11303, 5)
assert isnan(Ni.xray.f0(Q=7*4*pi))
f0 = Ni.xray.f0(Q=Q1)
|