-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_graph.py
More file actions
38 lines (25 loc) · 876 Bytes
/
preprocess_graph.py
File metadata and controls
38 lines (25 loc) · 876 Bytes
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
# coding: utf-8
import networkx as nx
import pandas as pd
from tqdm import tqdm
from helpers import get_lcc, get_v1
graph = 'bitcoin'
df = pd.read_csv('data/{}.txt'.format(graph), sep='\t', comment='#', header=None, names=['u', 'v', 'sign'])
g = nx.Graph()
dg = nx.DiGraph()
for i, r in tqdm(df.iterrows()):
u, v, sign = r['u'], r['v'], r['sign']
dg.add_edge(u, v, sign=sign)
if not g.has_edge(u, v):
g.add_edge(u, v, sign=sign)
else:
print('edge ({}, {}) exists'.format(u, v))
lcc = get_lcc(g)
mapping = {n: i for i, n in enumerate(lcc.nodes())}
g = nx.relabel_nodes(lcc, mapping=mapping)
dg = nx.relabel_nodes(dg.subgraph(g.nodes()), mapping=mapping)
l1, v1 = get_v1(g)
g.graph['lambda1'] = l1
g.graph['v1'] = v1
nx.write_gpickle(g, 'graphs/{}.pkl'.format(graph))
nx.write_gpickle(dg, 'graphs/{}_directed.pkl'.format(graph))