Example #1
0
 def test_sort_features(self):
     x = np.array([[0, 2, 30, 10],
                   [1, 2, 30, 10],
                   [0, 3, 33, 10],
                   [2, 5, 30, 7],
                   [2, 5, 30, 9]])
     x = spsp.csr_matrix(x)
     sdm = eda.SampleDistanceMatrix(
         x, metric='euclidean')
     sdm2 = eda.SampleDistanceMatrix(
         x, metric='euclidean')
     sdm2.sort_features(fdist_metric='euclidean', optimal_ordering=True)
     assert sdm2.fids == [2, 3, 1, 0]
Example #2
0
 def test_empty_init(self):
     # sparse matrix has 2 dims
     eda.SampleDistanceMatrix(
         spsp.csr_matrix(np.empty(0)),
         metric='euclidean')
     sdm = eda.SampleDistanceMatrix(
         spsp.csr_matrix(np.empty((0, 0))), metric='euclidean')
     assert len(sdm.sids) == 0
     assert len(sdm.fids) == 0
     assert sdm._x.shape == (0, 0)
     assert sdm._d.shape == (0, 0)
     assert sdm._col_sorted_d.shape == (0, 0)
     assert sdm._col_argsorted_d.shape == (0, 0)
     assert sdm.tsne(n_iter=250).shape == (0, 0)
Example #3
0
 def test_pca_var_explained(self):
     np.random.seed(123)
     x5k = np.random.normal(size=5000)
     x5k = spsp.csr_matrix(x5k)
     sdm = eda.SampleDistanceMatrix(x5k.reshape(20, -1))
     assert sdm._skd_pca.explained_variance_.shape == (20,)
     assert sdm._skd_pca.explained_variance_ratio_.shape == (20,)
Example #4
0
    def test_init_wrong_d_type(self):
        d_3x3 = np.array([[0, np.sqrt(2), np.sqrt(8)],
                          ['1a1', 0, np.sqrt(2)],
                          [np.sqrt(8), np.sqrt(2), 0]])

        with pytest.raises(Exception) as excinfo:
            eda.SampleDistanceMatrix(self.x_3x2, d_3x3)
Example #5
0
 def test_s_ith_nn_d_dist(self):
     nn_sdm = eda.SampleDistanceMatrix(
         spsp.csr_matrix([[0, 0, 0], [1, 1, 1], [5, 5, 5],
                          [6, 6, 6], [10, 10, 10],
                          [20, 20, 20]]),
                         metric='euclidean')
     nn_sdm.s_ith_nn_d_dist(1)
 def test_no_pdist_detect_rare_samples_wrong_args(self):
     tsdm = eda.SampleDistanceMatrix(
         [[0, 0], [1, 1], [200, 200], [200, 200], [200, 200], [100, 100],
          [101, 101], [99, 99], [100, 100], [102, 102]],
         metric="euclidean",
         use_pdist=False)
     rsd = knn.RareSampleDetection(tsdm)
     # Invalid parameters
     with pytest.raises(ValueError) as excinfo:
         rsd.detect_rare_samples(0, 1, 1)
     with pytest.raises(ValueError) as excinfo:
         rsd.detect_rare_samples(1, 0, 1)
     with pytest.raises(ValueError) as excinfo:
         rsd.detect_rare_samples(1, 1, 0)
     with pytest.raises(ValueError) as excinfo:
         rsd.detect_rare_samples(1, 1, 0.5)
     with pytest.raises(ValueError) as excinfo:
         rsd.detect_rare_samples(0.5, 1, 1)
     with pytest.raises(ValueError) as excinfo:
         rsd.detect_rare_samples(1, -0.1, 1)
     # Parameters of different length
     with pytest.raises(ValueError) as excinfo:
         rsd.detect_rare_samples([1, 2], 1, 1)
     with pytest.raises(ValueError) as excinfo:
         rsd.detect_rare_samples(1, [1, 2], 1)
     with pytest.raises(ValueError) as excinfo:
         rsd.detect_rare_samples(1, 1, [1, 2])
