Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
test(conftest): introduce new custom absolute tolerance
Browse files Browse the repository at this point in the history
add "atol=custom_atol" for every assert_allclose statement that did not have it previously
  • Loading branch information
mgrub committed Apr 30, 2024
1 parent b9debd7 commit 7d7488d
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 64 deletions.
1 change: 1 addition & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from PyDynamic import make_semiposdef
from PyDynamic.misc.tools import normalize_vector_or_matrix

custom_atol = 1e2 * np.finfo(np.float64).eps

def _check_for_ci_to_switch_off_performance_related_healthchecks():
if _running_in_ci():
Expand Down
8 changes: 5 additions & 3 deletions test/test_DFT.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
hypothesis_float_vector,
hypothesis_not_negative_float_strategy,
VectorAndCompatibleMatrix,
custom_atol,
)


Expand Down Expand Up @@ -259,7 +260,7 @@ def test_DFT_MC(params):
)

# compare analytical and numerical result
assert_allclose(X, X_MC, rtol=1e-1)
assert_allclose(X, X_MC, rtol=1e-1, atol=custom_atol)
assert_allclose(X_cov, X_MC_cov, atol=8e-1)


Expand Down Expand Up @@ -322,11 +323,11 @@ def test_iDFT_resampling_sensitivity(params):
x_resampled_numpy = np.fft.irfft(ri2c(X), n=Nx)

# check resampled signal against numpy implementation
assert_allclose(x_resampled_numpy, x_resampled, atol=1e-14)
assert_allclose(x_resampled_numpy, x_resampled, atol=custom_atol)

# check sensitivities against numpy implementation
C = np.hstack((sens["Cc"], sens["Cs"]))
assert_allclose(x_resampled_numpy, C @ X / Nx, atol=1e-14)
assert_allclose(x_resampled_numpy, C @ X / Nx, atol=custom_atol)


@pytest.mark.slow
Expand Down Expand Up @@ -406,6 +407,7 @@ def test_apply_window_with_known_result(known_inputs_and_outputs_for_apply_windo
assert_allclose(
actual=_apply_window(**known_inputs_and_outputs_for_apply_window["inputs"]),
desired=known_inputs_and_outputs_for_apply_window["outputs"],
atol=custom_atol,
)


Expand Down
5 changes: 4 additions & 1 deletion test/test_DFT_deconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
hypothesis_float_vector,
hypothesis_nonzero_complex_vector,
hypothesis_positive_powers_of_two,
custom_atol,
)


Expand Down Expand Up @@ -86,11 +87,13 @@ def test_dft_deconv(
x_deconv + x_deconv_shift_away_from_zero,
monte_carlo_mean + x_deconv_shift_away_from_zero,
rtol=6.8e-2,
atol=custom_atol,
)
assert_allclose(
u_deconv + u_deconv_shift_away_from_zero,
monte_carlo_cov + u_deconv_shift_away_from_zero,
rtol=4.98e-1,
atol=custom_atol,
)


Expand Down Expand Up @@ -209,4 +212,4 @@ def test_reveal_bug_in_dft_deconv_up_to_1_9(
n_monte_carlo_runs=n_monte_carlo_runs,
operator=complex_deconvolution_on_sets,
)
assert_allclose(u_deconv + 1, y_divided_by_h_mc_cov + 1)
assert_allclose(u_deconv + 1, y_divided_by_h_mc_cov + 1, atol=custom_atol)
9 changes: 7 additions & 2 deletions test/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from hypothesis import assume, given
from hypothesis.strategies import composite
from numpy.testing import assert_allclose
from PyDynamic.uncertainty.interpolate import interp1d_unc, make_equidistant
from pytest import raises

from PyDynamic.uncertainty.interpolate import interp1d_unc, make_equidistant
from .conftest import custom_atol

_MIN_NODES_FOR_CUBIC_SPLINE = 4

