Skip to content

stemflow.utils.validation


Validation module. Most of these functions are plain checking and easy to understand.

check_random_state(seed)

Turn seed into a np.random.RandomState instance.

Parameters:

  • seed (Union[None, int, RandomState]) –

    If seed is None, return the RandomState singleton used by np.random. If seed is an int, return a new RandomState instance seeded with seed. If seed is already a RandomState instance, return it. Otherwise raise ValueError.

Returns:

  • RandomState

    The random state object based on seed parameter.

Source code in stemflow/utils/validation.py
def check_random_state(seed: Union[None, int, np.random.RandomState]) -> np.random.RandomState:
    """Turn seed into a np.random.RandomState instance.

    Args:
        seed:
            If seed is None, return the RandomState singleton used by np.random.
            If seed is an int, return a new RandomState instance seeded with seed.
            If seed is already a RandomState instance, return it.
            Otherwise raise ValueError.

    Returns:
        The random state object based on `seed` parameter.
    """
    if seed is None or seed is np.random:
        return np.random.mtrand._rand
    if isinstance(seed, int):
        return np.random.RandomState(seed)
    if isinstance(seed, np.random.RandomState):
        return seed
    raise ValueError("%r cannot be used to seed a numpy.random.RandomState instance" % seed)