graphid.util.util_random module¶
- graphid.util.util_random.shuffle(items, rng=None)[source]¶
Shuffles a list inplace and then returns it for convinience
- Parameters:
items (list or ndarray) – list to shuffl
rng (RandomState or int) – seed or random number gen
- Returns:
this is the input, but returned for convinience
- Return type:
Example
>>> list1 = [1, 2, 3, 4, 5, 6] >>> list2 = shuffle(list(list1), rng=1) >>> assert list1 != list2 >>> result = str(list2) >>> print(result) [3, 2, 5, 1, 4, 6]
- graphid.util.util_random.random_combinations(items, size, num=None, rng=None)[source]¶
Yields num combinations of length size from items in random order
- Parameters:
items (List) – pool of items to choose from
size (int) – number of items in each combination
num (None, default=None) – number of combinations to generate
rng (int | RandomState, default=None) – seed or random number generator
- Yields:
tuple – combo
Example
>>> import ubelt as ub # NOQA >>> items = list(range(10)) >>> size = 3 >>> num = 5 >>> rng = 0 >>> combos = list(random_combinations(items, size, num, rng)) >>> result = ('combos = %s' % (ub.urepr(combos),)) >>> print(result)
Example
>>> import ubelt as ub # NOQA >>> items = list(zip(range(10), range(10))) >>> size = 3 >>> num = 5 >>> rng = 0 >>> combos = list(random_combinations(items, size, num, rng)) >>> result = ('combos = %s' % (ub.urepr(combos),)) >>> print(result)
- graphid.util.util_random.random_product(items, num=None, rng=None)[source]¶
Yields num items from the cartesian product of items in a random order.
- Parameters:
items (list of sequences) – items to get caresian product of packed in a list or tuple. (note this deviates from api of it.product)
Example
>>> items = [(1, 2, 3), (4, 5, 6, 7)] >>> rng = 0 >>> list(random_product(items, rng=0)) >>> list(random_product(items, num=3, rng=0))
- graphid.util.util_random._npstate_to_pystate(npstate)[source]¶
Convert state of a NumPy RandomState object to a state that can be used by Python’s Random.
References
https://stackoverflow.com/questions/44313620/converting-randomstate
Example
>>> py_rng = random.Random(0) >>> np_rng = np.random.RandomState(seed=0) >>> npstate = np_rng.get_state() >>> pystate = _npstate_to_pystate(npstate) >>> py_rng.setstate(pystate) >>> assert np_rng.rand() == py_rng.random()
- graphid.util.util_random._pystate_to_npstate(pystate)[source]¶
Convert state of a Python Random object to state usable by NumPy RandomState.
References
https://stackoverflow.com/questions/44313620/converting-randomstate
Example
>>> py_rng = random.Random(0) >>> np_rng = np.random.RandomState(seed=0) >>> pystate = py_rng.getstate() >>> npstate = _pystate_to_npstate(pystate) >>> np_rng.set_state(npstate) >>> assert np_rng.rand() == py_rng.random()
- graphid.util.util_random.ensure_rng(rng, api='numpy')[source]¶
Returns a random number generator
- Parameters:
seed – if None, then the rng is unseeded. Otherwise the seed can be an integer or a RandomState class
Example
>>> rng = ensure_rng(None) >>> ensure_rng(0).randint(0, 1000) 684 >>> ensure_rng(np.random.RandomState(1)).randint(0, 1000) 37
Example
>>> num = 4 >>> print('--- Python as PYTHON ---') >>> py_rng = random.Random(0) >>> pp_nums = [py_rng.random() for _ in range(num)] >>> print(pp_nums) >>> print('--- Numpy as PYTHON ---') >>> np_rng = ensure_rng(random.Random(0), api='numpy') >>> np_nums = [np_rng.rand() for _ in range(num)] >>> print(np_nums) >>> print('--- Numpy as NUMPY---') >>> np_rng = np.random.RandomState(seed=0) >>> nn_nums = [np_rng.rand() for _ in range(num)] >>> print(nn_nums) >>> print('--- Python as NUMPY---') >>> py_rng = ensure_rng(np.random.RandomState(seed=0), api='python') >>> pn_nums = [py_rng.random() for _ in range(num)] >>> print(pn_nums) >>> assert np_nums == pp_nums >>> assert pn_nums == nn_nums