예제 #1
0
def test_cca_crossvalidate():
    """Test CCA with crossvalidation."""
    # x = np.random.randn(1000, 11)
    # y = np.random.randn(1000, 9)
    # xx = [x, x, x]
    # yy = [x[:, :9], y, y]

    mat = loadmat('./tests/data/ccadata2.mat')
    xx = mat['x']
    yy = mat['y']
    R1 = mat['R']  # no shifts

    # Test with no shifts
    A, B, R = cca_crossvalidate(xx, yy)

    assert_almost_equal(R, R1, decimal=2)

    # Create data where 1st comps should be uncorrelated, and 2nd and 3rd comps
    # are very correlated
    x = np.random.randn(1000, 10)
    y = np.random.randn(1000, 10)
    xx = [x, x, x]
    yy = [y, x, x]
    A, B, R = cca_crossvalidate(xx, yy)
    assert_almost_equal(R[:, :, 0], 0, decimal=1)
    assert_almost_equal(R[:, :, 1:], 1, decimal=1)
예제 #2
0
def test_cca_crossvalidate_shifts2():
    """Test CCA crossvalidation with shifts."""
    mat = loadmat('./tests/data/ccacrossdata.mat')
    xx = mat['xx2']
    yy = mat['yy2']
    R2 = mat['R'][:, ::-1, :]  # shifts go in reverse direction in noisetools

    # Test with shifts
    A, B, R = cca_crossvalidate(xx, yy, shifts=[-3, -2, -1, 0, 1, 2, 3])

    # correlations are ~ those of Matlab
    assert_almost_equal(R, R2, decimal=3)
예제 #3
0
def test_cca_crossvalidate_shifts2():
    """Test CCA crossvalidation with shifts."""
    mat = loadmat('./tests/data/ccacrossdata.mat')
    xx = mat['xx2']
    yy = mat['yy2']
    R2 = mat['R']

    # Test with shifts
    A, B, R = cca_crossvalidate(xx, yy, shifts=[-3, -2, -1, 0, 1, 2, 3])

    # correlations are ~ those of Maltab (very loose check, suspect numerical
    # inaccuracy in normcol)
    assert_almost_equal(R[:3, 1:-1, ...], R2[:3, 1:-1, ...], decimal=2)
    assert_almost_equal(R, R2, decimal=1)
예제 #4
0
def test_cca_crossvalidate_shifts():
    """Test CCA crossvalidation with shifts."""
    n_times, n_trials = 10000, 2
    x = np.random.randn(n_times, 20, n_trials)
    y = np.random.randn(n_times, 8, n_trials)
    # perfectly correlated
    y[:, :2, :] = x[:, :2, :]
    # 1/2 correlated
    y[:, 2:4, :] = x[:, 2:4, :] + np.random.randn(n_times, 2, n_trials)
    # 1/4 correlated
    y[:, 4:6, :] = x[:, 4:6, :] + np.random.randn(n_times, 2, n_trials) * 3
    # uncorrelated
    y[:, 6:8, :] = np.random.randn(n_times, 2, n_trials)

    xx = multishift(x, -np.arange(1, 4), reshape=True, solution='valid')
    yy = multishift(y, -np.arange(1, 4), reshape=True, solution='valid')

    # Test with shifts
    A, B, R = cca_crossvalidate(xx, yy, shifts=[-3, -2, -1, 0, 1, 2, 3])