4
4
from random import choice
5
5
import networkx as nx
6
6
7
- def within_community_degree (weighted_partition , inf = 0.0 , catch_edgeless_node = True ):
8
- '''
9
- Computes the "within-module degree" (z-score) for each node (Guimera et al. 2005)
7
+ def within_community_degree (weighted_partition , nan = 0.0 , catch_edgeless_node = True ):
8
+ ''' Computes "within-module degree" (z-score) for each node (Guimera 2007, J Stat Mech)
10
9
11
10
------
12
- Inputs
11
+ Parameters
13
12
------
14
- partition = dictionary from modularity partition of graph using Louvain method
13
+ weighted_partition = Louvain Weighted Partition
14
+ nan = number to replace unexpected values (e.g., -infinity) with
15
+ catch_edgeless_node = raise ValueError if node degree is zero
15
16
16
17
------
17
- Output
18
+ Returns
18
19
------
19
- Dictionary of the within-module degree of each node.
20
+ Dictionary of the within community degree of each node.
20
21
21
22
'''
22
23
wc_dict = {}
23
24
for c , community in enumerate (weighted_partition .communities ):
24
25
community_degrees = []
25
- # community = list(community)
26
26
for node in community : #get average within-community-degree
27
- community_degrees .append (weighted_partition .node_degree_by_community (node )[c ])
28
- for node in community :
29
- within_community_degree = weighted_partition .node_degree_by_community (node )[c ]
30
- if within_community_degree > 3 or within_community_degree < - 3 :
31
- wc_dict [node ] = inf
32
- continue
33
- if node_degree == 0.0 :
27
+ node_degree = weighted_partition .node_degree (node )
28
+ if node_degree == 0.0 : #catch edgeless nodes
34
29
if catch_edgeless_node :
35
30
raise ValueError ("Node {} is edgeless" .format (node ))
36
31
wc_dict [node ] = 0.0
37
- continue
32
+ continue
33
+ community_degrees .append (weighted_partition .node_degree_by_community (node )[c ])
34
+ for node in community : #get node's within_community-degree z-score
35
+ within_community_degree = weighted_partition .node_degree_by_community (node )[c ]
38
36
std = np .std (community_degrees ) # std of community's degrees
37
+ if std == 0.0 : #so we don't divide by 0
38
+ wc_dict [node ] = (within_community_degree - mean ) #z_score
39
+ continue
39
40
mean = np .mean (community_degrees ) # mean of community's degrees
40
- wc_dict [node ] = (within_community_degree - mean / std ) #zscore
41
+ wc_dict [node ] = (within_community_degree - mean / std ) #z_score
41
42
return wc_dict
42
43
43
- def participation_coefficient (weighted_partition , catch_edgeless_node = True , nan = 0.0 ):
44
+ def participation_coefficient (weighted_partition , catch_edgeless_node = True ):
44
45
'''
45
- Computes the participation coefficient for each node (Guimera et al. 2005).
46
+ Computes the participation coefficient for each node (Guimera 2007, J Stat Mech)
46
47
47
48
------
48
- Inputs
49
+ Parameters
49
50
------
50
- partition = modularity partition of graph
51
+ weighted_partition = Louvain Weighted Partition
52
+ catch_edgeless_node = raise ValueError if node degree is zero
51
53
52
54
------
53
- Output
55
+ Returns
54
56
------
55
57
Dictionary of the participation coefficient for each node.
56
58
@@ -65,12 +67,8 @@ def participation_coefficient(weighted_partition, catch_edgeless_node=True, nan=
65
67
pc_dict [node ] = 0.0
66
68
continue
67
69
deg_per_comm = weighted_partition .node_degree_by_community (node )
68
- node_comm = weighted_partition .get_node_community (node )
69
- deg_per_comm .pop (node_comm )
70
+ deg_per_comm .pop (weighted_partition .get_node_community (node ))
70
71
bc_degree = sum (deg_per_comm ) #between community degree
71
- if bc_degree == np .nan ():
72
- pc_dict [node ] == nan
73
- continue
74
72
if bc_degree == 0.0 :
75
73
pc_dict [node ] = 0.0
76
74
continue
0 commit comments