-
Notifications
You must be signed in to change notification settings - Fork 4
/
sbmAmiModularity.py
163 lines (124 loc) · 4.02 KB
/
sbmAmiModularity.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
import numpy as np
import pandas as pd
import seaborn as sns
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib
import time
import ot
from scipy import linalg
from scipy import sparse
import gromovWassersteinAveraging as gwa
import spectralGW as sgw
from geodesicVisualization import *
from GromovWassersteinFramework import *
import json
from sklearn import manifold
from sklearn.model_selection import train_test_split
# Load the S-GWL code
import DataIO as DataIO
import EvaluationMeasure as Eval
import GromovWassersteinGraphToolkit as GwGt
import pickle
import warnings
from graphProcessing import load_graph
# Load modules for network partitioning experiments
import community
from networkx.algorithms.community import greedy_modularity_communities
from networkx.algorithms.community.asyn_fluid import asyn_fluidc
from networkx.algorithms.community.quality import modularity
from sklearn import metrics
from infomap import Infomap
warnings.filterwarnings("ignore")
def get_sbm(ns,ps):
# convert ps from 1d to 2d array
n = len(ns)
if n*(n+1)/2 != len(ps):
print('Error: check size of ps')
return None
else:
R,C = np.triu_indices(n)
pm = np.zeros((n,n))
pm[R,C] = ps
pm[C,R] = ps
G = nx.stochastic_block_model(ns, pm)
gt = []
for i in range(len(ns)):
for j in range(ns[i]):
gt.append(i)
return G,gt,pm
def get_gw_ami(G,t,gt):
# G -- graph
# t -- heat kernel scale parameter
# gt -- ground truth
distribution_exponent_hk = 0.001
distribution_offset_hk = 0
C1 = sgw.undirected_normalized_heat_kernel(G,t)
p1 = sgw.node_distribution(G,distribution_offset_hk,distribution_exponent_hk)
p2 = np.ravel(GwGt.estimate_target_distribution({0: p1.reshape(-1,1)}, dim_t=len(np.unique(gt))))
# Note that we are inserting prior information about the number of clusters
C2 = np.diag(p2)
coup, log = ot.gromov.gromov_wasserstein(C1, C2, p1, p2, loss_fun = 'square_loss', log = True)
est_idx = np.argmax(coup, axis=1)
ami = metrics.adjusted_mutual_info_score(est_idx,gt,average_method='max')
comms = [set() for v in np.unique(est_idx)]
for idx,val in enumerate(est_idx):
comms[val].add(idx)
mod = modularity(G,comms)
return ami,mod
## Construct sequence of SBMs that are increasingly indistinguishable
ns = [75,75]
ps = [0.5,0.35,0.5]
ts = np.linspace(0,20,20)
amis = []
mods = []
pvals = []
tvals = []
iterate = []
As = []
p_range = np.arange(0.15,0.35,0.02)
# Calculate modularity and AMI
for iteration in range(10):
for p in p_range:
p_copy = ps.copy()
p_copy[1] = p
G,gt,pm = get_sbm(ns,p_copy)
A = nx.adjacency_matrix(G).toarray()
if iteration==0:
As.append(A)
for t in ts:
ami, mod = get_gw_ami(G,t,gt)
amis.append(ami)
mods.append(mod)
tvals.append(t)
pvals.append(p)
iterate.append(iteration)
sbm_df = pd.DataFrame()
sbm_df['t'] = tvals
sbm_df['off-diag-p'] = pvals
sbm_df['AMI'] = amis
sbm_df['Modularity'] = mods
sbm_df['iteration'] = iterate
fig,axs = plt.subplots(2,5)
axs = axs.flatten()
for i in range(len(As)):
ax = axs[i]
ax.imshow(As[i])
fig.suptitle('SBMs with increasing cross-block edge densities')
fig.tight_layout()
fig.savefig('res_sbmAmiModularity_blocks.png',bbox_inches='tight',dpi=150)
melted = pd.melt(sbm_df,['off-diag-p','t','iteration'])
f = sns.FacetGrid(melted,col = 'off-diag-p',col_wrap=5,margin_titles=True)
fg = plt.gcf()
fg.dpi = 50
f.map_dataframe(sns.lineplot, x='t', y='value',hue='variable')
f.set_axis_labels("t", "value")
f.add_legend()
cn = [round(v,2) for v in f.col_names]
fg = plt.gcf()
fg.suptitle('AMI and Modularity peaks across scales')
axes = f.axes.flatten()
for i,val in enumerate(cn):
axes[i].set_title("cross-block edge density = %2.2f" % cn[i])
fg.savefig('res_sbmAmiModularity.png',bbox_inches='tight',dpi=300)
plt.show()