예제 #1
0
def test_revcorr_raises():
    """Test raising ValueErrors with incorrect inputs"""
    np.random.seed(0)
    with pytest.raises(ValueError):
        flt.revcorr(np.random.randn(10, ), np.random.randn(10, 2), 2)
    with pytest.raises(ValueError):
        flt.revcorr(np.random.randn(10, 2), np.random.randn(10, 3), 2)
예제 #2
0
def test_revcorr_acausal():
    """Test computation of a 1D linear filter by reverse correlation,
    including acausal lag values.
    """
    np.random.seed(0)

    # Create fake filter, 100 time points
    nbefore, nafter = 90, 10
    filter_length = nbefore + nafter
    true_filter = utils.create_temporal_filter(filter_length)

    # Compute linear response
    stim_length = 10000
    stimulus = np.random.randn(stim_length,)
    response = flt.linear_response(true_filter, stimulus)

    # Reverse correlation, pad response with zeros to so that
    # stim and response match. These will be ignored by
    # filtertools.revcorr anyway.
    padded_response = np.concatenate((np.zeros((filter_length - 1,)),
            response), axis=0)
    filt = flt.revcorr(stimulus, padded_response, nbefore, nafter)[0]
    filt /= np.linalg.norm(filt)
    tol = 0.1
    assert np.allclose(true_filter, filt, atol=tol)
예제 #3
0
def test_revcorr_1d_ignores_beginning():
    """Verify revcorr ignores the first filter-length points of the stimulus,
    to only consider those points which the response and stimulus overlap
    completely.
    """
    filt = np.array(((1, 0, 0)))
    stim = np.concatenate(((1, ), np.zeros((10, ))), axis=0)
    response = np.convolve(filt, stim, 'full')[:stim.size]
    recovered, lags = flt.revcorr(stim, response, filt.size)
    assert np.allclose(recovered, 0)
예제 #4
0
def test_revcorr_1d_ignores_beginning():
    """Verify revcorr ignores the first filter-length points of the stimulus,
    to only consider those points which the response and stimulus overlap
    completely.
    """
    filt = np.array(((1, 0, 0)))
    stim = np.concatenate(((1,), np.zeros((10,))), axis=0)
    response = np.convolve(filt, stim, 'full')[:stim.size]
    recovered, lags = flt.revcorr(stim, response, filt.size)
    assert np.allclose(recovered, 0)
예제 #5
0
def test_revcorr_nd():
    """Test computation of 3D linear filter by reverse correlation.
    The reverse correlation should return the time-reverse of the
    linear filter, scaled by the number of spatial points.
    """
    ndim = 3
    filt = np.zeros((3, ) + ((2, ) * ndim))
    filt[0] = 1.
    stim = np.zeros((10, ) + ((2, ) * ndim))
    stim[5] = 1.
    response = flt.linear_response(filt, stim)
    recovered, lags = flt.revcorr(stim, response, filt.shape[0])
    assert np.allclose(recovered[-1], filt[0].sum())
    assert np.allclose(recovered[:-1], 0)
예제 #6
0
def test_revcorr_1d():
    """Test computation of 1D reverse correlation.
    The reverse-correlation should recover the time-reverse of the
    linear filter, and the lags should be start at negative values
    and be strictly increasing.
    """
    filt = np.array(((1, 0, 0)))
    stim = np.zeros((10, ))
    stim[5] = 1
    response = np.convolve(filt, stim, 'full')[:stim.size]
    recovered, lags = flt.revcorr(stim, response, filt.size)
    assert np.allclose(recovered, filt[::-1])
    assert lags[0] == -(filt.size - 1)
    assert (np.diff(lags) == 1).all()
예제 #7
0
def test_revcorr_nd():
    """Test computation of 3D linear filter by reverse correlation.
    The reverse correlation should return the time-reverse of the
    linear filter, scaled by the number of spatial points.
    """
    ndim = 3
    filt = np.zeros((3,) + ((2,) * ndim))
    filt[0] = 1.
    stim = np.zeros((10,) + ((2,) * ndim))
    stim[5] = 1.
    response = flt.linear_response(filt, stim)
    recovered, lags = flt.revcorr(stim, response, filt.shape[0])
    assert np.allclose(recovered[-1], filt[0].sum())
    assert np.allclose(recovered[:-1], 0)
예제 #8
0
def test_revcorr_1d():
    """Test computation of 1D reverse correlation.
    The reverse-correlation should recover the time-reverse of the
    linear filter, and the lags should be start at negative values
    and be strictly increasing.
    """
    filt = np.array(((1, 0, 0)))
    stim = np.zeros((10,))
    stim[5] = 1
    response = np.convolve(filt, stim, 'full')[:stim.size]
    recovered, lags = flt.revcorr(stim, response, filt.size)
    assert np.allclose(recovered, filt[::-1])
    assert lags[0] == -(filt.size - 1)
    assert (np.diff(lags) == 1).all()