Example #7
0
    def test_id_x(self):
        sids = list("abcdef")
        fids = list(range(10, 20))
        sdm = eda.SampleDistanceMatrix(
            spsp.csr_matrix(np.random.ranf(60).reshape(6, -1)),
            sids=sids, fids=fids)
        # select sf
        ss_sdm = sdm.id_x(['a', 'f'], list(range(10, 15)))
        assert ss_sdm._x.shape == (2, 5)
        assert ss_sdm.sids == ['a', 'f']
        assert ss_sdm.fids == list(range(10, 15))
        np.testing.assert_equal(
            ss_sdm.d, sdm._d[np.ix_((0, 5), (0, 5))])

        # select with Default
        ss_sdm = sdm.id_x()
        assert ss_sdm._x.shape == (6, 10)
        assert ss_sdm.sids == list("abcdef")
        assert ss_sdm.fids == list(range(10, 20))
        np.testing.assert_equal(ss_sdm.d, sdm._d)

        # select with None
        ss_sdm = sdm.id_x(None, None)
        assert ss_sdm._x.shape == (6, 10)
        assert ss_sdm.sids == list("abcdef")
        assert ss_sdm.fids == list(range(10, 20))
        np.testing.assert_equal(ss_sdm.d, sdm._d)

        # select non-existent inds
        # id lookup raises ValueError
        with pytest.raises(ValueError) as excinfo:
            sdm.id_x([6])

        with pytest.raises(ValueError) as excinfo:
            sdm.id_x(None, ['a'])
    def test_impute_features_stat_fun(self):
        tsdm = eda.SampleDistanceMatrix(spsp.csr_matrix(self.tx),
                                        metric="euclidean",
                                        use_pdist=False)
        tsdm.s_knn_connectivity_matrix(5, use_hnsw=False)
        fmp = knn.FeatureImputation(tsdm)
        res_sdml = fmp.impute_features([8], [3], [0.5], [10])

        res_sdml2 = fmp.impute_features(8, 3, 0.5, 10, 1, np.median)
        np.testing.assert_equal(res_sdml2[0]._x.A, res_sdml[0]._x.A)
        assert len(fmp._res_lut) == 1

        res_sdml3 = fmp.impute_features(
            8, 3, 0.5, 10, 1, lambda x, axis=0: np.median(x, axis=axis))
        np.testing.assert_equal(res_sdml3[0]._x.A, res_sdml[0]._x.A)
        assert len(fmp._res_lut) == 2

        res_sdml4 = fmp.impute_features(
            8, 3, 0.5, 10, 1, lambda x, axis=0: np.median(x, axis=axis))
        np.testing.assert_equal(res_sdml4[0]._x.A, res_sdml[0]._x.A)
        assert len(fmp._res_lut) == 3

        res_sdml5 = fmp.impute_features(8, 3, 0.5, 10, 1, np.min)
        assert not np.array_equal(res_sdml5[0]._x.A, res_sdml[0]._x.A)
        assert len(fmp._res_lut) == 4

        res_sdml6 = fmp.impute_features(8,
                                        3,
                                        0.5,
                                        10,
                                        1,
                                        lambda x, axis=0: np.min(x, axis=axis))
        np.testing.assert_equal(res_sdml5[0]._x.A, res_sdml6[0]._x.A)
        assert len(fmp._res_lut) == 5
Example #9
0
 def test_put_tsne_wrong_args(self):
     tmet = 'euclidean'
     sdm = eda.SampleDistanceMatrix(self.x_3x2, metric=tmet)
     with pytest.raises(ValueError) as excinfo:
         sdm.put_tsne(1, [1, 2, 3])
     with pytest.raises(ValueError) as excinfo:
         sdm.put_tsne({1: 2}, [1, 2, 3])