Expand Down Expand Up @@ -595,7 +596,11 @@ def test_returnc_with_extrapolation_check_c_interp1d_unc(

# Check if each row of sensitivities sum to one, which should hold for the
# Lagrangians and proves equality with one for extrapolation sensitivities.
assert_allclose(np.sum(C, 1), np.ones_like(interp_inputs["x_new"]))
assert_allclose(
np.sum(C, 1),
np.ones_like(interp_inputs["x_new"]),
atol=custom_atol,
)


@given(
Expand Down
17 changes: 14 additions & 3 deletions test/test_lsfir/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import scipy.signal as dsp
from hypothesis.strategies import composite
from numpy.testing import assert_allclose, assert_almost_equal

from PyDynamic.misc.SecondOrderSystem import sos_phys2filter
from PyDynamic.misc.tools import (
complex_2_real_imag,
make_semiposdef,
real_imag_2_complex,
)

from ..conftest import (
custom_atol,
hypothesis_float_vector,
scale_matrix_or_vector_to_convex_combination,
)
Expand Down Expand Up @@ -101,6 +102,7 @@ def monte_carlo(
np.load(
os.path.join(reference_array_path, "test_LSFIR_H.npz"),
)["H"],
atol=custom_atol,
)
uAbs = np.std(np.abs(HMC), axis=0)
assert_allclose(
Expand All @@ -109,6 +111,7 @@ def monte_carlo(
os.path.join(reference_array_path, "test_LSFIR_uAbs.npz"),
)["uAbs"],
rtol=3.5e-2,
atol=custom_atol,
)
uPhas = np.std(np.angle(HMC), axis=0)
assert_allclose(
Expand All @@ -117,6 +120,7 @@ def monte_carlo(
os.path.join(reference_array_path, "test_LSFIR_uPhas.npz"),
)["uPhas"],
rtol=4.3e-2,
atol=custom_atol,
)
UH = np.cov(np.hstack((np.real(HMC), np.imag(HMC))), rowvar=False)
UH = make_semiposdef(UH)
Expand Down Expand Up @@ -145,10 +149,16 @@ def digital_filter(measurement_system, sampling_freq):
measurement_system["S0"], measurement_system["delta"], measurement_system["f0"]
)
assert_almost_equal(bc, [20465611686.098896])
assert_allclose(ac, np.array([1.00000000e00, 4.52389342e03, 5.11640292e10]))
assert_allclose(
ac,
np.array([1.00000000e00, 4.52389342e03, 5.11640292e10]),
atol=custom_atol,
)
b, a = dsp.bilinear(bc, ac, sampling_freq)
assert_allclose(
b, np.array([0.019386043211510096, 0.03877208642302019, 0.019386043211510096])
b,
np.array([0.019386043211510096, 0.03877208642302019, 0.019386043211510096]),
atol=custom_atol,
)
assert_allclose(a, np.array([1.0, -1.7975690550957188, 0.9914294872108197]))
return {"b": b, "a": a}
Expand All @@ -168,5 +178,6 @@ def complex_freq_resp(
np.load(
os.path.join(reference_array_path, "test_LSFIR_Hf.npz"),
)["Hf"],
atol=custom_atol,
)
return Hf
10 changes: 8 additions & 2 deletions test/test_lsfir/test_LSFIR.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .conftest import weights
from ..conftest import (
hypothesis_dimension,
custom_atol,
)


Expand Down Expand Up @@ -48,7 +49,7 @@ def simulated_measurement_input_and_output(
"test_LSFIR_x.npz",
),
)["x"],
atol=1e2*np.finfo(np.float64).eps,
atol=custom_atol,
)
y = dsp.lfilter(digital_filter["b"], digital_filter["a"], x)
noise = 1e-3
Expand All @@ -74,6 +75,7 @@ def LSFIR_filter_fit(monte_carlo, freqs, sampling_freq):
"test_LSFIR_bF.npz",
),
)["bF"],
atol=custom_atol,
)
assert_allclose(
UbF,
Expand All @@ -85,6 +87,7 @@ def LSFIR_filter_fit(monte_carlo, freqs, sampling_freq):
),
)["UbF"],
rtol=3e-1,
atol=custom_atol,
)
return {"bF": bF, "UbF": UbF, "N": N, "tau": tau}

Expand All @@ -103,6 +106,7 @@ def fir_low_pass(measurement_system, sampling_freq):
"test_LSFIR_blow.npz",
),
)["blow"],
atol=custom_atol,
)
return {"blow": blow, "lshift": lshift}

Expand All @@ -119,6 +123,7 @@ def shift(simulated_measurement_input_and_output, LSFIR_filter_fit, fir_low_pass
"test_LSFIR_shift.npz",
),
)["shift"],
atol=custom_atol,
)
return shift

Expand Down Expand Up @@ -239,6 +244,7 @@ def test_digital_deconvolution_FIR_example_figure_5(
"test_LSFIR_HbF.npz",
),
)["HbF"],
atol=custom_atol,
)
plt.semilogy(
freqs * 1e-3,
Expand Down Expand Up @@ -660,4 +666,4 @@ def test_compare_invLSFIR_to_LSFIR(monte_carlo, freqs, sampling_freq, filter_ord
tau=filter_order // 2,
inv=True,
)[0]
assert_allclose(b_fir, b_fir_inv_lsfir)
assert_allclose(b_fir, b_fir_inv_lsfir, atol=custom_atol)
5 changes: 3 additions & 2 deletions test/test_lsfir/test_compare_different_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)
from ..conftest import (
hypothesis_dimension,
custom_atol,
)


