예제 #1
0
def psth_per_file(rec):

    raise NotImplementedError

    resp = rec['resp'].rasterize()

    file_epochs = ep.epoch_names_matching(resp.epochs, "^FILE_")

    epoch_regex = "^STIM_"
    stim_epochs = ep.epoch_names_matching(resp.epochs, epoch_regex)

    r = []
    max_rep_id = np.zeros(len(file_epochs))
    for f in file_epochs:

        r.append(resp.as_matrix(stim_epochs, overlapping_epoch=f) * resp.fs)

    repcount = np.sum(np.isfinite(r[:, :, 0, 0]), axis=1)
    max_rep_id, = np.where(repcount == np.max(repcount))

    t = np.arange(r.shape[-1]) / resp.fs

    plt.figure()

    ax = plt.subplot(3, 1, 1)
    nplt.plot_spectrogram(s[max_rep_id[-1], 0, :, :],
                          fs=stim.fs,
                          ax=ax,
                          title="cell {} - stim".format(cellid))

    ax = plt.subplot(3, 1, 2)
    nplt.raster(t, r[max_rep_id[-1], :, 0, :], ax=ax, title='raster')

    ax = plt.subplot(3, 1, 3)
    nplt.psth_from_raster(t,
                          r[max_rep_id[-1], :, 0, :],
                          ax=ax,
                          title='raster',
                          ylabel='spk/s')

    plt.tight_layout()
예제 #2
0
파일: test_plots.py 프로젝트: LBHB/NEMS
def test_plots():
    recording.get_demo_recordings(name=recording_file)
    rec = recording.load_recording(uri)

    resp = rec['resp'].rasterize()
    stim = rec['stim'].rasterize()

    epoch_regex = "^STIM_"

    stim_epochs = ep.epoch_names_matching(resp.epochs, epoch_regex)

    r = resp.as_matrix(stim_epochs) * resp.fs
    s = stim.as_matrix(stim_epochs)
    repcount = np.sum(np.isfinite(r[:, :, 0, 0]), axis=1)
    max_rep_id, = np.where(repcount == np.max(repcount))

    t = np.arange(r.shape[-1]) / resp.fs

    plt.figure()

    ax = plt.subplot(3, 1, 1)
    nplt.plot_spectrogram(s[max_rep_id[-1], 0, :, :],
                          fs=stim.fs,
                          ax=ax,
                          title="cell {} - stim".format(cellid))

    ax = plt.subplot(3, 1, 2)
    nplt.raster(t, r[max_rep_id[-1], :, 0, :], ax=ax, title='raster')

    ax = plt.subplot(3, 1, 3)
    nplt.psth_from_raster(t,
                          r[max_rep_id[-1], :, 0, :],
                          ax=ax,
                          title='raster',
                          ylabel='spk/s')

    plt.tight_layout()
예제 #3
0
def model_pred_comp(cellid, batch, modelnames, occurrence=None,
                    pre_dur=None, dur=None):
    """
    return ccd12, ccd13, ccd23
    """

    modelcount = len(modelnames)
    epoch = 'REFERENCE'
    c = 0
    plot_colors = ['lightgray', 'g', 'lightgray', 'r', 'lightgray', 'b']
    legend = ['act','LN','act','GC','act','STP']
    times = []
    values = []
    r_test = []
    for i, m in enumerate(modelnames):
        xf, ctx = load_model_baphy_xform(cellid, batch, m)

        val = ctx['val'][0]

        if i == 0:
            d = val['resp'].get_epoch_bounds('PreStimSilence')
            if len(d):
                PreStimSilence = np.mean(np.diff(d))
            else:
                PreStimSilence = 0
            if pre_dur is None:
                pre_dur = PreStimSilence

            if occurrence is not None:
                # Get values from specified occurrence and channel
                extracted = val['resp'].extract_epoch(epoch)
                r_vector = extracted[occurrence][c]
            else:
                r_vector = val['resp'].as_continuous()[0, :]

            validbins = np.isfinite(r_vector)
            r_vector = nems.utils.smooth(r_vector[validbins], 7)
            r_vector = r_vector[3:-3]

            # Convert bins to time (relative to start of epoch)
            # TODO: want this to be absolute time relative to start of data?
            time_vector = np.arange(0, len(r_vector)) / val['resp'].fs - PreStimSilence

            # limit time range if specified
            good_bins = (time_vector >= -pre_dur)
            if dur is not None:
                good_bins[time_vector > dur] = False

        if occurrence is not None:
            extracted = val['pred'].extract_epoch(epoch)
            p_vector = extracted[occurrence][c]
        else:
            p_vector = val['pred'].as_continuous()
            p_vector = p_vector[0, validbins]

        times.append(time_vector[good_bins])
        values.append(r_vector[good_bins] + i)
        times.append(time_vector[good_bins])
        values.append(p_vector[good_bins] + i)

        r_test.append(ctx['modelspec'].meta['r_test'][0])

    times_all = times
    values_all = values

    cc12 = np.corrcoef(values_all[0], values_all[1])[0, 1]
    cc13 = np.corrcoef(values_all[0], values_all[2])[0, 1]
    cc23 = np.corrcoef(values_all[1], values_all[2])[0, 1]
    ccd23 = np.corrcoef(values_all[1]-values_all[0],
                        values_all[2]-values_all[0])[0, 1]