Example #10
0
 def test_get_tsne_kv(self):
     tmet = 'euclidean'
     sdm = eda.SampleDistanceMatrix(self.x_3x2, metric=tmet)
     assert sdm.get_tsne_kv(1) is None
     assert sdm.get_tsne_kv(1) is None
     assert sdm.get_tsne_kv(0) is None
     assert sdm.get_tsne_kv(2) is None
    def run_test_impute_features(self, nprocs, use_hnsw):
        tsdm = eda.SampleDistanceMatrix(spsp.csr_matrix(self.tx),
                                        metric="euclidean",
                                        use_pdist=False)
        tsdm.s_knn_connectivity_matrix(5, use_hnsw=use_hnsw)
        fmp = knn.FeatureImputation(tsdm)
        res_sdml = fmp.impute_features([1, 3, 5], [1, 1, 3], [0.5, 1.5, 0.5],
                                       [1, 1, 5],
                                       nprocs=nprocs)
        assert len(fmp._res_lut) == 3
        assert res_sdml[0]._x.A.shape == (9, 2)
        assert res_sdml[1]._x.A.shape == (9, 2)
        assert res_sdml[2]._x.A.shape == (9, 2)
        # value
        ref_res0 = np.array([[1, 1], [1, 1], [1, 2], [2, 3], [2, 5], [0, 100],
                             [0, 100], [0, 101], [10, 100]])
        np.testing.assert_equal(res_sdml[0]._x.A, ref_res0)

        ref_res1 = np.array([[np.median([1, 1, 2]),
                              np.median([1, 2, 3])],
                             [np.median([1, 1, 2]),
                              np.median([1, 2, 3])], [np.median([1, 1, 2]), 2],
                             [2, 3], [2, 5], [np.median([0, 0, 10]), 100],
                             [np.median([0, 0, 10]), 100],
                             [np.median([0, 0, 10]), 101], [10, 100]])
        np.testing.assert_equal(res_sdml[1]._x.A, ref_res1)

        assert res_sdml[2]._x.A[0, 0] > 0
        assert res_sdml[2]._x.A[0, 1] > 0
        assert res_sdml[2]._x.A[5, 0] > 0
        assert res_sdml[2]._x.A[6, 0] > 0
        assert res_sdml[2]._x.A[7, 0] > 0

        # lookup
        np.testing.assert_equal(fmp._res_lut[(1, 1, 0.5, 1, np.median)][0].A,
                                res_sdml[0]._x.A)
        np.testing.assert_equal(fmp._res_lut[(3, 1, 1.5, 1, np.median)][0].A,
                                res_sdml[1]._x.A)
        np.testing.assert_equal(fmp._res_lut[(5, 3, 0.5, 5, np.median)][0].A,
                                res_sdml[2]._x.A)
        fmp.impute_features([1, 3, 5], [1, 1, 3], [0.5, 1.5, 0.5], [1, 1, 5],
                            nprocs=nprocs)
        assert len(fmp._res_lut) == 3
        np.testing.assert_equal(fmp._sdm.x.A.tolist(), self.tx)
        # run results should be placed with the correct order
        fmp2 = knn.FeatureImputation(tsdm)
        res1 = fmp2.impute_features([1, 3, 5], [1, 1, 3], [0.5, 1.5, 0.5],
                                    [1, 1, 5],
                                    nprocs=nprocs)
        fmp3 = knn.FeatureImputation(tsdm)
        res2 = fmp3.impute_features([2, 3, 4], [1, 1, 3], [0.5, 1.5, 0.5],
                                    [1, 1, 5],
                                    nprocs=nprocs)
        res3 = fmp2.impute_features([2, 3, 4], [1, 1, 3], [0.5, 1.5, 0.5],
                                    [1, 1, 5],
                                    nprocs=nprocs)
        np.testing.assert_equal(res1[1]._x.A, res3[1]._x.A)
        np.testing.assert_equal(res2[0]._x.A, res3[0]._x.A)
        np.testing.assert_equal(res2[2]._x.A, res3[2]._x.A)
Example #12
0
 def test_provide_graph(self):
     sdm = eda.SampleDistanceMatrix(self.x_20x5)
     knn_conn_mat = sdm.s_knn_connectivity_matrix(5)
     knn_aff_graph = eda.SampleDistanceMatrix.knn_conn_mat_to_aff_graph(
         knn_conn_mat, 2)
     cluster.Community(
         self.x_20x5, graph=knn_aff_graph,
         partition_method="RBConfigurationVertexPartition").labs
Example #13
0
 def test_s_ith_nn_d(self):
     nn_sdm = eda.SampleDistanceMatrix(
         spsp.csr_matrix([[0], [1], [5], [6], [10], [20]]),
         metric='euclidean')
     np.testing.assert_allclose([0, 0, 0, 0, 0, 0],
                                nn_sdm.s_ith_nn_d(0))
     np.testing.assert_allclose([1, 1, 1, 1, 4, 10],
                                nn_sdm.s_ith_nn_d(1))
     np.testing.assert_allclose([5, 4, 4, 4, 5, 14],
                                nn_sdm.s_ith_nn_d(2))
