def get_lfp_in_window(cerebus_times_ms = None, t1_ms = -50, t2_ms = 150, p_thresh = +400, n_thresh = -400, hp_hz = None, lp_hz = None, nsx_basic_header = None, f_nsx = None, channel = 0): """A reading function that reads in data from the cerebus ns3 files to grab the lfp. Inputs: cerebus_times_ms : an array of cerebus times indicating the start of the stimuli t1_ms : start time relative to cerebus_times_ms t2_ms : end time relative to cerebus_times_ms p_thresh : If waveform exceeds this positive threshold, discard n_thresh : If waveform exceeds this negative threshold, discard hp_hz : high pass filter characteristic (no filtering if none) lp_hz : low pass filter characteristic (no filtering if none) nsx_basic_header : f_nsx : file handle channel = 0 Outputs: lfps : a list of arrays the same length as cerebus_times. Traces that have been thrown are marked by Nones mean_lfp : the mean lfp """ trace_count = 0 tdur_ms = t2_ms - t1_ms N_lfp = nsx.length_of_lfp(nsx_basic_header, tdur_ms) lfps = [] mean_lfp = pylab.zeros(N_lfp, dtype=float) for n in range(cerebus_times_ms.size): tstart_ms = cerebus_times_ms[n] + t1_ms #all times in ms this_lfp = nsx.read_channel(f_nsx, nsx_basic_header, channel, tstart_ms = tstart_ms, tdur_ms = tdur_ms) if (this_lfp.max() < p_thresh) and (this_lfp.min() > n_thresh): #if threshold == None or pylab.absolute(this_lfp.max()) < threshold: lfps.append(this_lfp) mean_lfp += this_lfp trace_count += 1 else: lfps.append(None) mean_lfp /= float(trace_count)#cerebus_times_ms.size Fs = float(nsx_basic_header['Fs Hz']) t_ms = 1000*pylab.arange(N_lfp)/Fs + t1_ms return lfps, mean_lfp, t_ms
def old_get_lfp(cerebus_times_ms=None, t1_ms=-50, t2_ms=150, fun_args=None): """A reading function that reads in data from the cerebus ns3 files to grab the lfp. Inputs: cerebus_times_ms : an array of cerebus times indicating the start of the stimuli t1_ms : start time relative to cerebus_times_ms t2_ms : end time relative to cerebus_times_ms fun_args : dict: 'basic nsx header', 'fnsx', - nsx file handle 'channel', - channel we are interested in 'pos threshold', - 'neg threshold' - discard traces with values outside this range (if not give, defaults to +/1 400 uV) Outputs: neural_response : a 7D list with ultimate cells containing a list, array or any other data structure comment : a string that should explain what the data is """ basic_header = fun_args['basic nsx header'] f_nsx = fun_args['fnsx'] channel = fun_args['channel'] p_thresh = fun_args.get('pos threshold', +400) n_thresh = fun_args.get('neg threshold', -400) trace_count = 0 tdur_ms = t2_ms - t1_ms N_lfp = nsx.length_of_lfp(basic_header, tdur_ms) mean_lfp = pylab.zeros(N_lfp, dtype=float) for n in range(cerebus_times_ms.size): tstart_ms = cerebus_times_ms[n] + t1_ms #all times in ms this_lfp = nsx.read_channel(f_nsx, basic_header, channel, tstart_ms=tstart_ms, tdur_ms=tdur_ms) if (this_lfp.max() < p_thresh) and (this_lfp.min() > n_thresh): #if threshold == None or pylab.absolute(this_lfp.max()) < threshold: mean_lfp += this_lfp trace_count += 1 mean_lfp /= float(trace_count) #cerebus_times_ms.size data = { 'total trials': cerebus_times_ms.size, 'trials': trace_count, #The number of valid trials 'mean lfp': mean_lfp, 'Fs Hz': float(basic_header['Fs Hz']) } return data
def old_get_lfp(cerebus_times_ms = None, t1_ms = -50, t2_ms = 150, fun_args = None): """A reading function that reads in data from the cerebus ns3 files to grab the lfp. Inputs: cerebus_times_ms : an array of cerebus times indicating the start of the stimuli t1_ms : start time relative to cerebus_times_ms t2_ms : end time relative to cerebus_times_ms fun_args : dict: 'basic nsx header', 'fnsx', - nsx file handle 'channel', - channel we are interested in 'pos threshold', - 'neg threshold' - discard traces with values outside this range (if not give, defaults to +/1 400 uV) Outputs: neural_response : a 7D list with ultimate cells containing a list, array or any other data structure comment : a string that should explain what the data is """ basic_header = fun_args['basic nsx header'] f_nsx = fun_args['fnsx'] channel = fun_args['channel'] p_thresh = fun_args.get('pos threshold', +400) n_thresh = fun_args.get('neg threshold', -400) trace_count = 0 tdur_ms = t2_ms - t1_ms N_lfp = nsx.length_of_lfp(basic_header, tdur_ms) mean_lfp = pylab.zeros(N_lfp, dtype=float) for n in range(cerebus_times_ms.size): tstart_ms = cerebus_times_ms[n] + t1_ms #all times in ms this_lfp = nsx.read_channel(f_nsx, basic_header, channel, tstart_ms = tstart_ms, tdur_ms = tdur_ms) if (this_lfp.max() < p_thresh) and (this_lfp.min() > n_thresh): #if threshold == None or pylab.absolute(this_lfp.max()) < threshold: mean_lfp += this_lfp trace_count += 1 mean_lfp /= float(trace_count)#cerebus_times_ms.size data = {'total trials':cerebus_times_ms.size, 'trials': trace_count, #The number of valid trials 'mean lfp': mean_lfp, 'Fs Hz': float(basic_header['Fs Hz'])} return data
def inspect_lfp(nsx_fname, channel = 1): """Plot the whole lfp for the given file.""" f_nsx = open(nsx_fname) nsx_basic_header = nsx.read_basic_header(f_nsx) this_lfp = nsx.read_channel(f_nsx, nsx_basic_header, channel, tstart_ms = 0, tdur_ms = -1) Fs = float(nsx_basic_header['Fs Hz']) t_ms = 1000*pylab.arange(this_lfp.size)/Fs pylab.plot(t_ms, this_lfp)
def inspect_lfp(nsx_fname, channel=1): """Plot the whole lfp for the given file.""" f_nsx = open(nsx_fname) nsx_basic_header = nsx.read_basic_header(f_nsx) this_lfp = nsx.read_channel(f_nsx, nsx_basic_header, channel, tstart_ms=0, tdur_ms=-1) Fs = float(nsx_basic_header['Fs Hz']) t_ms = 1000 * pylab.arange(this_lfp.size) / Fs pylab.plot(t_ms, this_lfp)
def get_lfp_in_window(cerebus_times_ms=None, t1_ms=-50, t2_ms=150, p_thresh=+400, n_thresh=-400, hp_hz=None, lp_hz=None, nsx_basic_header=None, f_nsx=None, channel=0): """A reading function that reads in data from the cerebus ns3 files to grab the lfp. Inputs: cerebus_times_ms : an array of cerebus times indicating the start of the stimuli t1_ms : start time relative to cerebus_times_ms t2_ms : end time relative to cerebus_times_ms p_thresh : If waveform exceeds this positive threshold, discard n_thresh : If waveform exceeds this negative threshold, discard hp_hz : high pass filter characteristic (no filtering if none) lp_hz : low pass filter characteristic (no filtering if none) nsx_basic_header : f_nsx : file handle channel = 0 Outputs: lfps : a list of arrays the same length as cerebus_times. Traces that have been thrown are marked by Nones mean_lfp : the mean lfp """ trace_count = 0 tdur_ms = t2_ms - t1_ms N_lfp = nsx.length_of_lfp(nsx_basic_header, tdur_ms) lfps = [] mean_lfp = pylab.zeros(N_lfp, dtype=float) for n in range(cerebus_times_ms.size): tstart_ms = cerebus_times_ms[n] + t1_ms #all times in ms this_lfp = nsx.read_channel(f_nsx, nsx_basic_header, channel, tstart_ms=tstart_ms, tdur_ms=tdur_ms) if (this_lfp.max() < p_thresh) and (this_lfp.min() > n_thresh): #if threshold == None or pylab.absolute(this_lfp.max()) < threshold: lfps.append(this_lfp) mean_lfp += this_lfp trace_count += 1 else: lfps.append(None) mean_lfp /= float(trace_count) #cerebus_times_ms.size Fs = float(nsx_basic_header['Fs Hz']) t_ms = 1000 * pylab.arange(N_lfp) / Fs + t1_ms return lfps, mean_lfp, t_ms