-
Notifications
You must be signed in to change notification settings - Fork 0
/
calNetwork.py
66 lines (49 loc) · 2.12 KB
/
calNetwork.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
# coding=utf-8
import networkx as nx
def cal_degree(m_graph):
""" 计算度 0x001"""
return m_graph.degree()
def cal_clustering(m_graph):
""" 计算聚类系数 根据源码,这里的的参数接收的图必须是undirected,不然就会报错 0x005"""
return nx.clustering(m_graph.to_undirected(), nx.nodes(m_graph))
def cal_degree_centrality(m_graph):
""" 计算度中心性 0x006"""
return nx.degree_centrality(m_graph)
def cal_betweness_centrality(m_graph):
""" 计算介数中心性0x007"""
return nx.betweenness_centrality(m_graph, None, True)
def cal_closeness_centrality(m_graph):
""" 计算接近度中心性0x008"""
return nx.closeness_centrality(m_graph)
def cal_eigenvector_centrality(m_graph):
""" 计算特征向量中心性0x009"""
return nx.eigenvector_centrality_numpy(m_graph)
def my_round(f):
"""保留三位并且科学计数表示"""
round_result = '{0:.2e}'.format(f)
if round_result == '{0:.2e}'.format(0):
round_result = 0
return round_result
def cal_characters_arguments(result):
"""计算传入的result的特征参数"""
graph = nx.DiGraph()
# 创建图
for link in result['links']:
graph.add_edge(link['source'], link['target'])
# 开始计算特征参数并且将结果保存到各个list
degree_list = cal_degree(graph)
degree_centrality_list = cal_degree_centrality(graph)
closeness_list = cal_closeness_centrality(graph)
betweness_list = cal_betweness_centrality(graph)
eigenvector_list = cal_eigenvector_centrality(graph)
clustering_list = cal_clustering(graph)
for node in result['nodes']:
node_id = node['id']
node['degree'] = str(degree_list[node_id])
node['degree_centrality'] = str(
my_round(degree_centrality_list[node_id]))
node['closeness_centrality'] = str(my_round(closeness_list[node_id]))
node['betweness_centrality'] = str(my_round(betweness_list[node_id]))
node['eigenvector_centrality'] = str(
my_round(eigenvector_list[node_id]))
node['clustering'] = str(my_round(clustering_list[node_id]))