-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery.py
executable file
·280 lines (217 loc) · 7.45 KB
/
query.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
import inspect
import networkx as nx
from tqdm import tqdm
import json
from .classification_tasks import EvaluationAndSettingTask, CGridTask, hDGridTask
from .classification_tasks import EvaluationTask
import numpy as np
__run__ = True
__graph__ = nx.read_gpickle('./graph.pickle')
index = './index/GraphIndexTask_2003808407.json'
with open(index, 'r') as i:
index = json.load(i)
def cmd_exit():
global __run__
__run__ = False
def buildRegistry():
registry = {}
possibles = globals().copy()
possibles.update(locals())
for k, v in possibles.items():
if k.startswith('cmd_'):
k = k[4:]
registry[k] = {}
registry[k]['func'] = v
registry[k]['args'] = inspect.signature(v).parameters.keys()
registry[k]['default'] = inspect.signature(v).parameters
return registry
def get_category(graphs):
global index
cat = []
for k, v in index['categories'].items():
for g in graphs:
if g in v:
cat.append(k)
break
return cat
def finish_count(G, n):
finish = 0
all_ = 0
for a in nx.algorithms.dag.ancestors(G, n):
if G.node[n]['finish']:
finish += 1
all_ += 1
return finish, all_
def get_exception(G, n):
for a in nx.algorithms.dag.ancestors(G, n):
node = G.node[a]
if 'exception' in node:
return node['exception']
def cmd_status(category=None):
global __graph__
G = __graph__
S = [n for n in G if G.out_degree(n) == 0]
for n in S:
cat = get_category(G.node[n]['task'].get_params()['graphs'])
if category is not None and category not in cat:
continue
finish, a = finish_count(G, n)
label = n[:4] + str(cat)
pbar = tqdm(total=a, desc=label)
pbar.update(finish)
pbar.close()
def cmd_eval(category=None, exception='False'):
global __graph__
G = __graph__
prefix = ''
S = [n for n in G if G.out_degree(n) == 0]
for n in S:
if not isinstance(G.node[n]['task'], EvaluationAndSettingTask):
continue
cat = get_category(G.node[n]['task'].get_params()['graphs'])
if category is not None and category not in cat:
continue
node = G.node[n]
label = n[:4] + str(cat)
if not node['finish']:
print('%s not finished' % label)
if exception == 'True':
ex = get_exception(G, n)
if ex is not None:
print('Cause: %s' % ex)
continue
node['output'].mode = 'r'
node['output'].path = prefix + node['output'].path
with node['output'] as o:
stats = o.query()
print('Evalution for %s: \n' % label)
setting = stats['setting']
for k, v in setting.items():
print('%s: %s' % (k, str(v)))
mean = stats['mean']
for k, v in mean.items():
print('%s: %4.2f (Std: %4.2f)' % (k, v[0], 2*v[1]))
def add_time(D, name, subname, time):
if name not in D:
D[name] = {}
d = D[name]
if subname not in d:
d[subname] = 0.0
d[subname] += time
def calc_time_c(G, t):
ignore = set([])
graph_time = {}
for a in nx.algorithms.dag.ancestors(G, t):
node = G.node[a]
if a.startswith('Graph') and not a.startswith('GraphIndexTask'):
name = node['task'].get_params()['name']
if not node['finish'] or name in ignore:
ignore.add(name)
continue
add_time(graph_time, name, 'graph', float(node['time']))
elif a.startswith('Prepare'):
name = node['task'].get_params()['graph']
if not node['finish'] or name in ignore:
ignore.add(name)
continue
add_time(graph_time, name, 'kernel', float(node['time']))
elif a.startswith('WLKernel') or a.startswith('Normalized'):
names = node['task'].get_params()['graphs']
if not node['finish']:
continue
div = len(names)
for name in names:
add_time(graph_time, name, 'kernel', float(node['time']) / div)
graph_time['gram'] = float(node['time'])
return graph_time
def time_c(G, t):
task = G.node[t]['task']
params = task.get_params()
graphs = params['graphs']
size = len(graphs)
train_index = params['train_index']
train_graph = [x for i, x in enumerate(graphs) if i in train_index]
graph_time = calc_time_c(G, t)
task_time = float(G.node[t]['time']) / 490
# print('%s: Time %f' % (t, task_time))
graph_ratio = []
test_times = []
train_time = 0.0
for k, v in graph_time.items():
if k == 'gram':
continue
time = v['graph'] + v['kernel']
graph_ratio.append(v['graph'] / time)
time += task_time
if k in train_graph:
train_time += time
else:
test_times.append(time+graph_time['gram'])
return train_time, test_times, graph_ratio, size
def find_gridTask(G, n):
cGrid = []
stack = [n]
while len(stack) > 0:
act = stack.pop()
node = G.node[act]['task']
if isinstance(node, CGridTask):
cGrid.append(act)
else:
for p in G.predecessors(act):
pNode = G.node[p]['task']
if isinstance(pNode, EvaluationTask)\
or isinstance(pNode, hDGridTask)\
or isinstance(pNode, CGridTask):
stack.append(p)
train_times = []
test_times = []
graph_ratios = []
size = 0
for c in cGrid:
train, test, graph, s = time_c(G, c)
size = max(size, s)
train_times.append(train)
test_times.extend(test)
graph_ratios.extend(graph)
return np.mean(train_times), np.mean(test_times), np.mean(graph_ratios), size
def cmd_time(category=None):
global __graph__
G = __graph__
prefix = '/home/cedricr/common/'
S = [n for n in G if G.out_degree(n) == 0]
for n in S:
if not isinstance(G.node[n]['task'], EvaluationAndSettingTask):
continue
cat = get_category(G.node[n]['task'].get_params()['graphs'])
if category is not None and category not in cat:
continue
node = G.node[n]
if not node['finish']:
continue
label = n[:4] + str(cat)
train, test, graph, size = find_gridTask(G, n)
print('Time for %s [%d graphs]:' % (label, size))
print('Average train time: %f' % train)
print('Average test time: %f' % test)
print('Average ratio of graph time: %f' % graph)
if __name__ == '__main__':
d = buildRegistry()
while __run__:
s = input('prompt: ')
cmd_str = s.split(' ')
if cmd_str[0] in d:
cmd = d[cmd_str[0]]
args = cmd_str[1:]
actual_param = {}
func_param = cmd['default']
for i, param in enumerate(cmd['args']):
if i < len(args):
val = args[i]
actual_param[param] = val
elif func_param[param].default is not inspect.Parameter.empty:
actual_param[param] = func_param[param].default
else:
print('%s is not given' % param)
cmd['func'](**actual_param)
else:
print('Unknown command %s' % cmd_str[0])