-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphify.py
55 lines (46 loc) · 1.45 KB
/
graphify.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
# Creates pruned digraphs
# Import libraries
import networkx as nx
import sys
# Load digraph from pickle file
filename = sys.argv[1] if (len(sys.argv) > 1) else 'graph.pickle'
print('Reading file: ' + filename)
G = nx.read_gpickle(filename)
print(nx.info(G))
# Recursively remove all nodes with degree < 2
print('Recursively removing all nodes with degree < 2:')
for i in range(10):
for node in G.nodes():
if G.degree(node) < 2:
G.remove_node(node)
print(nx.info(G))
# Save digraph to pickle file
filename = 'graph2.pickle'
print('Saving digraph to: ' + filename)
nx.write_gpickle(G, filename)
# Reload original digraph from pickle file
filename = sys.argv[1] if (len(sys.argv) > 1) else 'digraph.pickle'
print ('Reading file: ' + filename)
G = nx.read_gpickle(filename)
print(nx.info(G))
# Keep only marked nodes
print('Keeping only marked nodes:')
for node, data in G.nodes(data=True):
if 'marked' not in data:
G.remove_node(node)
print(nx.info(G))
# Save digraph to pickle file
filename = 'digraph3.pickle'
print('Saving digraph to: ' + filename)
nx.write_gpickle(G, filename)
# Recursively remove all nodes with degree < 2
print('Recursively removing all nodes with degree < 2:')
for i in range(10):
for node in G.nodes():
if G.degree(node) < 2:
G.remove_node(node)
print(nx.info(G))
# Save digraph to pickle file
filename = 'digraph4.pickle'
print('Saving digraph to: ' + filename)
nx.write_gpickle(G, filename)