-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest-refine.py
139 lines (121 loc) Β· 4.54 KB
/
test-refine.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os, sys
import numpy as np
from math import sqrt
# testing without install
#sys.path.insert(0, '../build/lib.macosx-10.9-x86_64-3.8')
import poppunk_refine
# Original PopPUNK function (with some improvements)
def withinBoundary(dists, x_max, y_max, slope=2):
boundary_test = np.ones((dists.shape[0]))
for row in range(boundary_test.size):
if slope == 2:
in_tri = dists[row, 1] * x_max + dists[row, 0] * y_max - x_max * y_max
elif slope == 0:
in_tri = dists[row, 0] - x_max
elif slope == 1:
in_tri = dists[row, 1] - y_max
if abs(in_tri) < np.finfo(np.float32).eps:
boundary_test[row] = 0
elif in_tri < 0:
boundary_test[row] = -1
return(boundary_test)
def check_tuples(t1, t2):
for t in t1:
if t not in t2:
raise RuntimeError("Results don't match")
def iter_tuples(assign_results, n_samples):
tuple_list = []
idx = 0
for i in range(n_samples):
for j in range(i + 1, n_samples):
if assign_results[idx] == -1:
tuple_list.append((i, j))
idx += 1
return tuple_list
def check_res(res, expected):
if (not np.all(res == expected)):
print(res)
print(expected)
raise RuntimeError("Results don't match")
# assigning
x = np.arange(0, 1, 0.1, dtype=np.float32)
y = np.arange(0, 1, 0.1, dtype=np.float32)
xv, yv = np.meshgrid(x, y)
distMat = np.hstack((xv.reshape(-1,1), yv.reshape(-1,1)))
assign0 = poppunk_refine.assignThreshold(distMat, 0, 0.5, 0.5, 2)
assign1 = poppunk_refine.assignThreshold(distMat, 1, 0.5, 0.5, 2)
assign2 = poppunk_refine.assignThreshold(distMat, 2, 0.5, 0.5, 2)
assign0_res = withinBoundary(distMat, 0.5, 0.5, 0)
assign1_res = withinBoundary(distMat, 0.5, 0.5, 1)
assign2_res = withinBoundary(distMat, 0.5, 0.5, 2)
check_res(assign0, assign0_res)
check_res(assign1, assign1_res)
check_res(assign2, assign2_res)
# Check results when returned as tuple
samples = 100
distMat = np.random.rand(int(0.5 * samples * (samples - 1)), 2)
distMat = np.array(distMat, dtype = np.float32)
assign0_res = withinBoundary(distMat, 0.5, 0.5, 0)
assign0_edge_res = iter_tuples(assign0_res, samples)
check_tuples(assign0_edge_res,
poppunk_refine.generateTuples([int(x) for x in assign0_res], -1))
assign1_edge_res = iter_tuples(withinBoundary(distMat, 0.5, 0.5, 1), samples)
assign2_edge_res = iter_tuples(withinBoundary(distMat, 0.5, 0.5, 2), samples)
assign0_edges = poppunk_refine.edgeThreshold(distMat, 0, 0.5, 0.5)
assign1_edges = poppunk_refine.edgeThreshold(distMat, 1, 0.5, 0.5)
assign2_edges = poppunk_refine.edgeThreshold(distMat, 2, 0.5, 0.5)
check_tuples(assign0_edges, assign0_edge_res)
check_tuples(assign1_edges, assign1_edge_res)
check_tuples(assign2_edges, assign2_edge_res)
# move boundary 1D
# example is symmetrical at points (0.1, 0.1); (0.2, 0.2); (0.3, 0.3)
offsets = [x * sqrt(2) for x in [-0.1, 0.0, 0.1]]
i_vec, j_vec, idx_vec = poppunk_refine.thresholdIterate1D(distMat, offsets, 2, 0.2, 0.2, 0.3, 0.3)
sketchlib_i = []
sketchlib_j = []
for offset_idx, offset in enumerate(offsets):
for i, j, idx in zip(i_vec, j_vec, idx_vec):
if idx > offset_idx:
break
elif idx == offset_idx:
sketchlib_i.append(i)
sketchlib_j.append(j)
py_i = []
py_j = []
xmax = 0.4 + (2 * (offset/sqrt(2)))
assign = poppunk_refine.assignThreshold(distMat, 2, xmax, xmax, 1)
dist_idx = 0
for i in range(samples):
for j in range(i + 1, samples):
if assign[dist_idx] <= 0:
py_i.append(i)
py_j.append(j)
dist_idx += 1
if set(zip(py_i, py_j)) != set(zip(sketchlib_i, sketchlib_j)):
raise RuntimeError("Threshold 1D iterate mismatch at offset " + str(offset))
# move boundary 2D
# example is for boundaries (0.1, 0.2); (0.2, 0.2); (0.3, 0.2)
offsets = [0.1, 0.2, 0.3]
y_max = 0.2
i_vec, j_vec, idx_vec = poppunk_refine.thresholdIterate2D(distMat, offsets, y_max)
sketchlib_i = []
sketchlib_j = []
for offset_idx, offset in enumerate(offsets):
for i, j, idx in zip(i_vec, j_vec, idx_vec):
if idx > offset_idx:
break
elif idx == offset_idx:
sketchlib_i.append(i)
sketchlib_j.append(j)
py_i = []
py_j = []
assign = poppunk_refine.assignThreshold(distMat, 2, offset, y_max, 1)
dist_idx = 0
for i in range(samples):
for j in range(i + 1, samples):
if assign[dist_idx] <= 0:
py_i.append(i)
py_j.append(j)
dist_idx += 1
if set(zip(py_i, py_j)) != set(zip(sketchlib_i, sketchlib_j)):
raise RuntimeError("Threshold 2D iterate mismatch at offset " + str(offset))