Ejemplo n.º 1
0
def test_kernels():
    rs = np.random.RandomState(0)
    x = rs.randn(10, 15)
    a = np.corrcoef(x)

    # with pytest.warns(UserWarning):
    a2 = compute_affinity(a, sparsity=None)
    assert np.count_nonzero(a2 < 0) == 0

    a2 = compute_affinity(a, sparsity=.7)
    assert np.count_nonzero(a2 < 0) == 0
    assert np.all(np.count_nonzero(a2, axis=1) == 3)

    a2 = compute_affinity(a, kernel='cosine', sparsity=.7)
    assert np.count_nonzero(a2 < 0) == 0
Ejemplo n.º 2
0
def test_embedding_gradient():
    rs = np.random.RandomState(0)
    x = rs.randn(100, 50)
    x2 = rs.randn(100, 50)
    a = compute_affinity(x, kernel='gaussian', sparsity=0.7)
    a_sparse = coo_matrix(a)

    dop = {
        'dm': emb.DiffusionMaps,
        'le': emb.LaplacianEigenmaps,
        'pca': emb.PCAMaps
    }

    for app_name, app in dop.items():
        # test dense
        m = app(random_state=0)
        m.fit(a)

        assert m.lambdas_.shape == (10, )
        assert m.maps_.shape == (100, 10)
        if app_name == 'le':
            assert np.allclose(m.lambdas_, np.sort(m.lambdas_))
        else:
            assert np.allclose(m.lambdas_, np.sort(m.lambdas_)[::-1])

        # test sparse
        m2 = app(random_state=0)
        if app_name == 'pca':
            with pytest.raises(Exception):
                m2.fit(a_sparse)
        else:
            m2.fit(a_sparse)
            assert np.allclose(m.lambdas_, m2.lambdas_)
            assert np.allclose(m.maps_, m2.maps_)

        # test with gradientmaps
        gm = GradientMaps(approach=app_name, kernel='gaussian', random_state=0)
        gm.fit(x, sparsity=0.7)

        assert np.allclose(gm.lambdas_, m.lambdas_)
        assert np.allclose(gm.gradients_, m.maps_)
        assert gm.aligned_ is None

    # test alignment
    for align in [None, 'procrustes', 'joint']:
        gm_dm = GradientMaps(approach='dm',
                             kernel='gaussian',
                             alignment=align,
                             random_state=0)
        gm_dm.fit([x, x2], sparsity=0.7)

        assert len(gm_dm.gradients_) == 2
        assert len(gm_dm.lambdas_) == 2

        if align is None:
            assert gm_dm.aligned_ is None

        elif align == 'procrutes':
            for i in range(2):
                assert not np.all(gm_dm.aligned_[i] == gm_dm.gradients_[i])

        elif align == 'joint':
            for i in range(2):
                assert np.all(gm_dm.aligned_[i] == gm_dm.gradients_[i])
Ejemplo n.º 3
0
for i in TD_index:
    conn_mat_TD.append(np.load(file_list[i]))

conn_mat_ASD = np.array(conn_mat_ASD)
conn_mat_TD = np.array(conn_mat_TD)
conn_mat = np.concatenate((conn_mat_ASD, conn_mat_TD))
print('conn_mat_ASD : ', conn_mat_ASD.shape, '     ', 'conn_mat_TD : ',
      conn_mat_TD.shape, '     ', 'conn_mat : ', conn_mat.shape)

# # Make affine matrix
affine_conn_mat = []

for i, x in enumerate(conn_mat):
    print(i, ' ', end='', flush=True)
    z_conn_mat = np.nan_to_num(np.arctanh(np.nan_to_num(x, nan=0.0)), nan=0.0)
    noaff_conn_mat = gradient.compute_affinity(z_conn_mat, sparsity=0.7)
    affine_conn_mat.append(noaff_conn_mat)

affine_conn_mat = np.array(affine_conn_mat)
np.save(savepath)

grpmean_conn_mat = np.nan_to_num(np.arctanh(
    np.nan_to_num(np.mean(conn_mat, axis=0), nan=0.0)),
                                 nan=0.0)

noaff_grpmean_conn_mat = gradient.compute_affinity(
    grpmean_conn_mat, sparsity=0.7)  # sparsity로 thresholding ratio 조절
noaff_grpmean_conn_mat = np.nan_to_num(noaff_grpmean_conn_mat, nan=0.0)

grpmean_conn_mat = noaff_grpmean_conn_mat
np.save(savepath)