-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfoldAndComDetect.py
174 lines (148 loc) · 5.06 KB
/
foldAndComDetect.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
import snap
import math
import time
edgeIndices = snap.TIntV()
fileName = "nodeCommunity.txt"
fileName2 = "communitySize.txt"
# assume at each key, there is a unique integer value and the intgers are sorted
# and at cntinuous + 1 increment
def getKeyAtVal(strIntH, val):
index = 0
for key in strIntH:
if val == index:
break;
else:
index = index + 1
return key
def getPartOfVector(strV, first, last):
vecPart = snap.TStrV()
for ind in range(first, last+1):
vecPart.Add(strV[ind])
return vecPart
# get all indices that have this value
def getIndicesWithVal(strV, val, first, edgeIndices):
myInd = strV.SearchForw(val,0)
if myInd == -1:
return
if strV.Len()==1:
edgeIndices.Add(first)
# print "%s at %d" %(val,first)
return
myLen = strV.Len()
half = int(math.floor(myLen/2.0))
# print "first, half, length = (%d, %d, %d)" % (first, half, myLen)
leftPart = snap.TStrV()
#strV.GetSubValV(0, half-1, leftPart)
leftPart = getPartOfVector(strV, 0, half-1,0)
getIndicesWithVal(leftPart, val, first, edgeIndices)
rightPart = snap.TStrV()
#strV.GetSubValV(half, myLen-1, rightPart)
rightPart = getPartOfVector(strV, half, myLen-1,1)
first = first + half
getIndicesWithVal(rightPart, val, first, edgeIndices)
startTime = time.time()
# Read the edges
print "----------------Reading The Bipartite Graph Edges-----------------"
loanEdgeNodes = snap.TStrV()
lenderEdgeNodes = snap.TStrV()
edgePairs = snap.TStrPrV()
numEdges = 0
file = open("lender_loans.txt", "r")
for line in file:
numEdges += 1
words = line.split()
loanEdgeNodes.Add(words[0])
lenderEdgeNodes.Add(words[1])
myPair = snap.TStrPr(words[0], words[1])
edgePairs.Add(myPair)
if numEdges >=1000:
break
file.close()
print "Edges from loans to lenders = %d" % numEdges
print "----------------Loan Nodes-----------------"
loanNodes = snap.TStrIntH()
prevId = ''
ind = 0
file = open("loanNameIdMap.map", "w")
for loanId in loanEdgeNodes:
if loanId != prevId:
loanNodes[loanId] = ind
file.write(loanId + "\t" + str(ind)+ "\n")
ind = ind + 1
prevId = loanId
file.close()
print "There are %d loans" % loanNodes.Len()
print "----------------Lender Nodes-----------------"
lenderNodes = snap.TStrIntH()
sortedV = snap.TStrV()
# copy into sortedV and sort it
for lenderId in lenderEdgeNodes:
sortedV.Add(lenderId)
sortedV.Sort(True)
prevId = ''
ind = 0
idToName = snap.TIntStrH()
print "sorted lenderId"
file = open("lenderNameIdMap.map", "w")
for lenderId in sortedV:
#print lenderId
if lenderId != prevId:
lenderNodes[lenderId] = ind
idToName[ind] = lenderId
file.write(lenderId + "\t" + str(ind)+ "\n")
ind = ind + 1
prevId = lenderId
file.close()
print "There are %d lenders" % lenderNodes.Len()
print "----------------Folding Lender to lender graph-----------------"
# undirected graph
GLender = snap.TUNGraph.New()
# add the nodes
for nodeId in range(0, lenderNodes.Len()):
GLender.AddNode(nodeId)
# add the edges
prevLoanName = ''
prevLenderIds = snap.TIntV()
for curEdge in range(0, numEdges):
curLoanName = loanEdgeNodes[curEdge]
curLenderId = lenderNodes[lenderEdgeNodes[curEdge]]
if curLoanName == prevLoanName:
for prevLenderId in prevLenderIds:
GLender.AddEdge(curLenderId, prevLenderId)
prevLenderIds.Add(curLenderId)
else:
prevLenderIds.Clr()
prevLenderIds.Add(curLenderId)
prevLoanName = curLoanName
snap.SaveEdgeList(GLender, "GLender.edgelist", "Folded Lender Graph")
if (False):
for EI in GLender.Edges():
lenderId1 = EI.GetSrcNId()
lenderId2 = EI.GetDstNId()
lenderName1 = getKeyAtVal(lenderNodes, lenderId1)
lenderName2 = getKeyAtVal(lenderNodes, lenderId2)
print "(%s, %s)" % (lenderName1,lenderName2)
print "number of edges in lender to lender graph is %d" %GLender.GetEdges()
elapsed_time = time.time() - startTime
print "total folding time is %d seconds" %elapsed_time
print "------------------Community Detection---------------------"
CmtyV = snap.TCnComV()
modularity = snap.CommunityCNM(GLender, CmtyV)
file = open(fileName, "w")
file2 = open(fileName2, "w")
communityCount = 0
numNodes = 0
for Cmty in CmtyV:
# print "Community: %d" % communityCount
file2.write(str(communityCount) + "\t"+ str(Cmty.Len())+ "\n")
for NI in Cmty:
numNodes = numNodes + 1
file.write(str(idToName[NI]) + "\t" + str(communityCount) + "\n")
# print "%d processed nodes" % numNodes
communityCount+=1
file.close()
file2.close()
print "The modularity of the network is %f" % modularity
print "The total number of communities is %d" % CmtyV.Len()
elapsed_time = time.time() - startTime
print "total Community Detection time is %d seconds" %elapsed_time