Expand Down Expand Up @@ -43,5 +44,5 @@ def test(monte_carlo, complex_H_with_UH, freqs, sampling_freq, filter_order):
UH=monte_carlo["UH"],
mc_runs=10000,
)
assert_allclose(b_real_imaginary, b_complex, rtol=4e-2)
assert_allclose(ub_real_imaginary, ub_complex, rtol=6e-1)
assert_allclose(b_real_imaginary, b_complex, rtol=4e-2, atol=custom_atol)
assert_allclose(ub_real_imaginary, ub_complex, rtol=6e-1, atol=custom_atol)
5 changes: 3 additions & 2 deletions test/test_lsfir/test_compare_inv_ls_fir_to_unc_mc_ls_fir.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)
from ..conftest import (
hypothesis_dimension,
custom_atol,
)


Expand Down Expand Up @@ -40,5 +41,5 @@ def test(monte_carlo, freqs, sampling_freq, filter_order):
UH=monte_carlo["UH"],
mc_runs=10000,
)
assert_allclose(b_fir_mc, b_fir, rtol=4e-2)
assert_allclose(Ub_fir_mc, Ub_fir, atol=6e-1, rtol=6e-1)
assert_allclose(b_fir_mc, b_fir, rtol=4e-2, atol=custom_atol)
assert_allclose(Ub_fir_mc, Ub_fir, rtol=6e-1, atol=6e-1)
3 changes: 2 additions & 1 deletion test/test_lsfir/test_compare_inv_ls_fir_unc_to_ls_fir.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)
from ..conftest import (
hypothesis_dimension,
custom_atol,
)


Expand Down Expand Up @@ -39,5 +40,5 @@ def test(monte_carlo, freqs, sampling_freq, filter_order):
inv=True,
UH=monte_carlo["UH"],
)
assert_allclose(b_fir_mc, b_fir)
assert_allclose(b_fir_mc, b_fir, atol=custom_atol)
assert_allclose(Ub_fir_mc, Ub_fir, atol=6e-1, rtol=6e-1)
5 changes: 3 additions & 2 deletions test/test_lsfir/test_compare_ls_fir_with_svd_and_with_mc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)
from ..conftest import (
hypothesis_dimension,
custom_atol,
)


Expand Down Expand Up @@ -42,5 +43,5 @@ def test(monte_carlo, freqs, sampling_freq, filter_order):
UH=monte_carlo["UH"],
mc_runs=10000,
)
assert_allclose(b_fir_mc, b_fir_svd, rtol=9e-2)
assert_allclose(Ub_fir_mc, Ub_fir_svd, atol=6e-1, rtol=6e-1)
assert_allclose(b_fir_mc, b_fir_svd, rtol=9e-2, atol=custom_atol)
assert_allclose(Ub_fir_mc, Ub_fir_svd, rtol=6e-1, atol=6e-1)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)
from ..conftest import (
hypothesis_dimension,
custom_atol,
)


Expand Down Expand Up @@ -42,7 +43,7 @@ def test_compare_LSFIR_with_zero_to_None_uncertainties_with_svd_for_fitting_one_
inv=True,
UH=None,
)[0]
assert_allclose(b_fir_svd, b_fir_none)
assert_allclose(b_fir_svd, b_fir_none, atol=custom_atol)


@given(hypothesis_dimension(min_value=4, max_value=8))
Expand Down Expand Up @@ -76,7 +77,7 @@ def test_compare_LSFIR_with_zero_to_None_uncertainties_and_mc_for_fitting_one_ov
inv=True,
UH=None,
)[0]
assert_allclose(b_fir_mc, b_fir_none)
assert_allclose(b_fir_mc, b_fir_none, atol=custom_atol)


@given(hypothesis_dimension(min_value=4, max_value=8))
Expand Down Expand Up @@ -110,4 +111,4 @@ def test_compare_LSFIR_with_zero_to_None_uncertainties_and_mc_for_fitting_H_dire
inv=False,
UH=None,
)[0]
assert_allclose(b_fir_mc, b_fir_none)
assert_allclose(b_fir_mc, b_fir_none, atol=custom_atol)
Loading

0 comments on commit 7d7488d

Please sign in to comment.