def embed_lse(*, adj, n_components=None): """ JHU LaplacianSpectralEmbed, w/ default settings """ X_lse = LaplacianSpectralEmbed(n_components=n_components).fit_transform(adj.toarray()) if isinstance(X_lse, tuple): X_lse = np.column_stack(X_lse) return X_lse
def _embed(self, adj=None): if adj is None: adj = self.adj # TODO look into PTR at this level as well # lp_inds, rp_inds = get_paired_inds(self.meta) lp_inds = self.left_pair_inds rp_inds = self.right_pair_inds embed_adj = pass_to_ranks(adj) if self.embed == "ase": embedder = AdjacencySpectralEmbed( n_components=self.n_components, n_elbows=self.n_elbows ) embed = embedder.fit_transform(embed_adj) elif self.embed == "lse": embedder = LaplacianSpectralEmbed( n_components=self.n_components, n_elbows=self.n_elbows, regularizer=self.regularizer, ) embed = embedder.fit_transform(embed_adj) elif self.embed == "unscaled_ase": embed_adj = pass_to_ranks(adj) embed_adj = augment_diagonal(embed_adj) embed = selectSVD( embed_adj, n_components=self.n_components, n_elbows=self.n_elbows ) embed = (embed[0], embed[2].T) X = np.concatenate(embed, axis=1) fraction_paired = (len(lp_inds) + len(rp_inds)) / len(self.root_inds) print(f"Learning transformation with {fraction_paired} neurons paired") R, _ = orthogonal_procrustes(X[lp_inds], X[rp_inds]) X[self.left_inds] = X[self.left_inds] @ R if self.normalize: row_sums = np.sum(X, axis=1) X /= row_sums[:, None] return X
def _embed(self, adj=None): if adj is None: adj = self.adj lp_inds = self.left_pair_inds rp_inds = self.right_pair_inds embed_adj = pass_to_ranks(adj) # TODO PTR here? if self.plus_c: embed_adj += 1 / adj.size if self.embed == "ase": embedder = AdjacencySpectralEmbed(n_components=self.n_components, n_elbows=self.n_elbows) embed = embedder.fit_transform(embed_adj) elif self.embed == "lse": embedder = LaplacianSpectralEmbed( n_components=self.n_components, n_elbows=self.n_elbows, regularizer=self.regularizer, ) embed = embedder.fit_transform(embed_adj) elif self.embed == "unscaled_ase": embed_adj = augment_diagonal(embed_adj) embed = selectSVD(embed_adj, n_components=self.n_components, n_elbows=self.n_elbows) embed = (embed[0], embed[2].T) X = np.concatenate(embed, axis=1) fraction_paired = (len(lp_inds) + len(rp_inds)) / len(self.root_inds) print(f"Learning transformation with {fraction_paired} neurons paired") X = self._procrustes(X) if self.normalize: row_norms = np.linalg.norm(X, axis=1) X /= row_norms[:, None] return X
# %% double checking on model params sbme = SBMEstimator(directed=False, loops=False) sbme.fit(adj, y=labels) block_p_hat = sbme.block_p_ block_heatmap(block_p_hat, title=r"Observed $\hat{B}$") block_p_hat_unscaled = block_p_hat * 1 / scaling_factor block_heatmap(block_p_hat_unscaled, title=r"Observed $\hat{B}$ (unscaled)") # %% [markdown] # ## Spectral embedding # Here I use graspy to do ASE, LSE, and regularized LSE. Note that we're just using the # SVDs here. There is an option on whether to throw out the first eigenvector. #%% embeddings embed_kws = dict(n_components=k + 1, algorithm="full", check_lcc=False) ase = AdjacencySpectralEmbed(**embed_kws) lse = LaplacianSpectralEmbed(form="DAD", **embed_kws) rlse = LaplacianSpectralEmbed(form="R-DAD", **embed_kws) ase_embed = ase.fit_transform(adj) lse_embed = lse.fit_transform(adj) rlse_embed = rlse.fit_transform(adj) embeddings_list = [ase_embed, lse_embed, rlse_embed] remove_first = True for i, embedding in enumerate(embeddings_list): if remove_first: embeddings_list[i] = embedding[:, 1:] else: embeddings_list[i] = embedding[:, :k] #%% setting up for plotting
) return ax # Try with my new labels adj = get_paired_adj("G", nodelist) adj = adj[:n_per_side, :n_per_side] + adj[n_per_side:, n_per_side:] embed_adj = pass_to_ranks(adj) skmeans_kws = dict(n_init=200, n_jobs=-2) n_clusters = 12 n_components = None regularizer = 1 lse = LaplacianSpectralEmbed(n_components=n_components, regularizer=regularizer) latent = lse.fit_transform(embed_adj) latent = np.concatenate(latent, axis=-1) models = [] meta_file = "maggot_models/data/processed/2019-09-18-v2/BP_metadata.csv" meta_df = pd.read_csv(meta_file, index_col=0) print(meta_df.head()) class_labels = meta_df.loc[nodelist.astype(int), "BP_Class"].values class_labels[class_labels == "LH2N"] = "Other" uni_class, class_counts = np.unique(class_labels, return_counts=True) inds = np.argsort(class_counts)[::-1] uni_class = uni_class[inds]
print(f"First eigenvalue: {evals[0]}") for i in range(n_blocks): plt.figure() clean_scatterplot(range(n_verts), evecs[:, i]) plt.title(f"Eigenvector {i + 1}") plt.xlabel("Element") plt.ylabel("Magnitude") # %% from graspy.embed import LaplacianSpectralEmbed lse = LaplacianSpectralEmbed(n_components=4) vecs = lse.fit_transform(A) plt.figure() clean_scatterplot(range(n_verts), vecs[:, 0]) plt.figure() clean_scatterplot(range(n_verts), vecs[:, 1]) plt.figure() clean_scatterplot(range(n_verts), vecs[:, 2]) plt.figure() clean_scatterplot(range(n_verts), vecs[:, 3]) #%% degrees = np.sum(A, axis=1) D = np.diag(degrees) D_inv = np.diag(1 / degrees)
use_weights=True, return_counts=False) plt.figure(figsize=(20, 20)) sns.heatmap(blockmodel_df, cmap="Reds") g = nx.from_pandas_adjacency(blockmodel_df, create_using=nx.DiGraph()) uni_labels, counts = np.unique(pred_labels, return_counts=True) size_scaler = 5 size_map = dict(zip(uni_labels, size_scaler * counts)) nx.set_node_attributes(g, size_map, name="Size") mini_adj = nx.to_numpy_array(g, nodelist=uni_labels) node_signal_flow = signal_flow(mini_adj) sf_map = dict(zip(uni_labels, node_signal_flow)) nx.set_node_attributes(g, sf_map, name="Signal Flow") sym_adj = symmetrize(mini_adj) node_lap = LaplacianSpectralEmbed(n_components=1).fit_transform(sym_adj) node_lap = np.squeeze(node_lap) lap_map = dict(zip(uni_labels, node_lap)) nx.set_node_attributes(g, lap_map, name="Laplacian-2") color_map = dict(zip(uni_labels, cc.glasbey_light)) nx.set_node_attributes(g, color_map, name="Color") g.nodes(data=True) nx.write_graphml(g, f"maggot_models/notebooks/outs/{FNAME}/mini_g.graphml") # %% sort minigraph based on signal flow sort_inds = np.argsort(node_signal_flow)[::-1] temp_labels = blockmodel_df.index.values temp_labels = temp_labels[sort_inds]
# %% double checking on model params sbme = SBMEstimator(directed=False, loops=False) sbme.fit(adj, y=labels) block_p_hat = sbme.block_p_ block_heatmap(block_p_hat, title=r"Observed $\hat{B}$") block_p_hat_unscaled = block_p_hat * 1 / scaling_factor block_heatmap(block_p_hat_unscaled, title=r"Observed $\hat{B}$ (unscaled)") # %% [markdown] # ## Spectral embedding # Here I use graspy to do ASE, LSE, and regularized LSE. Note that we're just using the # SVDs here. There is an option on whether to throw out the first eigenvector. #%% embeddings embed_kws = dict(n_components=k + 1, algorithm="full", check_lcc=False) ase = AdjacencySpectralEmbed(**embed_kws) lse = LaplacianSpectralEmbed(form="DAD", **embed_kws) rlse = LaplacianSpectralEmbed(form="R-DAD", **embed_kws) ase_embed = ase.fit_transform(adj) lse_embed = lse.fit_transform(adj) rlse_embed = rlse.fit_transform(adj) embeddings_list = [ase_embed, lse_embed, rlse_embed] remove_first = False for i, embedding in enumerate(embeddings_list): if remove_first: embeddings_list[i] = embedding[:, 1:] else: embeddings_list[i] = embedding[:, :k] #%% setting up for plotting
block_counts = _calculate_block_counts(adj, block_inds, block_vert_inds) block_count_df = pd.DataFrame(index=block_labels, columns=block_labels, data=block_counts) #%% # uni_pred_labels, counts = np.unique(pred_labels, return_counts=True) # uni_ints = range(len(uni_pred_labels)) # label_map = dict(zip(uni_pred_labels, uni_ints)) # int_labels = np.array(itemgetter(*uni_pred_labels)(label_map)) # synapse_counts = _calculate_block_counts(adj, uni_ints, pred_labels) block_df = sbm_prob block_adj = sbm_prob.values block_labels = sbm_prob.index.values sym_adj = symmetrize(block_adj) lse_embed = LaplacianSpectralEmbed(form="DAD", n_components=1) latent = lse_embed.fit_transform(sym_adj) latent = np.squeeze(latent) block_signal_flow = signal_flow(block_adj) block_g = nx.from_pandas_adjacency(block_df, create_using=nx.DiGraph()) pos = dict(zip(block_labels, zip(latent, block_signal_flow))) weights = nx.get_edge_attributes(block_g, "weight") node_colors = np.array(itemgetter(*block_labels)(pred_color_dict)) uni_pred_labels, pred_counts = np.unique(pred_labels, return_counts=True) size_map = dict(zip(uni_pred_labels, pred_counts)) node_sizes = np.array(itemgetter(*block_labels)(size_map)) node_sizes *= 4 norm = mpl.colors.Normalize(vmin=0.1, vmax=block_adj.max())
subgraph = sbm(n_vec, B) inds = block_labels == i graph[np.ix_(inds, inds)] = subgraph heatmap( graph, figsize=(15, 15), cbar=False, inner_hier_labels=subblock_labels, outer_hier_labels=block_labels, ) from graspy.embed import AdjacencySpectralEmbed, LaplacianSpectralEmbed from graspy.plot import pairplot ase = LaplacianSpectralEmbed(form="R-DAD") latent = ase.fit_transform(graph) pairplot(latent) norm_latent = latent.copy() norm_latent /= np.linalg.norm(latent, axis=1)[:, np.newaxis] pairplot(norm_latent, labels=block_labels) # %% [markdown] # # Embedding adj = graph n_verts = adj.shape[0] class_labels = block_labels # %% [markdown] # # Fitting divisive cluster model
embed_graph = get_lcc(full_graph) labels = get_simple(embed_graph) embed_graph = preprocess(embed_graph) ase = AdjacencySpectralEmbed(n_components=n_components) latent = ase.fit_transform(embed_graph) latent = np.concatenate(latent, axis=-1) pairplot(latent, labels=labels, title="ASE" + base_title) save("ASE") #%% LSE regularizer = 1 embed_graph = get_lcc(full_graph) labels = get_simple(embed_graph) embed_graph = preprocess(embed_graph) lse = LaplacianSpectralEmbed(form="R-DAD", n_components=n_components, regularizer=regularizer) latent = lse.fit_transform(embed_graph) latent = np.concatenate(latent, axis=-1) pairplot(latent, labels=labels, title="LSE" + base_title) save("LSE") #%% MASE embed_graphs = [] for graph in split_graph_list: graph = preprocess(graph) embed_graphs.append(graph) mase = MultipleASE(n_components=n_components, scaled=True) shared_latent = mase.fit_transform(embed_graphs) shared_latent = np.concatenate(shared_latent, axis=-1) labels = get_simple(split_graph_list[0])
#%% mean_within_adj = (left_left_adj + right_right_adj) / 2 heatmap( mean_within_adj, inner_hier_labels=class_labels[:n_per_side], transform="simple-all", figsize=(30, 30), hier_label_fontsize=10, sort_nodes=False, ) #%% from graspy.embed import LaplacianSpectralEmbed from graspy.plot import pairplot lse = LaplacianSpectralEmbed(form="R-DAD") latent = lse.fit_transform(mean_within_adj) latent = np.concatenate(latent, axis=-1) pairplot(latent) #%% graph_types = ["Gaan", "Gadn", "Gdan", "Gddn"] adjs = [] for g in graph_types: g = load_networkx("Gn") matched_graph = g.subgraph(nodelist) adj_df = nx.to_pandas_adjacency(matched_graph, nodelist=nodelist) adj = adj_df.values adjs.append(adj) # class_labels = meta_df.loc[nodelist.astype(int), "Class"]