-
Notifications
You must be signed in to change notification settings - Fork 5
/
voronoi_tools.py
50 lines (39 loc) · 1.3 KB
/
voronoi_tools.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
import numpy as np
def normalize(D):
Vmin, Vmax = D.min(), D.max()
if Vmax - Vmin > 1e-5:
D = (D-Vmin)/(Vmax-Vmin)
else:
D = np.zeros_like(D)
return D
def normalize_threshold(D,min,max):
Vmin, Vmax = D.min(), D.max()
if Vmax - Vmin > 1e-5:
D = (D-Vmin)/(Vmax-Vmin)
D = D*(max-min)+min
else:
D = np.zeros_like(D)
return D
def initialization(n, D):
"""
Return n points distributed over [xmin, xmax] x [ymin, ymax]
according to (normalized) density distribution.
with xmin, xmax = 0, density.shape[1]
ymin, ymax = 0, density.shape[0]
The algorithm here is a simple rejection sampling.
"""
samples = []
while len(samples) < n:
# X = np.random.randint(0, D.shape[1], 10*n)
# Y = np.random.randint(0, D.shape[0], 10*n)
X = np.random.uniform(0, D.shape[1], 10*n)
Y = np.random.uniform(0, D.shape[0], 10*n)
P = np.random.uniform(0, 1, 10*n)
index = 0
while index < len(X) and len(samples) < n:
x, y = X[index], Y[index]
x_, y_ = int(np.floor(x)), int(np.floor(y))
if P[index] < D[y_, x_]:
samples.append([x, y])
index += 1
return np.array(samples)