-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphtool-stuff.py
229 lines (171 loc) · 7.15 KB
/
graphtool-stuff.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
import graph_tool as gt
import graph_tool.centrality
import graph_tool.clustering
import csv
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from heapq import nlargest
import pickle
from collections import defaultdict
grap = gt.Graph()
df = pd.read_csv('name_name.csv', dtype={"reps": np.int64, "comm" : str, "auth" : str})
#Data Cleaning
df.isnull().values.sum()
nan_rows = df[df.isnull().any(1)]
nan_rows
df = df.dropna()
df.isnull().values.sum()
#NXGraph
dfn = df.sample(10000)
del df
graph = nx.from_pandas_edgelist(dfn, source = 'comm', target = 'auth', edge_attr = 'reps',create_using = nx.DiGraph())
del dfn
def get_prop_type(value, key=None):
"""
Performs typing and value conversion for the graph_tool PropertyMap class.
If a key is provided, it also ensures the key is in a format that can be
used with the PropertyMap. Returns a tuple, (type name, value, key)
"""
if isinstance(key, unicode):
# Encode the key as ASCII
key = key.encode('ascii', errors='replace')
# Deal with the value
if isinstance(value, bool):
tname = 'bool'
elif isinstance(value, int):
tname = 'float'
value = float(value)
elif isinstance(value, float):
tname = 'float'
elif isinstance(value, unicode):
tname = 'string'
value = value.encode('ascii', errors='replace')
elif isinstance(value, dict):
tname = 'object'
else:
tname = 'string'
value = str(value)
return tname, value, key
def nx2gt(nxG):
"""
Converts a networkx graph to a graph-tool graph.
"""
# Phase 0: Create a directed or undirected graph-tool Graph
gtG = gt.Graph(directed=nxG.is_directed())
# Add the Graph properties as "internal properties"
for key, value in nxG.graph.items():
# Convert the value and key into a type for graph-tool
tname, value, key = get_prop_type(value, key)
prop = gtG.new_graph_property(tname) # Create the PropertyMap
gtG.graph_properties[key] = prop # Set the PropertyMap
gtG.graph_properties[key] = value # Set the actual value
# Phase 1: Add the vertex and edge property maps
# Go through all nodes and edges and add seen properties
# Add the node properties first
nprops = set() # cache keys to only add properties once
for node, data in nxG.nodes(data=True):
# Go through all the properties if not seen and add them.
for key, val in data.items():
if key in nprops: continue # Skip properties already added
# Convert the value and key into a type for graph-tool
tname, _, key = get_prop_type(val, key)
prop = gtG.new_vertex_property(tname) # Create the PropertyMap
gtG.vertex_properties[key] = prop # Set the PropertyMap
# Add the key to the already seen properties
nprops.add(key)
# Also add the node id: in NetworkX a node can be any hashable type, but
# in graph-tool node are defined as indices. So we capture any strings
# in a special PropertyMap called 'id' -- modify as needed!
gtG.vertex_properties['id'] = gtG.new_vertex_property('string')
# Add the edge properties second
eprops = set() # cache keys to only add properties once
for src, dst, data in nxG.edges(data=True):
# Go through all the edge properties if not seen and add them.
for key, val in data.items():
if key in eprops: continue # Skip properties already added
# Convert the value and key into a type for graph-tool
tname, _, key = get_prop_type(val, key)
prop = gtG.new_edge_property(tname) # Create the PropertyMap
gtG.edge_properties[key] = prop # Set the PropertyMap
# Add the key to the already seen properties
eprops.add(key)
# Phase 2: Actually add all the nodes and vertices with their properties
# Add the nodes
vertices = {} # vertex mapping for tracking edges later
for node, data in nxG.nodes(data=True):
# Create the vertex and annotate for our edges later
v = gtG.add_vertex()
vertices[node] = v
# Set the vertex properties, not forgetting the id property
data['id'] = str(node)
for key, value in data.items():
gtG.vp[key][v] = value # vp is short for vertex_properties
# Add the edges
for src, dst, data in nxG.edges(data=True):
# Look up the vertex structs from our vertices mapping and add edge.
e = gtG.add_edge(vertices[src], vertices[dst])
# Add the edge properties
for key, value in data.items():
gtG.ep[key][e] = value # ep is short for edge_properties
# Done, finally!
return gtG
def get_prop_type(value, key=None):
"""
Performs typing and value conversion for the graph_tool PropertyMap class.
If a key is provided, it also ensures the key is in a format that can be
used with the PropertyMap. Returns a tuple, (type name, value, key)
"""
# Deal with the value
if isinstance(value, bool):
tname = 'bool'
elif isinstance(value, int):
tname = 'float'
value = float(value)
elif isinstance(value, float):
tname = 'float'
elif isinstance(value, unicode):
tname = 'string'
elif isinstance(value, dict):
tname = 'object'
else:
tname = 'string'
value = str(value)
return tname, value, key
gtG = nx2gt(graph)
gtG.list_properties()
pm = gtG.ep.properties['e', 'reps']
pgd, pg_itr = gt.centrality.pagerank(gtG, damping=0.85, pers=None, weight=pm, prop=None, epsilon=1e-06, max_iter=100, ret_iter=True)
ver_bet, edge_bet = gt.centrality.betweenness(gtG, pivots=None, vprop=None, eprop=None, weight=pm, norm=True)
ver_arr_bet = list(ver_bet.get_array())
with open('bet.pkl', 'wb') as f:
pickle.dump(ver_arr_bet, f)
ver_close = gt.centrality.closeness(gtG, weight=pm, source=None, vprop=None, norm=True, harmonic=False)
ver_arr_close = list(ver_close.get_array())
with open('close.pkl', 'wb') as f:
pickle.dump(ver_arr_close, f)
eigen, auth, hub = gt.centrality.hits(gtG, weight=pm, xprop=None, yprop=None, epsilon=1e-06, max_iter=100)
vertex_freq_x, vertex_freq_y = gt.stats.vertex_hist(gtG, "total", bins=[0, 1], float_count=True)
local_clustercoeff = gt.clustering.local_clustering(gtG, prop=None, undirected=True)
global_coeff = gt.clustering.global_clustering(gtG)
#Distributions
counter_bet = defaultdict(int)
counter_close = defaultdict(int)
for i in range(0,len(ver_arr_bet)):
counter_bet[ver_arr_bet[i]] += 1
for i in range(0,len(ver_arr_close)):
counter_close[ver_arr_close[i]] += 1
#plot
lists = sorted(counter_bet.items()) # sorted by key, return a list of tuples
x, y = zip(*lists) # unpack a list of pairs into two tuples
plt.figure(figsize=(20,10))
plt.plot(x, y)
plt.savefig('dist_centrality_betweeness.png', bbox_inches='tight')
plt.show()
lists = sorted(counter_close.items()) # sorted by key, return a list of tuples
x, y = zip(*lists) # unpack a list of pairs into two tuples
plt.figure(figsize=(20,10))
plt.plot(x, y)
plt.savefig('dist_centrality_closeness.png', bbox_inches='tight')
plt.show()