Skip to content

Commit bef5fbf

Browse files
author
mb3152
committed
nodal roles done
1 parent ef94399 commit bef5fbf

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

brainx/nodal_roles.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,55 @@
44
from random import choice
55
import networkx as nx
66

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)
109
1110
------
12-
Inputs
11+
Parameters
1312
------
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
1516
1617
------
17-
Output
18+
Returns
1819
------
19-
Dictionary of the within-module degree of each node.
20+
Dictionary of the within community degree of each node.
2021
2122
'''
2223
wc_dict = {}
2324
for c, community in enumerate(weighted_partition.communities):
2425
community_degrees = []
25-
# community = list(community)
2626
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
3429
if catch_edgeless_node:
3530
raise ValueError("Node {} is edgeless".format(node))
3631
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]
3836
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
3940
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
4142
return wc_dict
4243

43-
def participation_coefficient(weighted_partition, catch_edgeless_node=True, nan=0.0):
44+
def participation_coefficient(weighted_partition, catch_edgeless_node=True):
4445
'''
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)
4647
4748
------
48-
Inputs
49+
Parameters
4950
------
50-
partition = modularity partition of graph
51+
weighted_partition = Louvain Weighted Partition
52+
catch_edgeless_node = raise ValueError if node degree is zero
5153
5254
------
53-
Output
55+
Returns
5456
------
5557
Dictionary of the participation coefficient for each node.
5658
@@ -65,12 +67,8 @@ def participation_coefficient(weighted_partition, catch_edgeless_node=True, nan=
6567
pc_dict[node] = 0.0
6668
continue
6769
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))
7071
bc_degree = sum(deg_per_comm) #between community degree
71-
if bc_degree == np.nan():
72-
pc_dict[node] == nan
73-
continue
7472
if bc_degree == 0.0:
7573
pc_dict[node] = 0.0
7674
continue

0 commit comments

Comments
 (0)