示例#1
0
nlts *= st.refresh_rate

#%%
pd = steps_proj > 0.5
npd = steps_proj < -0.5

pd_spikes = all_spikes.copy()
npd_spikes = all_spikes.copy()

# Exclude spikes that are not in desired direction to zero
pd_spikes[~pd] = 0
npd_spikes[~npd] = 0

#%%
filter_length = 20
contrast = st.generatecontrast([100, 100], 100, filter_length-1) # subtract one because rw generates one extra
#contrast = st.generatecontrastmaxi([0, 0], 800, filter_length-1) # subtract one because rw generates one extra
contrast_avg = contrast[:, :, filter_length+1:].mean(axis=-1)
rw = asc.rolling_window(contrast, filter_length, preserve_dim=False)
#%%
stas = np.einsum('abcd,ec->eabd', rw, all_spikes)
stas = stas / all_spikes.sum(axis=1)[:, None, None, None]

from scratch_spikeshuffler import shufflebyrow

shuffled_spikes = shufflebyrow(all_spikes)

shuffled_stas = np.einsum('abcd,ec->eabd', rw, shuffled_spikes)
shuffled_stas = shuffled_stas / shuffled_spikes.sum(axis=1)[:, None, None, None]

pdstas = np.einsum('abcd,ec->eabd', rw, pd_spikes)
示例#2
0
from omb import OMB

exp, ombstimnr = '20180710', 8

checkerstim = 6

st = OMB(exp, ombstimnr, maxframes=10000)

all_spikes = np.zeros((st.nclusters, st.ntotal))

for i in range(st.nclusters):
    all_spikes[i, :] = st.binnedspiketimes(i)

filter_length = 20

contrast = st.generatecontrast(st.texpars.noiselim / 2, 50, filter_length - 1)
contrast_avg = contrast.mean(axis=-1)

rw = asc.rolling_window(contrast, filter_length, preserve_dims=False)
#rws = rw.transpose((2, 0, 1, 3))

stas = np.einsum('abcd,ec->eabd', rw, all_spikes)
stas = stas / all_spikes.sum(axis=1)[:, None, None, None]

from scratch_spikeshuffler import shufflebyrow

shuffled_spikes = shufflebyrow(all_spikes)

shuffled_stas = np.einsum('abcd,ec->eabd', rw, shuffled_spikes)
shuffled_stas = shuffled_stas / all_spikes.sum(axis=1)[:, None, None, None]
#shuffled_stas_normalized = asc.normalize(shuffled_stas, axis_inv=0)
示例#3
0
def ombtexturesta(exp, ombstimnr, maxframes=10000,
                  contrast_window=100, plot=False):
    """
    Calculates the spike-triggered average for the full texture for the OMB
    stimulus. Based on the maximum intensity pixel of the STAs, calculates
    the center of the receptive field and the contrast signal for this
    pixel throughout the stimulus; to be used as input for models.

    Parameters:
    --------
        exp:
            The experiment name
        ombstimulusnr:
            Number of the OMB stimulus in the experiment
        maxframes:
            Maximum number of frames that will be used, typically the
            array containing the contrast is very large and
            it is easy to fill the RAM. Refer to OMB.generatecontrast()
            documentation.
        contrast_window:
            Number of pixels to be used for the size of the texture.
            Measured in each direction starting from the center so
            a value of 100 will yield texture with size (201, 201, N)
            where N is the total number of frames.
        plot:
            If True draws an interactive plot for browsing all STAs,
            also marking the center pixels. Requires an interactive backend

    """
    st = OMB(exp, ombstimnr, maxframes=maxframes)
    st.clusterstats()

    contrast = st.generatecontrast(st.texpars.noiselim/2,
                                   window=contrast_window,
                                   pad_length=st.filter_length-1)

    contrast_avg = contrast.mean(axis=-1)

    RW = asc.rolling_window(contrast, st.filter_length, preserve_dim=False)

    all_spikes = np.zeros((st.nclusters, st.ntotal))
    for i in range(st.nclusters):
        all_spikes[i, :] = st.binnedspiketimes(i)

    texturestas = np.einsum('abcd,ec->eabd', RW, all_spikes)

    texturestas /= all_spikes.sum(axis=(-1))[:, np.newaxis,
                                             np.newaxis, np.newaxis]

    # Correct for the non-informative parts of the stimulus
    texturestas = texturestas - contrast_avg[None, ..., None]
    #%%
    if plot:
        fig_stas, _ = plf.multistabrowser(texturestas, cmap='Greys_r')

    texture_maxi = np.zeros((st.nclusters, 2), dtype=int)
    # Take the pixel with maximum intensity for contrast signal
    for i in range(st.nclusters):
        coords = np.unravel_index(np.argmax(np.abs(texturestas[i])),
                                  texturestas[i].shape)[:-1]
        texture_maxi[i, :] = coords
        if plot:
            ax = fig_stas.axes[i]
            # Coordinates need to be inverted for display
            ax.plot(*coords[::-1], 'r+', markersize=10, alpha=0.2)
    #%%
    contrast_signals = np.empty((st.nclusters, st.ntotal))
    # Calculate the time course of the center(maximal pixel of texture STAs
    stas_center = np.zeros((st.nclusters, st.filter_length))
    for i in range(st.nclusters):
        coords = texture_maxi[i, :]
        # Calculate the contrast signal that can be used for GQM
        # Cut the extra part at the beginning that was added by generatecontrast
        contrast_signals[i, :] = contrast[coords[0], coords[1],
                                          st.filter_length-1:]
        stas_center[i] = texturestas[i, coords[0], coords[1], :]

    stas_center_norm = asc.normalize(stas_center)

    fig_contrast, axes = plt.subplots(*plf.numsubplots(st.nclusters), sharey=True)
    for i, ax in enumerate(axes.ravel()):
        if i < st.nclusters:
            ax.plot(stas_center_norm[i, :])

    savepath = os.path.join(st.exp_dir, 'data_analysis', st.stimname)
    savefname = f'{st.stimnr}_texturesta'
    if not maxframes:
        maxframes = st.ntotal
    savefname += f'_{maxframes}fr'

    plt.ylim([np.nanmin(stas_center_norm), np.nanmax(stas_center_norm)])
    fig_contrast.suptitle('Time course of center pixel of texture STAs')
    fig_contrast.savefig(os.path.join(savepath, 'texturestas.svg'))

    # Do not save the contrast signal because it is ~6GB for 20000 frames of recording
    keystosave = ['texturestas', 'contrast_avg', 'stas_center',
                  'stas_center_norm', 'contrast_signals', 'texture_maxi',
                  'maxframes', 'contrast_window']
    datadict = {}
    for key in keystosave:
        datadict[key] = locals()[key]

    np.savez(os.path.join(savepath, savefname), **datadict)
    if plot:
        return fig_stas
