-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
57 lines (49 loc) · 1.86 KB
/
sample.py
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
import random
import numpy as np
from numba import jit
import logging
numba_logger = logging.getLogger('numba')
numba_logger.setLevel(logging.WARNING)
@jit(nopython=True)
def seq_binary_sample(ngh_binomial_prob, num_neighbor):
sampled_idx = []
for j in range(num_neighbor):
idx = seq_binary_sample_one(ngh_binomial_prob)
sampled_idx.append(idx)
sampled_idx = np.array(sampled_idx) # not necessary but just for type alignment with the other branch
return sampled_idx
@jit(nopython=True)
def seq_binary_sample_one(ngh_binomial_prob):
seg_len = 10
a_l_seg = np.random.random((seg_len,))
seg_idx = 0
for idx in range(len(ngh_binomial_prob)-1, -1, -1):
a = a_l_seg[seg_idx]
seg_idx += 1 # move one step forward
if seg_idx >= seg_len:
a_l_seg = np.random.random((seg_len,)) # regenerate a batch of new random values
seg_idx = 0 # and reset the seg_idx
if a < ngh_binomial_prob[idx]:
# print('=' * 50)
# print(a, len(ngh_binomial_prob) - idx, len(ngh_binomial_prob),
# (len(ngh_binomial_prob) - idx) / len(ngh_binomial_prob), ngh_binomial_prob)
return idx
return 0 # very extreme case due to float rounding error
@jit(nopython=True)
def bisect_left_adapt(a, x):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e < x, and all e in
a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
insert just before the leftmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
lo = 0
hi = len(a)
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x: lo = mid+1
else: hi = mid
return lo