Skip to content

Commit 346d5a1

Browse files
authored
Move toward determinism (#14090)
1 parent 9ead159 commit 346d5a1

115 files changed

Lines changed: 705 additions & 535 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/simulation/simulate_evoked_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@
5151
# Generate source time courses from 2 dipoles and the corresponding evoked data
5252

5353
times = np.arange(300, dtype=np.float64) / raw.info["sfreq"] - 0.1
54-
rng = np.random.RandomState(42)
54+
rng = np.random.default_rng(42)
5555

5656

5757
def data_fun(times):
5858
"""Generate random source time courses."""
5959
return (
6060
50e-9
6161
* np.sin(30.0 * times)
62-
* np.exp(-((times - 0.15 + 0.05 * rng.randn(1)) ** 2) / 0.01)
62+
* np.exp(-((times - 0.15 + rng.normal(scale=0.05, size=1)) ** 2) / 0.01)
6363
)
6464

6565

examples/simulation/simulate_raw_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
n_dipoles = 4 # number of dipoles to create
4646
epoch_duration = 2.0 # duration of each epoch/event
4747
n = 0 # harmonic number
48-
rng = np.random.RandomState(0) # random state (make reproducible)
48+
rng = np.random.default_rng(0) # random state (make reproducible)
4949

5050

5151
def data_fun(times):

examples/simulation/simulated_raw_data_using_subject_anatomy.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def data_fun(times, latency, duration):
136136
sigma = 0.375 * duration
137137
sinusoid = np.sin(2 * np.pi * f * (times - latency))
138138
gf = np.exp(
139-
-((times - latency - (sigma / 4.0) * rng.rand(1)) ** 2) / (2 * (sigma**2))
139+
-((times - latency - rng.uniform(high=sigma / 4.0, size=1)) ** 2)
140+
/ (2 * (sigma**2))
140141
)
141142
return 1e-9 * sinusoid * gf
142143

@@ -162,7 +163,7 @@ def data_fun(times, latency, duration):
162163

163164
times = np.arange(150, dtype=np.float64) / info["sfreq"]
164165
duration = 0.03
165-
rng = np.random.RandomState(7)
166+
rng = np.random.default_rng(7)
166167
source_simulator = mne.simulation.SourceSimulator(src, tstep=tstep)
167168

168169
for region_id, region_name in enumerate(region_names, 1):

examples/simulation/source_simulator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class to generate source estimates and raw data. It is meant to be a brief
8585
# simulator can be given directly to the simulate_raw function.
8686
raw = mne.simulation.simulate_raw(info, source_simulator, forward=fwd)
8787
cov = mne.make_ad_hoc_cov(raw.info)
88-
mne.simulation.add_noise(raw, cov, iir_filter=[0.2, -0.2, 0.04])
88+
mne.simulation.add_noise(raw, cov, iir_filter=[0.2, -0.2, 0.04], random_state=97)
8989
raw.plot()
9090

9191
# %%

examples/stats/cluster_stats_evoked.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
threshold=threshold,
7171
tail=1,
7272
n_jobs=None,
73+
seed=0,
7374
out_type="mask",
7475
)
7576

examples/stats/sensor_permutation_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
data = np.mean(data[:, :, temporal_mask], axis=2)
6262

6363
n_permutations = 50000
64-
T0, p_values, H0 = permutation_t_test(data, n_permutations, n_jobs=None)
64+
T0, p_values, H0 = permutation_t_test(data, n_permutations, n_jobs=None, seed=0)
6565

6666
significant_sensors = picks[p_values <= 0.05]
6767
significant_sensors_names = [raw.ch_names[k] for k in significant_sensors]

examples/time_frequency/time_frequency_simulated.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
n_times = 1024 # Just over 1 second epochs
4545
n_epochs = 40
4646
seed = 42
47-
rng = np.random.RandomState(seed)
48-
data = rng.randn(len(ch_names), n_times * n_epochs + 200) # buffer
47+
rng = np.random.default_rng(seed)
48+
data = rng.standard_normal((len(ch_names), n_times * n_epochs + 200)) # buffer
4949

