Exemple #1
0
def test_rfsize():
    np.random.seed(0)
    filter_length = 100
    nx, ny = 10, 10
    true_filter = utils.create_spatiotemporal_filter(nx, ny, filter_length)[1]

    xsize, ysize = flt.rfsize(true_filter, 1., 1.)
    assert np.allclose(xsize, 3., 0.1)  # 1 SD is about 3 units
    assert np.allclose(ysize, 3., 0.1)
def test_ellipse():
    """Test plotting an ellipse fitted to an RF."""
    nx, ny, nt = 10, 10, 50
    sta = utils.create_spatiotemporal_filter(nx, ny, nt)[-1]
    filename = os.path.join(IMG_DIR, 'test-ellipse.png')
    viz.ellipse(sta)
    plt.savefig(filename)
    assert not compare_images(os.path.join(IMG_DIR, 'baseline-ellipse.png'),
                              filename, 1)
    os.remove(filename)
    plt.close('all')
def test_spatial_filter():
    """Test plotting a spatial filter from a full 3D spatiotemporal STA."""
    nx, ny, nt = 10, 10, 50
    time = np.arange(nt)
    sta = utils.create_spatiotemporal_filter(nx, ny, nt)[-1]
    viz.spatial(sta)
    filename = os.path.join(IMG_DIR, 'test-spatial-filter.png')
    plt.savefig(filename)
    assert not compare_images(
        os.path.join(IMG_DIR, 'baseline-spatial-filter.png'), filename, 1)
    os.remove(filename)
    plt.close('all')
def test_anim_to_html():
    """Test converting an animation to HTML."""
    try:
        from IPython.display import HTML
    except ImportError:
        pytest.skip('Cannot convert movie to HTML without IPython.')

    nx, ny, nt = 10, 10, 50
    sta = utils.create_spatiotemporal_filter(nx, ny, nt)[-1]
    anim = viz.play_sta(sta)
    html = viz.anim_to_html(viz.play_sta(sta))
    assert isinstance(html, HTML)
Exemple #5
0
def test_normalize_spatial():
    """Test normalizing a noisy filter."""
    np.random.seed(0)
    filter_length = 100
    nx, ny = 10, 10
    true_filter = utils.create_spatiotemporal_filter(nx, ny, filter_length)[1]
    noise_std = 0.01
    noisy_filter = true_filter + 1.0 + np.random.randn(*true_filter.shape) * 0.01

    normalized = flt.normalize_spatial(noisy_filter)
    normalized /= np.linalg.norm(normalized)

    atol = 0.1
    assert np.allclose(normalized, true_filter, atol=atol)
def test_plot_cells():
    """Test plotting ellipses for multiple cells on the same axes."""
    nx, ny, nt = 10, 10, 50
    stas = []
    ncells = 2
    for cell in range(ncells):
        stas.append(utils.create_spatiotemporal_filter(nx, ny, nt)[-1])

    filename = os.path.join(IMG_DIR, 'test-plotcells.png')
    np.random.seed(0)  # plot_cells() uses random colors for each cell
    viz.plot_cells(stas)
    plt.savefig(filename)
    assert not compare_images(os.path.join(IMG_DIR, 'baseline-plotcells.png'),
                              filename, 1)
    os.remove(filename)
    plt.close('all')
Exemple #7
0
def test_decompose():
    """Tests computing a rank-1 approximation to a filter.
    Note that this tests both filtertools.decompose() and filtertools.lowranksta().
    """
    np.random.seed(0)
    filter_length = 50
    nx, ny = 10, 10
    temporal, spatial, true_filter = utils.create_spatiotemporal_filter(nx, ny, filter_length)

    noise_std = 0.01
    true_filter += np.random.randn(*true_filter.shape) * noise_std

    s, t = flt.decompose(true_filter)

    tol = 0.1
    assert np.allclose(temporal, t, atol=tol)
    assert np.allclose(spatial, s, atol=tol)
Exemple #8
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)
def test_play_sta():
    """Test playing an STA as a movie.
    
    Matplotlib doesn't yet have a way to compare movies, and the formats
    and precise bytes written by different encoding libraries are too variable
    to test reliably. Instead, we write a specific frame of the animation
    to disk as an image, and compare it with a baseline.
    """
    nx, ny, nt = 10, 10, 50
    sta = utils.create_spatiotemporal_filter(nx, ny, nt)[-1]
    anim = viz.play_sta(sta)
    filename = os.path.join(IMG_DIR, 'test-sta-movie.png')
    frame = 10
    anim._func(frame)
    plt.savefig(filename)
    assert not compare_images(
        os.path.join(IMG_DIR, 'baseline-sta-movie-frame.png'), filename, 1)
    os.remove(filename)
    plt.close('all')
def test_play_rates():
    """Test playing firing rates for cells as a movie."""
    nx, ny, nt = 10, 10, 50
    sta = utils.create_spatiotemporal_filter(nx, ny, nt)[-1]
    time = np.linspace(0, 10, 100)
    spikes = np.arange(10)
    binned_spikes = spiketools.binspikes(spikes, time)
    rate = spiketools.estfr(binned_spikes, time)

    # Plot cell
    fig, axes = viz.ellipse(sta)
    patch = plt.findobj(axes, Ellipse)[0]
    anim = viz.play_rates(rate, patch)
    filename = os.path.join(IMG_DIR, 'test-rates-movie.png')
    frame = 10
    anim._func(frame)
    plt.savefig(filename)
    assert not compare_images(
        os.path.join(IMG_DIR, 'baseline-rates-movie-frame.png'), filename, 1)
    os.remove(filename)
    plt.close('all')
Exemple #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_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)
def test_spatiotemporal_filter():
    """Test plotting a full 3D spatiotemporal STA."""
    nx, ny, nt = 10, 10, 50
    time = np.arange(nt)
    t, s, sta = utils.create_spatiotemporal_filter(nx, ny, nt)

    # Test plotting temporal component
    filename = os.path.join(IMG_DIR, 'test-temporal-filter.png')
    viz.plot_sta(time, t)
    plt.savefig(filename)
    assert not compare_images(
        os.path.join(IMG_DIR,
                     'baseline-temporal-from-spatiotemporal-filter.png'),
        filename, 1)
    os.remove(filename)
    plt.close('all')

    # Test plotting spatial component
    filename = os.path.join(IMG_DIR, 'test-temporal-filter.png')
    viz.plot_sta(time, s)
    plt.savefig(filename)
    assert not compare_images(
        os.path.join(IMG_DIR,
                     'baseline-spatial-from-spatiotemporal-filter.png'),
        filename, 1)
    os.remove(filename)
    plt.close('all')

    # Test plotting both spatial/temporal components
    filename = os.path.join(IMG_DIR, 'test-full-spatiotemporal-filter.png')
    viz.plot_sta(time, sta)
    plt.savefig(filename)
    assert not compare_images(
        os.path.join(IMG_DIR, 'baseline-full-spatiotemporal-filter.png'),
        filename, 1)
    os.remove(filename)
    plt.close('all')