Example #14
0
    def test_cosine_pdist(self):
        np.random.seed(222)
        x = np.random.ranf(10000).reshape(500, -1)
        x = spsp.csr_matrix(x)
        skd = sklearn.metrics.pairwise.pairwise_distances(x, metric='cosine')
        np.testing.assert_allclose(
            eda.SampleDistanceMatrix.cosine_pdist(x), skd)

        np.testing.assert_allclose(
            eda.SampleDistanceMatrix(x, metric='cosine')._d, skd)
Example #15
0
 def test_sdm_truncated_pca_plot(self):
     sids = list(range(8))
     fids = [str(i) for i in range(10)]
     np.random.seed(123)
     x = np.random.ranf(80).reshape(8, -1)
     x_sorted = x[np.argsort(x[:, 5])]
     g = x_sorted[:, 5]
     x_sorted = spsp.csr_matrix(x_sorted)
     sdm = eda.SampleDistanceMatrix(x_sorted, sids=sids, fids=fids)
     return sdm.pca_plot(gradient=g, figsize=(10, 10), s=50)
Example #16
0
 def test_detect_rare_samples_single_run(self):
     tsdm = eda.SampleDistanceMatrix(
         [[0, 0], [1, 1], [200, 200], [101, 101], [99, 99], [100, 100],
          [102, 102]],
         metric="euclidean")
     rsd = knn.RareSampleDetection(tsdm)
     resl = rsd.detect_rare_samples(1, 0.1, 1)
     assert resl[0] == []
     resl2 = rsd.detect_rare_samples(1, 0.1, 5)
     assert resl2[0] == []
Example #17
0
 def test_knn_filter_samples_single_run(self):
     tsdm = eda.SampleDistanceMatrix(
         [[0, 0], [1, 1], [200, 200], [101, 101], [99, 99], [100, 100],
          [102, 102]],
         metric="euclidean")
     skf = qc.SampleKNNFilter(tsdm)
     resl = skf.knn_filter_samples(1, 0.1, 1)
     assert resl[0] == []
     resl2 = skf.knn_filter_samples(1, 0.1, 5)
     assert resl2[0] == []
Example #18
0
    def test_mirac_cover_tests(self):
        # make all non-neg
        x = np.zeros((10, 10))
        cluster.MIRAC(x, metric='euclidean', min_cl_n=25)
        cluster.MIRAC(x, metric='euclidean', min_cl_n=25)
        cluster.MIRAC(x, metric='euclidean', min_cl_n=25, verbose=True)

        tx, tlab = skdset.make_blobs(n_samples=100, n_features=2,
                                     centers=10, random_state=8927)
        tx = tx - tx.min()
        sdm = eda.SampleDistanceMatrix(tx, metric='euclidean')
        m = cluster.MIRAC(sdm._x, sdm._d, metric='euclidean', linkage='ward',
                          min_cl_n=35, cl_mdl_scale_factor=1, verbose=True)
        assert len(m.labs) == 100
        hct = eda.HClustTree.hclust_tree(sdm._d, linkage='ward',
                                         optimal_ordering=True)
        m2 = cluster.MIRAC(sdm._x, hac_tree=hct, min_cl_n=35,
                           cl_mdl_scale_factor=1, encode_type='data',
                           mdl_method=eda.mdl.ZeroIGKdeMdl, verbose=False)
        assert m.labs == m2.labs
        assert m2._sdm._lazy_load_d is None

        tx2, tlab2 = skdset.make_blobs(n_samples=500, n_features=5,
                                       centers=5, cluster_std=1,
                                       random_state=8927)
        tx2 = tx2 - tx2.min()
        cluster.MIRAC(tx2, metric='euclidean', min_cl_n=15,
                      optimal_ordering=False, cl_mdl_scale_factor=0,
                      verbose=True)
        # auto to encode distance
        tx3, tlab3 = skdset.make_blobs(n_samples=100, n_features=101,
                                       cluster_std=0.01, centers=5,
                                       random_state=8927)
        tx3 = tx3 - tx3.min()
        sdm = eda.SampleDistanceMatrix(tx3, metric='euclidean')
        m = cluster.MIRAC(sdm._x, sdm._d, metric='euclidean', linkage='ward',
                          min_cl_n=5, cl_mdl_scale_factor=1, verbose=True)
        assert m._encode_type == 'distance'
        # empty
        sdm = eda.SampleDistanceMatrix([[], []], metric='euclidean')
        m = cluster.MIRAC(sdm._x, sdm._d, metric='euclidean', linkage='ward',
                          min_cl_n=35, cl_mdl_scale_factor=1, verbose=True)
