def nsxload( fn_mwk, fn_nev, fn_nsx, override_delay_us=OVERRIDE_DELAY_US, verbose=False, extinfo=False, c_success=C_SUCCESS, data_only=True, list_form=False, ): mf = MWKFile(fn_mwk) mf.open() br = BRReader(fn_nev, fn_nsx) br.open() # read TOC info from the "merged" mwk file toc = mf.get_events(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 mf.get_events(codes=[c_success])] t_success = np.array(t_success) img_onset, img_id = get_stim_info(mf, extinfo=extinfo) 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 a, b = toc["align_info"]["params"] f = lambda t_mwk: float(t_mwk - b) / a t_start = T_START - t_adjust t_stop = T_STOP - t_adjust # actual calculation ------------------------------- for i in range(n_stim): 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))) < 1: continue if verbose: print "At", (i + 1), "out of", n_stim t_nsx_start = f(t0 + t_start) t_nsx_stop = f(t0 + t_stop) yield [ data for data in br.nsx_read_range_ts(t_nsx_start, t_nsx_stop, data_only=data_only, list_form=list_form) ], br.nsx_chn_order
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()
def chk(fn, until=15, slack=0.3): sout = fn + ': ' br = BRReader(fn) assert br.open() ts_reset = 0 ts_reset_ts = [] t = -1 tstamps_set = [] tstamps = [] # === while t < until: dat = br.read_once() if dat == None: break # chk whether there's a time reset t_new = dat['timestamp'] / 1000000. # us -> s if t_new < t: ts_reset += 1 tstamps_set.append(tstamps) tstamps = [] ts_reset_ts.append(t) t = t_new if dat['id'] == 0: tstamps.append(t) tstamps_set.append(tstamps) # === All done if ts_reset == 0: sout += 'All OK.' else: ts_reset_t = max(ts_reset_ts) tstamps_after = tstamps_set[-1] tstamps_before = [] for tss in tstamps_set[:-1]: tstamps_before.extend(tss) """ print tstamps_set print tstamps_before print tstamps_after """ if len(tstamps_before) == 0: sout += 'Resynced (%d): ' % ts_reset if len(tstamps_after) > 0: if tstamps_after[0] - slack < ts_reset_t: sout += 'potential interference: ' + str(tstamps_after[0] - ts_reset_t) else: sout += 'OK' else: sout += 'OK (no after-resync ts?)' else: sout += 'bad (before-resync ts!) ' + str(tstamps_set) """ print 'Time stamp reset:', (ts_reset != 0, ts_reset, ts_reset_t) print 'Before:', tstamps_before print 'After:', tstamps_after """ print ts_reset_t, tstamps_after, tstamps_set print sout