-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdata_loader.py
523 lines (474 loc) · 18.5 KB
/
data_loader.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
"""
Created on Tue Oct 23 14:54:17 2018
Load data
@author: vayer
"""
from graph import Graph
import networkx as nx
from utils import read_files,per_section,indices_to_one_hot
from collections import defaultdict
import numpy as np
class NotImplementedError(Exception):
pass
def load_local_data(data_path,name,one_hot=False,attributes=True,use_node_deg=False):
if name=='mutag':
path=data_path+'/MUTAG_2/'
dataset=build_MUTAG_dataset(path,one_hot=one_hot)
if name=='ptc':
path=data_path+'/PTC_MR/'
dataset=build_PTC_dataset(path,one_hot=one_hot)
if name=='nci1':
path=data_path+'/NCI1/'
if one_hot==True:
raise NotImplementedError
dataset=build_NCI1_dataset(path)
if name=='imdb-b':
path=data_path+'/IMDB-BINARY/'
dataset=build_IMDB_dataset(path,s='BINARY',use_node_deg=use_node_deg)
if name=='imdb-m':
path=data_path+'/IMDB-MULTI/'
dataset=build_IMDB_dataset(path,s='MULTI',use_node_deg=use_node_deg)
if name=='enzymes':
path=data_path+'/ENZYMES_2/'
if attributes:
dataset=build_ENZYMES_dataset(path,type_attr='real')
else:
dataset=build_ENZYMES_dataset(path)
if name=='protein':
path=data_path+'/PROTEINS_full/'
if attributes:
dataset=build_PROTEIN_dataset(path,type_attr='real',use_node_deg=use_node_deg)
else:
dataset=build_PROTEIN_dataset(path)
if name=='protein_notfull':
path=data_path+'/PROTEINS/'
if attributes:
dataset=build_PROTEIN2_dataset(path,type_attr='real',use_node_deg=use_node_deg)
else:
dataset=build_PROTEIN2_dataset(path)
if name=='bzr':
path=data_path+'/BZR/'
if attributes:
dataset=build_BZR_dataset(path,type_attr='real',use_node_deg=use_node_deg)
else:
dataset=build_BZR_dataset(path)
if name=='cox2':
path=data_path+'/COX2/'
if attributes:
dataset=build_COX2_dataset(path,type_attr='real',use_node_deg=use_node_deg)
else:
dataset=build_COX2_dataset(path)
if name=='synthetic':
path=data_path+'/SYNTHETIC/'
if attributes:
dataset=build_SYNTHETIC_dataset(path,type_attr='real')
else:
dataset=build_SYNTHETIC_dataset(path)
if name=='aids':
path=data_path+'/AIDS/'
if attributes:
dataset=build_AIDS_dataset(path,type_attr='real')
else:
dataset=build_AIDS_dataset(path)
if name=='cuneiform':
path=data_path+'/Cuneiform/'
if attributes:
dataset=build_Cuneiform_dataset(path,type_attr='real')
else:
dataset=build_Cuneiform_dataset(path)
if name=='letter_high':
path=data_path+'/Letter-high/'
if attributes:
dataset=build_LETTER_dataset(path,type_attr='real',name='high')
else:
dataset=build_LETTER_dataset(path,name='med')
if name=='letter_med':
path=data_path+'/Letter-med/'
if attributes:
dataset=build_LETTER_dataset(path,type_attr='real',name='med')
else:
dataset=build_LETTER_dataset(path,name='med')
if name=='fingerprint':
path=data_path+'/Fingerprint/'
dataset=build_Fingerprint_dataset(path,type_attr='real')
return dataset
#%%
def generate_binary_uniform_tree(maxdepth,coupling='cross',a=0,b=5,c=5,d=10):#il faut que nlowLeaves soit une puissance de 2
graph=Graph()
#randint=np.random.randint(2,high=maxdepth)
randint=maxdepth
nlowLeaves=2**randint
groupe=('A',a,b)
graph.create_classes_uniform_leaves(nlowLeaves,groupe)
groupe=('B',c,d)
graph.create_classes_uniform_leaves(nlowLeaves,groupe)
noeud_0=graph.find_leaf('A')
noeud_1=graph.find_leaf('B')
if coupling=='cross':
k=0
for noeud in noeud_0:
graph.binary_link(noeud,noeud_1[k])
k=k+1
else :
graph.iterative_binary_link(noeud_0,maxIter=1)
graph.iterative_binary_link(noeud_1,maxIter=1)
otherNode=list(set(graph.nodes()).difference(set(noeud_1).union(set(noeud_0))))
graph.iterative_binary_link(otherNode)
graph.nx_graph=nx.relabel_nodes(graph.nx_graph,{max(graph.nodes(), key=len):1}) #renomer la racine
graph.construct_tree()
return graph
def build_binary_uniform_dataset(nTree1,nTree2,maxdepth,a=0,b=5,c=5,d=10):
data=[]
for i in range(nTree1):
data.append((generate_binary_uniform_tree(maxdepth,coupling='cross',a=a,b=b,c=c,d=d),0))
for i in range(nTree2):
data.append((generate_binary_uniform_tree(maxdepth,coupling='nocross',a=a,b=b,c=c,d=d),1))
return data
def build_one_tree_dataset_from_xml(path,classe,max_depth):
onlyfiles = read_files(path)
data=[]
for f in onlyfiles :
G=Graph()
G.build_Xml_tree(path+'/'+f,max_depth)
data.append((G,classe))
return data
def node_labels_dic(path,name):
node_dic=dict()
with open(path+name) as f:
sections = list(per_section(f))
k=1
for elt in sections[0]:
node_dic[k]=int(elt)
k=k+1
return node_dic
def node_attr_dic(path,name):
node_dic=dict()
with open(path+name) as f:
sections = list(per_section(f))
k=1
for elt in sections[0]:
node_dic[k]=[float(x) for x in elt.split(',')]
k=k+1
return node_dic
def graph_label_list(path,name):
graphs=[]
with open(path+name) as f:
sections = list(per_section(f))
k=1
for elt in sections[0]:
graphs.append((k,int(elt)))
k=k+1
return graphs
def graph_indicator(path,name):
data_dict = defaultdict(list)
with open(path+name) as f:
sections = list(per_section(f))
k=1
for elt in sections[0]:
data_dict[int(elt)].append(k)
k=k+1
return data_dict
def compute_adjency(path,name):
adjency= defaultdict(list)
with open(path+name) as f:
sections = list(per_section(f))
for elt in sections[0]:
adjency[int(elt.split(',')[0])].append(int(elt.split(',')[1]))
return adjency
def all_connected(X):
a=[]
for graph in X:
a.append(nx.is_connected(graph.nx_graph))
return np.all(a)
def build_NCI1_dataset(path):
node_dic=node_labels_dic(path,'NCI1_node_labels.txt')
node_dic2={}
for k,v in node_dic.items():
node_dic2[k]=v-1
node_dic=node_dic2
graphs=graph_label_list(path,'NCI1_graph_labels.txt')
adjency=compute_adjency(path,'NCI1_A.txt')
data_dict=graph_indicator(path,'NCI1_graph_indicator.txt')
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
data.append((g,i[1]))
return data
def build_PROTEIN_dataset(path,type_attr='label',use_node_deg=False):
if type_attr=='label':
node_dic=node_labels_dic(path,'PROTEINS_full_node_labels.txt') # A voir pour les attributes
if type_attr=='real':
node_dic=node_attr_dic(path,'PROTEINS_full_node_attributes.txt')
graphs=graph_label_list(path,'PROTEINS_full_graph_labels.txt')
adjency=compute_adjency(path,'PROTEINS_full_A.txt')
data_dict=graph_indicator(path,'PROTEINS_full_graph_indicator.txt')
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
if not use_node_deg:
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
if use_node_deg:
node_degree_dict=dict(g.nx_graph.degree())
normalized_node_degree_dict={k:v/len(g.nx_graph.nodes()) for k,v in node_degree_dict.items() }
nx.set_node_attributes(g.nx_graph,normalized_node_degree_dict,'attr_name')
data.append((g,i[1]))
return data
def build_PROTEIN2_dataset(path,type_attr='label',use_node_deg=False):
if type_attr=='label':
node_dic=node_labels_dic(path,'PROTEINS_node_labels.txt') # A voir pour les attributes
if type_attr=='real':
node_dic=node_attr_dic(path,'PROTEINS_node_attributes.txt')
graphs=graph_label_list(path,'PROTEINS_graph_labels.txt')
adjency=compute_adjency(path,'PROTEINS_A.txt')
data_dict=graph_indicator(path,'PROTEINS_graph_indicator.txt')
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
if not use_node_deg:
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
if use_node_deg:
node_degree_dict=dict(g.nx_graph.degree())
normalized_node_degree_dict={k:v/len(g.nx_graph.nodes()) for k,v in node_degree_dict.items() }
nx.set_node_attributes(g.nx_graph,normalized_node_degree_dict,'attr_name')
data.append((g,i[1]))
return data
def build_MUTAG_dataset(path,one_hot=False):
graphs=graph_label_list(path,'MUTAG_graph_labels.txt')
adjency=compute_adjency(path,'MUTAG_A.txt')
data_dict=graph_indicator(path,'MUTAG_graph_indicator.txt')
node_dic=node_labels_dic(path,'MUTAG_node_labels.txt') # ya aussi des nodes attributes ! The fuck ?
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
if one_hot:
attr=indices_to_one_hot(node_dic[node],7)
g.add_one_attribute(node,attr)
else:
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
data.append((g,i[1]))
return data
def build_IMDB_dataset(path,s='MULTI',use_node_deg=False):
graphs=graph_label_list(path,'IMDB-'+s+'_graph_labels.txt')
adjency=compute_adjency(path,'IMDB-'+s+'_A.txt')
data_dict=graph_indicator(path,'IMDB-'+s+'_graph_indicator.txt')
#node_dic=node_labels_dic(path,'MUTAG_node_labels.txt') # ya aussi des nodes attributes ! The fuck ?
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
#g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
if use_node_deg:
node_degree_dict=dict(g.nx_graph.degree())
normalized_node_degree_dict={k:v/len(g.nx_graph.nodes()) for k,v in node_degree_dict.items() }
nx.set_node_attributes(g.nx_graph,normalized_node_degree_dict,'attr_name')
data.append((g,i[1]))
return data
def build_PTC_dataset(path,one_hot=False):
graphs=graph_label_list(path,'PTC_MR_graph_labels.txt')
adjency=compute_adjency(path,'PTC_MR_A.txt')
data_dict=graph_indicator(path,'PTC_MR_graph_indicator.txt')
node_dic=node_labels_dic(path,'PTC_MR_node_labels.txt') # ya aussi des nodes attributes ! The fuck ?
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
if one_hot:
attr=indices_to_one_hot(node_dic[node],18)
g.add_one_attribute(node,attr)
else:
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
data.append((g,i[1]))
return data
def build_ENZYMES_dataset(path,type_attr='label',use_node_deg=False):
graphs=graph_label_list(path,'ENZYMES_graph_labels.txt')
if type_attr=='label':
node_dic=node_labels_dic(path,'ENZYMES_node_labels.txt') # A voir pour les attributes
if type_attr=='real':
node_dic=node_attr_dic(path,'ENZYMES_node_attributes.txt')
adjency=compute_adjency(path,'ENZYMES_A.txt')
data_dict=graph_indicator(path,'ENZYMES_graph_indicator.txt')
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
if not use_node_deg:
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
if use_node_deg:
node_degree_dict=dict(g.nx_graph.degree())
normalized_node_degree_dict={k:v/len(g.nx_graph.nodes()) for k,v in node_degree_dict.items() }
nx.set_node_attributes(g.nx_graph,normalized_node_degree_dict,'attr_name')
data.append((g,i[1]))
return data
def build_BZR_dataset(path,type_attr='label',use_node_deg=False):
graphs=graph_label_list(path,'BZR_graph_labels.txt')
if type_attr=='label':
node_dic=node_labels_dic(path,'BZR_node_labels.txt') # A voir pour les attributes
if type_attr=='real':
node_dic=node_attr_dic(path,'BZR_node_attributes.txt')
adjency=compute_adjency(path,'BZR_A.txt')
data_dict=graph_indicator(path,'BZR_graph_indicator.txt')
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
if not use_node_deg:
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
if use_node_deg:
node_degree_dict=dict(g.nx_graph.degree())
normalized_node_degree_dict={k:v/len(g.nx_graph.nodes()) for k,v in node_degree_dict.items() }
nx.set_node_attributes(g.nx_graph,normalized_node_degree_dict,'attr_name')
data.append((g,i[1]))
return data
def build_COX2_dataset(path,type_attr='label',use_node_deg=False):
graphs=graph_label_list(path,'COX2_graph_labels.txt')
if type_attr=='label':
node_dic=node_labels_dic(path,'COX2_node_labels.txt') # A voir pour les attributes
if type_attr=='real':
node_dic=node_attr_dic(path,'COX2_node_attributes.txt')
adjency=compute_adjency(path,'COX2_A.txt')
data_dict=graph_indicator(path,'COX2_graph_indicator.txt')
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
if not use_node_deg:
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
if use_node_deg:
node_degree_dict=dict(g.nx_graph.degree())
normalized_node_degree_dict={k:v/len(g.nx_graph.nodes()) for k,v in node_degree_dict.items() }
nx.set_node_attributes(g.nx_graph,normalized_node_degree_dict,'attr_name')
data.append((g,i[1]))
return data
def build_SYNTHETIC_dataset(path,type_attr='label'):
graphs=graph_label_list(path,'SYNTHETIC_graph_labels.txt')
if type_attr=='label':
node_dic=node_labels_dic(path,'SYNTHETIC_node_labels.txt') # A voir pour les attributes
if type_attr=='real':
node_dic=node_attr_dic(path,'SYNTHETIC_node_attributes.txt')
adjency=compute_adjency(path,'SYNTHETIC_A.txt')
data_dict=graph_indicator(path,'SYNTHETIC_graph_indicator.txt')
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
data.append((g,i[1]))
return data
def build_AIDS_dataset(path,type_attr='label'):
graphs=graph_label_list(path,'AIDS_graph_labels.txt')
if type_attr=='label':
node_dic=node_labels_dic(path,'AIDS_node_labels.txt') # A voir pour les attributes
if type_attr=='real':
node_dic=node_attr_dic(path,'AIDS_node_attributes.txt')
adjency=compute_adjency(path,'AIDS_A.txt')
data_dict=graph_indicator(path,'AIDS_graph_indicator.txt')
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
data.append((g,i[1]))
return data
def build_Cuneiform_dataset(path,type_attr='label'):
graphs=graph_label_list(path,'Cuneiform_graph_labels.txt')
if type_attr=='label':
node_dic=node_labels_dic(path,'Cuneiform_node_labels.txt') # A voir pour les attributes
if type_attr=='real':
node_dic=node_attr_dic(path,'Cuneiform_node_attributes.txt')
adjency=compute_adjency(path,'Cuneiform_A.txt')
data_dict=graph_indicator(path,'Cuneiform_graph_indicator.txt')
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
data.append((g,i[1]))
return data
def build_LETTER_dataset(path,type_attr='label',name='med'):
graphs=graph_label_list(path,'Letter-'+name+'_graph_labels.txt')
if type_attr=='label':
node_dic=node_labels_dic(path,'Letter-'+name+'_node_labels.txt') # A voir pour les attributes
if type_attr=='real':
node_dic=node_attr_dic(path,'Letter-'+name+'_node_attributes.txt')
adjency=compute_adjency(path,'Letter-'+name+'_A.txt')
data_dict=graph_indicator(path,'Letter-'+name+'_graph_indicator.txt')
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
data.append((g,i[1]))
return data
def build_Fingerprint_dataset(path,type_attr='real'):
graphs=graph_label_list(path,'Fingerprint_graph_labels.txt')
node_dic=node_attr_dic(path,'Fingerprint_node_attributes.txt')
adjency=compute_adjency(path,'Fingerprint_A.txt')
data_dict=graph_indicator(path,'Fingerprint_graph_indicator.txt')
data=[]
for i in graphs:
g=Graph()
for node in data_dict[i[0]]:
g.name=i[0]
g.add_vertex(node)
g.add_one_attribute(node,node_dic[node])
for node2 in adjency[node]:
g.add_edge((node,node2))
data.append((g,i[1]))
return data