-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_analysing.py
745 lines (642 loc) · 23.5 KB
/
data_analysing.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
"""
图数据分析
"""
import copy
from collections import Counter
from typing import Dict, List, Union
from functools import cached_property, cache
from itertools import combinations
import math
from pathlib import Path
from matplotlib import pyplot as plt
import numpy as np
import networkx as nx
from utils.toolbox import GraphTool
IMAGE_PATH = Path(__file__).parent / Path("images")
class GraphInfo:
"""
处理带节点属性有权无向图(无边属性)的最大连通子图【一般不考虑其边的权重】
"""
@staticmethod
def test():
G = nx.barabasi_albert_graph(10, 3)
info = GraphInfo(G)
assert all(
[
math.isclose(num1, num2)
for num1, num2 in zip(
info.clusteringCoefficient.values(),
info.clusteringCoefficientSubset(info.nodes).values(),
)
]
), "clusteringCoefficient failed..."
print("DONE!")
def __init__(self, originalGraph: nx.Graph):
self.originalGraph = originalGraph
# 提取原图中的最大连接子图
self.graph: nx.Graph = originalGraph.subgraph(
max(nx.connected_components(originalGraph), key=len)
)
@cached_property
def isConnected(self):
return nx.is_connected(self.originalGraph)
@cached_property
def numberOfNodes(self):
return self.graph.number_of_nodes()
@cached_property
def numberOfEdges(self):
return self.graph.number_of_edges()
@cached_property
def numberOfConnectedComponents(self):
return nx.number_connected_components(self.originalGraph)
def plot_graph(
self,
iterationsNum: int = 10,
path: Union[str, Path] = IMAGE_PATH / "graph.pdf",
save=False,
withEdge=True,
method: str = "DegreeCentralityMethod",
):
fig, ax = plt.subplots(figsize=(20, 12))
ax.axis("off")
if method == "DegreeCentralityMethod":
# NOTE: 由于最大连通子图中的节点度>1,因为取对数只要+1就可以
nodeSize = [np.log10(self.degree[i] + 1) * 10 for i in self.nodes]
nodeColor = [self.degree[i] for i in self.nodes]
elif method == "ClosenessCentralityMethod":
nodeSize = [v * 50 for v in self.closenessCentrality.values()]
nodeColor = [v for v in self.closenessCentrality.values()]
elif method == "BetweennessCentralityMethod":
nodeSize = [v * 50 for v in self.betweennessCentrality.values()]
nodeColor = [v for v in self.betweennessCentrality.values()]
elif method == "EigenvectorCentralityMethod":
nodeSize = [
np.sqrt(np.sqrt(v)) * 100 for v in self.eigenvectorCentrality.values()
]
nodeColor = [v for v in self.eigenvectorCentrality.values()]
else:
pass
options = {
"pos": nx.spring_layout(
self.unweightedGraph, iterations=iterationsNum, seed=0
),
"node_size": nodeSize,
"node_color": nodeColor,
"cmap": plt.cm.cool, # 设置节点colormap
"edge_color": "gray",
"with_labels": False,
"width": 0.15,
}
if withEdge:
options["edge_color"] = "gray"
options["with_labels"] = False
options["width"] = 0.15
nx.draw(self.unweightedGraph, ax=ax, **options)
else:
nx.draw_networkx_nodes(self.unweightedGraph, ax=ax, **options)
if save:
plt.savefig(path)
else:
plt.show()
plt.close()
@cached_property
def unweightedGraph(self):
U = nx.Graph()
U.add_edges_from(self.graph.edges()) # NOTE: 会自动忽略边属性
return U
@cached_property
def adjacencyMatrix(self):
return (
nx.adjacency_matrix(self.unweightedGraph, dtype=np.bool_)
.todense()
.astype(np.bool_)
)
@cached_property
def degree(self):
return dict(self.unweightedGraph.degree)
@cached_property
def nodes(self):
return list(self.unweightedGraph.nodes)
@cached_property
def maxDegree(self):
return max(self.degree.values())
@cached_property
def averageDegree(self):
return np.mean([d for _, d in self.degree])
@cached_property
def degreeDistribution(self):
return nx.degree_histogram(self.unweightedGraph)
def plot_degreeDistribution(
self,
path: Union[str, Path] = IMAGE_PATH / "degreeDistribution.pdf",
save=False,
bar=False,
):
fig, ax = plt.subplots(figsize=(20, 12))
x = list(range(self.maxDegree + 1))
y = [i / self.numberOfNodes for i in self.degreeDistribution]
if bar:
plt.bar(x, y)
else:
# NOTE: 去除零点
xCopy = copy.deepcopy(x)
yCopy = copy.deepcopy(y)
for ind in range(self.maxDegree + 1):
if self.degreeDistribution[ind] == 0:
# NOTE: 度为0的节点在连通图中不存在
if ind == 0:
pass
else:
xCopy[ind] = xCopy[ind - 1]
yCopy[ind] = yCopy[ind - 1]
x = xCopy
y = yCopy
ax.plot(x, y, "ro-")
ax.set_xscale("log")
ax.set_yscale("log")
xticks = [int(np.power(2, i)) for i in np.arange(np.log2(self.maxDegree))]
yticks = [
round(1 / 2 ** (-i), 4)
for i in np.linspace(
np.log2(np.min(np.array(y)[np.array(y) > 0])),
np.log2(np.max(y)),
10,
)
]
ax.set_xticks(xticks)
ax.set_yticks(yticks)
ax.set_xticklabels([str(ind) for ind in xticks])
ax.set_yticklabels([str(ind) for ind in yticks])
ax.set_title(
"度分布",
fontdict={
"fontname": "Songti SC",
"color": "darkred",
"weight": "bold",
"size": 30,
},
loc="center",
)
ax.set_xlabel("$k$")
ax.set_ylabel("$P(k)$")
if save:
plt.savefig(path)
else:
plt.show()
plt.close()
@cached_property
def shortestPathLengths(self):
return dict(nx.all_pairs_shortest_path_length(self.unweightedGraph))
def shortest_path_length(self, node1, node2):
return nx.shortest_path_length(self.unweightedGraph, node1, node2)
def shortest_path(self, node1, node2):
return nx.shortest_path(self.unweightedGraph, source=node1, target=node2)
def all_shortest_paths(self, node1, node2):
return list(
nx.all_shortest_paths(self.unweightedGraph, source=node1, target=node2)
)
@cached_property
def diameter(self) -> int:
return int(
max(
nx.eccentricity(
self.unweightedGraph, sp=self.shortestPathLengths
).values()
)
)
# return nx.diameter(G) NOTE: 效率低
@cached_property
def averageShortestPathLengths(self):
# NOTE: 这里考虑了到自身的距离,并不合理
averageShortestPathLengths = {
node: np.mean(list(spl.values()))
for node, spl in self.shortestPathLengths.items()
}
return averageShortestPathLengths
@cached_property
def averageDistance(self):
# NOTE: 相对图来说的平均距离
return np.mean(self.averageShortestPathLengths.values())
@cached_property
def shortestPathLengthDistribution(self):
distances = []
for node1, spl in self.shortestPathLengths.items():
for node2, distance in spl.items():
if node1 != node2:
distances.append(distance)
counter = Counter(distances)
distance_count = np.zeros(self.diameter + 1)
for key, value in counter.items():
distance_count[key] = 100.0 * value / sum(counter.values())
return distance_count
def plot_shortestPathLengthDistribution(
self,
path: Union[str, Path] = IMAGE_PATH / "shortestPathLengthDistribution.pdf",
save=False,
):
fig, ax = plt.subplots(figsize=(15, 8))
ax.bar(
np.arange(self.diameter + 1, dtype=np.int64),
height=self.shortestPathLengthDistribution,
)
ax.set_title(
"最短路径长度分布",
fontdict={
"fontname": "Songti SC",
"color": "darkred",
"weight": "bold",
"size": 30,
},
loc="center",
)
ax.set_xticks(np.arange(self.diameter + 1, dtype=np.int64))
ax.set_xlabel("Shortest Path Length", fontdict={"size": 22})
ax.set_ylabel("Frequency (%)", fontdict={"size": 22})
if save:
plt.savefig(path)
else:
plt.show()
plt.close()
def efficiency(self, node1, node2):
return nx.efficiency(self.unweightedGraph, node1, node2)
@cached_property
def localEfficiency(self):
return nx.local_efficiency(self.unweightedGraph)
@cached_property
def globalEfficiency(self):
return nx.global_efficiency(self.unweightedGraph)
@cached_property
def density(self):
return nx.density(self.unweightedGraph)
@cached_property
def clusteringCoefficient(self) -> Dict:
return nx.clustering(self.unweightedGraph)
def clusteringCoefficientSubset(self, nodeList) -> Dict:
clusteringCoefficient = {}
for node in nodeList:
neighborNodeList = self.all_neighbors(node)
if len(neighborNodeList) == 1:
clusteringCoefficient[node] = 0
else:
indexList = [self.nodes.index(node) for node in neighborNodeList]
coef = self.adjacencyMatrix[indexList][:, indexList].sum()
clusteringCoefficient[node] = coef / (
len(neighborNodeList) * (len(neighborNodeList) - 1)
)
return clusteringCoefficient
@cached_property
def averageClusteringCoefficient(self):
return np.mean(list(self.clusteringCoefficient.values()))
# return nx.average_clustering(self.unweightedGraph)
@cached_property
def globalClusteringCoefficient(self):
return nx.transitivity(self.unweightedGraph)
def plot_clusteringCoefficientHistogram(
self,
path: Union[str, Path] = IMAGE_PATH / "clusteringCoefficientHistogram.pdf",
save=False,
):
plt.figure(figsize=(15, 8))
plt.hist(self.clusteringCoefficient.values(), bins=20)
plt.title(
"集聚系数直方图",
fontdict={
"fontname": "Songti SC",
"color": "darkred",
"weight": "bold",
"size": 30,
},
loc="center",
)
plt.xlabel("Clustering Coefficient", fontdict={"size": 20})
plt.ylabel("Counts", fontdict={"size": 20})
if save:
plt.savefig(path)
else:
plt.show()
plt.close()
@cached_property
def degreeCentrality(self) -> Dict:
return nx.centrality.degree_centrality(self.unweightedGraph)
def plot_degreeCentralityHistogram(
self,
path: Union[str, Path] = IMAGE_PATH / "degreeCentralityHistogram.pdf",
save=False,
):
plt.figure(figsize=(15, 8))
plt.hist(self.degreeCentrality.values(), bins=25)
plt.title("度中心性直方图", fontdict={"size": 35}, loc="center")
plt.xlabel("Degree Centrality", fontdict={"size": 20})
plt.ylabel("Counts", fontdict={"size": 20})
if save:
plt.savefig(path)
else:
plt.show()
plt.close()
@cached_property
def closenessCentrality(self):
# NOTE: 也可以用节点到其他各点平均距离的倒数来计算
return nx.centrality.closeness_centrality(self.unweightedGraph)
def closenessCentralitySubset(self, nodeList: List):
resultDict = {
node: (self.numberOfNodes - 1)
/ sum(self.shortestPathLengths[node].values())
for node in nodeList
}
return resultDict
def plot_closenessCentralityHistogram(
self,
path: Union[str, Path] = IMAGE_PATH / "closenessCentralityHistogram.pdf",
save=False,
):
plt.figure(figsize=(15, 8))
plt.hist(self.closenessCentrality.values(), bins=20)
plt.title(
"接近度中心性直方图",
fontdict={
"fontname": "Songti SC",
"color": "darkred",
"weight": "bold",
"size": 30,
},
loc="center",
)
plt.xlabel("Closeness Centrality", fontdict={"size": 20})
plt.ylabel("Counts", fontdict={"size": 20})
if save:
plt.savefig(path)
else:
plt.show()
plt.close()
@cached_property
def betweennessCentrality(self):
return nx.centrality.betweenness_centrality(self.unweightedGraph)
# NOTE: 理论上正确,但计算效率实在太低
def betweennessCentralitySubsetBeta(self, nodeList: List):
resultDict = {node: 0 for node in nodeList}
allShortestPaths = {}
# 计算介数
node_combinations = combinations(self.nodes, 2)
for node in nodeList:
for node1, node2 in node_combinations:
if node1 == node or node2 == node:
continue
if f"{node1}-{node2}" not in allShortestPaths:
allShortestPaths[f"{node1}-{node2}"] = self.all_shortest_paths(
node1, node2
)
currentShortestPaths = allShortestPaths[f"{node1}-{node2}"]
resultDict[node] += sum(
1 for path in currentShortestPaths if node in path
) / len(currentShortestPaths)
# 计算介数中心性
resultDict = {
node: 2 * value / self.numberOfNodes / (self.numberOfNodes - 1)
for node, value in resultDict.items()
}
return resultDict
@cached_property
def edgeBetweennessCentrality(self):
return nx.edge_betweenness_centrality(self.unweightedGraph)
def plot_betweennessCentralityHistogram(
self,
path: Union[str, Path] = IMAGE_PATH / "betweennessCentralityHistogram.pdf",
save=False,
):
plt.figure(figsize=(15, 8))
plt.hist(self.betweennessCentrality.values(), bins=20)
plt.title(
"介数中心性直方图",
fontdict={
"fontname": "Songti SC",
"color": "darkred",
"weight": "bold",
"size": 30,
},
loc="center",
)
plt.xlabel("Betweenness Centrality", fontdict={"size": 20})
plt.ylabel("Counts", fontdict={"size": 20})
if save:
plt.savefig(path)
else:
plt.show()
plt.close()
@cached_property
def eigenvectorCentrality(self):
# NOTE: 根据特征向量中心性的计算方法,易得中心性大的节点总是与中心性大的节点相连
return nx.centrality.eigenvector_centrality(self.unweightedGraph)
def plot_eigenvectorCentralityHistogram(
self,
path: Union[str, Path] = IMAGE_PATH / "eigenvectorCentralityHistogram.pdf",
save=False,
):
plt.figure(figsize=(15, 8))
plt.hist(self.eigenvectorCentrality.values(), bins=20)
plt.title(
"特征向量中心性直方图",
fontdict={
"fontname": "Songti SC",
"color": "darkred",
"weight": "bold",
"size": 30,
},
loc="center",
)
plt.xlabel("Eigenvector Centrality", fontdict={"size": 20})
plt.ylabel("Counts", fontdict={"size": 20})
if save:
plt.savefig(path)
else:
plt.show()
plt.close()
@cached_property
def degreeAssortativityCoefficient(self):
return nx.degree_assortativity_coefficient(self.unweightedGraph)
@cached_property
def degreePearsonCorrelationCoefficient(self):
"""基于Pearson相关系数的度-度相关性"""
return nx.degree_pearson_correlation_coefficient(self.unweightedGraph)
@cache
def all_neighbors(self, node: str):
return list(nx.all_neighbors(self.unweightedGraph, node))
@cached_property
def averageNearestNeighborDegreeWithMatrix(self):
"""基于最近邻平均度值的度-度相关性(矩阵方法)"""
A = nx.to_numpy_array(
self.unweightedGraph, dtype=np.int64
) # A = nx.adjacency_matrix(self.unweightedGraph).todense()
k_array = np.array([self.degree[node] for node in self.nodes])
# k_array = A.sum(axis=1) # NOTE: 只有对角线无元素时,才是等价方法
sorted_k = sorted(set(k_array)) # 获取所有可能的度值
k_nn_i = A @ k_array / k_array
isK = np.zeros((self.numberOfNodes, len(sorted_k)))
for ind in range(len(sorted_k)):
x_index = k_array == sorted_k[ind]
isK[x_index, ind] = 1
Knn = k_nn_i @ isK / np.array([self.degreeDistribution[k] for k in sorted_k])
return sorted_k, Knn
@cached_property
def averageNearestNeighborDegree(self):
"""基于最近邻平均度值的度-度相关性"""
k = set([self.degree[i] for i in self.nodes]) # 获取所有可能的度值
sorted_k = sorted(k)
k_nn_k = []
for ki in sorted_k:
if ki == 0:
k_nn_k.append(0.0)
else:
c = 0
k_nn_i = 0
for i in self.nodes:
if self.degree[i] == ki:
k_nn_i += (
sum([self.degree[j] for j in self.all_neighbors(i)]) / ki
)
c += 1
k_nn_k.append(k_nn_i / c)
return sorted_k, k_nn_k
def plot_averageNearestNeighborDegree(
self, path: Union[str, Path] = IMAGE_PATH / "degreeCorrelation.pdf", save=False
):
fig, ax = plt.subplots(figsize=(20, 12))
sorted_k, Knn = self.averageNearestNeighborDegreeWithMatrix
ax.plot(sorted_k, Knn, color="red")
plt.title(
"基于最近邻平均度值的度-度相关性",
fontdict={
"fontname": "Songti SC",
"color": "darkred",
"weight": "bold",
"size": 30,
},
loc="center",
)
ax.set_xscale("log")
ax.set_yscale("log")
if save:
plt.savefig(path)
else:
plt.show()
plt.close()
@cached_property
def hasBridges(self):
return nx.has_bridges(self.unweightedGraph)
@cached_property
def bridges(self):
return list(nx.bridges(self.unweightedGraph))
@cached_property
def localBridges(self):
"""
The edges that are local bridges are saved in a list and their number is printed.
In detaill, an edge joining two nodes $C$ and $D$ in a graph is a local bridge, if its endpoints $C$ and $D$ have no friends in common.
"""
return list(
nx.local_bridges(self.unweightedGraph, with_span=False)
) # NOTE: span就是被桥连接的节点的距离
def plot_bridges(
self,
iterationsNum: int = 10,
path: Union[str, Path] = IMAGE_PATH / "bridges.pdf",
save=False,
):
fig, ax = plt.subplots(figsize=(15, 8))
options = {
"pos": nx.spring_layout(
self.unweightedGraph, iterations=iterationsNum, seed=0
),
"node_size": 1,
"width": 0.5,
}
nx.draw_networkx(
self.unweightedGraph,
ax=ax,
edge_color="gray",
node_color="blue",
with_labels=False,
**options,
)
nx.draw_networkx_edges(
self.unweightedGraph,
ax=ax,
edgelist=self.localBridges,
edge_color="green",
**options,
) # green color for local bridges
nx.draw_networkx_edges(
self.unweightedGraph,
ax=ax,
edgelist=self.bridges,
edge_color="r",
**options,
) # red color for bridges
plt.axis("off")
if save:
plt.savefig(path)
else:
plt.show()
plt.close()
@cached_property
def graphlet3(self):
return nx.triangles(self.unweightedGraph)
@cached_property
def sumOfGraphlet3(self):
return sum(list(self.graphlet3.values())) / 3
@cached_property
def averageOfGraphlet3(self):
return np.mean(list(self.graphlet3.values()))
@cached_property
def medianOfGraphlet3(self):
return np.median(list(self.graphlet3.values()))
@cached_property
def coreNumber(self):
return nx.core_number(self.unweightedGraph)
@cache
def page_rank(self, alpha: float = 0.85):
return nx.pagerank(self.unweightedGraph, alpha=alpha)
@cached_property
def spectrum(self):
# laplacian_spectrum
# adjacency_spectrum
return nx.linalg.spectrum.normalized_laplacian_spectrum(self.unweightedGraph)
@cached_property
def hits(self):
authority, hub = nx.hits(self.unweightedGraph, max_iter=100, normalized=True)
return authority
@cache
def trust_rank(self, alpha: float = 0.85):
return GraphTool.trust_rank(self.unweightedGraph, alpha=alpha)
def identify_important_nodes(self, ratio: float = 0.05):
def standardization(rankDict):
meanValue = np.mean(list(rankDict.values()))
stdValue = np.std(list(rankDict.values()))
return {k: (v - meanValue) / stdValue for k, v in rankDict.items()}
def get_top_x_percent_keys(dic: Dict, percent: float = 0.01) -> List:
# 按照字典的值降序排序
sorted_items = sorted(dic.items(), key=lambda x: x[1], reverse=True)
# 获取前{percent}的key
top_x_percent = int(len(sorted_items) * percent)
top_items = sorted_items[:top_x_percent]
keys = [item[0] for item in top_items]
return keys
resultDict = {node: 0 for node in self.nodes}
clusteringCoefficientDict = self.clusteringCoefficient
closenessCentralityDict = self.closenessCentrality
coreNumberDict = self.coreNumber
hitsDict = self.hits
# 归一化[标准化]并累加
for rankDict in [
clusteringCoefficientDict,
closenessCentralityDict,
coreNumberDict,
hitsDict,
]:
rankDict = standardization(rankDict)
for key in resultDict:
resultDict[key] += rankDict[key]
# 以{ratio*10}%为seeds作trustrank
seeds = get_top_x_percent_keys(rankDict, ratio)
resultDict = GraphTool.trust_rank(self.unweightedGraph, seeds=seeds)
return resultDict
if __name__ == "__main__":
GraphInfo.test()