clinicadl.utils.seed.seed_everything_context

clinicadl.utils.seed.seed_everything_context(seed: int | None = None, deterministic: bool = False) Generator[None, None, None][source]

Context manager to control reproducibility.

Does the same as seed_everything(), but restore all previous random states when exiting the context.

Parameters:
  • seed (Optional[int], default=None) – The seed to use. If None, a random seed will be generated.

  • deterministic (bool, default=False) – Whether to configure PyTorch’s operations in deterministic mode.

Examples

from clinicadl.utils.seed import seed_everything_context
import torch
import numpy as np
import random
>>> with seed_everything_context(0): print(torch.randn(1), np.random.randn(), random.randint(0, 100))
tensor([1.5410]) 1.764052345967664 49
>>> print(torch.randn(1), np.random.randn(), random.randint(0, 100))
tensor([0.8120]) 0.5023488957207493 50
>>> with seed_everything_context(0): print(torch.randn(1), np.random.randn(), random.randint(0, 100))
tensor([1.5410]) 1.764052345967664 49