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", )
#%% from src.data import load_left from graspy.models import DCSBMEstimator graph, labels = load_left() dcsbm = DCSBMEstimator(directed=True, loops=False, degree_directed=True) dcsbm.fit(graph, y=labels) dcsbm.mse(graph) #%% from src.models import GridSearchUS from src.models import select_rdpg n_init = 3 n_components_try = range(1, 5) param_grid = dict(n_components=n_components_try) select_rdpg(graph, param_grid) #%% from graspy.utils import cartprod import numpy as np s = range(20, 25) f = np.random.uniform(size=5) out = cartprod(s, f) from itertools import product out = product(s, f) for i, j in product(s, f): print(i)