Beispiel #1
0
def main(c_success=C_SUCCESS):
    #mf = MWKFile('../analysis/data_merged/Chabo_20110426_MovieGallant110413_S110204_RefAA_001.mwk')
    mf = MWKFile('../analysis/data_merged/Chabo_20110331_RSVPNicole305_S110204_RefAA_001.mwk')
    mf.open()
    #br = BRReader('../analysis/data_nev/Chabo_20110426_MovieGallant110413_S110204_RefAA_001.nev')
    br = BRReader('../analysis/data_nev/Chabo_20110331_RSVPNicole305_S110204_RefAA_001.nev')
    br.open()

    if PLOT_ONLY_ABV:
        adj_reject = 5./3.
        new_thr = {}
        for ch in br.chn_info:
            lthr = br.chn_info[ch]['low_thr']; hthr = br.chn_info[ch]['high_thr']
            if lthr == 0: thr0 = hthr
            else: thr0 = lthr
            new_thr[ch] = thr0 * adj_reject * 0.249
            print '*', ch, new_thr[ch]

    toc = xget_events(mf, codes=['#merged_data_toc'])[0].value
    # MAC-NSP time translation
    if OVERRIDE_DELAY_US != None: 
        t_delay = toc['align_info']['delay']
        t_adjust = int(np.round(OVERRIDE_DELAY_US - t_delay))
    else: 
        t_adjust = 0

    # when the visual stimuli presented is valid?
    t_success = [ev.time for ev in mf.get_events(codes=[c_success])]
    t_success = np.array(t_success)

    img_onset, img_id = get_stim_info(mf)
    print 'Len =', len(img_onset)

    i_plot_ac = 0
    i_spk = 0
    i_spk2 = 0
    i_spk_nvis = 0
    i_spk_vis = 0
    M = np.zeros((MAX_SPK, DIM))
    M2 = np.zeros((MAX_SPK, DIM))
    Mnvis = np.zeros((MAX_SPK, DIM))
    Mvis  = np.zeros((MAX_SPK, DIM))
    t = (np.arange(DIM) - 11) * 1000. / 30000.   # in ms
    #t = np.arange(len(DIM))    # in index

    for t0 in img_onset:
        if i_spk >= MAX_SPK: break
        if i_spk_nvis >= MAX_SPK: break
        if i_spk_vis >= MAX_SPK: break
        if np.sum((t_success > t0) & (t_success < (t0 + T_SUCCESS))) < 1: continue

        spks = mf.get_events(codes=['merged_spikes'], time_range=[t0 +START, t0 + END])

        for spk in spks:
            ch = spk.value['id']
            ts = spk.time
            if ch != CH: continue

            offset = spk.value['foffset']
            wav = br.read_once(pos=offset, proc_wav=True)['waveform']
            y = np.array(wav) * 0.249  # in uV

            if PLOT_ONLY_DOWN:
                if y[12] > y[11]: continue
            if PLOT_ONLY_ABV:
                wav = set_new_threshold(wav, new_thr[ch], rng=(11, 13), i_chg=32) 
                if wav == None: continue
            if PLOT_ONLY_EXCHG:
                if np.max(y[:32]) < 0: continue

            t_rel = ts + t_adjust - t0
            # print t_rel
            # -- monitor noise?
            if (t_rel/1000.) % NPERIOD < 1. or (t_rel/1000.) % NPERIOD > (NPERIOD - 1):
                if t_rel > -50000 and t_rel < 50000:
                    M[i_spk] = y
                    i_spk += 1
                    pl.figure(ch)
                    pl.plot(t, y, 'k-')
                    pl.title('Noise responses (OFF region)')
                    pl.xlabel('Time/ms')
                    pl.ylabel(r'Response/$\mu$V')
                elif t_rel > 70000 and t_rel < 170000:
                    M2[i_spk2] = y
                    i_spk2 += 1
                    pl.figure(1000 + ch)
                    pl.plot(t, y, 'k-')
                    pl.title('Noise responses (ON region)')
                    pl.xlabel('Time/ms')
                    pl.ylabel(r'Response/$\mu$V')

            elif (t_rel/1000.) % NPERIOD > 2. and (t_rel/1000.) % NPERIOD < (NPERIOD - 2):
                if t_rel > -50000 and t_rel < 50000:
                    Mnvis[i_spk_nvis] = y
                    i_spk_nvis += 1
                    pl.figure(2000 + ch)
                    pl.plot(t, y, 'k-')
                    pl.title('Non-noise blank responses')
                    pl.xlabel('Time/ms')
                    pl.ylabel(r'Response/$\mu$V')
                elif t_rel > 70000 and t_rel < 170000:
                    Mvis[i_spk_vis] = y
                    i_spk_vis += 1
                    pl.figure(3000 + ch)
                    pl.plot(t, y, 'k-')
                    pl.title('Non-noise visual responses')
                    pl.xlabel('Time/ms')
                    pl.ylabel(r'Response/$\mu$V')

            i_plot_ac += 1

    M = M[:i_spk]
    # pl.figure()
    # pl.hist(np.ravel(M[:,12] - M[:,11]), bins=20)
    print 'i_spk =', i_spk
    print 'i_spk2 =', i_spk2
    print 'i_spk_nvis =', i_spk_nvis
    print 'i_spk_vis =', i_spk_vis
    print 'i_plot_ac =', i_plot_ac
    M = M[:i_spk,:]
    M2 = M2[:i_spk2,:]
    Mnvis = Mnvis[:i_spk_nvis,:]
    Mvis  = Mvis[:i_spk_vis,:] 

    pl.figure()
    xb, yb = myhist(np.min(M,axis=1), norm='peak')
    xb2, yb2 = myhist(np.min(M2,axis=1), norm='peak')
    xg, yg = myhist(np.min(Mnvis,axis=1), norm='peak')
    xv, yv = myhist(np.min(Mvis,axis=1), norm='peak')
    pl.plot(xb, yb, 'r-', label='Noise responses (OFF)')
    pl.plot(xb2, yb2, 'm-', label='Noise responses (ON)')
    pl.plot(xg, yg, 'g-', label='Non-noise responses (OFF)')
    pl.plot(xv, yv, 'b-', label='Non-noise responses (ON)')
    #pl.axvline(ptbad.mean(), color='r',alpha=0.3)
    #pl.axvline(ptgood.mean(), color='b',alpha=0.3)
    #pl.axvline(ptvis.mean(), color='g',alpha=0.3)
    pl.xlabel(r'Peak response/$\mu$V')
    pl.ylabel('Normalized probability')
    pl.legend(loc='upper left')

    pl.figure()
    # --
    m = M.mean(axis=0); s = M.std(axis=0, ddof=1)
    pl.plot(t, m, 'r-', label='Noise responses (OFF)')
    pl.fill_between(t, m-s, m+s, color='r', alpha=0.2)
    # --
    m = Mnvis.mean(axis=0); s = Mnvis.std(axis=0, ddof=1)
    pl.plot(t, m, 'g-', label='Non-noise responses (OFF)')
    pl.fill_between(t, m-s, m+s, color='g', alpha=0.2)
    # -- 
    pl.xlabel('Time/ms')
    pl.ylabel(r'Response/$\mu$V')
    pl.legend(loc='lower right')

    pl.figure()
    # --
    m = M2.mean(axis=0); s = M2.std(axis=0, ddof=1)
    pl.plot(t, m, 'm-', label='Noise responses (ON)')
    pl.fill_between(t, m-s, m+s, color='m', alpha=0.2)
    # --
    m = Mvis.mean(axis=0); s = Mvis.std(axis=0, ddof=1)
    pl.plot(t, m, 'b-', label='Non-noise responses (ON)')
    pl.fill_between(t, m-s, m+s, color='b', alpha=0.2)
    # --
    pl.xlabel('Time/ms')
    pl.ylabel(r'Response/$\mu$V')
    pl.legend(loc='lower right')

    pl.show()