Example #19
0
    def test_mirac_dim_reduction(self):
        tx, tlab = skdset.make_blobs(n_samples=100, n_features=2,
                                     centers=10, random_state=8927)
        tx = tx - tx.min()
        sdm = eda.SampleDistanceMatrix(tx, metric='euclidean')
        m = cluster.MIRAC(sdm._x, sdm._d, metric='euclidean', linkage='ward',
                          min_cl_n=35, cl_mdl_scale_factor=1,
                          dim_reduct_method='PCA', verbose=True)
        assert len(m.labs) == 100
        hct = eda.HClustTree.hclust_tree(sdm._d, linkage='ward',
                                         optimal_ordering=True)
        m2 = cluster.MIRAC(sdm._x, hac_tree=hct, min_cl_n=35,
                           cl_mdl_scale_factor=1, encode_type='data',
                           mdl_method=eda.mdl.ZeroIGKdeMdl,
                           dim_reduct_method='t-SNE', verbose=False)
        assert m.labs == m2.labs
        assert m2._sdm._lazy_load_d is None

        tx2, tlab2 = skdset.make_blobs(n_samples=500, n_features=50,
                                       centers=5, cluster_std=15,
                                       random_state=8927)
        tx2 = tx2 - tx2.min()
        cluster.MIRAC(tx2, metric='euclidean', min_cl_n=15,
                      optimal_ordering=False, cl_mdl_scale_factor=0,
                      dim_reduct_method='UMAP', verbose=True)
        # auto to encode distance
        tx3, tlab3 = skdset.make_blobs(n_samples=100, n_features=101,
                                       cluster_std=0.01, centers=5,
                                       random_state=8927)
        tx3 = tx3 - tx3.min()
        sdm = eda.SampleDistanceMatrix(tx3, metric='euclidean')
        m = cluster.MIRAC(sdm._x, sdm._d, metric='euclidean', linkage='ward',
                          min_cl_n=5, cl_mdl_scale_factor=1,
                          dim_reduct_method='t-SNE', verbose=True)
        # empty
        sdm = eda.SampleDistanceMatrix([[], []], metric='euclidean')

        with pytest.raises(Exception) as excinfo:
            m = cluster.MIRAC(sdm._x, sdm._d, metric='euclidean',
                              linkage='ward',
                              min_cl_n=35, cl_mdl_scale_factor=1,
                              dim_reduct_method='PCA', verbose=True)
