Skip to content

Commit bc550c2

Browse files
Fix random seeds in non-deterministic tests
1 parent e8d6705 commit bc550c2

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

tests/test_multi_bunch_gauss_gen.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import xpart as xp
1212
import xtrack as xt
1313

14-
from xobjects.test_helpers import for_all_test_contexts
14+
from xobjects.test_helpers import for_all_test_contexts, fix_random_seed
15+
1516
test_data_folder = xt._pkg_root.joinpath('../test_data').absolute()
1617

1718
@for_all_test_contexts
19+
@fix_random_seed(6453645)
1820
def test_multi_bunch_gaussian_generation(test_context):
1921
bunch_intensity = 1e11
2022
sigma_z = 22.5e-2 / 5

tests/test_pencil_with_absolute_cut.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
import xpart as xp
1111
import xtrack as xt
1212
import xobjects as xo
13-
from xobjects.test_helpers import for_all_test_contexts
14-
from xpart.test_helpers import flaky_assertions, retry
13+
from xobjects.test_helpers import for_all_test_contexts, fix_random_seed
1514

1615

1716
@for_all_test_contexts
18-
@retry()
17+
@fix_random_seed(7363444)
1918
def test_pencil_with_absolute_cut(test_context):
2019

2120
num_particles = 10000
@@ -97,26 +96,24 @@ def test_pencil_with_absolute_cut(test_context):
9796
sigma_v = np.sqrt(betv*nemitt_v
9897
/particles._xobject.beta0[0]/particles._xobject.gamma0[0])
9998

100-
with flaky_assertions():
101-
assert(np.isclose(np.min(np.abs(v)), abs(absolute_cut), atol=1e-7))
102-
assert(np.isclose(np.max(np.abs(v)), abs(absolute_cut) + sigma_v*pencil_dr_sigmas,
103-
rtol=1e-3, atol=0))
99+
assert(np.isclose(np.min(np.abs(v)), abs(absolute_cut), atol=1e-7))
100+
assert(np.isclose(np.max(np.abs(v)), abs(absolute_cut) + sigma_v*pencil_dr_sigmas,
101+
rtol=1e-3, atol=0))
104102

105-
i_tip = np.argmax(np.abs(v))
106-
assert np.isclose(pv[i_tip]/v[i_tip], -alfv/betv, atol=5e-4)
103+
i_tip = np.argmax(np.abs(v))
104+
assert np.isclose(pv[i_tip]/v[i_tip], -alfv/betv, atol=5e-4)
107105

108-
if side == '+':
109-
assert np.all(v >= 0)
110-
else:
111-
assert np.all(v <= 0)
112-
113-
other_plane = {'x': 'y', 'y': 'x'}[plane]
114-
w_norm = getattr(norm_coords, other_plane+'_norm')
115-
pw_norm = getattr(norm_coords, 'p'+other_plane+'_norm')
106+
if side == '+':
107+
assert np.all(v >= 0)
108+
else:
109+
assert np.all(v <= 0)
116110

117-
xo.assert_allclose(w_in_sigmas, w_norm, 1e-12)
118-
xo.assert_allclose(pw_in_sigmas, pw_norm, 1e-12)
111+
other_plane = {'x': 'y', 'y': 'x'}[plane]
112+
w_norm = getattr(norm_coords, other_plane+'_norm')
113+
pw_norm = getattr(norm_coords, 'p'+other_plane+'_norm')
119114

120-
assert(np.allclose(zeta, particles.zeta, atol=1e-12))
121-
assert(np.allclose(delta, particles.delta, atol=1e-12))
115+
xo.assert_allclose(w_in_sigmas, w_norm, 1e-12)
116+
xo.assert_allclose(pw_in_sigmas, pw_norm, 1e-12)
122117

118+
xo.assert_allclose(zeta, particles.zeta, atol=1e-12)
119+
xo.assert_allclose(delta, particles.delta, atol=1e-12)

tests/test_single_rf_harmonic_matcher.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import xobjects as xo
1212

1313
import xpart as xp
14-
from xpart.test_helpers import flaky_assertions, retry
15-
from xobjects.test_helpers import for_all_test_contexts
14+
from xobjects.test_helpers import fix_random_seed
15+
1616

1717
@pytest.mark.parametrize('scenario', ['psb_injection', 'sps_ions', 'lhc_protons'])
1818
@pytest.mark.parametrize('distribution', ['gaussian', 'parabolic'])
19-
@retry(n_times=3)
19+
@fix_random_seed(46398498)
2020
def test_single_rf_harmonic_matcher_rms_and_profile_and_tune(
2121
scenario, distribution):
2222
for ctx in xo.context.get_test_contexts():
@@ -74,16 +74,11 @@ def test_single_rf_harmonic_matcher_rms_and_profile_and_tune(
7474

7575
twiss_tune = line.twiss()['qs']
7676
theoretical_synchrotron_tune = matcher.get_synchrotron_tune()
77-
print(twiss_tune, theoretical_synchrotron_tune)
78-
print(twiss_tune - theoretical_synchrotron_tune,
79-
(theoretical_synchrotron_tune - twiss_tune)
80-
/(theoretical_synchrotron_tune))
8177

8278
assert np.isclose(theoretical_synchrotron_tune,
8379
twiss_tune, rtol=3.e-3, atol=1.e-15)
8480

85-
with flaky_assertions():
86-
assert np.isclose(rms_bunch_length, np.std(zeta),
87-
rtol=2e-2, atol=1e-15)
88-
assert np.all(np.isclose(hist, tau_distr_y,
89-
atol=3.e-2, rtol=1.e-2))
81+
assert np.isclose(rms_bunch_length, np.std(zeta),
82+
rtol=2e-2, atol=1e-15)
83+
84+
xo.assert_allclose(hist, tau_distr_y, atol=3.e-2, rtol=1.e-2)

0 commit comments

Comments
 (0)