Qall = np.zeros((st.nclusters, stimdim, st.filter_length, st.filter_length))
muall = np.zeros((st.nclusters))

all_pars_progress = []
all_res = []

cells_of_interest = [0, 1, 3]

for i, cl in enumerate(st.clusters):
    if i not in cells_of_interest:
        continue

    sta = data['stas'][i]
    spikes = st.binnedspiketimes(i)

    stimulus[-1, :] = st.generatecontrast(coords[i, :])

    # Track the progress of the parameters, optim_tracker uses global variable
    pars_progress = []

    start = time.time()

    res = gqm.minimize_loglikelihood(np.zeros((stimdim, st.filter_length)),
                                     np.zeros((stimdim, st.filter_length,
                                               st.filter_length)),
                                     0,
                                     stimulus,
                                     st.frame_duration,
                                     spikes,
                                     method='BFGS',
                                     callback=optim_tracker)
示例#5
0
    for i in range(st.nclusters):
        Q_m = pars_m['Qall'][i, ...]
        k_m = pars_m['kall'][i, ...]
        mu_m = pars_m['muall'][i, ...]

        Q_cm = pars_cm['Qall'][i, ...]
        k_cm = pars_cm['kall'][i, ...]
        mu_cm = pars_cm['muall'][i, ...]

        stimdim = 2  # Terrible hack, beacuse of the way gqm is written
        neuron_m = gqm.gqm_neuron(k_m, Q_m, mu_m, st.frame_duration)
        stimdim = 3  # Terrible hack, beacuse of the way gqm is written
        neuron_cm = gqm.gqm_neuron(k_cm, Q_cm, mu_cm, st.frame_duration)

        # This was not changing on 2019-07-03, but still does not work
        stim_cm[-1, :] = st.generatecontrast(coords[i, :])[..., :stim_cm.shape[-1]]

        fr_m = neuron_m(stim_m)
        fr_cm = neuron_cm(stim_cm)

        frs_m[i, :] = fr_m
        frs_cm[i, :] = fr_cm

        spikes = st.binnedspiketimes(i)

        toplot = slice(0, None)
        spikes_cut = spikes[toplot]

        fr_real = smoothspikes(spikes_cut, sigma=sigma)
        frs_real[i, :] = fr_real
#
示例#6
0
exp, ombstimnr = 'Kuehn', 13
checkerstimnr = 1


st = OMB(exp, ombstimnr,
         maxframes=500
         )

traj = st.bgtraj*3
traj = np.flipud(traj)
st.bgtraj = traj

#%matplotlib qt
st.bgtraj_clipped = np.fmod(st.bgtraj, 1.5*st.texpars.noiselim[0])

contrast = st.generatecontrast([0, 0], 100)

plt.imshow(contrast[..., 0], cmap='Greys_r')

fig, sl = plf.stabrowser(contrast, cmap='Greys_r')


#%%
# HINT: The loop and other versions are slightly different between

st._generatetexture_withloops()
tex_loop_sm = st.texturewithloops[::4, ::4]
tiled = st.texture[200:400, 200:400]

np.allclose(tex_loop_sm, st.texturebasic)
np.allclose(tiled, tex_loop_sm)
示例#7
0
         maxframes=10000
         )

choosecells = [54, 55, 108, 109]
nrcells = len(choosecells)

ombcoords = np.zeros((nrcells, 2))
all_spikes = np.zeros((nrcells, st.ntotal), dtype=np.int8)
#all_contrasts = np.zeros((nrcells, st.ntotal))

for i, cell in enumerate(choosecells):
    ombcoords[i, :] = moc.chkmax2ombcoord(cell, exp, ombstimnr, checkerstimnr)
    all_spikes[i, :] = st.binnedspiketimes(cell)
#    all_contrasts[i, :] = st.generatecontrast(ombcoords[i, :])

contrast = st.generatecontrast(st.texpars.noiselim/2, 100).astype(np.float32)
contrast_sum = asc.normalize(contrast.sum(axis=2), axis_inv=None)
plt.imshow(contrast_sum, cmap='Greys_r')
#%%
rw = asc.rolling_window(contrast, 20)
#rws = rw.transpose((2, 0, 1, 3))

stas = np.einsum('abcd,ec->eabd', rw, all_spikes)
stas = stas / all_spikes.sum(axis=1)[:, None, None, None]
stas_normalized = asc.normalize(stas, axis_inv=0)
plt.imshow(stas[0, ..., 0], cmap='Greys_r')
#%%
from scratch_spikeshuffler import shufflebyrow

shuffled_spikes = shufflebyrow(all_spikes)