def fit_gm_exp(adj, alpha, beta=1, c=0, n_init=5, norm=False): gm = GraphMatch( n_init=n_init, init_method="rand", max_iter=80, eps=0.05, shuffle_input=True ) length = len(adj) match_mat = make_exp_match(length, alpha=alpha) if norm: match_mat = normalize_match(adj, match_mat) gm.fit(match_mat, adj) return gm.perm_inds_
def fit_gm_exp( adj, alpha, beta=1, c=0, n_init=5, norm=False, max_iter=50, eps=0.05, n_jobs=1, verbose=0, ): warnings.filterwarnings("ignore") gm = GraphMatch(n_init=1, init_method="rand", max_iter=max_iter, eps=eps, shuffle_input=True) match_mat = make_exp_match(adj, alpha=alpha, beta=beta, c=c, norm=norm) seeds = np.random.choice(int(1e8), size=n_init) def _fit(seed): np.random.seed(seed) gm.fit(match_mat, adj) return gm.perm_inds_, gm.score_ outs = Parallel(n_jobs=n_jobs, verbose=verbose)(delayed(_fit)(s) for s in seeds) outs = list(zip(*outs)) perms = np.array(outs[0]) scores = np.array(outs[1]) return perms, scores
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)
cbar=False, title="P unshuffled") for init_weight in [0, 0.25, 0.5, 0.75, 0.9, 0.95, "random"]: import matplotlib.transforms as transforms n_verts = A1.shape[0] all_positions = [] init_indicator = [] gm = GraphMatch( n_init=n_init, init="barycenter", init_weight=init_weight, max_iter=20, shuffle_input=False, eps=eps, ) gm.fit(A1, A2_shuffle) results = gm.results_ progress = gm.progress_ final_scores = progress.groupby("init_idx")["pseudoscore"].max() progress["final_score"] = progress["init_idx"].map(final_scores) progress["optimal"] = np.abs( (progress["final_score"] - max_score)) < 0.1 p_found = progress["optimal"].mean() row = {"p_found": p_found, "init_weight": init_weight} rows.append(row) init_weight_results = pd.DataFrame(rows)
n_verts = adj.shape[0] adjplot(adj, sort_class=labels, cbar=False, ax=axs[1], square=True) axs[1].set_title("Adjacency matrix", pad=25) plt.tight_layout() stashfig("sbm" + basename) # %% [markdown] # ## methods = [make_flat_match, make_linear_match, make_exp_match] names = ["Flat", "Linear", "Exp"] gm = GraphMatch( n_init=25, init_method="rand", max_iter=80, eps=0.05, shuffle_input=True ) alpha = 0.005 match_mats = [] permutations = [] for method, name in zip(methods, names): print(name) match_mat = method(len(adj), alpha=alpha) match_mat = normalize_match(adj, match_mat) match_mats.append(match_mat) gm.fit(match_mat, adj) permutations.append(gm.perm_inds_) # %% [markdown] # ## from src.hierarchy import signal_flow
def run_gmp(seed): np.random.seed(seed) sgm = GMP(n_init=1, init_method="rand") sgm.fit(A1, A2_shuffle) return sgm.score_, sgm.perm_inds_
# plot shuffled fig, axs = plt.subplots(1, 3, figsize=(10, 5)) heatmap(A1, ax=axs[0], cbar=False, title="Graph 1") heatmap(A2_shuffle, ax=axs[1], cbar=False, title="Graph 2 shuffled") heatmap(A1 - A2_shuffle, ax=axs[2], cbar=False, title="Diff (G1 - G2 shuffled)") n_init = 100 # parameter for GMP # run GMP in serial currtime = time.time() sgm = GMP(n_init=n_init, init_method="rand") sgm = sgm.fit(A1, A2_shuffle) A2_unshuffle = A2_shuffle[np.ix_(sgm.perm_inds_, sgm.perm_inds_)] fig, axs = plt.subplots(1, 3, figsize=(10, 5)) heatmap(A1, ax=axs[0], cbar=False, title="Graph 1") heatmap(A2_unshuffle, ax=axs[1], cbar=False, title="Graph 2 unshuffled") heatmap(A1 - A2_unshuffle, ax=axs[2], cbar=False, title="Diff (G1 - G2 unshuffled)") match_ratio = 1 - ( np.count_nonzero(abs(sgm.perm_inds_ - node_unshuffle_input)) / n_verts) print("Match Ratio (serial) ", match_ratio)
def run_gmp(seed): np.random.seed(seed) sgm = GraphMatch(n_init=1, init_method="rand", max_iter=100, eps=0.05) sgm.fit(match_mat, adj) return sgm.score_, sgm.perm_inds_