Spaces:
Running
Running
File size: 3,058 Bytes
681fa96 |
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# Use `Any` as the return type to avoid mypy problems with Union data types,
# because numpy can return single number and ndarray
import random as py_random
from typing import Any, Optional, Sequence, Type, Union
import numpy as np
from .core.transforms_interface import NumType
IntNumType = Union[int, np.ndarray]
Size = Union[int, Sequence[int]]
def get_random_state() -> np.random.RandomState:
return np.random.RandomState(py_random.randint(0, (1 << 32) - 1))
def uniform(
low: NumType = 0.0,
high: NumType = 1.0,
size: Optional[Size] = None,
random_state: Optional[np.random.RandomState] = None,
) -> Any:
if random_state is None:
random_state = get_random_state()
return random_state.uniform(low, high, size)
def rand(d0: NumType, d1: NumType, *more, random_state: Optional[np.random.RandomState] = None, **kwargs) -> Any:
if random_state is None:
random_state = get_random_state()
return random_state.rand(d0, d1, *more, **kwargs) # type: ignore
def randn(d0: NumType, d1: NumType, *more, random_state: Optional[np.random.RandomState] = None, **kwargs) -> Any:
if random_state is None:
random_state = get_random_state()
return random_state.randn(d0, d1, *more, **kwargs) # type: ignore
def normal(
loc: NumType = 0.0,
scale: NumType = 1.0,
size: Optional[Size] = None,
random_state: Optional[np.random.RandomState] = None,
) -> Any:
if random_state is None:
random_state = get_random_state()
return random_state.normal(loc, scale, size)
def poisson(
lam: NumType = 1.0, size: Optional[Size] = None, random_state: Optional[np.random.RandomState] = None
) -> Any:
if random_state is None:
random_state = get_random_state()
return random_state.poisson(lam, size)
def permutation(
x: Union[int, Sequence[float], np.ndarray], random_state: Optional[np.random.RandomState] = None
) -> Any:
if random_state is None:
random_state = get_random_state()
return random_state.permutation(x)
def randint(
low: IntNumType,
high: Optional[IntNumType] = None,
size: Optional[Size] = None,
dtype: Type = np.int32,
random_state: Optional[np.random.RandomState] = None,
) -> Any:
if random_state is None:
random_state = get_random_state()
return random_state.randint(low, high, size, dtype)
def random(size: Optional[NumType] = None, random_state: Optional[np.random.RandomState] = None) -> Any:
if random_state is None:
random_state = get_random_state()
return random_state.random(size) # type: ignore
def choice(
a: NumType,
size: Optional[Size] = None,
replace: bool = True,
p: Optional[Union[Sequence[float], np.ndarray]] = None,
random_state: Optional[np.random.RandomState] = None,
) -> Any:
if random_state is None:
random_state = get_random_state()
return random_state.choice(a, size, replace, p) # type: ignore
|