# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/06_seed.ipynb.

# %% auto 0
__all__ = ['osseed', 'npseed', 'ptseed', 'plseed', 'seedfn', 'seedall']

# %% ../nbs/06_seed.ipynb 6
#| export


# %% ../nbs/06_seed.ipynb 8
from types import FunctionType

# %% ../nbs/06_seed.ipynb 11
#| export

# %% ../nbs/06_seed.ipynb 13
#| export


# %% ../nbs/06_seed.ipynb 15
#| export


# %% ../nbs/06_seed.ipynb 17
#| export


# %% ../nbs/06_seed.ipynb 19
from lull import lull

# %% ../nbs/06_seed.ipynb 21
from .cons import _SEEDED
from .attr import _seeded

# %% ../nbs/06_seed.ipynb 24
def osseed(seed: int) -> int:
    '''Set python random seed for reproducibility.'''
    try: 
        import random
        random.seed(seed)
    except ImportError: ...
    return seed

def npseed(seed: int) -> int:
    '''Set numpy seed for reproducibility.'''
    try: 
        import numpy as np
        np.random.seed(seed)
    except ImportError: ...
    return seed

def ptseed(seed: int) -> int:
    '''Set pytorch seed for reproducibility.'''
    try: 
        import torch
        torch.manual_seed(seed)
    except ImportError: ...
    return seed


def plseed(seed: int) -> int:
    '''Set pytorch lightning seed for reproducibility.'''
    try: 
        import pytorch_lightning as pl
        pl.seed_everything(seed)
    except ImportError: ...
    return seed

# %% ../nbs/06_seed.ipynb 25
def seedfn(fn: FunctionType, seed: int = 3, reseed: bool = True) -> int:
    # seeded = getattr(fn, _SEEDED, False)
    if not _seeded(fn) or reseed:
        fn(seed)
        _seeded.set(fn, True)
        # setattr(fn, _SEEDED, True)
    return seed

# %% ../nbs/06_seed.ipynb 27
def seedall(seed: int = 3, reseed: bool = False) -> int:
    '''Set seeds for reproducibility.
    
    Parameters
    ----------
    seed : int

    Returns
    -------
    seed: int
        The seed used to set the random number generators.

    Notes
    -----
    There is a lot to say about [randomness](https://pytorch.org/docs/stable/notes/randomness.html) and 
    reproducibiliity in python. For a more detailed discussion, see the 
    documentation on 
    [random.seed](https://docs.python.org/3/library/random.html#random.seed), 
    [np.random.seed](https://numpy.org/doc/stable/reference/random/generated/numpy.random.seed.html),
    [torch.manual_seed](https://pytorch.org/docs/stable/generated/torch.manual_seed.html), and
    [pl.seed_everything]( https://pytorch-lightning.readthedocs.io/en/latest/common/seed_everything.html).

    See Also
    --------
    random.seed, np.random.seed, torch.manual_seed, pl.seed_everything

    .. _randomness: https://pytorch.org/docs/stable/notes/randomness.html
    .. _random.seed: https://docs.python.org/3/library/random.html#random.seed
    .. _np.random.seed: https://numpy.org/doc/stable/reference/random/generated/numpy.random.seed.html
    .. _torch.manual_seed: https://pytorch.org/docs/stable/generated/torch.manual_seed.html
    .. _pl.seed_everything: https://pytorch-lightning.readthedocs.io/en/latest/common/seed_everything.html
    '''    
    with lull():
        seedfn(osseed, seed, reseed=reseed)
        seedfn(npseed, seed, reseed=reseed)
        seedfn(ptseed, seed, reseed=reseed)
        seedfn(plseed, seed, reseed=reseed)  
    return seed

# %% ../nbs/06_seed.ipynb 29
#| export
