Exemplo n.º 1
0
def test_linear_response_only_acausal():
    """Test that calling ``linear_response`` with only acausal
    points is invalid.
    """
    with pytest.raises(ValueError):
        flt.linear_response(np.zeros((3,)), np.zeros((10,)),
                nsamples_after=3)
Exemplo n.º 2
0
def test_linear_response_raises():
    """Test raising ValueErrors with incorrect inputs"""
    np.random.seed(0)
    with pytest.raises(ValueError):
        flt.linear_response(np.random.randn(10,), np.random.randn(10,2))
    with pytest.raises(ValueError):
        flt.linear_response(np.random.randn(10, 2), np.random.randn(10, 3))
Exemplo n.º 3
0
def test_linear_response_only_acausal():
    """Test that calling ``linear_response`` with only acausal
    points is invalid.
    """
    with pytest.raises(ValueError):
        flt.linear_response(np.zeros((3, )),
                            np.zeros((10, )),
                            nsamples_after=3)
Exemplo n.º 4
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)
Exemplo n.º 5
0
def test_linear_response_1d():
    """Test method for computing linear response from a
    filter to a one-dimensional stimulus. The linear response of the
    filter to an impulse should return the filter itself.
    """
    filt = np.array(((1, 0, 0)))
    stim = np.concatenate(((1,), np.zeros((10,))), axis=0)
    pred = flt.linear_response(filt, stim) # Impulse response is linear filter
    assert np.allclose(pred[:filt.size], filt)
    assert np.allclose(pred[filt.size:], np.zeros_like(pred[filt.size:]))
Exemplo n.º 6
0
def test_linear_response_1d():
    """Test method for computing linear response from a
    filter to a one-dimensional stimulus. The linear response of the
    filter to an impulse should return the filter itself.
    """
    filt = np.array(((1, 0, 0)))
    stim = np.concatenate(((1, ), np.zeros((10, ))), axis=0)
    pred = flt.linear_response(filt, stim)  # Impulse response is linear filter
    assert np.allclose(pred[:filt.size], filt)
    assert np.allclose(pred[filt.size:], np.zeros_like(pred[filt.size:]))
Exemplo n.º 7
0
def test_linear_response_acausal():
    """Test computing a linear response from a filter to a 1D stimulus,
    including acausal portions of the stimulus.
    """
    np.random.seed(0)
    filt = np.random.randn(100,)
    stim = np.random.randn(1000,)
    pred = flt.linear_response(filt, stim, 10)

    sl = slicestim(stim, filt.shape[0] - 10, 10)
    assert np.allclose(sl.dot(filt), pred)
Exemplo n.º 8
0
def test_linear_response_1d():
    """Test method for computing linear response from a
    filter to a one-dimensional stimulus.
    """
    np.random.seed(0)
    filt = np.random.randn(100,)
    stim = np.random.randn(1000,)
    pred = flt.linear_response(filt, stim)

    sl = slicestim(stim, filt.shape[0])
    assert np.allclose(sl.dot(filt), pred)
Exemplo n.º 9
0
def test_linear_response_acausal():
    """Test computing a linear response from a filter to a 1D stimulus,
    including acausal portions of the stimulus. The linear response of
    the filter to an impulse should return the filter itself, plus
    zeros at any acausal time points.
    """
    nacausal_points = 1
    filt = np.concatenate((np.zeros((nacausal_points, )), (1, 0, 0)), axis=0)
    stim = np.concatenate(((1, ), np.zeros((10, ))), axis=0)
    pred = flt.linear_response(filt, stim, nacausal_points)
    assert np.allclose(pred[:filt.size - nacausal_points],
                       filt[nacausal_points:])
    assert np.allclose(pred[filt.size:], np.zeros_like(pred[filt.size:]))
Exemplo n.º 10
0
def test_linear_response_acausal():
    """Test computing a linear response from a filter to a 1D stimulus,
    including acausal portions of the stimulus. The linear response of
    the filter to an impulse should return the filter itself, plus
    zeros at any acausal time points.
    """
    nacausal_points = 1
    filt = np.concatenate((np.zeros((nacausal_points,)), (1, 0, 0)), axis=0)
    stim = np.concatenate(((1,), np.zeros((10,))), axis=0)
    pred = flt.linear_response(filt, stim, nacausal_points)
    assert np.allclose(pred[:filt.size - nacausal_points],
            filt[nacausal_points:])
    assert np.allclose(pred[filt.size:], np.zeros_like(pred[filt.size:]))
Exemplo n.º 11
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)
Exemplo n.º 12
0
def test_linear_response_nd():
    """Test method for computing linear response from a
    filter to a multi-dimensional stimulus. The linear response of
    the filter to an impulse (1 at first time point in all spatial dimensions)
    should return the filter itself, scaled by the number of spatial points.
    """
    for ndim in range(2, 4):
        filt = np.zeros((3, ) + ((2, ) * ndim))
        filt[0] = 1.
        stim = np.zeros((10, ) + ((2, ) * ndim))
        stim[0] = 1.
        pred = flt.linear_response(filt, stim)
        assert np.allclose(pred[0], filt[0].sum())
        assert np.allclose(pred[1:], np.zeros_like(pred[1:]))
Exemplo n.º 13
0
def test_linear_response_nd():
    """Test method for computing linear response from a
    filter to a multi-dimensional stimulus. The linear response of
    the filter to an impulse (1 at first time point in all spatial dimensions)
    should return the filter itself, scaled by the number of spatial points.
    """
    for ndim in range(2, 4):
        filt = np.zeros((3,) + ((2,) * ndim))
        filt[0] = 1.
        stim = np.zeros((10,) + ((2,) * ndim))
        stim[0] = 1.
        pred = flt.linear_response(filt, stim)
        assert np.allclose(pred[0], filt[0].sum())
        assert np.allclose(pred[1:], np.zeros_like(pred[1:]))
Exemplo n.º 14
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)
Exemplo n.º 15
0
def test_linear_response_nd():
    """Test method for computing linear response from a
    filter to a multi-dimensional stimulus.
    """
    np.random.seed(0)
    for ndim in range(2, 4):
        filt = np.random.randn(100, *((10,) * ndim))
        stim = np.random.randn(1000, *((10,) * ndim))
        pred = flt.linear_response(filt, stim)

        sl = slicestim(stim, filt.shape[0])
        tmp = np.zeros(sl.shape[0])
        for i in range(tmp.size):
            tmp[i] = np.inner(filt.ravel(), sl[i].ravel())

        assert np.allclose(tmp, pred)
Exemplo n.º 16
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)
Exemplo n.º 17
0
def test_linear_response_raises():
    """Test raising ValueErrors with incorrect inputs"""
    with pytest.raises(ValueError):
        flt.linear_response(np.zeros((10, )), np.zeros((10, 2)))
    with pytest.raises(ValueError):
        flt.linear_response(np.zeros((10, 2)), np.zeros((10, 3)))
Exemplo n.º 18
0
def test_linear_response_raises():
    """Test raising ValueErrors with incorrect inputs"""
    with pytest.raises(ValueError):
        flt.linear_response(np.zeros((10,)), np.zeros((10,2)))
    with pytest.raises(ValueError):
        flt.linear_response(np.zeros((10, 2)), np.zeros((10, 3)))