def run_fit(seed, directed, n_components_range): # run left left_graph, labels = load_left() if not directed: left_graph = symmetrize(left_graph, method="avg") # run right right_graph, labels = load_right() if not directed: right_graph = symmetrize(right_graph, method="avg") outs = [] for n_components in n_components_range: ldt = LatentDistributionTest(n_components=n_components, n_bootstraps=500) ldt.fit(left_graph, right_graph) result = {} result["p-value"] = ldt.p_ result["sample-t"] = ldt.sample_T_statistic_ result["n_components"] = n_components outs.append(result) print(f"Done with {n_components}") out_df = pd.DataFrame(outs) save_obj(out_df, file_obs, "ldt_df") return 0
def run_fit(seed): np.random.seed(seed) # load left_graph, left_labels = load_left() right_graph, right_labels = load_right() # fit SBM left, predict right sbm_fit_left = SBMEstimator(directed=True, loops=False) sbm_fit_left.fit(left_graph, y=left_labels) right_pred_mse = mse_on_other(sbm_fit_left, right_graph, right_labels) right_pred_likelihood = likelihood_on_other(sbm_fit_left, right_graph, right_labels) right_pred_sc_likelihood = likelihood_on_other( sbm_fit_left, right_graph, right_labels, clip=1 / (right_graph.size - right_graph.shape[0]), ) right_pred_dict = { "n_params": sbm_fit_left._n_parameters(), "mse": right_pred_mse, "likelihood": right_pred_likelihood, "zc_likelihood": right_pred_likelihood, "sc_likelihood": right_pred_sc_likelihood, } right_pred_df = pd.DataFrame(right_pred_dict, index=[0]) print(right_pred_df) save_obj(right_pred_df, file_obs, "right_pred_sbm_df") # fit SBM right, predict left sbm_fit_right = SBMEstimator(directed=True, loops=False) sbm_fit_right.fit(right_graph, y=right_labels) left_pred_mse = mse_on_other(sbm_fit_right, left_graph, left_labels) left_pred_likelihood = likelihood_on_other(sbm_fit_right, left_graph, left_labels) left_pred_sc_likelihood = likelihood_on_other( sbm_fit_right, left_graph, left_labels, clip=1 / (left_graph.size - left_graph.shape[0]), ) left_pred_dict = { "n_params": sbm_fit_right._n_parameters(), "mse": left_pred_mse, "likelihood": left_pred_likelihood, "zc_likelihood": left_pred_likelihood, "sc_likelihood": left_pred_sc_likelihood, } left_pred_df = pd.DataFrame(left_pred_dict, index=[0]) print(left_pred_df) save_obj(left_pred_df, file_obs, "left_pred_sbm_df") # sbm_fit_right = SBMEstimator(directed=True, loops=False) # sbm_fit_right.fit(right_graph, y=right_labels) # right_b = sbm_fit_right.block_p_ # # save_obj(sbm_left_df, file_obs, "sbm_left_df") return 0
def run_fit(seed, directed): # run left graph, labels = load_left() print(labels) if not directed: graph = symmetrize(graph, method="avg") # fit SBM sbm = SBMEstimator(directed=True, loops=False) sbm_left_df = fit_a_priori(sbm, graph, labels) print(sbm_left_df["n_params"]) save_obj(sbm_left_df, file_obs, "sbm_left_df") # fit DCSBM dcsbm = DCSBMEstimator(directed=True, loops=False, degree_directed=False) dcsbm_left_df = fit_a_priori(dcsbm, graph, labels) save_obj(dcsbm_left_df, file_obs, "dcsbm_left_df") # fit dDCSBM ddcsbm = DCSBMEstimator(directed=True, loops=False, degree_directed=True) ddcsbm_left_df = fit_a_priori(ddcsbm, graph, labels) save_obj(ddcsbm_left_df, file_obs, "ddcsbm_left_df") # run right graph, labels = load_right() if not directed: graph = symmetrize(graph, method="avg") # fit SBM sbm = SBMEstimator(directed=True, loops=False) sbm_right_df = fit_a_priori(sbm, graph, labels) save_obj(sbm_right_df, file_obs, "sbm_right_df") # fit DCSBM dcsbm = DCSBMEstimator(directed=True, loops=False, degree_directed=False) dcsbm_right_df = fit_a_priori(dcsbm, graph, labels) save_obj(dcsbm_right_df, file_obs, "dcsbm_right_df") # fit dDCSBM ddcsbm = DCSBMEstimator(directed=True, loops=False, degree_directed=True) ddcsbm_right_df = fit_a_priori(ddcsbm, graph, labels) save_obj(ddcsbm_right_df, file_obs, "ddcsbm_right_df") return 0
def run_fit(seed, directed, n_components_range): # run left left_graph, labels = load_left() if not directed: left_graph = symmetrize(left_graph, method="avg") # run right right_graph, labels = load_right() if not directed: right_graph = symmetrize(right_graph, method="avg") def fit(n_components): # np.random.seed(seed) return fit_ldt(left_graph, right_graph, n_components) outs = Parallel(n_jobs=-2, verbose=5)(delayed(fit)(n) for n in n_components_range) out_df = pd.DataFrame(outs) save_obj(out_df, file_obs, "ldt_df") return 0
#%% from src.data import load_left, load_right from graspy.plot import heatmap from graspy.utils import binarize import matplotlib.pyplot as plt left_adj, left_labels = load_left() right_adj, right_labels = load_right() heatmap_kws = dict(cbar=False, font_scale=2) fig, ax = plt.subplots(1, 2, figsize=(18, 10)) heatmap(left_adj, inner_hier_labels=left_labels, ax=ax[0], title="Left", **heatmap_kws) heatmap(right_adj, inner_hier_labels=right_labels, ax=ax[1], title="Right", **heatmap_kws) plt.tight_layout() plt.savefig( "./maggot_models/reports/figures/raw_heatmaps/heatmaps.pdf", facecolor="w", format="pdf", )
param_name="param_n_components", score_name=score_name) #%% from pathlib import Path import matplotlib.pyplot as plt def save(name, fmt="pdf"): path = Path( "/Users/bpedigo/JHU_code/maggot_models/maggot_models/notebooks/outs") plt.savefig(path / str(name + "." + fmt), fmt=fmt, facecolor="w") plt.style.use("seaborn-white") right_graph, right_labels = load_right() np.random.seed(8888) n_init = 200 clip = 1 / (right_graph.size - right_graph.shape[0]) heatmap_kws = dict(vmin=0, vmax=1, font_scale=1.5, hier_label_fontsize=20, cbar=False) fig, ax = plt.subplots(4, 2, figsize=(15, 30)) # A priori SBM ap_estimator = SBMEstimator() ap_estimator.fit(right_graph, y=right_labels)