Beispiel #2
0
def get_data(
    fn_mwk,
    fn_nev,
    clus_info,
    override_delay_us=None,
    override_elecs=None,
    verbose=True,
    c_success=C_SUCCESS,
    t_success_lim=T_SUCCESS,
    max_clus=MAX_CLUS,
    extinfo=False,
    imgmax=IMG_MAX,
    exclude_img=EXCLUDE_IMG,
):
    mf = MWKFile(fn_mwk)
    mf.open()
    nf = load_spike_data(fn_nev)
    nf.open()
    cnt = defaultdict(int)

    # read TOC info from the "merged" mwk file
    toc = xget_events(mf, codes=[Merge.C_MAGIC])[0].value
    c_spikes = toc[Merge.K_SPIKE]  # get the code name from the toc

    # when the visual stimuli presented is valid?
    t_success = [ev.time for ev in xget_events(mf, codes=[c_success])]
    t_success = np.array(t_success)

    # get the active electrodes
    if override_elecs is None:
        actvelecs = toc[Merge.K_SPIKEWAV].keys()
    else:
        actvelecs = override_elecs  # e.g, range(1, 97)

    img_onset, img_id = get_stim_info(mf, extinfo=extinfo, exclude_img=exclude_img)
    n_stim = len(img_onset)

    # MAC-NSP time translation
    if override_delay_us != None:
        t_delay = toc["align_info"]["delay"]
        t_adjust = int(np.round(override_delay_us - t_delay))
    else:
        t_adjust = 0
    t_start = T_START - t_adjust
    t_stop = T_STOP - t_adjust

    # actual calculation -------------------------------
    # all_spike[chn_id][img_id]: when the neurons spiked?
    for i in range(n_stim):
        if i > imgmax:
            break
        t0 = img_onset[i]
        iid = img_id[i]
        # -- check if this presentation is successful. if it's not ignore this.
        if np.sum((t_success > t0) & (t_success < (t0 + t_success_lim))) < 1:
            continue

        if verbose:
            print "At", (i + 1), "out of", n_stim, "         \r",
            sys.stdout.flush()

        spikes = xget_events(mf, codes=[c_spikes], time_range=[t0 + t_start, t0 + t_stop])
        for s in spikes:
            ch = s.value["id"]
            cid = s.value["cluster_id"]
            pos = s.value["foffset"]
            key = (ch, cid)

            if cnt[key] >= MAX_SPK:
                continue
            cnt[key] += 1

            dat = nf.read_once(pos=pos, proc_wav=True)
            wav = dat["waveform"]
            clus_info[key].append(np.array(wav))