File size: 1,907 Bytes
749bffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Deterministic seeding.

WHY: "Seed everything" is a stated engineering convention, and it is not
cosmetic here. With n ~ 124-169 cells, the difference between two models is
often smaller than the difference between two random 5-fold partitions of the
same data. Unless the partitions are reproducible, a benchmark table cannot be
regenerated and a reported improvement cannot be distinguished from a lucky
split.
"""

from __future__ import annotations

import os
import random

DEFAULT_SEED = 42


def set_global_seed(seed: int = DEFAULT_SEED) -> int:
    """Seed Python's ``random``, NumPy, and hash randomisation.

    ``PYTHONHASHSEED`` is set for completeness but only affects interpreters
    started afterwards; it is recorded so that the provenance stamp is honest
    about what was and was not controlled.

    Args:
        seed: The seed to apply.

    Returns:
        The seed applied, for convenient logging.
    """
    os.environ["PYTHONHASHSEED"] = str(seed)
    random.seed(seed)

    try:
        import numpy as np

        np.random.seed(seed)
    except ImportError:  # NumPy is a hard dependency in practice; tolerated here
        pass                # so the scaffold validates before deps are installed.

    return seed


def seed_for_repeat(base_seeds: list[int], repeat_index: int) -> int:
    """Return the fixed seed for a given outer-CV repeat.

    WHY explicit per-repeat seeds rather than ``base + i``: the seeds are
    recorded in ``configs/models.yaml``, so a re-run reproduces the same ten
    partitions even if the number of repeats is later changed.
    """
    if not 0 <= repeat_index < len(base_seeds):
        raise IndexError(
            f"Repeat index {repeat_index} is outside the {len(base_seeds)} configured repeat seeds."
        )
    return base_seeds[repeat_index]


if __name__ == "__main__":
    print(f"Global seed set to {set_global_seed()}")