diff --git a/impurityModel/test/test_comparison_with_reference.py b/impurityModel/test/test_comparison_with_reference.py index 2c14434..fb01620 100644 --- a/impurityModel/test/test_comparison_with_reference.py +++ b/impurityModel/test/test_comparison_with_reference.py @@ -3,14 +3,13 @@ """ import inspect - -# import math +import math import os import subprocess import tempfile -# import h5py -# import numpy as np +import h5py +import numpy as np def test_comparison(): @@ -55,23 +54,22 @@ def compare_spectra( assert os.path.isfile("spectra.h5") assert os.path.isfile(reference_file_path), reference_file_path # Open spectra file and the reference spectra file - with h5py.File("spectra.h5", "r") as file_handle: - with h5py.File(reference_file_path, "r") as ref_file_handle: - # Compare file contents - for key in ref_file_handle: - print("Compare dataset:", key) - x = file_handle[key][()] - x_ref = ref_file_handle[key][()] - abs_diff = np.abs(x - x_ref) - i = np.argmax(abs_diff) - print("Max abs diff:", np.ravel(abs_diff)[i]) - print("Reference value at max diff:", np.ravel(x_ref)[i]) - np.testing.assert_allclose(x, x_ref, atol=3e-2) - np.testing.assert_allclose(x, x_ref, atol=2e-2, rtol=0.1) - print("Mean abs diff:", np.mean(abs_diff)) - assert math.isclose(np.mean(abs_diff), 0, abs_tol=2e-5) - print("Median abs diff:", np.median(abs_diff)) - assert math.isclose(np.median(abs_diff), 0, abs_tol=1e-8) + with h5py.File("spectra.h5", "r") as file_handle, h5py.File(reference_file_path, "r") as ref_file_handle: + # Compare file contents + for key in ref_file_handle: + print("Compare dataset:", key) + x = file_handle[key][()] + x_ref = ref_file_handle[key][()] + abs_diff = np.abs(x - x_ref) + i = np.argmax(abs_diff) + print("Max abs diff:", np.ravel(abs_diff)[i]) + print("Reference value at max diff:", np.ravel(x_ref)[i]) + np.testing.assert_allclose(x, x_ref, atol=3e-2) + np.testing.assert_allclose(x, x_ref, atol=2e-2, rtol=0.1) + print("Mean abs diff:", np.mean(abs_diff)) + assert math.isclose(np.mean(abs_diff), 0, abs_tol=2e-5) + print("Median abs diff:", np.median(abs_diff)) + assert math.isclose(np.median(abs_diff), 0, abs_tol=1e-8) print("Comparison successful")