#    ccd12 = np.corrcoef(values_all[0]-values_all[3],
#                        values_all[1]-values_all[3])[0, 1]
#    ccd13 = np.corrcoef(values_all[0]-values_all[3],
#                        values_all[2]-values_all[3])[0, 1]
#    ccd23 = np.corrcoef(values_all[1]-values_all[3],
#                        values_all[2]-values_all[3])[0, 1]

    print("CC LN-GC: {:.3f}  LN-STP: {:.3f} STP-GC: {:.3f}".format(
            cc12,cc13,cc23))
#    print("CCd LN-GC: {:.3f}  LN-STP: {:.3f} STP-GC: {:.3f}".format(
#            ccd12,ccd13,ccd23))

    plt.figure()
    ax = plt.subplot(2, 1, 1)
    if occurrence is not None:
        extracted = val['stim'].extract_epoch(epoch)
        sg = extracted[occurrence]
    else:
        sg = val['stim'].as_continuous()
        sg = sg[:, validbins]
    sg = sg[:, good_bins]
    nplt.plot_spectrogram(sg, val['resp'].fs, ax=ax,
                          title='{} Stim {}'.format(cellid, occurrence),
                          time_offset=pre_dur, cmap='gist_yarg')

    ax = plt.subplot(2, 1, 2)
    title = 'Preds LN {:.3f} GC {:.3f} STP {:.3f} /CC LN-GC: {:.3f}  LN-STP: {:.3f} STP-GC: {:.3f} dSTP-dGC: {:.3f}'.format(
            r_test[0],r_test[1],r_test[2],cc12,cc13,cc23,ccd23)
    nplt.plot_timeseries(times_all, values_all, ax=ax, legend=legend,
                         title=title, colors=plot_colors)

    plt.tight_layout()

    return cc12, cc13, cc23, ccd23
예제 #4
0
Ya = rec_avg['resp'].as_continuous()

print('Trial-averaged stim/resp extracted to matrices Xa=({},{}) / Ya=({},{})'.
      format(Xa.shape[0], Xa.shape[1], Ya.shape[0], Ya.shape[1]))

# here's how you get rasters aligned to each stimulus:
# define regex for stimulus epochs
epoch_regex = '^STIM_'
epochs_to_extract = ep.epoch_names_matching(rec.epochs, epoch_regex)
folded_resp = resp.extract_epochs(epochs_to_extract)

print('Response to each stimulus extracted to folded_resp dictionary')

# or just plot the PSTH for an example stimulus
raster = resp.extract_epoch(epoch)
psth = np.mean(raster, axis=0)
spec = stim.extract_epoch(epoch)[0, :, :]

plt.figure()
ax = plt.subplot(2, 1, 1)
nplt.plot_spectrogram(spec, fs=resp.fs, ax=ax, title=epoch)

ax = plt.subplot(2, 1, 2)
nplt.timeseries_from_vectors(psth,
                             fs=resp.fs,
                             ax=ax,
                             title="PSTH ({} cells, {} reps)".format(
                                 raster.shape[1], raster.shape[0]))

plt.tight_layout()
예제 #5
0
ax = plt.subplot(2, 2, 1)
ax.imshow(sc, aspect='equal', cmap='gray')
mm = np.max(sc[sc < 1])
i, j = np.where(sc == mm)

ax = plt.subplot(2, 1, 2)
nplt.timeseries_from_vectors(psth,
                             fs=resp.fs,
                             ax=ax,
                             title="PSTH ({} cells, {} reps)".format(
                                 raster.shape[1], raster.shape[0]))

fig = plt.figure()

