Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion mriqc/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,14 @@ def _bids_filter(value):
default=False,
help='Despike the functional scans during head motion correction preprocessing.',
)
g_func.add_argument(
'--random-seed',
dest='_random_seed',
action='store',
type=int,
default=None,
help='Initialize the random seed for the workflow',
)
g_func.add_argument(
'--start-idx',
action=DeprecateAction,
Expand All @@ -461,7 +469,6 @@ def _bids_filter(value):
help='DEPRECATED Final volume in functional timeseries that should be '
'considered for preprocessing.',
)

latest = check_latest()
if latest is not None and currentv < latest:
print(
Expand Down
41 changes: 41 additions & 0 deletions mriqc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
from time import strftime
from typing import TYPE_CHECKING, Any, Iterable
from uuid import uuid4
import random

try:
# This option is only available with Python 3.8
Expand Down Expand Up @@ -696,11 +697,51 @@ def getLogger(cls, name) -> logging.Logger:
return retval


class seeds(_Config):
"""Initialize the PRNG and track random seed assignments"""

_random_seed = None
master = None
"""Master random seed to initialize the Pseudorandom Number Generator (PRNG)"""
ants = None
"""Seed used for antsRegistration, antsAI, antsMotionCorr"""
numpy = None
"""Seed used by NumPy"""

@classmethod
def init(cls):
if cls._random_seed is not None:
cls.master = cls._random_seed
if cls.master is None:
cls.master = random.randint(1, 65536)
random.seed(cls.master) # initialize the PRNG
# functions to set program specific seeds
cls.ants = _set_ants_seed()
cls.numpy = _set_numpy_seed()


def _set_ants_seed():
"""Fix random seed for antsRegistration, antsAI, antsMotionCorr"""
val = random.randint(1, 65536)
os.environ['ANTS_RANDOM_SEED'] = str(val)
return val


def _set_numpy_seed():
"""NumPy's random seed is independent from Python's `random` module"""
import numpy as np

val = random.randint(1, 65536)
np.random.seed(val)
return val


def from_dict(sections: dict) -> None:
"""Read settings from a flat dictionary."""
execution.load(sections)
workflow.load(sections)
nipype.load(sections, init=False)
seeds.load(sections, init=False)


def load(filename: str | os.PathLike) -> None:
Expand Down