def test_run_mot_embedding_sparse_no_restrict(): np.random.seed(9235) adj_mat = sparse.csr_matrix(np.array([ 0, 2, 0, 0, 0, 3, 4, 0, 0 ]).reshape((3, 3))) # answers ans_adj_mat = np.array([ 0, 2, 0, 0, 0, 3, 4, 0, 0 ]).reshape((3, 3)) ans_motif_adj_mat = np.array([ 0, 3, 3, 3, 0, 3, 3, 3, 0 ]).reshape((3, 3)) ans_vals = [0, 1.5] ans_vects = np.array([ 0.577, -0.293, 0.577, 0.806, 0.577, -0.513 ]).reshape((3, 2)) # run motif embedding emb_list = mcsp.run_motif_embedding(adj_mat, "M1", "func", "mean", "dense", 2, "rw", restrict=False) # flip eigenvector signs if necessary for i in range(len(ans_vals)): if np.sign(emb_list["vects"][0, i]) != np.sign(ans_vects[0, i]): emb_list["vects"][:, i] = -emb_list["vects"][:, i] assert np.allclose(ans_adj_mat, emb_list["adj_mat"].toarray()) assert np.allclose(ans_motif_adj_mat, emb_list["motif_adj_mat"].toarray()) assert np.allclose(ans_vals, emb_list["vals"], atol=0.01) assert np.allclose(ans_vects, emb_list["vects"], atol=0.01)
def run_motif_clustering(adj_mat, motif_name, motif_type="struc", mam_weight_type="unweighted", mam_method="sparse", num_eigs=2, type_lap="comb", num_clusts=2, restrict=True, gr_method="sparse"): """ Run motif-based clustering. Run motif-based clustering on the adjacency matrix of a (weighted directed) network, using a specified motif, motif type, weighting scheme, embedding dimension, number of clusters and Laplacian type. Optionally restrict to the largest connected component before clustering. Parameters ---------- adj_mat : matrix Adjacency matrix to be embedded. motif_name : str Motif used for the motif adjacency matrix. motif_type : str Type of motif adjacency matrix to use. One of `"func"` or `"struc"`. mam_weight_type : str Weighting scheme for the motif adjacency matrix. One of `"unweighted"`, `"mean"` or `"product"`. mam_method : str The method to use for building the motif adjacency matrix. One of `"sparse"` or `"dense"`. num_eigs : int Number of eigenvalues and eigenvectors for the embedding. type_lap : str Type of Laplacian for the embedding. One of `"comb"` or `"rw"`. num_clusts : int The number of clusters to find. restrict : bool Whether or not to restrict the motif adjacency matrix to its largest connected component before embedding. gr_method : str Format to use for getting largest component. One of `"sparse"` or `"dense"`. Returns ------- adj_mat : sparse matrix The original adjacency matrix. motif_adj_mat : sparse matrix The motif adjacency matrix. comps : list The indices of the largest connected component of the motif adjacency matrix (if restrict=True). adj_mat_comps : matrix The original adjacency matrix restricted to the largest connected component of the motif adjacency matrix (if restrict=True). motif_adj_mat_comps : matrix The motif adjacency matrix restricted to its largest connected component (if restrict=True). vals : list A length-`num_eigs` list containing the eigenvalues associated with the Laplace embedding of the (restricted) motif adjacency matrix. vects : matrix A matrix containing the eigenvectors associated with the Laplace embedding of the (restricted) motif adjacency matrix. clusts : A vector containing integers representing the cluster assignment of each vertex in the (restricted) graph. Examples -------- >>> adj_mat = np.array(range(1, 10)).reshape((3, 3)) >>> run_motif_clustering(adj_mat, "M1") """ assert motif_type in ["struc", "func"] assert mam_weight_type in ["unweighted", "mean", "product"] assert mam_method in ["sparse", "dense"] assert isinstance(restrict, bool) assert type_lap in ["comb", "rw"] spectrum = mcsp.run_motif_embedding(adj_mat, motif_name, motif_type, mam_weight_type, mam_method, num_eigs, type_lap, restrict, gr_method) cluster_assigns = cluster_spectrum(spectrum, num_clusts) ans = { "adj_mat": adj_mat, "motif_adj_mat": spectrum["motif_adj_mat"], "comps": spectrum["comps"], "adj_mat_comps": spectrum["adj_mat_comps"], "motif_adj_mat_comps": spectrum["motif_adj_mat_comps"], "vals": spectrum["vals"], "vects": spectrum["vects"], "clusts": cluster_assigns } return ans