From bc550c28bfda140c4c1a8e69c3d3e3ec7eec7a22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20=C5=81opaciuk?= Date: Thu, 16 Jan 2025 14:26:32 +0100 Subject: [PATCH] Fix random seeds in non-deterministic tests --- tests/test_multi_bunch_gauss_gen.py | 4 ++- tests/test_pencil_with_absolute_cut.py | 39 +++++++++++------------- tests/test_single_rf_harmonic_matcher.py | 19 +++++------- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/tests/test_multi_bunch_gauss_gen.py b/tests/test_multi_bunch_gauss_gen.py index a3285bb..3bc88da 100644 --- a/tests/test_multi_bunch_gauss_gen.py +++ b/tests/test_multi_bunch_gauss_gen.py @@ -11,10 +11,12 @@ import xpart as xp import xtrack as xt -from xobjects.test_helpers import for_all_test_contexts +from xobjects.test_helpers import for_all_test_contexts, fix_random_seed + test_data_folder = xt._pkg_root.joinpath('../test_data').absolute() @for_all_test_contexts +@fix_random_seed(6453645) def test_multi_bunch_gaussian_generation(test_context): bunch_intensity = 1e11 sigma_z = 22.5e-2 / 5 diff --git a/tests/test_pencil_with_absolute_cut.py b/tests/test_pencil_with_absolute_cut.py index bcf8168..e7361f9 100644 --- a/tests/test_pencil_with_absolute_cut.py +++ b/tests/test_pencil_with_absolute_cut.py @@ -10,12 +10,11 @@ import xpart as xp import xtrack as xt import xobjects as xo -from xobjects.test_helpers import for_all_test_contexts -from xpart.test_helpers import flaky_assertions, retry +from xobjects.test_helpers import for_all_test_contexts, fix_random_seed @for_all_test_contexts -@retry() +@fix_random_seed(7363444) def test_pencil_with_absolute_cut(test_context): num_particles = 10000 @@ -97,26 +96,24 @@ def test_pencil_with_absolute_cut(test_context): sigma_v = np.sqrt(betv*nemitt_v /particles._xobject.beta0[0]/particles._xobject.gamma0[0]) - with flaky_assertions(): - assert(np.isclose(np.min(np.abs(v)), abs(absolute_cut), atol=1e-7)) - assert(np.isclose(np.max(np.abs(v)), abs(absolute_cut) + sigma_v*pencil_dr_sigmas, - rtol=1e-3, atol=0)) + assert(np.isclose(np.min(np.abs(v)), abs(absolute_cut), atol=1e-7)) + assert(np.isclose(np.max(np.abs(v)), abs(absolute_cut) + sigma_v*pencil_dr_sigmas, + rtol=1e-3, atol=0)) - i_tip = np.argmax(np.abs(v)) - assert np.isclose(pv[i_tip]/v[i_tip], -alfv/betv, atol=5e-4) + i_tip = np.argmax(np.abs(v)) + assert np.isclose(pv[i_tip]/v[i_tip], -alfv/betv, atol=5e-4) - if side == '+': - assert np.all(v >= 0) - else: - assert np.all(v <= 0) - - other_plane = {'x': 'y', 'y': 'x'}[plane] - w_norm = getattr(norm_coords, other_plane+'_norm') - pw_norm = getattr(norm_coords, 'p'+other_plane+'_norm') + if side == '+': + assert np.all(v >= 0) + else: + assert np.all(v <= 0) - xo.assert_allclose(w_in_sigmas, w_norm, 1e-12) - xo.assert_allclose(pw_in_sigmas, pw_norm, 1e-12) + other_plane = {'x': 'y', 'y': 'x'}[plane] + w_norm = getattr(norm_coords, other_plane+'_norm') + pw_norm = getattr(norm_coords, 'p'+other_plane+'_norm') - assert(np.allclose(zeta, particles.zeta, atol=1e-12)) - assert(np.allclose(delta, particles.delta, atol=1e-12)) + xo.assert_allclose(w_in_sigmas, w_norm, 1e-12) + xo.assert_allclose(pw_in_sigmas, pw_norm, 1e-12) + xo.assert_allclose(zeta, particles.zeta, atol=1e-12) + xo.assert_allclose(delta, particles.delta, atol=1e-12) diff --git a/tests/test_single_rf_harmonic_matcher.py b/tests/test_single_rf_harmonic_matcher.py index c44671d..9b8a827 100644 --- a/tests/test_single_rf_harmonic_matcher.py +++ b/tests/test_single_rf_harmonic_matcher.py @@ -11,12 +11,12 @@ import xobjects as xo import xpart as xp -from xpart.test_helpers import flaky_assertions, retry -from xobjects.test_helpers import for_all_test_contexts +from xobjects.test_helpers import fix_random_seed + @pytest.mark.parametrize('scenario', ['psb_injection', 'sps_ions', 'lhc_protons']) @pytest.mark.parametrize('distribution', ['gaussian', 'parabolic']) -@retry(n_times=3) +@fix_random_seed(46398498) def test_single_rf_harmonic_matcher_rms_and_profile_and_tune( scenario, distribution): for ctx in xo.context.get_test_contexts(): @@ -74,16 +74,11 @@ def test_single_rf_harmonic_matcher_rms_and_profile_and_tune( twiss_tune = line.twiss()['qs'] theoretical_synchrotron_tune = matcher.get_synchrotron_tune() - print(twiss_tune, theoretical_synchrotron_tune) - print(twiss_tune - theoretical_synchrotron_tune, - (theoretical_synchrotron_tune - twiss_tune) - /(theoretical_synchrotron_tune)) assert np.isclose(theoretical_synchrotron_tune, twiss_tune, rtol=3.e-3, atol=1.e-15) - with flaky_assertions(): - assert np.isclose(rms_bunch_length, np.std(zeta), - rtol=2e-2, atol=1e-15) - assert np.all(np.isclose(hist, tau_distr_y, - atol=3.e-2, rtol=1.e-2)) + assert np.isclose(rms_bunch_length, np.std(zeta), + rtol=2e-2, atol=1e-15) + + xo.assert_allclose(hist, tau_distr_y, atol=3.e-2, rtol=1.e-2)