-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHandleCPM.py
180 lines (165 loc) · 6.04 KB
/
HandleCPM.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#CPM算法运行结果的可视化处理
import os
import csv
import datetime
from numpy import genfromtxt,zeros
from pylab import plot,show
from math import radians, cos, sin, asin, sqrt
import types
import networkx as nx
import numpy as np
import math
import random as rand
import sys
import matplotlib.pyplot as plt
from numpy import genfromtxt,zeros
def buildG(G, file_, delimiter_):
reader = csv.reader(open(file_), delimiter=delimiter_)
for line in list(reader):
G.add_edge(int(line[0]),int(line[1]))
def showG(partition,initG):
color = ["dimgrey","rosybrown","brown","tomato","chocolate",
"gold","darkkhaki","olivedrab","yellowgreen","darkseagreen",
"lightseagreen","cyan","deepskyblue","steelblue","mediumpurple","indigo",
"fuchsia","mediumvioletred","crimson","darkslategrey","darkgreen"
]
#size = len(set(partition.values()))
pos = nx.random_layout(initG)
count = 0
nx.draw(initG, pos, node_size=250, alpha=0.5, edge_color='r', font_size=12, with_labels=True)
plt.show()
for com in set(partition.values()):
list_nodes = [nodes for nodes in partition.keys()
if partition[nodes] == com]
print("Community "+str(count))
print("Nodes: "+str(list_nodes))
print("Size:"+str(len(list_nodes)))
print(initG.edges(list_nodes))
# list_edges = []
# for node in list_nodes:
# for edge in initG.edges():
# if node == edge[0]:
# for nod in list_nodes:
# if nod == edge[1]:
# list_edges.append(edge)
nx.draw_networkx_nodes(initG, pos, list_nodes, node_size=250,
node_color=color[count])
# nx.draw_networkx_edges(initG, pos, list_edges, with_labels=True, alpha=0.5)
# nx.draw_networkx_labels(initG, pos)
# plt.show()
# plt.clf()
count = count + 1
nx.draw_networkx_edges(initG, pos, with_labels=True, alpha=0.5)
nx.draw_networkx_labels(initG, pos)
plt.show()
def resultG(partition,G):
#size = len(set(partition.values()))
pos = nx.random_layout(G)
count = 0
for com in set(partition.values()):
list_nodes = [nodes for nodes in partition.keys()
if partition[nodes] == com]
list_edges = []
for node in list_nodes:
for edge in G.edges():
if node==edge[0]:
for nod in list_nodes:
if nod==edge[1]:
continue
list_edges.append(edge)
continue
else:
if node==edge[1]:
for nod in list_nodes:
if nod==edge[0]:
continue
list_edges.append(edge)
continue
list_edges.append(edge)
G.remove_edges_from(list_edges)
print(G.edges())
#print(list_edges)
#nx.draw_networkx_nodes(G, pos, list_nodes, node_size=250,
# node_color=color[count], label=True)
#nx.draw_networkx_edges(G, pos, list_edges,with_labels=True, alpha=0.5)
#plt.show()
#plt.clf()
#nx.draw_networkx_nodes(G, pos, node_size=250, label=True)
#plt.show()
return G
def UpdateDeg(A, nodes):
deg_dict = {}
n = len(nodes)#图中点的个数
B = A.sum(axis = 1)#将矩阵的每一行向量相加,所得一个数组赋给B,表示与每个点相关的边数
for i in range(n):
deg_dict[list(nodes)[i]] = B[i,0]#将该值存到索引是i的元组中
return deg_dict
def GetModularity(G, deg_, m_):
New_A = nx.adj_matrix(G)#建立一个表示边的邻接矩阵
New_deg = {}
New_deg = UpdateDeg(New_A, G.nodes())
#计算Q值
print('Number of communities in decomposed G: %d' % nx.number_connected_components(G))
Mod = 0#设定社团划分的模块化系数并设初始值为0
for c in sorted(nx.connected_components(G), key=len, reverse=True):
AVW = 0 # 两条边在邻接矩阵中的值
K = 0 # 两条边的度值
for u in c:
AVW += New_deg[u]
K += deg_[u]
Mod += (float(AVW) - float(K * K) / float(2 * m_)) # 计算出Q值公式累加符号后的值
Mod = Mod/float(2*m_)#计算出模块化Q值
return Mod
def HandleCPM():
with open("E:\\Geolife\\Geolife Trajectories 1.3" + '\\CPMCommeet.csv', "r") as f:
lines = f.readlines()
# print(lines)
with open("E:\\Geolife\\Geolife Trajectories 1.3" + '\\CPMCommeet.csv', "w") as f_w:
for line in lines:
if "components_1" in line:
continue
f_w.write(line)
com = genfromtxt("E:\\Geolife\\Geolife Trajectories 1.3" + '\\CPMCommeet.csv', delimiter=',')
where_are_nan = np.isnan(com)
com[where_are_nan] = -1
print(com)
for a in com:
for b in a:
if b == -1:
continue
b = int(b)
print(com)
partition = {}
i = 0
for a in com:
for b in a:
if b==-1:
continue
partition[b] = i
i = i + 1
print(partition)
return partition
partition = HandleCPM()
graph_fn = 'E:\\Geolife\\Geolife Trajectories 1.3\\meetforGN.csv'
G = nx.Graph()
buildG(G, graph_fn, ',')
n = G.number_of_nodes() # 顶点数量
A = nx.adj_matrix(G) # 邻接矩阵
m_ = 0.0 # 计算边的数量
for i in range(0, n):
for j in range(0, n):
m_ += A[i, j]
m_ = m_ / 2.0
# 计算点的度
Orig_deg = {}
Orig_deg = UpdateDeg(A, G.nodes())
# 最初图
G = resultG(partition,G)
Q = GetModularity(G,Orig_deg,m_)
print(Q)
initG = nx.Graph()
buildG(initG, graph_fn, ',')
#plt.show()
showG(partition,initG)