-
Notifications
You must be signed in to change notification settings - Fork 1
/
script_plot_recall_graphs.py
171 lines (133 loc) · 4.62 KB
/
script_plot_recall_graphs.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from utils.results_perser_utils import conv2dic
from utils.dynamic_plot_lib_v3 import dynamic_plot
import argparse
import os
import numpy as np
import math
import pandas as pd
class plots():
def __init__(self,title,grid_on = True):
SIZE = 25
fontsize = {'text':15,
'xtick':SIZE,
'ytick':SIZE,
'title':15,
'axis':SIZE,
'legend':SIZE,
'labels':SIZE,
'figure':SIZE}
self.plot = dynamic_plot(title,'N-Number of top candidates ','Recall@N',fontsize = fontsize)
self.grid_on = grid_on
def update(self,label,x,y,**arg):
key = label
y = np.array(y)
x = np.array(x)
# default parameters
color = 'g'
karg = {'linestyle':'-'}
if 'color' in arg:
color = arg['color']
if 'linestyle' in arg:
karg['linestyle'] = arg['linestyle']
self.axis_limit = {'xmin':min(x),'xmax':max(x),'ymin':0,'ymax':1}
self.plot.add_plot( key,
color=color,
save=False,
scale = 10,
window = 0,
label = key,
**karg)
self.plot.update_plot(key,x,y)
self.plot.show(grid_on =self.grid_on,axis= self.axis_limit)
def save_data_file(self,root):
self.plot.save_data_file(root)
def hold(self):
self.plot.hold_fig()
def file_parser(file):
if not os.path.isfile(file):
print("[INF] Result file does not exist!")
raise Exception
df = pd.DataFrame()
for line in open(file):
line = line.strip()
if line =='': # Empty line
continue
line = line.split("||")
if line[1] == '': # Line transition
continue
header = line[0]
data = line[1].split(' ')
# Convert from str to dictionairy
persed_data = conv2dic(data)
# convert to pandas
persed_data_DF = pd.DataFrame(persed_data,index = persed_data.keys())
# append to global data frame
df = df.append(persed_data_DF,ignore_index=True )
return(df)
def compt_sequence_stats(results,seq,field):
attention = range(5)
net = 3
sessions = 'cross_val_' + '%02d'%(seq)
net_values = {}
values = {'mean':[],'std':[],'layers':[]}
for att in attention:
#for layer in layers:
encoder = results[(results.session == sessions) & (results.modelA == att)& (results.modelB == net)][field]
mean_value = round(np.mean(encoder[encoder!=-1]),3)
std_value = round(np.std(encoder[encoder!=-1]),3)
values['mean'].append(mean_value)
values['std'].append(std_value)
values['layers'].append(att)
return(values)
def compt_recall_stats(results, seq,models):
top_cand_range = np.unique([int(v) for v in results.C])
values = {'recall':[],'model':[],'can':[]}
for enc,At in models.items():
for at in At:
model = "E" + str(enc) + 'A'+str(at)
values['model'].append(model)
recall_array = []
top_cand = []
for can in top_cand_range:
recall = results[(results.modelA == at) & (results.modelB == enc) & (results.session == seq) & (results.C == can)]['R']
mean_value = round(np.mean(recall[recall!=-1]),3)
if math.isnan(mean_value) :
continue
recall_array.append(mean_value)
top_cand.append(can)
values['recall'].append(recall_array)
values['can'].append(top_cand)
return(values)
if __name__ == '__main__':
parser = argparse.ArgumentParser("./infer.py")
parser.add_argument(
'--file', '-f',
type=str,
default = "results_paper/recall_results.txt",
#default = "results_paper/attention_study.txt",
required=False,
help='Dataset to train with. No Default',
)
sequences = ['cross_val_00']
fig = plots('',grid_on=True)
FLAGS, unparsed = parser.parse_known_args()
# Get File
file_to_parse = FLAGS.file
# Parse file
results = file_parser(file_to_parse)
# Demo: get all data belonging to cross_val_00
#
colors = np.array(['g','b','y','k','r'])
line = np.array(['-','--',':','-','--'])
models = {3:[0,1,4],5:[0,3]}
for seq in sequences:
recall_curves = compt_recall_stats(results,seq,models)
recall_a,model_a,top_a = list(recall_curves.values())
for i,(recall,model,top) in enumerate(zip(recall_a,model_a,top_a)):
fig.update(label = model,
y = recall[0:11],
x =top[0:11],
color = colors[i],
linestyle = line[i]
)
fig.hold()