Example #20
0
 def test_s_ith_nn_ind(self):
     nn_sdm = eda.SampleDistanceMatrix(
         spsp.csr_matrix([[0, 0, 0], [1, 1, 1], [5, 5, 5],
                          [6, 6, 6], [10, 10, 10],
                          [20, 20, 20]]),
         metric='euclidean')
     np.testing.assert_allclose([0, 1, 2, 3, 4, 5],
                                nn_sdm.s_ith_nn_ind(0))
     np.testing.assert_allclose([1, 0, 3, 2, 3, 4],
                                nn_sdm.s_ith_nn_ind(1))
     np.testing.assert_allclose([2, 2, 1, 4, 2, 3],
                                nn_sdm.s_ith_nn_ind(2))
 def test_hnsw_detect_rare_samples_single_run(self):
     tsdm = eda.SampleDistanceMatrix(
         [[0.00001, 0.00001], [1, 1], [200, 200], [101, 101], [99, 99],
          [100, 100], [102, 102]],
         metric="euclidean",
         use_pdist=False)
     rsd = knn.RareSampleDetection(tsdm)
     res = rsd._no_pdist_rare_s_detect(1, 0.1, 1)
     resl = rsd.detect_rare_samples(1, 0.1, 1, use_pca=True, use_hnsw=True)
     assert resl[0] == []
     resl2 = rsd.detect_rare_samples(1, 0.1, 5, use_pca=True, use_hnsw=True)
     assert resl2[0] == []
    def test_impute_features_single_run(self):
        tsdm = eda.SampleDistanceMatrix(spsp.csr_matrix(self.tx),
                                        metric="euclidean",
                                        use_pdist=False)
        tsdm.s_knn_connectivity_matrix(5, use_hnsw=False)
        fmp = knn.FeatureImputation(tsdm)
        res_sdml = fmp.impute_features([8], [3], [0.5], [10])

        fmp2 = knn.FeatureImputation(tsdm)
        res_sdml2 = fmp2.impute_features(8, 3, 0.5, 10)
        np.testing.assert_equal(res_sdml2[0]._x.A, res_sdml[0]._x.A)

        tsdm = eda.SampleDistanceMatrix(spsp.csr_matrix(self.tx),
                                        metric="euclidean",
                                        use_pdist=False)
        tsdm.s_knn_connectivity_matrix(5, use_hnsw=True)
        fmp = knn.FeatureImputation(tsdm)
        res_sdml = fmp.impute_features([8], [3], [0.5], [10])

        fmp2 = knn.FeatureImputation(tsdm)
        res_sdml2 = fmp2.impute_features(8, 3, 0.5, 10, verbose=True)
        np.testing.assert_equal(res_sdml2[0]._x.A, res_sdml[0]._x.A)
 def test_no_pdist_detect_rare_samples_par(self):
     tsdm = eda.SampleDistanceMatrix(
         [[0, 0], [1, 1], [200, 200], [200, 200], [200, 200], [100, 100],
          [101, 101], [99, 99], [100, 100], [102, 102]],
         metric="euclidean",
         use_pdist=False)
     rsd = knn.RareSampleDetection(tsdm)
     resl = rsd.detect_rare_samples([3, 4, 5], [10] * 3, [5, 6, 7], 3)
     assert resl == [list(range(5, 10)), list(range(5, 10)), []]
     assert len(rsd._res_lut) == 3
     assert rsd._res_lut[(3, 10, 5)][1][-1] == resl[0]
     assert rsd._res_lut[(4, 10, 6)][1][-1] == resl[1]
     assert rsd._res_lut[(5, 10, 7)][1][-1] == resl[2]
Example #24
0
 def test_s_knn_graph_grad_lab_same_marker(self):
     np.random.seed(123)
     x = np.concatenate((np.random.normal(0, 1, 10),
                         np.random.normal(20, 1, 20))).reshape(30, -1)
     x = spsp.csr_matrix(x)
     sdm = eda.SampleDistanceMatrix(x, metric='euclidean')
     sdm.s_knn_graph(5, figsize=(5, 5))
     assert (5, 1) in sdm._knn_ng_lut
     assert len(sdm._knn_ng_lut) == 1
     gradient = np.array([1] * 10 + [10] * 20)
     labs = gradient = np.array([1] * 10 + [2] * 20)
     return sdm.s_knn_graph(5, gradient=gradient, labels=labs,
                            different_label_markers=False,
                            figsize=(5, 5),
                            alpha=0.8, random_state=123)
Example #25
0
    def test_tsne_default_init(self):
        tmet = 'euclidean'
        tsne_kwargs = {'metric': tmet, 'n_iter': 250,
                       'random_state': 123}
        ref_tsne = eda.tsne(self.x_3x2.toarray(), **tsne_kwargs)
        sdm = eda.SampleDistanceMatrix(self.x_3x2, metric=tmet)

        init_tsne = sdm._last_tsne

        assert init_tsne.shape == (3, 2)
        assert len(sdm.tsne_lut) == 1

        tsne2 = sdm.tsne(store_res=True, **tsne_kwargs)
        np.testing.assert_allclose(ref_tsne, tsne2)
        assert len(sdm.tsne_lut) == 2
    def test_no_pdist_detect_rare_samples_empty_subset(self):
        tsdm = eda.SampleDistanceMatrix(
            [[0, 0], [1, 1], [200, 200], [200, 200], [200, 200], [100, 100],
             [101, 101], [99, 99], [100, 100], [102, 102]],
            metric="euclidean",
            use_pdist=False)

        rsd = knn.RareSampleDetection(tsdm)
        resl = rsd.detect_rare_samples(1, 10, 5)
        resl2 = rsd.detect_rare_samples([1], [10], 5)
        # scalar and list params should have the same results
        assert resl == resl2
        # result lut should be the same length
        assert len(rsd._res_lut) == 1
        assert rsd._res_lut[(1, 10, 5)][1][-1] == resl[0]
