Skip to content

Commit d66cf37

Browse files
author
mb3152
committed
chaged import on weighted_modularity, cleaned up nodal_roles
1 parent 764a973 commit d66cf37

File tree

1 file changed

+38
-93
lines changed

1 file changed

+38
-93
lines changed

brainx/nodal_roles.py

Lines changed: 38 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
from random import choice
55
import networkx as nx
66

7-
def within_module_degree(graph, partition, weighted = False):
7+
def within_community_degree(weighted_partition):
88
'''
9-
Computes the within-module degree for each node (Guimera et al. 2005)
9+
Computes the "within-module degree" (z-score) for each node (Guimera et al. 2005)
1010
1111
------
1212
Inputs
1313
------
14-
graph = Networkx Graph, unweighted, undirected.
1514
partition = dictionary from modularity partition of graph using Louvain method
1615
1716
------
@@ -20,48 +19,33 @@ def within_module_degree(graph, partition, weighted = False):
2019
Dictionary of the within-module degree of each node.
2120
2221
'''
23-
new_part = {}
24-
for m,n in zip(partition.values(),partition.keys()):
25-
try:
26-
new_part[m].append(n)
27-
except KeyError:
28-
new_part[m] = [n]
29-
partition = new_part
30-
wd_dict = {}
31-
32-
#loop through each module, look at nodes in modules
33-
for m in partition.keys():
34-
mod_list = partition[m]
35-
mod_wd_dict = {}
36-
#get within module degree of each node
37-
for source in mod_list:
38-
count = 0
39-
for target in mod_list:
40-
if (source,target) in graph.edges() or (target,source) in graph.edges():
41-
if weighted == True:
42-
count += graph.get_edge_data(source,target)['weight']
43-
count += graph.get_edge_data(target,source)['weight'] # i assume this will only get one weighted edge.
44-
else:
45-
count += 1
46-
mod_wd_dict[source] = count
22+
all_community_degrees = {}
23+
wc_dict = {}
24+
for node in weighted_partition.graph:
25+
node_community = weighted_partition.get_node_community(node)
26+
within_community_degree = weighted_partition.degree_within_community(node)
27+
try: # to load average within module degree of community
28+
all_community_degree = all_community_degrees[node_community]
29+
except: # collect within module degree of community
30+
all_community_degree = []
31+
for node in node_community:
32+
partition.degree_within_community(node)
33+
all_community_degree.append()
34+
all_community_degrees[node_community] = all_community_degree
35+
std = np.std(all_community_degree) # std of community's degrees
36+
mean = np.mean(all_community_degree) # mean of community's degrees
4737
# z-score
48-
all_mod_wd = mod_wd_dict.values()
49-
avg_mod_wd = float(sum(all_mod_wd) / len(all_mod_wd))
50-
std = np.std(all_mod_wd)
51-
#add to dictionary
52-
for source in mod_list:
53-
wd_dict[source] = (mod_wd_dict[source] - avg_mod_wd) / std
54-
return wd_dict
38+
wc_dict[node] = (within_community_degree - mean / std)
39+
return wc_dict
5540

5641

57-
def participation_coefficient(graph, partition, weighted = False):
42+
def participation_coefficient(weighted_partition, catch_edgeless_node=True):
5843
'''
5944
Computes the participation coefficient for each node (Guimera et al. 2005).
6045
6146
------
6247
Inputs
6348
------
64-
graph = Networkx graph
6549
partition = modularity partition of graph
6650
6751
------
@@ -70,61 +54,22 @@ def participation_coefficient(graph, partition, weighted = False):
7054
Dictionary of the participation coefficient for each node.
7155
7256
'''
73-
#this is because the dictionary output of Louvain is "backwards"
74-
new_part = {}
75-
for m,n in zip(partition.values(),partition.keys()):
76-
try:
77-
new_part[m].append(n)
78-
except KeyError:
79-
new_part[m] = [n]
80-
partition = new_part
8157
pc_dict = {}
82-
all_nodes = set(graph.nodes())
83-
# loop through modules
84-
if weighted == False:
85-
for m in partition.keys():
86-
#set of nodes in modules
87-
mod_list = set(partition[m])
88-
#set of nodes outside that module
89-
between_mod_list = list(set.difference(all_nodes, mod_list))
90-
for source in mod_list:
91-
#degree of node
92-
degree = float(nx.degree(G=graph, nbunch=source))
93-
count = 0
94-
# between module degree
95-
for target in between_mod_list:
96-
if (source,target) in graph.edges() or(source,target) in graph.edges():
97-
count += 1
98-
bm_degree = float(count)
99-
if bm_degree == 0.0:
100-
pc = 0.0
101-
else:
102-
pc = 1 - ((float(bm_degree) / float(degree))**2)
103-
pc_dict[source] = pc
104-
return pc_dict
105-
#this is because the dictionary output of Louvain is "backwards"
106-
if weighted == True:
107-
for m in partition.keys():
108-
#set of nodes in modules
109-
mod_list = set(partition[m])
110-
#set of nodes outside that module
111-
between_mod_list = list(set.difference(all_nodes, mod_list))
112-
for source in mod_list:
113-
#degree of node
114-
degree = 0
115-
edges = G.edges([source],data=True)
116-
for edge in edges:
117-
degree += graph.get_edge_data(edge[0],edge[1])['weight']
118-
count = 0
119-
# between module degree
120-
for target in between_mod_list:
121-
if (source,target) in graph.edges() or(source,target) in graph.edges():
122-
count += graph.get_edge_data(source,target)['weight']
123-
count += graph.get_edge_data(target,source)['weight'] # i assume this will only get one weighted edge.
124-
bm_degree = float(count)
125-
if bm_degree == 0.0:
126-
pc = 0.0
127-
else:
128-
pc = 1 - ((float(bm_degree) / float(degree))**2)
129-
pc_dict[source] = pc
130-
return pc_dict
58+
graph = weighted_partition.graph
59+
for node in graph:
60+
node_degree = weighted_partition.node_degree(node)
61+
if node_degree == 0.0:
62+
if catch_edgeless_node:
63+
raise ValueError("Node {} is edgeless".format(node))
64+
pc_dict[node] = 0.0
65+
continue
66+
deg_per_comm = weighted_partition.node_degree_by_community(node)
67+
node_comm = weighted_partition.get_node_community(node)
68+
deg_per_comm.pop(node_comm)
69+
bc_degree = sum(deg_per_comm) #between community degree
70+
if bc_degree == 0.0:
71+
pc_dict[node] = 0.0
72+
continue
73+
pc = 1 - ((float(bc_degree) / float(node_degree))**2)
74+
pc_dict[node] = pc
75+
return pc_dict

0 commit comments

Comments
 (0)