-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.py
More file actions
139 lines (123 loc) · 4.67 KB
/
utils.py
File metadata and controls
139 lines (123 loc) · 4.67 KB
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
import numpy as np
from sklearn.preprocessing import OneHotEncoder
from astropy import stats
def normalize_biweight(x, eps=1e-10):
median = np.median(x)
scale = stats.biweight.biweight_scale(x)
if np.std(x) < 1e+2 or np.isnan(scale) or scale < 1e-4:
norm = (x-np.mean(x))/np.std(x)
else:
norm = (x - median) / (scale + eps)
return norm
def normalize(x):
norm = lambda x: (x-np.mean(x))/np.std(x)
return np.apply_along_axis(norm, 0, x)
def data_preprocess(df):
def _encoding(i):
if df.iloc[:,i].dtype == 'O' or df.iloc[:, i].dtype.name == 'category':
tempX = df.iloc[:, i].values.reshape(-1, 1)
enc = OneHotEncoder(handle_unknown='ignore')
enc.fit(tempX)
out = enc.transform(tempX).toarray()
else:
out = df.iloc[:, i].values.reshape(-1, 1)
return out
p = df.shape[1]
X_encode = [_encoding(i) for i in np.arange(p)]
return X_encode
def evaluate_binary(trueG, estG):
TP, TN, FP, FN, FD, MD, FPMD = 0, 0, 0, 0, 0, 0, 0
n_node = trueG.shape[0]
for i in range(1, n_node):
for j in range(i):
if trueG[i, j] == 1 and trueG[j, i] == 0 and estG[i, j] == 1 and \
estG[j, i] == 0:
TP += 1
if trueG[i, j] == 0 and trueG[j, i] == 1 and estG[i, j] == 0 and \
estG[j, i] == 1:
TP += 1
if trueG[i, j] == 0 and trueG[j, i] == 0 and estG[i, j] == 0 and \
estG[j, i] == 0:
TN += 1
if trueG[i, j] == 0 and trueG[j, i] == 0 and estG[i, j] == 1 and \
estG[j, i] == 0:
FP += 1
if trueG[i, j] == 0 and trueG[j, i] == 0 and estG[i, j] == 0 and \
estG[j, i] == 1:
FP += 1
if trueG[i, j] == 1 and trueG[j, i] == 0 and estG[i, j] == 0 and \
estG[j, i] == 0:
FN += 1
if trueG[i, j] == 0 and trueG[j, i] == 1 and estG[i, j] == 0 and \
estG[j, i] == 0:
FN += 1
if trueG[i, j] == 1 and trueG[j, i] == 0 and estG[i, j] == 0 and \
estG[j, i] == 1:
FD += 1
if trueG[i, j] == 0 and trueG[j, i] == 1 and estG[i, j] == 1 and \
estG[j, i] == 0:
FD += 1
if trueG[i, j] == 0 and trueG[j, i] == 1 and estG[i, j] == 1 and \
estG[j, i] == 1:
MD += 1
if trueG[i, j] == 1 and trueG[j, i] == 0 and estG[i, j] == 1 and \
estG[j, i] == 1:
MD += 1
if trueG[i, j] == 0 and trueG[j, i] == 0 and estG[i, j] == 1 and \
estG[j, i] == 1:
FPMD += 1
if (TP + FP + FD)>0:
Precision = TP / (TP + FP + FD)
else:
Precision = 0.0
Recall = TP / sum(sum(trueG))
if (TP + FN + FD) > 0:
Recall1 = TP / (TP + FN + FD)
else:
Recall1 = 0.0
SHD = sum(sum((trueG != estG) | np.transpose((trueG != estG)))) / 2
return {'TP': TP, 'TN': TN, 'FP': FP, 'FN': FN, 'FD': FD, 'MD': MD,
'FPMD': FPMD,'Precision': Precision, 'Recall': Recall,
'Recall_NOMD': Recall1, 'SHD': SHD}
def skeleton_metrics(trueG, estG):
TP,TN,FP,FN = 0,0,0,0
n = trueG.shape[0]
for i in range(n):
for j in range(i):
if trueG[i, j] == 1 or trueG[j, i] == 1:
if estG[i, j] != 0 or estG[j, i] != 0:
TP += 1
else:
FN += 1
else:
if estG[i, j] == 0 and estG[j, i] == 0:
TN += 1
else:
FP += 1
return {'TP': TP, 'TN': TN, 'FP': FP, 'FN': FN}
def check_connect_skel(skeleton, i, j):
depth = set.union(set(np.where(skeleton[:,i]==1)[0]),
set(np.where(skeleton[i,:]==1)[0]))
checked = depth
while depth:
if j in depth:
return True
next = {}
for k in depth:
next = set.union(next, set.union(set(np.where(skeleton[:,k]==1)[0]),
set(np.where(skeleton[k,:]==1)[0])))
depth = set.difference(next, checked)
checked = set.union(checked, depth)
return False
def reachable(dag, fr, to):
depth = set(np.where(dag[fr,:]==1)[0])
checked = depth
while depth:
if to in depth:
return True
next = set()
for k in depth:
next = set.union(next, set(np.where(dag[k,:]==1)[0]))
depth = set.difference(next, checked)
checked = set.union(checked, depth)
return False