[sym_adj], transform="binarize", height=20, sizes=(10, 20), inner_hier_labels=side_labels, sort_nodes=False, palette="deep", legend=False, ) stashfig("sym-adj") mg = MetaGraph(sym_adj, mg.meta) # %% [markdown] # # Preprocess graph n_verts = mg.n_verts mg.make_lcc() print(f"Removed {n_verts - mg.n_verts} when finding the LCC") # old_n_verts = sym_adj.shape[0] # sym_adj, class_labels, side_labels = preprocess_graph( # sym_adj, class_labels, side_labels # ) # n_verts = sym_adj.shape[0] # print(f"Removed {old_n_verts - n_verts} nodes") # %% [markdown] # # Embedding n_verts = mg.n_verts sym_adj = mg.adj side_labels = mg["Hemisphere"] class_labels = mg["Merge Class"] latent, laplacian = lse(sym_adj, N_COMPONENTS, regularizer=None, ptr=PTR)
threshs = [0.01, 0.02] else: threshs = [1, 2, 3] for thresh in threshs: for r in [0.3, 0.5, 0.7, 1]: mg = load_metagraph(graph_type, version=BRAIN_VERSION) edgelist = mg.to_edgelist() edgelist = edgelist[edgelist["weight"] > thresh] nodelist = list(mg.g.nodes()) thresh_g = nx.from_pandas_edgelist(edgelist, edge_attr=True, create_using=nx.DiGraph) nx.set_node_attributes(thresh_g, mg.meta.to_dict(orient="index")) mg = MetaGraph(thresh_g) print(len(mg.meta)) mg = mg.make_lcc() print(len(mg.meta)) not_pdiff = np.where(~mg["is_pdiff"])[0] mg = mg.reindex(not_pdiff) print(len(mg.meta)) g_sym = nx.to_undirected(mg.g) skeleton_labels = np.array(list(g_sym.nodes())) out_dict = cm.best_partition(g_sym, resolution=r) partition = np.array(itemgetter(*skeleton_labels)(out_dict)) adj = nx.to_numpy_array(g_sym, nodelist=skeleton_labels) part_unique, part_count = np.unique(partition, return_counts=True) for uni, count in zip(part_unique, part_count): if count < 3: inds = np.where(partition == uni)[0] partition[inds] = -1
# sym_contra_adj = (left_right_adj + right_left_adj) / 2 # max sym_ipsi_adj = np.maximum(left_left_adj, right_right_adj) sym_contra_adj = np.maximum(left_right_adj, right_left_adj) sym_adj = adj.copy() sym_adj[:n_pairs, :n_pairs] = sym_ipsi_adj sym_adj[n_pairs : 2 * n_pairs, n_pairs : 2 * n_pairs] = sym_ipsi_adj sym_adj[:n_pairs, n_pairs : 2 * n_pairs] = sym_contra_adj sym_adj[n_pairs : 2 * n_pairs, :n_pairs] = sym_contra_adj full_sym_mg = MetaGraph(sym_adj, mg.meta) n_verts = full_sym_mg.n_verts full_sym_mg.make_lcc() print(f"Removed {n_verts - full_sym_mg.n_verts} when finding the LCC") adj = full_sym_mg.adj side_labels = full_sym_mg["Hemisphere"] class_labels = full_sym_mg["Merge Class"] latent, laplacian = lse(adj, N_COMPONENTS, regularizer=None, ptr=PTR) latent_dim = latent.shape[1] // 2 screeplot( laplacian, title=f"Laplacian scree plot, R-DAD (ZG2 = {latent_dim} + {latent_dim})" ) quick_gridmap(adj, side_labels) stashfig(preface + "adj") n_components = latent.shape[1] for dim1 in range(n_components):