Example #27
0
    def test_init_wrong_d_size(self):
        d_2x2 = np.array([[0, np.sqrt(2)],
                          [np.sqrt(2), 0]])

        d_2x2 = np.array([[0, np.sqrt(2)],
                          [np.sqrt(2), 0]])

        d_1x6 = np.arange(6)

        d_3x2 = np.array([[0, np.sqrt(2)],
                          [np.sqrt(2), 0],
                          [1, 2]])

        with pytest.raises(Exception) as excinfo:
            eda.SampleDistanceMatrix(self.x_3x2, d_2x2)

        with pytest.raises(Exception) as excinfo:
            eda.SampleDistanceMatrix(self.x_3x2, d_3x2)

        with pytest.raises(Exception) as excinfo:
            eda.SampleDistanceMatrix(self.x_3x2, d_3x2)

        with pytest.raises(Exception) as excinfo:
            eda.SampleDistanceMatrix(self.x_3x2, d_1x6)
Example #28
0
 def test_sdm_umap_feature_gradient_plus10_plot_sparse(self):
     sids = list(range(8))
     fids = [str(i) for i in range(10)]
     np.random.seed(123)
     x = np.random.ranf(80).reshape(8, -1)
     x_sorted = x[np.argsort(x[:, 5])]
     x_sorted = spsp.csr_matrix(x_sorted)
     sdm = eda.SampleDistanceMatrix(
         x_sorted, sids=sids, fids=fids)
     fig = sdm.umap_feature_gradient_plot(
         '5', transform=lambda x: x + 10, figsize=(10, 10), s=50)
     assert (sdm._x != x_sorted).nnz == 0
     np.testing.assert_equal(sdm._sids, sids)
     np.testing.assert_equal(sdm._fids, fids)
     return fig
Example #29
0
 def test_knn_ind_lut(self):
     nn_sdm = eda.SampleDistanceMatrix(
         spsp.csr_matrix([[0, 0, 0], [1, 1, 1], [5, 5, 5],
                          [6, 6, 6], [10, 10, 10],
                          [20, 20, 20]]),
                         metric='euclidean')
     assert nn_sdm.s_knn_ind_lut(0) == dict(zip(range(6), [[]]*6))
     assert (nn_sdm.s_knn_ind_lut(1) ==
             dict(zip(range(6), [[1], [0], [3], [2], [3], [4]])))
     assert (nn_sdm.s_knn_ind_lut(2) ==
             dict(zip(range(6), [[1, 2], [0, 2], [3, 1],
                                 [2, 4], [3, 2], [4, 3]])))
     assert (nn_sdm.s_knn_ind_lut(3) ==
             dict(zip(range(6), [[1, 2, 3], [0, 2, 3], [3, 1, 0],
                                 [2, 4, 1], [3, 2, 1], [4, 3, 2]])))
     nn_sdm.s_knn_ind_lut(5)
Example #30
0
 def test_sdm_truncated_pca_feature_gradient_plot_sslabs_empty(self):
     sids = list(range(8))
     fids = [str(i) for i in range(10)]
     np.random.seed(123)
     x = np.random.ranf(80).reshape(8, -1)
     x_sorted = x[np.argsort(x[:, 5])]
     x_sorted = spsp.csr_matrix(x_sorted)
     sdm = eda.SampleDistanceMatrix(
         x_sorted, sids=sids, fids=fids)
     fig = sdm.pca_feature_gradient_plot(
         '5', labels=list('abcdefgh'), selected_labels=[],
         figsize=(10, 10), s=50)
     assert (sdm._x != x_sorted).nnz == 0
     np.testing.assert_equal(sdm._sids, sids)
     np.testing.assert_equal(sdm._fids, fids)
     return fig