ax = plt.subplot(3, 1, 1)
nplt.plot_spectrogram(spec, fs=resp.fs, ax=ax, title=epoch, cmap='gray_r')

ax = plt.subplot(3, 1, 2)
tt = np.arange(psth.shape[1]) / resp.fs - 2

ax.plot(tt, psth[25, :], color='black', lw=1)
ax.plot(tt, psth[27, :], color='gray', lw=1)
ax.set_title('sc={:.3f}'.format(sc[25, 27]))
ax.set_ylabel('Spikes/sec')
nplt.ax_remove_box(ax)

ax = plt.subplot(3, 1, 3)

ax.plot(tt, psth[25, :], color='black', lw=1)
ax.plot(tt, psth[32, :], color='gray', lw=1)
ax.set_title('sc={:.3f}'.format(sc[25, 32]))
예제 #6
0
def compare_model_preds(cellid,
                        batch,
                        modelname1,
                        modelname2,
                        max_pre=0.25,
                        max_dur=1.0):
    """
    compare prediction accuracy of two models on validation stimuli

    borrows a lot of functionality from nplt.quickplot()

    """
    xf1, ctx1 = get_model_preds(cellid, batch, modelname1)
    xf2, ctx2 = get_model_preds(cellid, batch, modelname2)

    rec = ctx1['rec']
    val1 = ctx1['val']
    val2 = ctx2['val']

    stim = rec['stim'].rasterize()
    resp = rec['resp'].rasterize()
    pred1 = val1['pred']
    pred2 = val2['pred']
    fs = resp.fs

    d = resp.get_epoch_bounds('PreStimSilence')
    PreStimSilence = np.mean(np.diff(d))
    d = resp.get_epoch_bounds('PostStimSilence')
    PostStimSilence = np.mean(np.diff(d))

    epoch_regex = "^STIM_"
    stim_epochs = ep.epoch_names_matching(resp.epochs, epoch_regex)

    r = resp.as_matrix(stim_epochs)
    s = stim.as_matrix(stim_epochs)
    repcount = np.sum(np.isfinite(r[:, :, 0, 0]), axis=1)
    max_rep_id, = np.where(repcount == np.max(repcount))

    # keep a max of two stimuli
    stim_ids = max_rep_id[:2]
    stim_count = len(stim_ids)
    # print(max_rep_id)

    # stim_i=max_rep_id[-1]
    # print("Max rep stim={} ({})".format(stim_i, stim_epochs[stim_i]))

    p1 = pred1.as_matrix(stim_epochs)
    p2 = pred2.as_matrix(stim_epochs)

    ms1 = ctx1['modelspec']
    ms2 = ctx2['modelspec']
    r_test1 = ms1.meta['r_test'][0]
    r_test2 = ms2.meta['r_test'][0]

    fh = plt.figure(figsize=(16, 6))

    # model 1 modules
    ax = plt.subplot(5, 4, 1)
    nplt.strf_timeseries(ms1,
                         ax=ax,
                         clim=None,
                         show_factorized=True,
                         title="{}/{} rtest={:.3f}".format(
                             cellid, modelname1, r_test1),
                         fs=resp.fs)
    nplt.ax_remove_box(ax)

    ax = plt.subplot(5, 4, 3)
    nplt.strf_timeseries(ms2,
                         ax=ax,
                         clim=None,
                         show_factorized=True,
                         title="{}/{} rtest={:.3f}".format(
                             cellid, modelname2, r_test2),
                         fs=resp.fs)
    nplt.ax_remove_box(ax)

    if find_module('stp', ms1):
        ax = plt.subplot(5, 4, 5)
        nplt.before_and_after_stp(ms1,
                                  sig_name='pred',
                                  ax=ax,
                                  title='',
                                  channels=0,
                                  xlabel='Time (s)',
                                  ylabel='STP',
                                  fs=resp.fs)
        nplt.ax_remove_box(ax)

    nlidx = find_module('double_exponential', ms1, find_all_matches=True)
    if len(nlidx):
        nlidx = nlidx[-1]
        fn1, fn2 = nplt.before_and_after_scatter(rec,
                                                 ms1,
                                                 nlidx,
                                                 smoothing_bins=200,
                                                 mod_name='double_exponential')
        ax = plt.subplot(5, 4, 6)
        fn1(ax=ax)
        nplt.ax_remove_box(ax)

    # model 2 modules
    wcidx = find_module('weight_channels', ms2)
    if wcidx:
        ax = plt.subplot(5, 4, 4)
        coefs = ms2[wcidx]['phi']['coefficients']
        plt.imshow(coefs,
                   clim=np.array([-1, 1]) * np.max(np.abs(coefs)),
                   cmap='bwr')
        plt.xlabel('in')
        plt.ylabel('out')
        plt.colorbar()
        nplt.ax_remove_box(ax)

    if find_module('stp', ms2):
        ax = plt.subplot(5, 4, 7)
        nplt.before_and_after_stp(ms2,
                                  sig_name='pred',
                                  ax=ax,
                                  title='',
                                  channels=0,
                                  xlabel='Time (s)',
                                  ylabel='STP',
                                  fs=resp.fs)
        nplt.ax_remove_box(ax)

    nlidx = find_module('double_exponential', ms2, find_all_matches=True)
    if len(nlidx):
        nlidx = nlidx[-1]
        fn1, fn2 = nplt.before_and_after_scatter(rec,
                                                 ms2,
                                                 nlidx,
                                                 smoothing_bins=200,
                                                 mod_name='double_exponential')
        ax = plt.subplot(5, 4, 8)
        fn1(ax=ax)
        nplt.ax_remove_box(ax)

    max_bins = int((PreStimSilence + max_dur) * fs)
    pre_cut_bins = int((PreStimSilence - max_pre) * fs)
    if pre_cut_bins < 0:
        pre_cut_bins = 0
    else:
        PreStimSilence = max_pre

    for i, stim_i in enumerate(stim_ids):

        ax = plt.subplot(5, 2, 5 + i)
        if s.shape[2] <= 2:
            nplt.timeseries_from_vectors(
                [
                    s[stim_i, 0, 0, pre_cut_bins:max_bins],
                    s[max_rep_id[-1], 0, 1, pre_cut_bins:max_bins]
                ],
                fs=stim.fs,
                time_offset=PreStimSilence,
                ax=ax,
                title="{}/{} rfit={:.3f}/{:.3f}".format(
                    cellid, stim_epochs[stim_i], r_test1, r_test2))
        else:
            nplt.plot_spectrogram(s[stim_i, 0, :, pre_cut_bins:max_bins],
                                  fs=stim.fs,
                                  time_offset=PreStimSilence,
                                  ax=ax,
                                  title="{}/{} rfit={:.3f}/{:.3f}".format(
                                      cellid, stim_epochs[stim_i], r_test1,
                                      r_test2))
        ax.get_xaxis().set_visible(False)
        nplt.ax_remove_box(ax)

        ax = plt.subplot(5, 2, 7 + i)
        _r = r[stim_i, :, 0, pre_cut_bins:max_bins]
        t = np.arange(_r.shape[-1]) / resp.fs - PreStimSilence - 0.5 / resp.fs
        nplt.raster(t, _r)
        ax.get_xaxis().set_visible(False)

        ax = plt.subplot(5, 2, 9 + i)
        nplt.timeseries_from_vectors([
            np.nanmean(_r, axis=0), p1[stim_i, 0, 0, pre_cut_bins:max_bins],
            p2[stim_i, 0, 0, pre_cut_bins:max_bins]
        ],
                                     fs=resp.fs,
                                     time_offset=PreStimSilence,
                                     ax=ax)
        nplt.ax_remove_box(ax)

    plt.tight_layout()
    return fh, ctx1, ctx2
예제 #7
0
for i in [4, 5, 7, 13]:
    epoch = epochs_to_extract[i]
    # or just plot the PSTH for an example stimulus
    raster = resp.extract_epoch(epoch)
    psth = np.mean(raster, axis=0)
    spec = stim.extract_epoch(epoch)[0, :, :]

    norm_psth = psth - np.mean(psth, axis=1, keepdims=True)
    norm_psth /= np.std(psth, axis=1, keepdims=True)

    f, ax = plt.subplots(3, 1, sharex=True)
    PreStimSilence = 0.25

    nplt.plot_spectrogram(spec,
                          fs=resp.fs,
                          ax=ax[0],
                          time_offset=PreStimSilence,
                          title=epoch,
                          cmap='gray_r')

    nplt.plot_spectrogram(psth,
                          fs=resp.fs,
                          ax=ax[1],
                          time_offset=PreStimSilence,
                          cmap='gray_r')

    PreStimSilence = 0.25
    tt = np.arange(psth.shape[1]) / resp.fs - PreStimSilence
    rasterfs = resp.fs

    ax[2].plot(tt,
               psth[25, :] * rasterfs,