def run_sim(seed): A1, A2 = sbm_corr(block_members, block_probs, rho, directed=directed, loops=loops) node_shuffle_input = np.random.permutation(n) A2_shuffle = A2[np.ix_(node_shuffle_input, node_shuffle_input)] node_unshuffle_input = np.array(range(n)) node_unshuffle_input[node_shuffle_input] = np.array(range(n)) W1 = np.sort(random.sample(list(range(n)), i)) W1 = W1.astype(int) W2 = np.array(node_unshuffle_input[W1]) faq = GMP(gmp=True) faq = faq.fit(A1, A2_shuffle, W1, W2) return match_ratio(faq.perm_inds_, node_unshuffle_input)
def run_sim(seed): A1, A2 = sbm_corr( block_members, block_probs, rho, directed=directed, loops=loops ) score = 0 res_opt = None score_ss = 0 res_opt_ss = None for j in range(t): res = quadratic_assignment_sim(A1,A2, sim=False, maximize=True, options={'seed':seed[j]}) if res['score']>score: res_opt = res score = res['score'] res = quadratic_assignment_sim(A1,A2, sim=True, maximize=True, options={'seed':seed[j]}) if res['score']>score_ss: res_opt_ss = res score_ss = res['score'] ratio = match_ratio(res_opt['col_ind'], n) score = res_opt['score'] ratio_ss = match_ratio(res_opt_ss['col_ind'], n) score_ss = res_opt_ss['score'] res = quadratic_assignment_sim(A1,A2, sim=False, maximize=True, options={'shuffle_input':False}) ratio_opt = match_ratio(res['col_ind'], n) score_opt = res['score'] res = quadratic_assignment_sim(A1,A2, sim=True, maximize=True, options={'shuffle_input':False}) ratio_opt_ss = match_ratio(res['col_ind'], n) score_opt_ss = res['score'] return ratio, score, ratio_ss, score_ss, ratio_opt, score_opt, ratio_opt_ss, score_opt_ss
# ## show_adjs = False B = np.array([[0.3, 0.7], [0.05, 0.3]]) rho = 0.9 n_per_block = 10 n_blocks = len(B) comm = n_blocks * [n_per_block] n_init = 50 eps = 1.0 n_samples = 20 rows = [] for i in range(n_samples): A1, A2 = sbm_corr(comm, B, rho) max_score = np.trace(A1 @ A2.T) shuffle_inds = np.random.choice(len(A1), replace=False, size=len(A1)) A2_shuffle = A2[np.ix_(shuffle_inds, shuffle_inds)] if show_adjs: fig, axs = plt.subplots(1, 4, figsize=(10, 5)) heatmap(A1, ax=axs[0], cbar=False, title="Graph 1") heatmap(A2, ax=axs[1], cbar=False, title="Graph 2") heatmap(A1 - A2, ax=axs[2], cbar=False, title="Diff (G1 - G2)") heatmap(A2_shuffle, ax=axs[3], cbar=False, title="Graph 2 shuffled") P = np.zeros_like(A1) P[np.arange(len(P)), shuffle_inds] = 1 fig, axs = plt.subplots(1, 4, figsize=(20, 5))
block_probs = np.array([[0.7, 0.3, 0.4], [0.3, 0.7, 0.3], [0.4, 0.3, 0.7]]) # plot block probs fig, ax = plt.subplots(1, 1, figsize=(4, 4)) sns.heatmap(block_probs, cbar=False, annot=True, square=True, cmap="Reds", ax=ax) ax.set_title("SBM block probabilities") # generate graphs A1, A2 = sbm_corr(block_members, block_probs, rho, directed=directed, loops=loops) fig, axs = plt.subplots(1, 3, figsize=(10, 5)) heatmap(A1, ax=axs[0], cbar=False, title="Graph 1") heatmap(A2, ax=axs[1], cbar=False, title="Graph 2") heatmap(A1 - A2, ax=axs[2], cbar=False, title="Diff (G1 - G2)") # shuffle for testing node_shuffle_input = np.random.permutation(n_verts) A2_shuffle = A2[np.ix_(node_shuffle_input, node_shuffle_input)] node_unshuffle_input = np.array(range(n_verts)) node_unshuffle_input[node_shuffle_input] = np.array(range(n_verts)) # plot shuffled fig, axs = plt.subplots(1, 3, figsize=(10, 5))
def run_sim(seed): A1, A2 = sbm_corr(block_members, block_probs, rho, directed=directed, loops=loops) score = 0 res_opt = None score_ss = 0 res_opt_ss = None ase = AdjacencySpectralEmbed(n_components=3, algorithm='truncated') Xhat1 = ase.fit_transform(A1) Xhat2 = ase.fit_transform(A2) if flip == 'median': xhh1, xhh2 = _median_sign_flips(Xhat1, Xhat2) S = xhh1 @ xhh2.T elif flip == 'jagt': sp = SeedlessProcrustes().fit(Xhat1, Xhat2) xhh1 = Xhat1 @ sp.Q xhh2 = Xhat2 S = xhh1 @ xhh2.T else: S = None for j in range(t): res = quadratic_assignment_sim(A1, A2, True, options={'seed': seed[j]}) if res['score'] > score: res_opt = res score = res['score'] res = quadratic_assignment_sim(A1, A2, True, S, options={'seed': seed[j]}) if res['score'] > score_ss: res_opt_ss = res score_ss = res['score'] ratio = match_ratio(res_opt['col_ind'], n) score = res_opt['score'] ratio_ss = match_ratio(res_opt_ss['col_ind'], n) score_ss = res_opt_ss['score'] res = quadratic_assignment_sim(A1, A2, True, options={'shuffle_input': False}) ratio_opt = match_ratio(res['col_ind'], n) score_opt = res['score'] res = quadratic_assignment_sim(A1, A2, True, S, options={'shuffle_input': False}) ratio_opt_ss = match_ratio(res['col_ind'], n) score_opt_ss = res['score'] return ratio, score, ratio_ss, score_ss, ratio_opt, score_opt, ratio_opt_ss, score_opt_ss
def run_sim(seed): A1, A2 = sbm_corr(block_members, block_probs, rho, directed=directed, loops=loops) score_10 = 0 res_10_opt = None score_3 = 0 res_3_opt = None ase3 = AdjacencySpectralEmbed(n_components=3, algorithm='truncated') Xhat31 = ase3.fit_transform(A1) Xhat32 = ase3.fit_transform(A2) ase10 = AdjacencySpectralEmbed(n_components=10, algorithm='truncated') Xhat101 = ase10.fit_transform(A1) Xhat102 = ase10.fit_transform(A2) if flip == 'median': xhh31, xhh32 = _median_sign_flips(Xhat31, Xhat32) xhh101, xhh102 = _median_sign_flips(Xhat101, Xhat102) elif flip == 'jagt': sp3 = SeedlessProcrustes(init='sign_flips').fit(Xhat31, Xhat32) xhh31 = Xhat31 @ sp3.Q_ xhh32 = Xhat32 sp10 = SeedlessProcrustes(init='sign_flips').fit( Xhat101, Xhat102) xhh101 = Xhat101 @ sp10.Q_ xhh102 = Xhat102 else: S = None if sim == 'mult': S3 = xhh31 @ xhh32.T S10 = xhh101 @ xhh102.T elif sim == 'pdist': S3 = pdist_youngser(xhh31, xhh32) S10 = pdist_youngser(xhh101, xhh102) for j in range(t): res = quadratic_assignment_sim(A1, A2, True, S3, options={'seed': seed[j]}) if res['score'] > score_3: res_3_opt = res score_3 = res['score'] res = quadratic_assignment_sim(A1, A2, True, S10, options={'seed': seed[j]}) if res['score'] > score_10: res_10_opt = res score_10 = res['score'] ratio_3 = match_ratio(res_3_opt['col_ind'], n) score_3 = res_3_opt['score'] ratio_10 = match_ratio(res_10_opt['col_ind'], n) score_10 = res_10_opt['score'] return ratio_3, score_3, ratio_10, score_10