-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessGraphs.py
More file actions
327 lines (284 loc) · 9.38 KB
/
processGraphs.py
File metadata and controls
327 lines (284 loc) · 9.38 KB
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
#using python 2.7.x
import sys
sys.path.insert(0, '../')
from snap import *
import random
import numpy as np
import math
from scipy.stats import kurtosis
from scipy.stats import skew
import csv
########################################## PROCESS TEXT FILE #########################################
#Parses keywords file
def keyWords():
keyWordsList = []
f = open('key_words.csv')
lines = f.readlines()
f.close()
for i, line in enumerate(lines):
if i == 0:
continue
if i == len(lines) - 1:
continue
word = line.split(',')[0]
keyWordsList.append(word)
return keyWordsList
#Returns total edge counts
def getCounts(edgeWeights):
examined = set()
counts = []
for key in edgeWeights:
src, dst = key[0], key[1]
if (src, dst) in examined:
continue
examined.add((src, dst))
examined.add((dst, src))
counts.append(int(edgeWeights[(src, dst)]))
return counts
#parses through text file and creates graph, edgeWeights, IdMap, counts
#edge weights is srcId, dstId
def processFile(filename):
G = PUNGraph.New()
edgeWeights = {}
IdMap = {}
iterator = 0
f = open(filename, 'r')
lines = f.readlines()
f.close()
for i, line in enumerate(lines):
if i == 0:
continue
elems = line.split(',')
countStr = elems[2]
countStr = countStr[:-2]
if countStr == '': #weird edge case
continue
src, dst = elems[0], elems[1]
count = int(countStr)
if src not in IdMap: #IdMap: name -> Id
IdMap[src] = iterator
IdMap[str(iterator)] = src
iterator = iterator + 1
if dst not in IdMap:
IdMap[dst] = iterator
IdMap[str(iterator)] = dst
iterator = iterator + 1
srcId = IdMap[src]
dstId = IdMap[dst]
if G.IsNode(srcId) == False:
G.AddNode(srcId)
if G.IsNode(dstId) == False:
G.AddNode(dstId)
G.AddEdge(srcId, dstId)
edgeWeights[(srcId, dstId)] = count
edgeWeights[(dstId, srcId)] = count
counts = getCounts(edgeWeights)
return G, edgeWeights, IdMap, counts
def createGraphs(words):
print "Creating graphs..."
s = {}
for word in words:
name1 = 'russian graphs/' + word + '_rus.csv'
name2 = 'english graphs/' + word + '_eng.csv'
G1, edgeWeights1, IdMap1, counts1 = processFile(name1)
G2, edgeWeights2, IdMap2, counts2 = processFile(name2)
processGraph(word + "_RUS", G1, counts1, edgeWeights1, IdMap1)
processGraph(word + "_ENG", G2, counts2, edgeWeights2, IdMap2)
if 'DIGITAL' in word:
print "Russian %s: %d" % (word, G1.GetNodes())
print "English %s: %d" % (word, G2.GetNodes())
s[word + "_RUS"] = (G1, edgeWeights1, IdMap1, word)
s[word + "_ENG"] = (G2, edgeWeights2, IdMap2, word)
print "Created %d graphs" % len(s)
return s
def getTotalWeight(G, edgeWeights):
total = 0
for edge in G.Edges():
src, dst = edge.GetSrcNId(), edge.GetDstNId()
count = edgeWeights[(src, dst)]
total = total + int(count)
return total
#write processed version of graph to file
def writeToFile(filename, G, idMap, edgeWeights):
print "Writing %s to file" % filename
filename2 = "processedGraphCSVs/" + filename + "_proc.csv"
f = open(filename2, 'w')
fields = ('Source', 'Target', 'count', 'Type', 'Weight')
wr = csv.DictWriter(f, fieldnames=fields, lineterminator = '\n')
wr.writeheader()
totalWeight = getTotalWeight(G, edgeWeights)
for edge in G.Edges():
src, dst = edge.GetSrcNId(), edge.GetDstNId()
srcS = idMap[str(src)]
dstS = idMap[str(dst)]
count = edgeWeights[(src, dst)]
weight = int(count) / float(totalWeight)
wr.writerow({'Source':srcS, 'Target': dstS, 'count':str(count), 'Type': 'Undirected', 'Weight': str(weight)})
f.close()
#Examine every edge that exists in the graph. If its count is < t, delete it
def processGraph(filename, G, counts, edgeWeights, idMap):
t = math.floor(sum(counts) / float(len(counts)))
for Edge in G.Edges():
srcId, dstId = Edge.GetSrcNId(), Edge.GetDstNId()
count = edgeWeights[srcId, dstId]
if count < t:
G.DelEdge(srcId, dstId)
writeToFile(filename, G, idMap, edgeWeights)
############################################## FEATURE EXTRACTION / AGGREGATION ###############################################
#helper function
def getNoNeighbors(node):
count = 0
for neighborId in node.GetOutEdges():
count = count + 1
return count
#helper function
def getAvgTwoHop(G, node):
dNeighbors = 0
for neighborId in node.GetOutEdges():
dNeighbors = dNeighbors + G.GetNI(neighborId).GetDeg()
if node.GetDeg() == 0:
return 0
return dNeighbors / float(node.GetDeg())
#helper function
def getAvgNodeCC(G, node):
cc = 0
for neighborId in node.GetOutEdges():
cc = cc + GetNodeClustCf(G, neighborId)
if node.GetDeg() == 0:
return 0
return cc / float(node.GetDeg())
#helper function
def getNodeFeatures(G, m, centerNodeId):
for node in G.Nodes():
nodeIdNo = node.GetId()
# if nodeIdNo == centerNodeId: No need to differentiate between centerNodeId for now ***
# continue
neighborNo = getNoNeighbors(node)
nodeCC = GetNodeClustCf(G, nodeIdNo)
avgTwoHop = getAvgTwoHop(G, node)
avgNodeCC = getAvgNodeCC(G, node)
f = [neighborNo, nodeCC, avgTwoHop, avgNodeCC]
m.append(f)
#extracts features
'''
features:
1. number of neighbors
2. clustering coefficient of node i
3. average number of node i's two-hop away neighbors
4. average clustering coefficient
'''
def getFeatures(G, IdMap, centerNodeId):
featureMatrix = []
getNodeFeatures(G, featureMatrix, centerNodeId)
return featureMatrix
#Iterates through features and returns a set of signature vectors
# Returns matrix of median, mean, std, skew, kurtosis for each feature
def aggregateFeatures(G, m, centerNode, IdMap):
sv = []
neighborNo, nodeCC, avg2Hop, avgNodeCC = [], [], [], []
for feat in m:
neighborNo.append(feat[0])
nodeCC.append(feat[1])
avg2Hop.append(feat[2])
avgNodeCC.append(feat[3])
features = [neighborNo, nodeCC, avg2Hop, avgNodeCC]
#for each feature, add aggregators to sv
for feat in features:
sv.append(np.median(np.array(feat)))
sv.append(np.mean(np.array(feat)))
sv.append(np.std(np.array(feat)))
sv.append(skew(feat))
sv.append(kurtosis(feat))
#add center node features
centerNodeId = IdMap[centerNode]
centerNodeObj = G.GetNI(centerNodeId)
#get features
center_neighborCount = getNoNeighbors(centerNodeObj)
center_nodeCC = GetNodeClustCf(G, centerNodeId)
center_avgTwoHop = getAvgTwoHop(G, centerNodeObj)
center_avgNodeCC = getAvgNodeCC(G, centerNodeObj)
#append to sv
sv.append(center_neighborCount)
sv.append(center_nodeCC)
sv.append(center_avgTwoHop)
sv.append(center_avgNodeCC)
return sv
def netSimile(graphs): #order is russian and then english
print "Starting net simile alg..."
signatureVectors = {}
for i, key in enumerate(graphs):
value = graphs[key]
G, edgeWeights, IdMap, centerNode = value[0], value[1], value[2], value[3]
m = getFeatures(G, IdMap, IdMap[centerNode])
sv = aggregateFeatures(G, m, centerNode, IdMap)
signatureVectors[key] = sv
return signatureVectors
############################################## DISTANCE FUNCTION ###############################################
def evaluate(signatureVectors, keywords):
print "Evaluating distances..."
allDistances = []
for word in keywords:
rus = word + "_RUS"
eng = word + "_ENG"
svRus = signatureVectors[rus]
svEng = signatureVectors[eng]
d1 = getDistance(svRus, svEng)
allDistances.append([word, d1])
allDistances = sorted(allDistances, key=lambda x: x[1],reverse=False)
vals = []
for d in allDistances:
vals.append(d[1])
median = vals[len(vals) / 2]
mean = sum(vals) / len(vals)
print allDistances
print "Median: %d, Mean: %f" %(median, mean)
#Canberra distance function
def getDistance(sv1, sv2):
d = 0
for i in range(24):
elem1 = sv1[i]
elem2 = sv2[i]
if elem1 + elem2 != 0: #avoid the divide by 0 case
d = d + abs(elem1 - elem2) / float(elem1 + elem2)
return d
def getDistanceContribution(sv1, sv2):
d = 0
for i in range(len(sv1)):
elem1 = sv1[i]
elem2 = sv2[i]
if elem1 + elem2 != 0: #avoid the divide by 0 case
d = d + abs(elem1 - elem2) / float(elem1 + elem2)
return d
#network distance analyis
#find which feature is driving network distance
def analysis(signatureVectors, keywords):
features = [0, 0, 0, 0, 0]
for word in keywords:
rus = word + "_RUS"
eng = word + "_ENG"
svRus = signatureVectors[rus]
svEng = signatureVectors[eng]
neighborNo = getDistanceContribution(svRus[0:5], svEng[0:5])
nodeCC = getDistanceContribution(svRus[5:11], svEng[5:11])
avg2Hop = getDistanceContribution(svRus[10:15], svEng[10:15])
avgNodeCC = getDistanceContribution(svRus[15:20], svEng[15:20])
centerNode = getDistanceContribution(svRus[20:], svEng[20:])
featuresVals = [neighborNo, nodeCC, avg2Hop, avgNodeCC, centerNode]
for i in range(len(features)):
features[i] = features[i] + featuresVals[i]
featureKey = ["Number of Neighbors", "Node Clustering Coefficients", "Average Two Hop Number of Neighbors", "Average Node Clustering Coefficient", "Center Node Features"]
output = []
for i, elem in enumerate(features):
if i == 3:
continue
output.append([featureKey[i], features[i]])
print "Contribution of %s: %d" % (featureKey[i], features[i])
print output
############################################## DRIVER ###############################################
key_words = keyWords()
graphs = createGraphs(key_words)
# print len(graphs)
signatureVectors = netSimile(graphs)
# print len(signatureVectors)
evaluate(signatureVectors, key_words)
analysis(signatureVectors, key_words)