예제 #9
0
def test_revcorr_acausal():
    """Test computation of a 1D linear filter by reverse correlation,
    including acausal lag values. The reverse-correlation should recover
    the time-reverse of the linear filter.
    """
    filt = np.array(((1, 0, 0)))
    stim = np.zeros((10, ))
    stim[5] = 1.0
    response = np.convolve(filt, stim, 'full')[:stim.size]
    nsamples_after = 2
    recovered, lags = flt.revcorr(stim, response, filt.size, nsamples_after)
    assert np.allclose(recovered[nsamples_after:], filt[::-1])
    assert np.allclose(recovered[:filt.size], 0)
    assert lags[0] == -(filt.size - 1)
    assert lags[-1] == nsamples_after
    assert (np.diff(lags) == 1).all()
예제 #10
0
def test_revcorr_acausal():
    """Test computation of a 1D linear filter by reverse correlation,
    including acausal lag values. The reverse-correlation should recover
    the time-reverse of the linear filter.
    """
    filt = np.array(((1, 0, 0)))
    stim = np.zeros((10,))
    stim[5] = 1.0
    response = np.convolve(filt, stim, 'full')[:stim.size]
    nsamples_after = 2
    recovered, lags = flt.revcorr(stim, response, filt.size, nsamples_after)
    assert np.allclose(recovered[nsamples_after:], filt[::-1])
    assert np.allclose(recovered[:filt.size], 0)
    assert lags[0] == -(filt.size - 1)
    assert lags[-1] == nsamples_after
    assert (np.diff(lags) == 1).all()
예제 #11
0
def test_revcorr_nd():
    np.random.seed(0)
    """Test computation of 3D linear filter by reverse correlation"""
    # Create fake filter
    filter_length = 100
    nx, ny = 10, 10
    true_filter = utils.create_spatiotemporal_filter(nx, ny, filter_length)[-1]

    # Compute linear response
    stim_length = 10000
    stimulus = np.random.randn(stim_length, nx, ny)
    response = flt.linear_prediction(true_filter, stimulus)

    # Reverse correlation
    filt = flt.revcorr(response, stimulus, filter_length)
    filt /= np.linalg.norm(filt)
    tol = 0.1
    assert np.allclose(true_filter, filt, atol=tol)
예제 #12
0
def test_revcorr_1d():
    """Test computation of a 1D linear filter by reverse correlation"""
    np.random.seed(0)

    # Create fake filter, 100 time points
    filter_length = 100
    true_filter = utils.create_temporal_filter(filter_length)

    # Compute linear response
    stim_length = 10000
    stimulus = np.random.randn(stim_length, )
    response = flt.linear_prediction(true_filter, stimulus)

    # Reverse correlation
    filt = flt.revcorr(response, stimulus, filter_length)
    filt /= np.linalg.norm(filt)
    tol = 0.1
    assert np.allclose(true_filter, filt, atol=tol)
예제 #13
0
def test_revcorr_nd():
    np.random.seed(0)

    """Test computation of 3D linear filter by reverse correlation"""
    # Create fake filter
    filter_length = 100
    nx, ny = 10, 10
    true_filter = utils.create_spatiotemporal_filter(nx, ny, filter_length)[-1]

    # Compute linear response
    stim_length = 10000
    stimulus = np.random.randn(stim_length, nx, ny)
    response = flt.linear_response(true_filter, stimulus)

    # Reverse correlation, pad response with zeros to so that
    # stim and response match. These will be ignored by
    # filtertools.revcorr anyway.
    padded_response = np.concatenate((np.zeros((filter_length - 1,)),
            response), axis=0)
    filt = flt.revcorr(stimulus, padded_response, filter_length)[0]
    filt /= np.linalg.norm(filt)
    tol = 0.1
    assert np.allclose(true_filter, filt, atol=tol)
예제 #14
0
def test_revcorr_raises():
    """Test raising ValueErrors with incorrect inputs"""
    with pytest.raises(ValueError):
        flt.revcorr(np.zeros((10, 1)), np.zeros((11, )), 2)[0]
    with pytest.raises(ValueError):
        flt.revcorr(np.zeros((10, 3)), np.zeros((10, 2)), 2)[0]
예제 #15
0
def test_revcorr_raises():
    """Test raising ValueErrors with incorrect inputs"""
    with pytest.raises(ValueError):
        flt.revcorr(np.zeros((10, 1)), np.zeros((11,)), 2)[0]
    with pytest.raises(ValueError):
        flt.revcorr(np.zeros((10, 3)), np.zeros((10, 2)), 2)[0]