forked from tddschn/easygraph-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_loaders.py
407 lines (358 loc) · 11.4 KB
/
dataset_loaders.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
#!/usr/bin/env python3
from functools import cache, partial
from pathlib import Path
import pickle
import easygraph as eg
import networkx as nx
from config import (
DATASET_DIR,
random_erdos_renyi_graphs_dir,
random_erdos_renyi_graphs_paths,
random_erdos_renyi_graphs_paths_date_s,
)
from utils import (
list_allfile,
load_func_for_nx,
directed_dataset,
print_with_hr,
load_large_datasets_with_read_edgelist,
)
# --------------------
# @coreturn's datasets
# --------------------
@cache
@directed_dataset
def load_cheminformatics():
"""
https://networkrepository.com/ENZYMES-g1.php
"""
print_with_hr(f'loading graph cheminformatics ...')
G = eg.DiGraph()
with open("./dataset/ENZYMES_g1/ENZYMES_g1.edges") as f:
for l in f.readlines():
v = l.split()
G.add_edge(v[0], v[1])
print_with_hr(
'finished loading graph cheminformatics\n'
+ "ENZYMES_g1: "
+ "nodes: "
+ str(len(G.nodes))
+ " edges: "
+ str(len(G.edges))
+ " is_directed: "
+ str(G.is_directed())
)
return G
@cache
def load_bio():
"""
https://networkrepository.com/bio-yeast.php
"""
print_with_hr('loading graph bio ...')
G = eg.Graph()
jump_lines = 2
with open("./dataset/bio-yeast/bio-yeast.mtx") as f:
for i, l in enumerate(f):
if i < jump_lines:
continue
v = l.split()
G.add_edge(v[0], v[1])
print_with_hr(
'finished loading graph bio\n'
+ "bio-yeast: "
+ "nodes: "
+ str(len(G.nodes))
+ " edges: "
+ str(len(G.edges))
+ " is_directed: "
+ str(G.is_directed())
)
return G
@cache
@load_func_for_nx
def load_eco():
"""
https://networkrepository.com/econ-mahindas.php
"""
print_with_hr('loading graph eco ...')
from scipy.io import mmread
# G = nx.from_numpy_array(mmread("./dataset/econ-mahindas/econ-mahindas.mtx"))
G = nx.from_scipy_sparse_matrix(mmread("./dataset/econ-mahindas/econ-mahindas.mtx"))
print_with_hr(
"finished loading graph eco\n"
+ "econ-mahindas: "
+ "nodes: "
+ str(len(G.nodes))
+ " edges: "
+ str(len(G.edges))
+ " is_directed: "
+ str(G.is_directed())
)
return G
@cache
@directed_dataset
def load_soc():
# gplus: digraph
G = eg.DiGraph()
files = list_allfile("./dataset/gplus/", ".edges")
print(files)
for filename in files: # type: ignore
name = filename.split("/")[-1].split(".")[0]
with open(filename) as f:
for l in f:
v = l.split()
print(v)
# a follow b
G.add_edge(v[0], v[1])
G.add_edge(name, v[0])
G.add_edge(name, v[1])
f.close()
print("finish load " + filename)
print(
"gplus: "
+ "nodes: "
+ str(len(G.nodes))
+ " edges: "
+ str(len(G.edges))
+ " is_directed: "
+ str(G.is_directed())
)
return G
# --------------------
# road-usa
# --------------------
@cache
@load_func_for_nx
def load_road() -> eg.Graph:
"""
partial dataset from
https://networkrepository.com/road-usa.php
"""
ROAD_DIR = DATASET_DIR / 'road'
road_file_path = ROAD_DIR / 'road.edges'
print_with_hr(f'loading graph road-usa from {str(road_file_path)} ...')
G = nx.read_edgelist(str(road_file_path))
print_with_hr(
f'finished loading graph road-usa\nnodes: {len(G.nodes)}, edges: {len(G.edges)}, is_directed: {G.is_directed()}'
)
return G
# --------------------
# uspowergrid
# --------------------
@cache
@load_func_for_nx
def load_uspowergrid() -> nx.Graph:
"""
https://toreopsahl.com/datasets/#uspowergrid
"""
UPG_DIR = DATASET_DIR / 'uspowergrid'
upg_file_path = UPG_DIR / 'us_powergrid.edges'
print_with_hr(f'loading graph uspowergrid from {str(upg_file_path)} ...')
G = nx.read_edgelist(str(upg_file_path))
print_with_hr(
f'finished loading graph uspowergrid\nnodes: {len(G.nodes)}, edges: {len(G.edges)}, is_directed: {G.is_directed()}'
)
return G
# --------------------
# pgp network of trust
# --------------------
@cache
@directed_dataset
@load_func_for_nx
def load_pgp() -> nx.DiGraph:
PGP_DIR = DATASET_DIR / "pgp"
graph_file_path = PGP_DIR / "pgp.xml"
print_with_hr(f'loading graph pgp from {str(graph_file_path)} ...')
g = nx.read_graphml(str(graph_file_path))
print_with_hr(
f'finish loading graph pgp.\nnodes: {len(g.nodes)}, edges: {len(g.edges)}, is_directed: {g.is_directed()}'
)
return g
@cache
@load_func_for_nx
def load_pgp_undirected() -> nx.Graph:
PGP_DIR = DATASET_DIR / "pgp"
graph_file_path = PGP_DIR / "pgp_undirected.xml"
print_with_hr(f'loading graph pgp_undirected from {str(graph_file_path)} ...')
g = nx.read_graphml(str(graph_file_path))
print_with_hr(
f'finish loading graph pgp_undirected.\nnodes: {len(g.nodes)}, edges: {len(g.edges)}, is_directed: {g.is_directed()}'
)
return g
# --------------------
# other datasets used in the paper
# --------------------
# @load_func_for_nx
# def load_food() -> nx.
# --------------------
# really large datasets
# --------------------
@cache
@load_func_for_nx
def load_enron() -> nx.Graph:
"""
https://snap.stanford.edu/data/email-Enron.html
"""
p = Path('enron.txt')
print_with_hr(f'loading graph enron from {str(p)} ...')
if not p.exists():
error_msg = f'enron.txt not found. Download from http://snap.stanford.edu/data/email-Enron.txt.gz .'
raise FileNotFoundError(error_msg)
g = load_large_datasets_with_read_edgelist(str(p), create_using=nx.Graph())
print_with_hr(
f'finish loading graph enron.\nnodes: {len(g.nodes)}, edges: {len(g.edges)}, is_directed: {g.is_directed()}'
)
return g
@cache
@directed_dataset
@load_func_for_nx
def load_google() -> nx.DiGraph:
"""
https://snap.stanford.edu/data/web-Google.html
"""
p = Path('google.txt')
print_with_hr(f'loading graph google from {str(p)} ...')
if not p.exists():
error_msg = f'google.txt not found. Download from http://snap.stanford.edu/data/web-Google.txt.gz .'
raise FileNotFoundError(error_msg)
g = load_large_datasets_with_read_edgelist(str(p))
print_with_hr(
f'finish loading graph google.\nnodes: {len(g.nodes)}, edges: {len(g.edges)}, is_directed: {g.is_directed()}'
)
return g
@cache
@directed_dataset
@load_func_for_nx
def load_amazon() -> nx.DiGraph:
"""
https://snap.stanford.edu/data/amazon0302.html
"""
p = Path('amazon.txt')
print_with_hr(f'loading graph amazon from {str(p)} ...')
if not p.exists():
error_msg = f'amazon.txt not found. Download from http://snap.stanford.edu/data/amazon0302.txt.gz .'
raise FileNotFoundError(error_msg)
g = load_large_datasets_with_read_edgelist(str(p))
print_with_hr(
f'finish loading graph amazon.\nnodes: {len(g.nodes)}, edges: {len(g.edges)}, is_directed: {g.is_directed()}'
)
return g
@cache
@directed_dataset
@load_func_for_nx
def load_pokec() -> nx.DiGraph:
"""
https://snap.stanford.edu/data/soc-Pokec.html
"""
p = Path('pokec.txt')
print_with_hr(f'loading graph pokec from {str(p)} ...')
if not p.exists():
error_msg = f'pokec.txt not found. Download from http://snap.stanford.edu/data/soc-pokec-relationships.txt.gz .'
raise FileNotFoundError(error_msg)
g = load_large_datasets_with_read_edgelist(str(p))
print_with_hr(
f'finish loading graph pokec.\nnodes: {len(g.nodes)}, edges: {len(g.edges)}, is_directed: {g.is_directed()}'
)
return g
# --------------------
# chenyang03/co-authorship-network (very large)
# --------------------
# @directed_dataset
@cache
@load_func_for_nx
def load_coauthorship(
coauthorship_edges_file: str = '../co-authorship-network/edges.txt',
) -> nx.DiGraph:
"""
https://github.com/chenyang03/co-authorship-network
"""
print_with_hr(f'loading graph coauthorship from {coauthorship_edges_file} ...')
number_of_nodes = 402392
g: nx.DiGraph = nx.read_edgelist(
coauthorship_edges_file,
delimiter=',',
nodetype=int, # create_using=nx.DiGraph()
)
g.add_nodes_from(range(number_of_nodes))
print_with_hr(
f'finish loading graph coauthorship.\nnodes: {len(g.nodes)}, edges: {len(g.edges)}, is_directed: {g.is_directed()}'
)
return g
# --------------------
# stub loaders
# --------------------
@cache
def load_stub():
print_with_hr('loading graph stub ...')
G: eg.Graph = eg.complete_graph(5) # type: ignore
print_with_hr(
f'finished loading graph stub\nnodes: {len(G.nodes)}, edges: {len(G.edges)}, is_directed: {G.is_directed()}'
)
return G
@cache
def load_stub_with_underscore():
print_with_hr('loading graph stub ...')
G: eg.Graph = eg.complete_graph(5) # type: ignore
print_with_hr(
f'finished loading graph stub\nnodes: {len(G.nodes)}, edges: {len(G.edges)}, is_directed: {G.is_directed()}'
)
return G
@cache
@directed_dataset
def load_stub_directed():
print_with_hr('loading graph stub_directed ...')
G: eg.DiGraph = eg.complete_graph(5, create_using=eg.DiGraph) # type: ignore
print_with_hr(
f'finished loading graph stub_directed\nnodes: {len(G.nodes)}, edges: {len(G.edges)}, is_directed: {G.is_directed()}'
)
return G
@cache
@load_func_for_nx
def load_stub_nx():
print_with_hr('loading graph stub_nx ...')
G = nx.complete_graph(5) # type: ignore
print_with_hr(
f'finished loading graph stub_nx\nnodes: {len(G.nodes)}, edges: {len(G.edges)}, is_directed: {G.is_directed()}'
)
return G
# --------------------
# random-erdos-renyi
# --------------------
def load_random_erdos_renyi(
date_s: str | None = None,
filepath: Path | None = None,
node_number: int | None = None,
directed: bool | None = False,
use_pickle: bool | None = False,
) -> eg.Graph | eg.DiGraph:
if filepath is None:
dataset_dir = (
random_erdos_renyi_graphs_dir
if date_s is None
else DATASET_DIR / f'er-paper-{date_s}'
)
filepath = (
dataset_dir
/ f'{node_number}{"_directed" if directed else ""}.{"pickle" if use_pickle else "edgelist"}'
)
if not filepath.exists():
raise FileNotFoundError(f'{filepath} not found.')
filepath_s = str(filepath)
print_with_hr(f'loading graph random_erdos_renyi from {filepath} ...')
if filepath.suffix == '.pickle':
with open(filepath_s, 'rb') as f:
G = pickle.load(f)
else:
G = eg.read_edgelist(filepath_s, nodetype=int, create_using=eg.DiGraph() if directed or filepath.stem.endswith('_directed') else eg.Graph()) # type: ignore
print_with_hr(
f'finished loading graph random_erdos_renyi\nnodes: {len(G.nodes)}, edges: {len(G.edges)}, is_directed: {G.is_directed()}' # type: ignore
)
return G # type: ignore
for p in random_erdos_renyi_graphs_paths:
g = globals()
g[f'load_er_{p.stem}'] = partial(load_random_erdos_renyi, filepath=p)
for p in random_erdos_renyi_graphs_paths_date_s:
g = globals()
g[f'load_er_paper_{p.parent.name.removeprefix("er-paper-")}_{p.stem}'] = partial(
load_random_erdos_renyi, filepath=p
)