-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculations.py
50 lines (46 loc) · 2.03 KB
/
calculations.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
#Function to compute F1-score from p_matrices and val_matrices
def get_metric_f1(ref_p_matrix, p_matrix, ref_val_matrix, val_matrix, alpha,
tau_min=0, tau_diff=1, same_sign=True):
N, N, taumaxp1 = val_matrix.shape
TP = 0
FP = 0
FN = 0
auto = 0
count = 0
for i in range(N):
for j in range(N):
if i != j:
for tau in range(tau_min, taumaxp1):
# print(np.sum(ref_p_matrix[i,j,tau] < alpha),np.sum(p_matrix[i,j,tau] < alpha))
if ref_p_matrix[i,j,tau] > alpha and p_matrix[i,j,tau] < alpha:
FP += 1
elif ref_p_matrix[i,j,tau] < alpha and np.any(p_matrix[i,j,max(0,tau-tau_diff):tau+tau_diff+1] < alpha):
count +=1
if same_sign==True and np.sign(ref_val_matrix[i,j,tau]) == np.sign(val_matrix[i,j,tau]):
TP += 1
elif same_sign==True and np.sign(ref_val_matrix[i,j,tau]) != np.sign(val_matrix[i,j,tau]):
FN += 1
elif same_sign==False:
TP += 1
elif ref_p_matrix[i,j,tau] < alpha and not(np.any(p_matrix[i,j,max(0,tau-tau_diff):tau+tau_diff+1] < alpha)):
FN += 1
else:
auto +=1
precision = float(TP+1e-10) / float(TP + FP +1e-10)
recall = float(TP+1e-10) / float(TP + FN +1e-10)
f1 = 2.0*precision*recall/float(precision + recall)
return precision, recall, TP, FP, FN, f1, auto, count
def put_row_first(df,row_name):
#put the row_name row at the first position of the df
df_out=df
df_out["new"] = range(1,len(df)+1)
df_out.loc[row_name, 'new'] = 0
df_out=df_out.sort_values("new").drop('new', axis=1)
return df_out
def put_column_first(df,column_name):
#put the column_name col at the first position of the df
df_out=df
col= df_out.pop(column_name)
df_out.insert(0,column_name,col)
return df_out