-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathdepGraph.py
More file actions
406 lines (295 loc) · 12.1 KB
/
Copy pathdepGraph.py
File metadata and controls
406 lines (295 loc) · 12.1 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 9 21:56:18 2015
@author: david
"""
import numpy as np
import toposort
# import pylab
import matplotlib.pyplot as plt
import matplotlib.cm
from scipy import optimize
from six.moves import xrange
import six
def makeGraph(dg):
import networkx as nx
G = nx.DiGraph()
for k, v in dg.items():
for e in v:
G.add_edge(e, k)
return G
def _pos_cost(y_s, yps, ypw, tol=1.0):
#print yps.shape, y_s.shape,
return np.sum(ypw*((-9 + np.cumsum(y_s**2) + tol*np.arange(len(y_s))) - yps)**2)
def _pos_cost_ls(y_s, yps, ypw, tol=1.0):
#print yps.shape, y_s.shape,
return ypw*((-9 + np.cumsum(y_s**2) + tol*np.arange(len(y_s))) - yps)
def arrangeNodes(dg):
ts = list(toposort.toposort(dg))
xc = 0
ips = {}
yvs = []
forward_deps = {}
for k, v in dg.items():
for vi in v:
try:
forward_deps[vi].add(k)
except KeyError:
forward_deps[vi] = {k}
#ts gives a list of all the steps of the computation
for st in ts:
#iterate over the steps
#keep a list of y positions at this step
yps = []
ypfs = []
ypw = []
st = list(st)
#loop over items to be calculated at each step
#and work out a preferred position
for si in st:
#see what the dependancies of this item are
if si in dg.keys():
ri = list(dg[si])
if len(ri) > 0:
#assign a y position as the mean of the dependancies y positions
#yp = np.mean([ips[rr][1] for rr in ri])
w = np.array([1.0/(1 + (xc - ips[rr][0])) for rr in ri])
yp = np.sum(np.array([ips[rr][1] for rr in ri])*w)/w.sum()
ypw.append(1.)
else:
# module has no inputs (i.e. a simulation module)
yp = 0
ypw.append(1.)
else:
#else assign a position of 0
yp = 0
ypw.append(0.1)
#yps.append(yp)
ypf = 0
#look for forward dependencies
if si in forward_deps.keys():
outputs = forward_deps[si]
fd_ys = []
fd_ws = []
if (isinstance(si,six.string_types)):
pass
#we are a result node - look 1 step ahead
#dependencies of forward deps
#for fdi in fd:
# fdd = list(dg[fdi])
# fd_ys += [ips[rr][1] for rr in fdd if rr in ips.keys()]
else:
#We are a computation node - look 2 steps ahead and backwards
#look over the node outputs
for out_i in outputs:
#find the nodes which consume these outputs
if out_i in forward_deps.keys():
consuming_nodes = forward_deps[out_i]
for cnode in consuming_nodes:
#find the nodes on which these nodes depend (these will be other outputs)
cnode_inputs = list(dg[cnode])
for inp_i in cnode_inputs:
#find the computational nodes which generate these outputs
try:
gnodes = list(dg[inp_i])
except KeyError:
gnodes = []
fd_ys += [ips[rr][1] for rr in gnodes if rr in ips.keys()]
fd_ws += [1.0/(1 + ips[rr][0] - xc) for rr in gnodes if rr in ips.keys()]
if len(fd_ys) > 0:
ypf = np.mean(fd_ys)
#fd_ws = np.array(fd_ws)
#ypf = np.sum(np.array(fd_ys)*fd_ws)/np.sum(fd_ws)
#print ypf
if ypw[-1] == 1:
yp = 0.5*yp + 0.5*ypf
else:
yp = ypf
#print yp, ypf
yps.append(yp)
ypfs.append(ypf)
Is = np.argsort(yps)
#space out positions
ypss = np.zeros(len(yps)) - 50
for i in np.argsort(yps):
si = st[i]
yp = yps[i]
if isinstance(si, str):
#vertical spacing between outputs is .1
tol = .1
else:
#vertical spacing between blocks
tol = 1
#space out the y positions so blocks don't overlap
#while min(abs(yp - np.array(ypss + [-50]))) < tol:
while min(abs(yp - ypss)) < tol:
yp += min(tol, .1)
ypss[i] = yp
yest = ypss.copy()
#print ypss
if len(yest) > 1:
sp = 0*yest
sp[1:] = np.diff(yest[Is] - tol*np.arange(len(yps)))
sp[0] = yest[Is][0]+9
sp = np.sqrt(sp)
#yest[Is] = -9 + np.cumsum(optimize.fmin(_pos_cost, sp, (np.array(yps)[Is], np.array(ypw)[Is], tol), disp=1)[0]**2) + tol*np.arange(len(yps))
yest[Is] = -9 + np.cumsum(optimize.leastsq(_pos_cost_ls, sp, (np.array(yps)[Is], np.array(ypw)[Is], tol))[0]**2) + tol*np.arange(len(yps))
#print yps, yest, tol
if np.any(np.isnan(yest)):
print('NaN detected in yest')
yest = ypss
if np.any(np.isnan(ypss)):
print('NaN detected in ypss ??!!')
#ysi = np.argsort(yps)
#ys = (np.arange(len(ysi)) - ysi.mean())
#assign the y positions
ysi = np.arange(len(ypss))
#ys = ypss
#for i, yi in zip(ysi, ys):
# ips[st[i]] = (xc, yi)
for i, yi in zip(ysi, yest):
ips[st[i]] = (xc, yi)
xc += 1
yvs.append(yest[np.argsort(yest)])
return ips, yvs
def _vertCost(yvs, vert_neighbours, ysize=1.0):
#ynn = [yvs[n] for n in vert_neighbours]
cost = []
for y, n in zip(yvs, vert_neighbours):
c = np.max(np.max(y - (yvs[n] - ysize)), 0)*np.max(np.max(-y + (yvs[n] + ysize)), 0)
cost.append(c)
return np.array(cost)
def _vertForce(yvs, vert_neighbours, ysize=1.0):
#ynn = [yvs[n] for n in vert_neighbours]
force = 0*yvs
for i, y, n in zip(xrange(len(yvs)), yvs, vert_neighbours):
if len(n) > 0:
yn = yvs[n]
#f1 = np.sum(np.max(y - (yn + ysize), 0)*(y < (yn + ysize)))
#f2 = np.sum(np.max(-y + (yn - ysize), 0)*(y > (yn - ysize)))
#ft = f1*f2
#if (ft) > 0:
# c = (2.0*(f1 > f2) - 1)
# force[i] = c
#force[i] = np.sum(1.0*(y>yn)*(y<(yn+ysize)) - 1.0*(y<=yn)*(y>(yn-ysize)))
force[i] = np.sum(0.5*(1 + np.tanh((-abs(y-yn) + ysize)*5))*(2.0*(y>yn) - 1))
return force
def __edgeCost(yvs, xvs, edges):
dy = np.diff(yvs[edges],1)
dx = np.diff(xvs[edges],1)
return np.sqrt(dx*dx + dy*dy)
def _edgeForce(yvs, xvs, edges):
force = 0*yvs
for x, y, e, i in zip(xvs, yvs, edges, xrange(len(yvs))):
dx = x - xvs[e]
dy = y - yvs[e]
r = np.sqrt(dx*dx + dy*dy)
force[i] = np.sum(-dy/r*r)/np.sum(1.0/r)
#mi = np.argmax(abs(dy)/r)
#force[i] = -dy[mi]/r[mi]
return force
def _totForce(yvs, xvs, edges, vert_neighbours, vsize=1.0, vert_weight=1.0):
return vert_weight*_vertForce(yvs, vert_neighbours, ysize=1.0) + _edgeForce(yvs, xvs, edges)
def _totCost(yvs, xvs, edges, vert_neighbours, vsize=1.0):
return _vertCost(yvs, vert_neighbours, ysize=1.0).sum() + _edgeCost(yvs, xvs, edges).sum()
def arrangeNodes_(dg):
ts = list(toposort.toposort(dg))
xc = 0
ips = {}
i = 0
yvs = []
xvs = []
nodes = []
vert_neighbours = []
node_nums = {}
edge_db = {}
edges = []
#ts gives a list of all the steps of the computation
for step in ts:
#iterate over the steps
step = list(step)
yis = []
#loop over items to be calculated at each step
#and work out a preferred position
for si in step:
xvs.append(xc)
yvs.append(np.random.randn())
nodes.append(si)
node_nums[si] = i
yis.append(i)
#see what the dependancies of this item are
#record the edges
if si in dg.keys():
ri = list(dg[si])
for r in ri:
n = node_nums[r]
edges.append((n, i))
old_edges = edge_db.get(i, [])
edge_db[i] = old_edges + [n,]
old_edges = edge_db.get(n, [])
edge_db[n] = old_edges + [i, ]
i += 1
for yi in yis:
vert_neighbours.append([yi_ for yi_ in yis if not yi_ == yi])
#vert_neighbours.append(yis)
xc += 1
edgel = [edge_db[k] for k in range(i)]
return np.array(xvs), np.array(yvs), nodes, vert_neighbours, edgel, edges
def drawGraph(dg):
ips = arrangeNodes(dg)
#ts = list(toposort.toposort(dg))
f = plt.figure()
a = plt.axes([0,0,1,1])
axisWidth = a.get_window_extent().width
nCols = max([v[0] for v in ips.values()])
pix_per_col = axisWidth/float(nCols)
fontSize = min(10, 10*pix_per_col/400.)
#print pix_per_col, fontSize
cols = {}
for k, v in dg.items():
if not isinstance(k, six.string_types):
yv0 = []
yoff = .1*np.arange(len(v))
yoff -= yoff.mean()
for e in v:
x0, y0 = ips[e]
yv0.append(y0 + 0.01*x0)
yvi = np.argsort(np.array(yv0))
#print yv0, yvi
yos = np.zeros(3)
yos[yvi] = yoff
for e, yo in zip(v, yos):
x0, y0 = ips[e]
x1, y1 = ips[k]
if not e in cols.keys():
cols[e] = 0.7*np.array(matplotlib.cm.hsv(np.random.rand()))
#yo = yoff[i]
plt.plot([x0,x0+.5, x0+.5, x1], [y0,y0,y1+yo,y1+yo], c=cols[e], lw=2)
for k, v in ips.items():
if not isinstance(k, six.string_types):
s = k.__class__.__name__
#plt.plot(v[0], v[1], 'o', ms=5)
rect = plt.Rectangle([v[0], v[1]-.25], 1, .5, ec='k', fc=[.8,.8, 1], picker=True)
rect._data = k
plt.gca().add_patch(rect)
plt.text(v[0]+.05, v[1]+.18 , s, size=fontSize, weight='bold')
s2 = '\n'.join(['%s : %s' %i for i in k.get().items()])
plt.text(v[0]+.05, v[1]-.22 , s2, size=.8*fontSize, stretch='ultra-condensed')
else:
s = k
if not k in cols.keys():
cols[k] = 0.7*np.array(matplotlib.cm.hsv(np.random.rand()))
plt.plot(v[0], v[1], 'o', color=cols[k])
plt.text(v[0]+.1, v[1] + .02, s, color=cols[k], size=fontSize, weight='bold')
#plt.ylim(-1, 2)
ipsv = np.array(ips.values())
xmn, ymn = ipsv.min(0)
xmx, ymx = ipsv.max(0)
plt.ylim(ymn-1, ymx+1)
plt.xlim(xmn-.5, xmx + .7)
plt.axis('off')
def OnPick(event):
k = event.artist._data
if not isinstance(k, six.string_types):
k.edit_traits()
f.canvas.mpl_connect('pick_event', OnPick)