5050
# Add a 50 Hz sinusoidal burst to the noise and ramp it.
5151
t = np.arange(n_times, dtype=np.float64) / sfreq

mne/_fiff/tests/test_meas_info.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ def test_get_valid_units():
137137

138138
def test_coil_trans():
139139
"""Test loc<->coil_trans functions."""
140-
rng = np.random.RandomState(0)
141-
x = rng.randn(4, 4)
140+
rng = np.random.default_rng(0)
141+
x = rng.standard_normal((4, 4))
142142
x[3] = [0, 0, 0, 1]
143143
assert_allclose(_loc_to_coil_trans(_coil_trans_to_loc(x)), x)
144-
x = rng.randn(12)
144+
x = rng.standard_normal(12)
145145
assert_allclose(_coil_trans_to_loc(_loc_to_coil_trans(x)), x)
146146

147147

mne/_fiff/tests/test_pick.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,9 @@ def test_pick_forward_seeg_ecog():
474474

475475
def test_picks_by_channels():
476476
"""Test creating pick_lists."""
477-
rng = np.random.RandomState(909)
477+
rng = np.random.default_rng(909)
478478

479-
test_data = rng.random_sample((4, 2000))
479+
test_data = rng.random((4, 2000))
480480
ch_names = [f"MEG {i:03d}" for i in [1, 2, 3, 4]]
481481
ch_types = ["grad", "mag", "mag", "eeg"]
482482
sfreq = 250.0
@@ -495,7 +495,7 @@ def test_picks_by_channels():
495495
assert len(pick_list) == len(pick_list2) + 1
496496
assert pick_list2[0][0] == "meg"
497497

498-
test_data = rng.random_sample((4, 2000))
498+
test_data = rng.random((4, 2000))
499499
ch_names = [f"MEG {i:03d}" for i in [1, 2, 3, 4]]
500500
ch_types = ["mag", "mag", "mag", "mag"]
501501
sfreq = 250.0
@@ -754,7 +754,7 @@ def test_get_channel_types_equiv(meg, eeg, ordered):
754754
pick_types(raw.info, meg=meg, eeg=eeg)
755755
picks = pick_types(raw.info, meg=meg, eeg=eeg)
756756
if not ordered:
757-
picks = np.random.RandomState(0).permutation(picks)
757+
picks = np.random.default_rng(0).permutation(picks)
758758
if not meg and not eeg:
759759
with pytest.raises(ValueError, match="No appropriate channels"):
760760
raw.get_channel_types(picks=picks)

mne/_fiff/tests/test_reference.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ def test_set_eeg_reference_ch_type(ch_type, msg, projection):
277277
# gh-6454
278278
# gh-8739 added DBS
279279
ch_names = ["ECOG01", "ECOG02", "DBS01", "DBS02", "MISC"]
280-
rng = np.random.RandomState(0)
281-
data = rng.randn(5, 1000)
280+
rng = np.random.default_rng(0)
281+
data = rng.standard_normal((5, 1000))
282282
raw = RawArray(
283283
data, create_info(ch_names, 1000.0, ["ecog"] * 2 + ["dbs"] * 2 + ["misc"])
284284
)
@@ -976,7 +976,8 @@ def test_bipolar_combinations():
976976
info = create_info(
977977
ch_names=ch_names, sfreq=1000.0, ch_types=["eeg"] * len(ch_names)
978978
)
979-
raw_data = np.random.randn(len(ch_names), 1000)
979+
rng = np.random.default_rng(0)
980+
raw_data = rng.standard_normal((len(ch_names), 1000))
980981
raw = RawArray(raw_data, info)
981982

982983
def _check_bipolar(raw_test, ch_a, ch_b):

0 commit comments

Comments
 (0)