-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool_contract.py
449 lines (392 loc) · 16.8 KB
/
tool_contract.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import networkx as nx
import pydot
from copy import deepcopy
from networkx.drawing.nx_pydot import write_dot
import time
import global_var as gvar
def contract_grey(G,passPair,lag_cri=30):
"""Contract short lifetime grey contraction.
Args:
G (graph) : Reaction graph.
lag_cri : Lifetime criteria. (default=30)
passPair: Transformation pairs need to be neglected
Returns:
contrac_result: Grey transformation contaction Suggestion,[(a,b),(e,f), .....]
currentpassPair:Pair should be passed.
"""
#1. Get all node pairs contain grey transformations.
greyList =[]
# [(node1,node2),(node_n,node_n-1),....]
#
RawEdgeList = G.edges(data=True)
for rec in RawEdgeList:
if(rec[2]['color'] == 'grey' and \
(rec[0],rec[1]) not in greyList and \
(rec[1],rec[0]) not in greyList and \
(rec[0],rec[1]) not in passPair and \
(rec[1],rec[0]) not in passPair and
rec[1] != rec[0]):
greyList.append((rec[0],rec[1]))
#2. Build node information dictionary.
builtRec={}
for pair in greyList:
for inode in pair:
if(inode in builtRec):
continue
rec_in = G.in_edges(inode,data=True)
rec_out = G.out_edges(inode,data=True)
#print(rec_in,rec_out)
GreyDicRec=[]
# Build In record
for rec1 in rec_in:
if(rec1[2]['color']=='grey'):
Instep = int(rec1[2]['label'])
mini_sub = 200000
iloc = 0
rec_out_l = list(rec_out)
flag = False
for i in range(len(rec_out_l)):
Outstep = int(rec_out_l[i][2]['label'])
if(Outstep - Instep >0 and Outstep - Instep < mini_sub):
iloc = i
mini_sub = Outstep - Instep
flag = True
if(flag):
GreyDicRec.append([rec_out_l[iloc][1], "in",Instep ,int(rec_out_l[iloc][2]['label'])])
else:
if(len(rec_out_l) == 0):
GreyDicRec.append([-1, "in",Instep ,None])
else:
GreyDicRec.append([rec_out_l[iloc][1], "in",Instep ,None])
# Build Out record
for rec1 in rec_out:
if(rec1[2]['color']=='grey'):
Outstep = int(rec1[2]['label'])
mini_sub = 200000
iloc = 0
rec_in_l = list(rec_in)
flag = False
for i in range(len(rec_in_l)):
Instep = int(rec_in_l[i][2]['label'])
if(Outstep - Instep >0 and Outstep - Instep < mini_sub):
iloc = i
mini_sub = Outstep - Instep
flag = True
if(flag):
GreyDicRec.append([rec_in_l[iloc][0],"Out",Outstep ,int(rec_in_l[iloc][2]['label']) ])
else:
if(len(rec_in_l) == 0):
GreyDicRec.append([-1, "Out",Outstep ,None])
else:
GreyDicRec.append([rec_in_l[iloc][0],"Out",Outstep ,None])
builtRec[inode] = GreyDicRec
#for key in builtRec:
#print(key,builtRec[key])
#3. Pair Opinions
# pair1:(a-b;a-b;b-a)
# pair2:(c-d;c-d;none)
#print("start pair deciding")
passRec = []
pairOpinionList = []
for pair in greyList:
# 3.1 Add search pair ot pass pair.
if (pair[0] in passRec or pair[1] in passRec):
continue
passRec.append(pair[0])
passRec.append(pair[1])
# 3.2 start getting opinion.
edge1_lifeTime = 10000
edge2_lifeTime = 10000
opinion_list = []
# 0->1
#print(lag_cri,"lag_cri")
edges_1 = G.get_edge_data(pair[0],pair[1])
if(edges_1 == None): edges_1=[]
for edge_key in edges_1:
grey_step = int(edges_1[edge_key]['label'])
# Life time of 0
opinion =None
for rec_t in builtRec[pair[0]]:
#print (rec_t[1],rec_t[2])
if(rec_t[3] is None and rec_t[2] == grey_step):
edge1_lifeTime = lag_cri+10
break
if(rec_t[1] == 'Out' and rec_t[2] == grey_step):
edge1_lifeTime = rec_t[2] - rec_t[3]
# Life time of 1
for rec_t in builtRec[pair[1]]:
if(rec_t[2] == grey_step and rec_t[3] is None):
edge2_lifeTime = lag_cri+10
break
if(rec_t[1] == 'in' and rec_t[2] == grey_step):
edge2_lifeTime = rec_t[3] - rec_t[2]
#print(edge2_lifeTime)
if(opinion =="no"):
opinion ="no"
opinion_list.append(opinion)
break
if(opinion is None and edge2_lifeTime > edge1_lifeTime and edge1_lifeTime < lag_cri):
opinion ="a->b"
opinion_list.append(opinion)
if(opinion is None and edge1_lifeTime > edge2_lifeTime and edge2_lifeTime < lag_cri):
opinion ="b->a"
opinion_list.append(opinion)
if(opinion is None and edge1_lifeTime == edge2_lifeTime and edge2_lifeTime < lag_cri \
and len(G.nodes[pair[0]]["SMILE"]) < len(G.nodes[pair[1]]["SMILE"]) ):
opinion ="b->a"
opinion_list.append(opinion)
if(opinion is None and edge1_lifeTime == edge2_lifeTime and edge2_lifeTime < lag_cri \
and len(G.nodes[pair[0]]["SMILE"]) > len(G.nodes[pair[1]]["SMILE"]) ):
opinion ="a->b"
opinion_list.append(opinion)
if('no' in opinion_list):
pairOpinionList.append( [pair,opinion_list])
break
#print("0->1 opinion: ", opinion_list)
# 1->0
edges_2 = G.get_edge_data(pair[1],pair[0])
if(edges_2 == None): edges_2=[]
for edge_key in edges_2:
grey_step = int(edges_2[edge_key]['label'])
# Life time of 0
opinion =None
for rec_t in builtRec[pair[0]]:
#print (rec_t[1],rec_t[2])
if(rec_t[3] is None and rec_t[2] == grey_step):
edge1_lifeTime = lag_cri+10
break
if(rec_t[1] == 'in' and rec_t[2] == grey_step):
edge1_lifeTime = rec_t[3] - rec_t[2]
# Life time of 1
for rec_t in builtRec[pair[1]]:
if(rec_t[3] is None and rec_t[2] == grey_step):
edge2_lifeTime = lag_cri+10
break
if(rec_t[1] == 'Out' and rec_t[2] == grey_step):
edge2_lifeTime = rec_t[2] - rec_t[3]
if(opinion =="no"):
opinion ="no"
opinion_list.append(opinion)
break
if(opinion is None and edge2_lifeTime > edge1_lifeTime and edge1_lifeTime < lag_cri):
opinion ="a->b"
opinion_list.append(opinion)
if(opinion is None and edge1_lifeTime > edge2_lifeTime and edge2_lifeTime < lag_cri):
opinion ="b->a"
opinion_list.append(opinion)
if(opinion is None and edge1_lifeTime == edge2_lifeTime and edge2_lifeTime < lag_cri \
and len(G.nodes[pair[0]]["SMILE"]) < len(G.nodes[pair[1]]["SMILE"]) ):
opinion ="b->a"
opinion_list.append(opinion)
if(opinion is None and edge1_lifeTime == edge2_lifeTime and edge2_lifeTime < lag_cri \
and len(G.nodes[pair[0]]["SMILE"]) > len(G.nodes[pair[1]]["SMILE"]) ):
opinion ="a->b"
opinion_list.append(opinion)
if('no' in opinion_list):
pairOpinionList.append( pair,opinion_list)
break
pairOpinionList.append( [pair,opinion_list])
# 4. Final Opinion.
contrac_result = []
currentpassPair = []
for opinion in pairOpinionList:
if ( len(opinion[1]) ==0 ):
currentpassPair.append((opinion[0][0],opinion[0][1]))
continue
if ('no' in opinion[1] ):
currentpassPair.append((opinion[0][0],opinion[0][1]))
continue
result = dict((i, opinion[1].count(i)) for i in opinion[1])
if(len(result)>1):
if(result['a->b'] >= result['b->a']):
contrac_result.append((opinion[0][0],opinion[0][1]))
else:
contrac_result.append((opinion[0][1],opinion[0][0]))
else:
if('a->b'in result):
contrac_result.append((opinion[0][0],opinion[0][1]))
else:
contrac_result.append((opinion[0][1],opinion[0][0]))
return contrac_result,currentpassPair
def contract_blue(G,lag_cri=5):
"""Contract blue transformation.
Args:
G (graph) : Reaction graph.
lag_cri : Lifetime criteria. (default=30)
Returns:
cont_pair: Blue transformation contaction Suggestion,[(a,b),(e,f), .....]
"""
#1. Get all node pairs contain blue transformations.
blueList =[]
passNode = []
# [(node1,node2),(node_n,node_n-1),....]
RawEdgeList = G.edges(data=True)
for rec in RawEdgeList:
if(rec[2]['color'] == 'blue' and \
(rec[0],rec[1]) not in blueList and \
len(G.out_edges(rec[1])) == 2 and \
len(G.in_edges(rec[1])) == 1 and \
G.number_of_edges(rec[0],rec[1]) == 1):
if(isinstance(rec[2]['label'], int)):
blueList.append([rec[0], rec[1], rec[2]['label'] ])
else:
blueList.append([rec[0], rec[1], int(rec[2]['label'] ) ])
# 1, Check To_node information.
cont_pair = []
for edge in blueList:
rec_out = G.out_edges(edge[1],data=True)
if(rec_out is None or edge[0] in passNode or edge[1] in passNode): continue
if(len(rec_out) == 2):
rec_out = list(rec_out)
if (rec_out[0][2]['color'] == 'blue' and \
rec_out[1][2]['color'] == 'blue' and \
rec_out[0][2]['label'] == rec_out[1][2]['label'] and \
rec_out[0][2]['label'] - edge[2] <lag_cri):
edge.append( [rec_out[0][1],rec_out[1][1],rec_out[0][2]['label']])
passNode.append(rec_out[0][1])
passNode.append(rec_out[0][1])
passNode.append(edge[1])
passNode.append(edge[0])
cont_pair.append(edge)
return(cont_pair)
def contract_red(G,lag_cri=5):
"""Contract combination transformation.
Args:
G (graph) : Reaction graph.
lag_cri : Lifetime criteria. (default=30)
Returns:
cont_pair: Red transformation contaction Suggestion,[(a,b),(e,f), .....]
"""
#1. Get all node pairs contain blue transformations.
redList =[]
passNode = []
# [(node1,node2),(node_n,node_n-1),....]
#
RawEdgeList = G.edges(data=True)
for rec in RawEdgeList:
if(rec[2]['color'] == 'red' and \
(rec[0],rec[1]) not in redList and \
len(G.in_edges(rec[0])) == 2 and \
len(G.out_edges(rec[0]))== 1 and \
G.number_of_edges(rec[0],rec[1]) == 1):
if(isinstance(rec[2]['label'], int)):
redList.append([rec[0], rec[1], rec[2]['label'] ])
else:
redList.append([rec[0], rec[1], int(rec[2]['label']) ])
# 1, Check To_node information.
cont_pair = []
for edge in redList:
rec_in = G.in_edges(edge[0],data=True)
if(rec_in is None or edge[1] in passNode or edge[0] in passNode): continue
if(len(rec_in) == 2):
rec_in = list(rec_in)
if (rec_in[0][2]['color'] == 'red' and \
rec_in[1][2]['color'] == 'red' and \
rec_in[0][2]['label'] == rec_in[1][2]['label'] and \
edge[2] - rec_in[0][2]['label'] < lag_cri):
edge.append( [rec_in[0][0],rec_in[1][0],rec_in[0][2]['label']])
passNode.append(rec_in[0][0])
passNode.append(rec_in[1][0])
passNode.append(edge[1])
passNode.append(edge[0])
cont_pair.append(edge)
return(cont_pair)
def contract_nodes(G):
"""Nodes contraction driver.
Args:
G (graph) : Reaction graph.
Returns:
G_t (graph) : Reaction Graph after contraction.
"""
G_t = deepcopy(G)
# Contract grey.
N_nodes = len(G_t.nodes(data=True))
N_nodesp = -1
passPair = []
T0 = time.time()
while (N_nodes-N_nodesp != 0):
N_nodesp = N_nodes
contrR,currPass = contract_grey(G_t,passPair,gvar.StableMolLag)
for cont in contrR:
G_t = nx.contracted_nodes(G_t,cont[1], cont[0],self_loops=False,copy=False)
for itm in currPass:
passPair.append(itm)
N_nodes = len(G_t.nodes(data=True))
# print(N_nodesp,N_nodes,"number of nodes after grey")
N_nodes = len(G_t.nodes(data=True))
N_nodesp = -1
T1 = time.time()
while (N_nodes-N_nodesp != 0):
#Contract Blue edges.
N_nodesp = N_nodes
cont_pairs = contract_blue(G_t,gvar.StableMolLag)
for pair in cont_pairs:
G_t = nx.contracted_nodes(G_t,pair[0], pair[1],self_loops=False,copy=False)
for key in G_t[pair[0]][pair[3][0]]:
if( G_t[pair[0]][pair[3][0]][key]['label'] == pair[3][2]):
G_t[pair[0]][pair[3][0]][key]['label'] = pair[2]
for key in G_t[pair[0]][pair[3][1]]:
if( G_t[pair[0]][pair[3][1]][key]['label'] == pair[3][2]):
G_t[pair[0]][pair[3][1]][key]['label'] = pair[2]
cont_pairs = contract_red(G_t,gvar.StableMolLag)
#Contract Red edges.
for pair in cont_pairs:
G_t = nx.contracted_nodes(G_t,pair[1], pair[0],self_loops=False)
for key in G_t[pair[3][0]][pair[1]]:
if( G_t[pair[3][0]][pair[1]][key]['label'] == pair[3][2]):
G_t[pair[3][0]][pair[1]][key]['label'] = pair[2]
for key in G_t[pair[3][1]][pair[1]]:
if( G_t[pair[3][1]][pair[1]][key]['label'] == pair[3][2]):
G_t[pair[3][1]][pair[1]][key]['label'] = pair[2]
N_nodes = len(G_t.nodes(data=True))
T2 = time.time()
return G_t
def contract_nodes_Onlygrey(G):
"""Nodes contraction driver.
Args:
G (graph) : Reaction graph.
Returns:
G_t (graph) : Reaction Graph after contraction.
"""
G_t = deepcopy(G)
# Contract grey.
N_nodes = len(G_t.nodes(data=True))
N_nodesp = -1
passPair = []
T0 = time.time()
while (N_nodes-N_nodesp != 0):
T00 = time.time()
N_nodesp = N_nodes
contrR,currPass = contract_grey(G_t,passPair,gvar.StableMolLag)
T01 = time.time()
for cont in contrR:
G_t = nx.contracted_nodes(G_t,cont[1], cont[0],self_loops=False)
for itm in currPass:
passPair.append(itm)
T02 = time.time()
N_nodes = len(G_t.nodes(data=True))
N_nodes = len(G_t.nodes(data=True))
N_nodesp = -1
T1 = time.time()
return G_t
# For test
if __name__ == '__main__':
#G = nx.drawing.nx_pydot.read_dot("/home/xchen/Develop/jupyter-lab/ChenXin-2021/ReactionNet-analysis/34813.dot")
G = nx.drawing.nx_pydot.read_dot("/home/xchen/Develop/CPX-MechGen/CPX-MechGen/example/354781.dot")
G_t = deepcopy(G)
for edge in G_t.edges(data=True,keys=True):
for key in G_t[edge[0]][edge[1]]:
if(isinstance(G_t[edge[0]][edge[1]][key]['label'], str)):
G_t[edge[0]][edge[1]][key]['label'] = int(G_t[edge[0]][edge[1]][key]['label'].strip('\"'))
dic = nx.get_node_attributes(G,'label')
N_nodes0 = G.number_of_nodes()
N_edges0 = G.number_of_edges()
print("total nodes in original graph: ",N_nodes0)
print("total edges in original graph: ",N_edges0)
G_t = contract_nodes(G_t)
G_t = contract_nodes(G_t)
N_nodes = G_t.number_of_nodes()
N_edges = G_t.number_of_edges()
print("total nodes in contracted graph: ",N_nodes)
print("total edges in contracted graph: ",N_edges)
write_dot(G_t, "exp_cont.dot")