示例#1
0
def test_2d_stimulus_resolution():
    s2 = InvertedEncoding2D(stim_xlim=[-1, 1],
                            stim_ylim=[-1, 1],
                            stimulus_resolution=10)
    assert len(s2.stim_pixels[0] == 10)
    assert len(s2.stim_pixels[1] == 10)
    s2 = InvertedEncoding2D(stim_xlim=[-1, 1],
                            stim_ylim=[-2, 2],
                            stimulus_resolution=[10, 20])
    assert len(s2.stim_pixels[0] == 10)
    assert len(s2.stim_pixels[1] == 20)
示例#2
0
def test_can_instantiate():
    s = InvertedEncoding1D()
    assert s, "Invalid InvertedEncoding1D instance"
    s2 = InvertedEncoding2D(stim_xlim=[0, 1],
                            stim_ylim=[0, 1],
                            stimulus_resolution=[1, 1])
    assert s2, "Invalid InvertedEncoding2D instance"
示例#3
0
def test_fit_2d_radius_list():
    i2 = InvertedEncoding2D(stim_xlim=xlim,
                            stim_ylim=ylim,
                            stimulus_resolution=res,
                            stim_radius=np.random.rand(nobs))
    i2.define_basis_functions_sqgrid(nchannels=[12, 6])
    i2.fit(Xd, yd)
示例#4
0
def test_modify_2d_properties():
    nchan = 8
    res = 10
    npix = res * res
    channels = np.random.rand(nchan, npix) * 2 - 1
    bds = [-1, 1]
    s = InvertedEncoding2D(stim_xlim=bds,
                           stim_ylim=bds,
                           stimulus_resolution=res,
                           chan_xlim=bds,
                           chan_ylim=bds,
                           channels=channels)
    with pytest.raises(ValueError):
        s = s.set_params(n_channels=nchan - 1)
        assert s, "Invalid InvertedEncoding2D instance"
    with pytest.raises(ValueError):
        s = s.set_params(xp=np.random.rand(npix - 10))
        assert s, "Invalid InvertedEncoding2D instance"
    with pytest.raises(ValueError):
        s = s.set_params(stim_fov=[[0, 1], [0, -1]])
        assert s, "Invalid InvertedEncoding2D instance"
    with pytest.raises(ValueError):
        s = s.set_params(stim_fov=[[0, 1]])
        assert s, "Invalid InvertedEncoding2D instance"
    with pytest.raises(ValueError):
        s = s.set_params(stim_fov=[[0], [0, 1]])
        assert s, "Invalid InvertedEncoding2D instance"
示例#5
0
def test_cannot_instantiate_2d_channels():
    # Channel definition over wrong number of pixels (5 instead of 100)
    with pytest.raises(ValueError):
        s = InvertedEncoding2D(stim_xlim=[-1, 1],
                               stim_ylim=[-1, 1],
                               stimulus_resolution=10,
                               channels=np.random.rand(5, 5))
        assert s, "Invalid InvertedEncoding2D instance"
示例#6
0
def test_instantiate_improper_range():
    with pytest.raises(ValueError):
        s = InvertedEncoding1D(6,
                               5,
                               'halfcircular',
                               range_start=20,
                               range_stop=0)
        assert s, "Invalid InvertedEncoding1D instance"
    with pytest.raises(ValueError):
        s2 = InvertedEncoding2D(stim_xlim=[0, -1],
                                stim_ylim=[0, -1],
                                stimulus_resolution=[10, 10])
        assert s2, "Invalid InvertedEncoding2D instance"
    with pytest.raises(ValueError):
        s2 = InvertedEncoding2D(stim_xlim=[0],
                                stim_ylim=[-1, 0],
                                stimulus_resolution=10)
        assert s2, "Invalid InvertedEncoding2D instance"
示例#7
0
def test_fit_invalid_2d():
    # C=None and stim_radius=None here, cannot define C
    i2 = InvertedEncoding2D(stim_xlim=xlim,
                            stim_ylim=ylim,
                            stimulus_resolution=res,
                            stim_radius=None)
    i2.define_basis_functions_sqgrid(nchannels=[12, 6])
    with pytest.raises(ValueError):
        i2.fit(Xd, yd)
示例#8
0
def test_get_2d_params():
    bds = [-1, 1]
    res = 10
    s = InvertedEncoding2D(stim_xlim=bds,
                           stim_ylim=bds,
                           stimulus_resolution=res)
    param_out = s.get_params()
    assert np.all(param_out.get('stim_fov')[0] == bds)
    assert param_out.get('xp').size == res * res
示例#9
0
def test_data_dimensions():
    x = np.random.rand(5, 10, 2)
    s = InvertedEncoding1D()
    with pytest.raises(ValueError):
        s.fit(x, np.random.rand(5))
    s2 = InvertedEncoding2D(stim_xlim=[-1, 1],
                            stim_ylim=[-1, 1],
                            stimulus_resolution=10)
    with pytest.raises(ValueError):
        s2.fit(x, np.random.rand(5))
示例#10
0
def test_2d_cos_size_fcns():
    bds = [-1, 1]
    s = np.random.rand()
    imodel = InvertedEncoding2D(stim_xlim=bds,
                                stim_ylim=bds,
                                stimulus_resolution=10)
    fwhm = imodel._2d_cosine_sz_to_fwhm(s)
    s2 = imodel._2d_cosine_fwhm_to_sz(fwhm)
    assert np.isclose(s, s2)
    fwhm2 = imodel._2d_cosine_sz_to_fwhm(s2)
    assert np.isclose(fwhm, fwhm2)
