Skip to content

Commit

Permalink
use better random seeds and tell others about it (#1340)
Browse files Browse the repository at this point in the history
* use better random seeds and tell others about it

* add more details about rng reasoning
  • Loading branch information
sappelhoff authored Nov 22, 2024
1 parent 247ad2a commit 049fa39
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions examples/anonymize_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
# To ensure results are reproducible across runs, you can pass the
# ``random_state`` parameter, causing the random number generator to produce
# the same results every time you execute the function. This may come in handy
# e.g. in situations where you discover a problem with the data while working
# in situations where you discover a problem with the data while working
# with the anonymized dataset, fix the issue in the original dataset, and
# run anonymization again.
#
Expand All @@ -203,6 +203,15 @@
# in a dataset with multiple subjects will the effects of randomly-picked IDs
# become apparent.)
#
# A good random seed is truly random. Avoid using random seeds from popular
# culture, like "42", or "1337". To obtain a truly random seed, you can paste
# the following into your console:
# ``python -c "import secrets; print(secrets.randbits(31))"``
# Here, 31 bits correspond to the maximum seed "size" that the the legacy
# ``RandomState`` by NumPy, which many scientific libraries still rely on,
# can accept. For more information, see also this blog post on
# `NumPy RNG best practices <https://blog.scientific-python.org/numpy/numpy-rng/>`_.
#
# .. note::
# Passing ``random_state`` merely guarantees that subject IDs and time shift
# remain the same across anonymization runs if the original dataset
Expand All @@ -218,6 +227,6 @@
bids_root_in=bids_root,
bids_root_out=bids_root_anon,
datatypes="meg",
random_state=42,
random_state=293201004,
)
print_dir_tree(bids_root_anon)
2 changes: 1 addition & 1 deletion mne_bids/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -3868,7 +3868,7 @@ def test_anonymize_dataset(_bids_validate, tmpdir):
bids_root_in=bids_root,
bids_root_out=bids_root_anon,
datatypes=["meg", "anat"],
random_state=42,
random_state=1442792182,
)
_bids_validate(bids_root_anon)
assert (bids_root_anon / "sub-1" / "ses-01" / "meg").exists()
Expand Down

0 comments on commit 049fa39

Please sign in to comment.