def calc_dist_mat(self, seqs, seqs2=None): """Don't calculate distances, but return the hard-coded distance matrix needed for the test""" mat_seqs = np.array(["AAA", "AHA", "KK", "KKK", "KKY", "LLL"]) mask = np.isin(mat_seqs, seqs) dist_mat = scipy.sparse.csr_matrix( np.array([ [1, 4, 0, 0, 0, 0], [4, 1, 0, 0, 0, 0], [0, 0, 1, 10, 10, 0], [0, 0, 10, 1, 5, 0], [0, 0, 10, 5, 1, 0], [0, 0, 0, 0, 0, 1], ])[mask, :][:, mask]) assert _is_symmetric(dist_mat) return dist_mat
def adata_conn(): """Adata with connectivities computed""" adata = AnnData(obs=pd.DataFrame().assign( cell_id=["cell1", "cell2", "cell3", "cell4"], TRA_1_v_gene=["av1", "av1", "av2", "av1"], TRB_1_v_gene=["bv1", "bv1", "bv2", "bv1"], TRA_2_v_gene=["a2v1", "a2v2", "a2v2", "a2v1"], TRB_2_v_gene=["b2v1", "b2v2", "b2v2", "b2v1"], ).set_index("cell_id")) adata.uns["tcr_neighbors_aa_alignment"] = { "connectivities": np.array([[1, 0, 0.5, 0], [0, 1, 1, 0], [0.5, 1, 1, 0], [0, 0, 0, 1]]) } assert _is_symmetric( adata.uns["tcr_neighbors_aa_alignment"]["connectivities"]) return adata
def adata_clonotype_network(): """Adata with clonotype network computed""" adata = AnnData(obs=pd.DataFrame().assign( cell_id=["cell1", "cell2", "cell3", "cell4"]).set_index("cell_id")) adata.uns["foo_neighbors"] = { "connectivities": np.array([[1, 0, 0.5, 0], [0, 1, 1, 0], [0.5, 1, 1, 0], [0, 0, 0, 1]]) } adata.uns["clonotype_network"] = {"neighbors_key": "foo_neighbors"} adata.obsm["X_clonotype_network"] = np.array([ [2.41359095, 0.23412465], [np.nan, np.nan], [1.61680611, 0.80266963], [3.06104282, 2.14395562], ]) assert _is_symmetric(adata.uns["foo_neighbors"]["connectivities"]) return adata
def adata_conn(): """Adata with connectivities computed""" adata = AnnData(obs=pd.DataFrame().assign( cell_id=["cell1", "cell2", "cell3", "cell4"], IR_VJ_1_v_gene=["av1", "av1", "av2", "av1"], IR_VDJ_1_v_gene=["bv1", "bv1", "bv2", "bv1"], IR_VJ_2_v_gene=["a2v1", "a2v2", "a2v2", "a2v1"], IR_VDJ_2_v_gene=["b2v1", "b2v2", "b2v2", "b2v1"], IR_VJ_1_LOCUS=["TRA", "IGL", "IGL", "IGK"], IR_VDJ_1_LOCUS=["TRB", "IGH", "IGH", "IGH"], IR_VJ_2_LOCUS=["TRA", "IGL", "IGL", "IGK"], IR_VDJ_2_LOCUS=["TRB", "IGH", "IGH", "IGH"], receptor_type=["TCR", "BCR", "BCR", "BCR"], ).set_index("cell_id")) adata.uns["ir_neighbors_aa_alignment"] = { "connectivities": csr_matrix([[1, 0, 0.5, 0], [0, 1, 1, 0], [0.5, 1, 1, 0], [0, 0, 0, 1]]) } assert _is_symmetric( adata.uns["ir_neighbors_aa_alignment"]["connectivities"]) return adata
def test_is_symmatric(): M = np.array([[1, 2, 2], [2, 1, 3], [2, 3, 1]]) S_csr = scipy.sparse.csr_matrix(M) S_csc = scipy.sparse.csc_matrix(M) S_lil = scipy.sparse.lil_matrix(M) assert _is_symmetric(M) assert _is_symmetric(S_csr) assert _is_symmetric(S_csc) assert _is_symmetric(S_lil) M = np.array([[1, 2, 2], [2, 1, np.nan], [2, np.nan, np.nan]]) S_csr = scipy.sparse.csr_matrix(M) S_csc = scipy.sparse.csc_matrix(M) S_lil = scipy.sparse.lil_matrix(M) assert _is_symmetric(M) assert _is_symmetric(S_csr) assert _is_symmetric(S_csc) assert _is_symmetric(S_lil) M = np.array([[1, 2, 2], [2, 1, 3], [3, 2, 1]]) S_csr = scipy.sparse.csr_matrix(M) S_csc = scipy.sparse.csc_matrix(M) S_lil = scipy.sparse.lil_matrix(M) assert not _is_symmetric(M) assert not _is_symmetric(S_csr) assert not _is_symmetric(S_csc) assert not _is_symmetric(S_lil)