def omni_procrust_svd(embed_adjs): omni = OmnibusEmbed(n_components=None, check_lcc=False) joint_embed = omni.fit_transform(embed_adjs) cat_embed = np.concatenate(joint_embed, axis=-1) # print(f"Omni concatenated embedding shape: {cat_embed.shape}") for e in cat_embed: e[left_inds] = e[left_inds] @ orthogonal_procrustes( e[lp_inds], e[rp_inds])[0] cat_embed = np.concatenate(cat_embed, axis=-1) U, S, Vt = selectSVD(cat_embed, n_elbows=3) return U
def ase_procrust_svd(embed_adjs): ase = AdjacencySpectralEmbed(n_components=None) all_embeds = [] for a in embed_adjs: embed = ase.fit_transform(a) embed = np.concatenate(embed, axis=1) embed[left_inds] = (embed[left_inds] @ orthogonal_procrustes( embed[lp_inds], embed[rp_inds])[0]) print(embed.shape) all_embeds.append(embed) cat_embed = np.concatenate(all_embeds, axis=1) print(cat_embed.shape) U, S, Vt = selectSVD(cat_embed, n_elbows=3) return U
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
def svd(X, n_components=n_svd_components): return selectSVD(X, n_components=n_components, algorithm="full")[0]
mg = mg.make_lcc() print(len(mg)) mg.calculate_degrees(inplace=True) meta = mg.meta meta["inds"] = range(len(meta)) adj = mg.adj.copy() lp_inds, rp_inds = get_paired_inds(meta) left_inds = meta[meta["left"]]["inds"] embed = lateral_omni(adj, lp_inds, rp_inds) labels = np.concatenate((meta["merge_class"].values[lp_inds], meta["merge_class"].values[rp_inds])) U, S, V = selectSVD(embed, n_components=6) plot_pairs( U, labels, left_pair_inds=np.arange(len(lp_inds)), right_pair_inds=np.arange(len(lp_inds)) + len(lp_inds), ) stashfig(f"pairs-low-threshold-quantile={quantile}") print() # %% [markdown] # ## look at removing high degree nodes quantiles = np.linspace(0, 0.5, 6) master_mg = load_metagraph(graph_type, version="2020-04-01") for quantile in quantiles:
verbose=10, ) agmm.fit(all_hop_hist.T) # %% [markdown] # ## from graspy.embed import select_dimension select_dimension(all_hop_hist.T, n_elbows=5) #%% from graspy.embed import selectSVD from graspy.plot import pairplot n_elbows = 3 U, S, V = selectSVD(all_hop_hist.T, n_elbows=n_elbows) plot_df = pd.DataFrame(data=U) plot_df["label"] = meta["merge_class"].values pg = sns.PairGrid(plot_df, hue="label", palette=CLASS_COLOR_DICT, vars=np.arange(U.shape[1]), height=4) # pg._legend.remove() # pg.map_diag(plt.hist) pg.map_offdiag(sns.scatterplot, s=15, linewidth=0, alpha=0.7)
all_embeds.append(embed) pdist = pairwise_distances(embed, metric="cosine") all_pdists.append(pdist) from mvlearn.embed import MVMDS mvmds = MVMDS(n_components=6) mv_embed = mvmds.fit_transform(all_pdists) pairs(mv_embed) # %% [markdown] # ## cat_embeds = np.concatenate(all_embeds, axis=-1) U, S, Vt = selectSVD(cat_embeds, n_elbows=4) pairs(U) # %% [markdown] # ## results = crossval_cluster(U, left_inds, right_inds, left_pair_inds=lp_inds, right_pair_inds=rp_inds) plot_metrics(results) metric = "bic" k = 6
# %% [markdown] # ## meta = mg.meta lp_inds, rp_inds = get_paired_inds(meta) left_inds = meta[meta["left"]]["inds"] right_inds = meta[meta["right"]]["inds"] cat_embed = np.concatenate(joint_embed, axis=-1) for e in cat_embed: e[left_inds] = e[left_inds] @ orthogonal_procrustes( e[lp_inds], e[rp_inds])[0] cat_embed = np.concatenate(cat_embed, axis=-1) print(select_dimension(cat_embed, n_elbows=3)) U, S, Vt = selectSVD(cat_embed, n_elbows=3) pg = pairplot(U, labels=meta["merge_class"].values, palette=CLASS_COLOR_DICT, size=20, alpha=0.4) pg._legend.remove() stashfig("omni-reduced-dim") # %% [markdown] # ## results = crossval_cluster(U, left_inds, right_inds,
embed = bilateral_ase(a) all_embeds.append(embed[0]) all_embeds.append(embed[1]) # U, _, _ = selectSVD(embed, n_elbows=2) # plot_pairs( # U, # labels=meta["merge_class"].values, # left_pair_inds=lp_inds, # right_pair_inds=rp_inds, # ) cat_all_embeds = np.concatenate(all_embeds, axis=1) # %% [markdown] # ## # align_joint_embed = np.concatenate((align_ipsi_embed, align_contra_embed), axis=1) # U, S, V = selectSVD(align_joint_embed) U, S, V = selectSVD(cat_all_embeds, n_elbows=4) print(U.shape) plt.plot(S) # %% [markdown] # ## plot_pairs( U, labels=meta["merge_class"].values, left_pair_inds=lp_inds, right_pair_inds=rp_inds, ) # %% [markdown] # ## from graspy.utils import symmetrize
temp_adj = temp_mg.adj adjs.append(temp_adj) # embed_adjs = [pass_to_ranks(a) for a in adjs] # %% [markdown] # ## just omni on the 4 colors for the right right subgraph right_embed_adjs = [pass_to_ranks(a[np.ix_(rp_inds, rp_inds)]) for a in adjs] omni = OmnibusEmbed(check_lcc=False) embeds = omni.fit_transform(right_embed_adjs) embeds = np.concatenate(embeds, axis=-1) embeds = np.concatenate(embeds, axis=-1) print(embeds.shape) U, S, V = selectSVD(embeds, n_components=8) labels = meta["merge_class"].values[rp_inds] plot_pairs(U, labels) stashfig(f"simple-omni-right-reduced-4-color") # %% [markdown] # ## Look at what each edge color type looks like when regularized by g # only the right right subgraph right_full_adj = pass_to_ranks(adj[np.ix_(rp_inds, rp_inds)]) labels = meta["merge_class"].values[rp_inds] all_reg_embeds = [] for a in right_embed_adjs: omni = OmnibusEmbed(check_lcc=False) embeds = omni.fit_transform([right_full_adj, a])