-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval.py
157 lines (132 loc) · 4.92 KB
/
eval.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
import torch
import random
def calc_frac_idx(x1_mat,x2_mat):
"""
Returns fraction closer than true match for each sample (as an array)
"""
fracs = []
x = []
nsamp = x1_mat.shape[0]
rank=0
for row_idx in range(nsamp):
euc_dist = np.sqrt(np.sum(np.square(np.subtract(x1_mat[row_idx,:], x2_mat)), axis=1))
true_nbr = euc_dist[row_idx]
sort_euc_dist = sorted(euc_dist)
rank =sort_euc_dist.index(true_nbr)
frac = float(rank)/(nsamp -1)
fracs.append(frac)
x.append(row_idx+1)
return fracs,x
def calc_domainAveraged_FOSCTTM(x1_mat, x2_mat):
"""
Metric from SCOT: "FOSCTTM"
Outputs average FOSCTTM measure (averaged over both domains)
Get the fraction matched for all data points in both directions
Averages the fractions in both directions for each data point
"""
fracs1,xs = calc_frac_idx(x1_mat, x2_mat)
fracs2,xs = calc_frac_idx(x2_mat, x1_mat)
fracs = []
for i in range(len(fracs1)):
fracs.append((fracs1[i]+fracs2[i])/2)
return fracs
def test_transfer_accuracy(data1, data2, type1, type2):
"""
Metric from UnionCom: "Label Transfer Accuracy"
"""
Min = np.minimum(len(data1), len(data2))
k = np.maximum(10, (len(data1) + len(data2))*0.01)
k = k.astype(np.int)
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(data2, type2)
type1_predict = knn.predict(data1)
# np.savetxt("type1_predict.txt", type1_predict)
count = 0
for label1, label2 in zip(type1_predict, type1):
if label1 == label2:
count += 1
return count / len(type1)
def test_alignment_score(data1_shared, data2_shared, data1_specific=None, data2_specific=None):
N = 2
if len(data1_shared) < len(data2_shared):
data1 = data1_shared
data2 = data2_shared
else:
data2 = data1_shared
data1 = data2_shared
data2 = data2[random.sample(range(len(data2)), len(data1))]
k = np.maximum(10, (len(data1) + len(data2))*0.01)
k = k.astype(np.int)
data = np.vstack((data1, data2))
bar_x1 = 0
for i in range(len(data1)):
diffMat = data1[i] - data
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1)
NearestN = np.argsort(sqDistances)[1:k+1]
for j in NearestN:
if j < len(data1):
bar_x1 += 1
bar_x1 = bar_x1 / len(data1)
bar_x2 = 0
for i in range(len(data2)):
diffMat = data2[i] - data
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1)
NearestN = np.argsort(sqDistances)[1:k+1]
for j in NearestN:
if j >= len(data1):
bar_x2 += 1
bar_x2 = bar_x2 / len(data2)
bar_x = (bar_x1 + bar_x2) / 2
score = 0
score += 1 - (bar_x - k/N) / (k - k/N)
data_specific = None
flag = 0
if data1_specific is not None:
data_specific = data1_specific
if data2_specific is not None:
data_specific = np.vstack((data_specific, data2_specific))
flag=1
else:
if data2_specific is not None:
data_specific = data2_specific
if data_specific is None:
return score
else:
bar_specific1 = 0
bar_specific2 = 0
data = np.vstack((data, data_specific))
if flag==0: # only one of data1_specific and data2_specific is not None
for i in range(len(data_specific)):
diffMat = data_specific[i] - data
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1)
NearestN = np.argsort(sqDistances)[1:k+1]
for j in NearestN:
if j > (len(data1)+len(data2)):
bar_specific1 += 1
bar_specific = bar_specific1
else: # both data1_specific and data2_specific are not None
for i in range(len(data1_specific)):
diffMat = data1_specific[i] - data
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1)
NearestN = np.argsort(sqDistances)[1:k+1]
for j in NearestN:
if j > (len(data1)+len(data2)) and j < (len(data1)+len(data2)+len(data1_specific)):
bar_specific1 += 1
for i in range(len(data2_specific)):
diffMat = data2_specific[i] - data
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1)
NearestN = np.argsort(sqDistances)[1:k+1]
for j in NearestN:
if j > (len(data1)+len(data2)+len(data1_specific)):
bar_specific2 += 1
bar_specific = bar_specific1 + bar_specific2
bar_specific = bar_specific / len(data_specific)
score += (bar_specific - k/N) / (k - k/N)
return score / 2