示例#11
0
def test_data_amount():
    x = np.random.rand(5, 1000)
    s = InvertedEncoding1D()
    with pytest.raises(ValueError):
        s.fit(x, np.random.rand(5))
        assert s, "Invalid data"
    s2 = InvertedEncoding2D(stim_xlim=[-1, 1],
                            stim_ylim=[-1, 1],
                            stimulus_resolution=10)
    with pytest.raises(ValueError):
        s2.fit(x, np.random.rand(5))
示例#12
0
def test_square_basis_grid():
    nchan = 8
    bds = [-1, 1]
    s = InvertedEncoding2D(stim_xlim=bds,
                           stim_ylim=bds,
                           stimulus_resolution=10)
    _, centers = s.define_basis_functions_sqgrid(nchannels=nchan)
    assert centers.shape[0] == nchan * nchan
    xspacing = np.round(np.diff(centers[:, 0]), 5)
    yspacing = np.round(np.diff(centers[:, 1]), 5)
    assert xspacing[0] == xspacing[28] == xspacing[-1]
    assert yspacing[0] == yspacing[25] == yspacing[-1]
示例#13
0
def test_fit_custom_channel_activations():
    i2 = InvertedEncoding2D(stim_xlim=xlim,
                            stim_ylim=ylim,
                            stimulus_resolution=res,
                            stim_radius=12)
    i2.define_basis_functions_sqgrid(nchannels=[12, 6])
    # Define C by expanding y & adding noise to avoid singular W matrix error
    C0 = np.repeat(np.expand_dims(yd[:, 0], 1), 12*3, axis=1) + \
        np.random.rand(nobs, 12*3)
    C1 = np.repeat(np.expand_dims(yd[:, 1], 1), 12*3, axis=1) + \
        np.random.rand(nobs, 12*3)
    i2.fit(Xd, yd, np.hstack((C0, C1)))
    assert np.all(i2.W_)
示例#14
0
def test_2d_custom_channels():
    nchan = 8
    res = 10
    npix = res * res
    channels = np.random.rand(nchan, npix) * 2 - 1
    bds = [-1, 1]
    s = InvertedEncoding2D(stim_xlim=bds,
                           stim_ylim=bds,
                           stimulus_resolution=res,
                           chan_xlim=bds,
                           chan_ylim=bds,
                           channels=channels)
    assert s, "Unable to define custom InvertedEncoding2D channels"
示例#15
0
def test_triangular_basis_grid():
    grid_rad = 3
    n_channels = (grid_rad * 2 + 1) * (grid_rad * 2)
    bds = [-1, 1]
    s = InvertedEncoding2D(stim_xlim=bds,
                           stim_ylim=bds,
                           stimulus_resolution=10)
    _, centers = s.define_basis_functions_trigrid(grid_rad)
    assert centers.shape[0] == n_channels
    xspacing = np.round(np.diff(centers[:, 0]), 4)
    assert xspacing[0] == xspacing[np.random.randint(n_channels)] == \
        xspacing[-1]
    ysp = xspacing[0] * np.sqrt(3) * 0.5
    yspacing = np.diff(centers[:, 1])
    yspace = yspacing[yspacing > 0.0]
    assert np.all((ysp - yspace) < 1e-5)
示例#16
0
def test_2d_cos():
    nchan = 8
    res = 10
    npix = res * res
    bds = [-1, 1]
    sz = 2
    s = InvertedEncoding2D(stim_xlim=bds,
                           stim_ylim=bds,
                           stimulus_resolution=res,
                           channels=np.random.rand(nchan, npix))
    sz = s._2d_cosine_fwhm_to_sz(1)
    fcn = s._make_2d_cosine(s.xp.reshape(-1, 1), s.yp.reshape(-1, 1),
                            np.linspace(bds[0], bds[1], nchan),
                            np.linspace(bds[0], bds[1], nchan), sz)
    assert fcn.shape == (nchan, npix)
    # Test that masking works -- basis function should have fewer non-zero
    # elements than specified by the size constant
    xd = np.diff(s.xp)[0][0]
    nval = (np.nonzero(fcn[0, :])[0]).size
    assert nval * (xd**2) <= sz**2
示例#17
0
    i2 = InvertedEncoding2D(stim_xlim=xlim,
                            stim_ylim=ylim,
                            stimulus_resolution=res,
                            stim_radius=12)
    i2.define_basis_functions_sqgrid(nchannels=[12, 6])
    # Define C by expanding y & adding noise to avoid singular W matrix error
    C0 = np.repeat(np.expand_dims(yd[:, 0], 1), 12*3, axis=1) + \
        np.random.rand(nobs, 12*3)
    C1 = np.repeat(np.expand_dims(yd[:, 1], 1), 12*3, axis=1) + \
        np.random.rand(nobs, 12*3)
    i2.fit(Xd, yd, np.hstack((C0, C1)))
    assert np.all(i2.W_)


iem_2d = InvertedEncoding2D(stim_xlim=xlim,
                            stim_ylim=ylim,
                            stimulus_resolution=res,
                            stim_radius=12)
iem_2d.define_basis_functions_sqgrid(nchannels=[12, 6])


# Test if valid data can be fit.
def test_can_fit_2d_data():
    iem_2d.fit(Xd, yd)


# Show that a data matrix with improper format (dimensions) breaks the
# algorithm.
def test_cannot_fit_2d_data():
    with pytest.raises(ValueError):
        iem_2d.fit(Xd.transpose(), yd)