-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
348 lines (268 loc) · 12.9 KB
/
menu.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import viztools
from PyQt5 import QtWidgets
from h5xplorer.menu_tools import *
from deeprank.tools import sparse
from pdb2sql import pdb2sql
from deeprank.learn import rankingMetrics
import numpy as np
def context_menu(self, treeview, position):
"""Generate a right-click menu for the items"""
# make sure tha there is only one item selected
all_item = get_current_item(self,treeview,single=False)
if len(all_item) == 1:
item = all_item[0]
try:
_type = self.root_item.data_file[item.name].attrs['type']
if _type == 'molecule':
molgrp = self.root_item.data_file[item.name]
_context_mol(item,treeview,position,molgrp)
if _type == 'sparse_matrix':
_context_sparse(item,treeview,position)
if _type == 'epoch':
_task = self.root_item.data_file[item.name].attrs['task']
_context_one_epoch(item,treeview,position,_task)
if _type == 'losses':
_context_losses(item,treeview,position)
except Exception as inst:
print(type(inst))
print(inst)
return
else :
_type = np.array([self.root_item.data_file[item.name].attrs['type'] for item in all_item])
epoch_item = [item for item in all_item if self.root_item.data_file[item.name].attrs['type'] == 'epoch' ]
haddock_item = [item for item in all_item if self.root_item.data_file[item.name].attrs['type'] == 'haddock' ]
_context_multiple_epoch_multilevel(epoch_item,treeview,position,haddock_item)
def _context_mol(item,treeview,position,molgrp):
menu = QtWidgets.QMenu()
actions = {}
list_operations = ['Load in PyMol','Load in VMD','PDB2SQL']
for operation in list_operations:
actions[operation] = menu.addAction(operation)
action = menu.exec_(treeview.viewport().mapToGlobal(position))
_,cplx_name, mol_name = item.name.split('/')
mol_name = mol_name.replace('-','_')
if action == actions['Load in VMD']:
viztools.create3Ddata(mol_name, molgrp)
viztools.launchVMD(mol_name)
if action == actions['Load in PyMol']:
viztools.create3Ddata(mol_name,molgrp)
viztools.launchPyMol(mol_name)
if action == actions['PDB2SQL']:
db = pdb2sql(molgrp['complex'].value)
treeview.emitDict.emit({'sql_' + item.basename: db})
def _context_sparse(item,treeview,position):
menu = QtWidgets.QMenu()
list_operations = ['Load Matrix','Plot Histogram']
action,actions = get_actions(treeview,position,list_operations)
name = item.basename + '_' + item.name.split('/')[2]
if action == actions['Load Matrix']:
subgrp = item.data_file[item.name]
data_dict = {}
if not subgrp.attrs['sparse']:
data_dict[item.name] = subgrp['value'].value
else:
molgrp = item.data_file[item.parent.parent.parent.name]
grid = {}
lx = len(molgrp['grid_points/x'].value)
ly = len(molgrp['grid_points/y'].value)
lz = len(molgrp['grid_points/z'].value)
shape = (lx,ly,lz)
spg = sparse.FLANgrid(sparse=True,index=subgrp['index'].value,value=subgrp['value'].value,shape=shape)
data_dict[name] = spg.to_dense()
treeview.emitDict.emit(data_dict)
if action == actions['Plot Histogram']:
value = item.data_file[item.name]['value'].value
data_dict = {'value':value}
treeview.emitDict.emit(data_dict)
cmd = "%matplotlib inline\nimport matplotlib.pyplot as plt\nplt.hist(value,25)\nplt.show()\n"
data_dict = {'exec_cmd':cmd}
treeview.emitDict.emit(data_dict)
def _context_one_epoch(item,treeview,position,task):
menu = QtWidgets.QMenu()
actions = {}
if task == 'reg':
list_operations = ['Scatter Plot','Hit Rate']
action,actions = get_actions(treeview,position,list_operations)
if action == actions['Scatter Plot']:
values = []
train_out = item.data_file[item.name+'/train/outputs'].value
train_tar = item.data_file[item.name+'/train/targets'].value
values.append([x for x in train_out])
values.append([x for x in train_tar])
valid_out = item.data_file[item.name+'/valid/outputs'].value
valid_tar = item.data_file[item.name+'/valid/targets'].value
values.append([x for x in valid_tar])
values.append([x for x in valid_out])
if 'test' in item.data_file[item.name]:
test_out = item.data_file[item.name+'/test/outputs'].value
test_tar = item.data_file[item.name+'/test/targets'].value
values.append([x for x in test_tar])
values.append([x for x in test_out])
vmin = np.array([x for a in values for x in a]).min()
vmax = np.array([x for a in values for x in a]).max()
delta = vmax-vmin
values.append([vmax + 0.1*delta])
values.append([vmin - 0.1*delta])
data_dict = {'_values':values}
treeview.emitDict.emit(data_dict)
data_dict = {}
cmd = "%matplotlib inline\nimport matplotlib.pyplot as plt\n"
cmd += "fig,ax = plt.subplots()\n"
cmd += "ax.scatter(_values[0],_values[1],c='red',label='train')\n"
cmd += "ax.scatter(_values[2],_values[3],c='blue',label='valid')\n"
if 'test' in item.data_file[item.name]:
cmd += "ax.scatter(_values[4],_values[5],c='green',label='test')\n"
cmd += "legen = ax.legend(loc='upper left')\n"
cmd += "ax.set_xlabel('Targets')\n"
cmd += "ax.set_ylabel('Predictions')\n"
cmd += "ax.plot([_values[-2],_values[-1]],[_values[-2],_values[-1]])\n"
cmd += "plt.show()\n"
data_dict['exec_cmd'] = cmd
treeview.emitDict.emit(data_dict)
if action == actions['Hit Rate']:
values = []
train_hit = item.data_file[item.name+'/train/hit'].value
valid_hit = item.data_file[item.name+'/valid/hit'].value
values.append(rankingMetrics.hitrate(train_hit))
values.append(rankingMetrics.hitrate(valid_hit))
if 'test' in item.data_file[item.name]:
test_hit = item.data_file[item.name+'/test/hit'].value
values.append(rankingMetrics.hitrate(test_hit))
data_dict = {'_values':values}
treeview.emitDict.emit(data_dict)
data_dict = {}
cmd = "%matplotlib inline\nimport matplotlib.pyplot as plt\n"
cmd += "plt.plot(_values[0],c='red',label='train')\n"
cmd += "plt.plot(_values[1],c='blue',label='valid')\n"
if 'test' in item.data_file[item.name]:
cmd += "plt.plot(_values[2],c='green',label='test')\n"
cmd += "legen = ax.legend(loc='upper left')\n"
cmd += "ax.set_xlabel('Top M')\n"
cmd += "ax.set_ylabel('Hit rate')\n"
cmd += "plt.show()\n"
data_dict['exec_cmd'] = cmd
treeview.emitDict.emit(data_dict)
elif task == 'class':
list_operations = ['Hit Rate']
action,actions = get_actions(treeview,position,list_operations)
if action == actions['Hit Rate']:
values = []
train_hit = item.data_file[item.name+'/train/hit'].value
valid_hit = item.data_file[item.name+'/valid/hit'].value
values.append(rankingMetrics.hitrate(train_hit))
values.append(rankingMetrics.hitrate(valid_hit))
if 'test' in item.data_file[item.name]:
test_hit = item.data_file[item.name+'/test/hit'].value
values.append(rankingMetrics.hitrate(test_hit))
data_dict = {'_values':values}
treeview.emitDict.emit(data_dict)
data_dict = {}
cmd = "%matplotlib inline\nimport matplotlib.pyplot as plt\n"
cmd += "fig,ax = plt.subplots()\n"
cmd += "plt.plot(_values[0],c='red',label='train')\n"
cmd += "plt.plot(_values[1],c='blue',label='valid')\n"
if 'test' in item.data_file[item.name]:
cmd += "plt.plot(_values[2],c='green',label='test')\n"
cmd += "legen = ax.legend(loc='upper left')\n"
cmd += "ax.set_xlabel('Top M')\n"
cmd += "ax.set_ylabel('Hit rate')\n"
cmd += "plt.show()\n"
data_dict['exec_cmd'] = cmd
treeview.emitDict.emit(data_dict)
def _context_multiple_epoch(epoch_items,treeview,position,haddock_item=None):
list_operations = ['Hit Rate (Train)','Hit Rate (Valid)', 'Hit Rate (Test)']
action,actions = get_actions(treeview,position,list_operations)
values = []
names = []
if action == actions['Hit Rate (Train)']:
for item in epoch_items:
hit = item.data_file[item.name+'/train/hit'].value
names.append(item.name.split('/')[-1])
values.append(rankingMetrics.hitrate(hit))
if action == actions['Hit Rate (Valid)']:
for item in epoch_items:
hit = item.data_file[item.name+'/valid/hit'].value
names.append(item.name.split('/')[-1])
values.append(rankingMetrics.hitrate(hit))
if action == actions['Hit Rate (Test)']:
for item in epoch_items:
if 'test' in item.data_file[item.name]:
hit = item.data_file[item.name+'/test/hit'].value
names.append(item.name.split('/')[-1])
values.append(rankingMetrics.hitrate(hit))
if haddock_item is not None:
for item in haddock_item:
hit = item.data_file[item.name+'/hitrate'].value
names.append('haddock')
values.append(hit)
data_dict = {'_values':values,'_names':names}
treeview.emitDict.emit(data_dict)
data_dict = {}
cmd = "%matplotlib inline\nimport matplotlib.pyplot as plt\n"
cmd += "fig,ax = plt.subplots()\n"
cmd += "for v,n in zip(_values,_names):"
cmd += " plt.plot(v,label=n)\n"
cmd += "legen = ax.legend(loc='lower right')\n"
cmd += "ax.set_xlabel('Top M')\n"
cmd += "ax.set_ylabel('Hitrate')\n"
cmd += "plt.show()\n"
data_dict['exec_cmd'] = cmd
treeview.emitDict.emit(data_dict)
def _context_multiple_epoch_multilevel(epoch_items,treeview,position,haddock_item=None):
func_operations = {'Hit Rate':rankingMetrics.hitrate, 'Av. Prec.': rankingMetrics.avprec }
list_operations = ['Hit Rate','Av. Prec.']
list_subop = [['Train','Valid','Test'],['Train','Valid','Test']]
action,actions = get_multilevel_actions(treeview,position,list_operations,list_subop)
for iop,op in enumerate(list_operations):
for subop in list_subop[iop]:
if action == actions[(op,subop)]:
plot_type,data_type = op,subop.lower()
names, values =[], []
for item in epoch_items:
hit = item.data_file[item.name+'/'+data_type+'/hit'].value
names.append(item.name.split('/')[-1])
values.append(func_operations[plot_type](hit))
data_dict = {'_values':values,'_names':names}
treeview.emitDict.emit(data_dict)
data_dict = {}
cmd = "%matplotlib inline\nimport matplotlib.pyplot as plt\n"
cmd += "fig,ax = plt.subplots()\n"
cmd += "for v,n in zip(_values,_names):"
cmd += " plt.plot(v,label=n)\n"
cmd += "legen = ax.legend(loc='lower right')\n"
cmd += "ax.set_xlabel('Top M')\n"
cmd += "ax.set_ylabel('%s')\n" %plot_type
cmd += "plt.show()\n"
data_dict['exec_cmd'] = cmd
treeview.emitDict.emit(data_dict)
def _context_losses(item,treeview,position):
menu = QtWidgets.QMenu()
actions = {}
list_operations = ['Plot Losses']
for operation in list_operations:
actions[operation] = menu.addAction(operation)
action = menu.exec_(treeview.viewport().mapToGlobal(position))
if action == actions['Plot Losses']:
values = []
train = item.data_file[item.name+'/train'].value
valid = item.data_file[item.name+'/valid'].value
values.append([x for x in train])
values.append([x for x in valid])
if 'test' in item.data_file[item.name]:
test = item.data_file[item.name+'/test'].value
values.append([x for x in test])
data_dict = {'_values':values}
treeview.emitDict.emit(data_dict)
data_dict = {}
cmd = "%matplotlib inline\nimport matplotlib.pyplot as plt\n"
cmd += "fig,ax = plt.subplots()\n"
cmd += "plt.plot(_values[0],c='red',label='train')\n"
cmd += "plt.plot(_values[1],c='blue',label='valid')\n"
if 'test' in item.data_file[item.name]:
cmd += "plt.plot(_values[2],c='green',label='test')\n"
cmd += "legen = ax.legend(loc='upper right')\n"
cmd += "ax.set_xlabel('Epoch')\n"
cmd += "ax.set_ylabel('Losses')\n"
cmd += "plt.show()\n"
data_dict['exec_cmd'] = cmd
treeview.emitDict.emit(data_dict)