def test_multishift(): """Test matrix multi-shifting.""" # multishift() x = np.array([ [1, 2, 3, 4], [5, 6, 7, 8]]) assert_equal(multishift(x, [1], axis=0), np.array([[0, 0, 0, 0], [1, 2, 3, 4]])) # multishift 3d, solution='valid' x = np.ones((4, 4, 3)) x[..., 1] *= 2 x[..., 2] *= 3 xx = multishift(x, [-1, -2], reshape=True, solution='valid') assert_equal(xx[..., 0], np.array([[1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1.]])) assert_equal(xx[..., 1], np.array([[1., 1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1., 1.]]) * 2) # relshift() 1d y, y_ref = relshift([1, 2, 3, 4], [11, 12, 13, 14], [1], axis=0) assert_equal(y.flatten(), [0, 1, 2, 3]) assert_equal(y_ref.flatten(), [0, 12, 13, 14]) y, y_ref = relshift(np.arange(1, 6), np.arange(1, 6), [0, 1], axis=0) assert_equal(y, np.array([[1., 0.], [2., 1.], [3., 2.], [4., 3.], [5., 4.]])) # relshift() 2d x = [[1, 2, 3, 4], [5, 6, 7, 8]] x_ref = [[11, 12, 13, 14], [15, 16, 17, 18]] y, y_ref = relshift(x, x_ref, [0, 1]) assert_equal(y[..., 0], [[1, 2, 3, 4], [5, 6, 7, 8]]) assert_equal(y[..., 1], [[0, 0, 0, 0], [1, 2, 3, 4]]) assert_equal(y_ref[..., 0], [[11, 12, 13, 14], [15, 16, 17, 18]]) assert_equal(y_ref[..., 1], [[0, 0, 0, 0], [15, 16, 17, 18]])
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])