def get_dv_dt(slice_indicies=(0, 0)): """Main function to take 1st derivative of V_trace and return an array with V_values and dv_dt value for plotting --input tuple to use slice of trace""" #determine if using whole trace or slice if slice_indicies != 0: sample_start = slice_indicies[0] sample_end = slice_indicies[1] else: sample_start = 0 sample_end = len(stf.get_trace()) #get sampling interval to create dt part of dv/dt #dt is just sampling interval si = stf.get_sampling_interval() #read V values from trace, V_values = stf.get_trace()[sample_start:sample_end] #compute dv and by iterating over voltage vectors dv = [V_values[i + 1] - V_values[i] for i in range(len(V_values) - 1)] #compute dv/dt dv_dt = [(dv[i] / si) for i in range(len(dv))] #V values for a dv/dt / V graph is just truncated trace with final sample point removed V_plot = V_values[:-1] #combine for a plotting function/further manipulation V_dv_dt = np.vstack([V_plot, dv_dt]) stf.new_window(dv_dt) return (V_dv_dt)
def average_sweeps(*argv): sweeps = stf.get_trace(argv[0]) for sweep in argv[1:]: sweep_ = stf.get_trace(sweep) sweeps = np.vstack((sweeps, sweep_)) sweeps_mean = np.mean(sweeps, axis=0) stf.new_window(sweeps_mean) return (sweeps_mean)
def remove_artifacts(first_artifact_start, length_time_to_remove, time_between_artifacts, number_of_artifacts): #get sampling interval of current tract sampling_interval = stf.get_sampling_interval() #convert input paramenters (units of time) to samples first_artifact_start_samples = float(first_artifact_start) / float( sampling_interval) length_time_to_remove_samples = float(length_time_to_remove) / float( sampling_interval) time_between_artifacts_samples = float(time_between_artifacts) / float( sampling_interval) #create variable for artifact end first_artifact_end_samples = first_artifact_start_samples + length_time_to_remove_samples #get trace, convert trace to normal list original_trace = list(stf.get_trace()) #create list for section of trace up until 1st artifact trace_artifacts_removed = original_trace[0:int(first_artifact_start_samples )] #add remaining sections of trace with artifacts removed for artifact_number in range(0, number_of_artifacts): start_sample = int(first_artifact_end_samples + (time_between_artifacts_samples * artifact_number)) end_sample = int(first_artifact_start_samples + (time_between_artifacts_samples * (artifact_number + 1))) print(start_sample) print(end_sample) trace_artifacts_removed.extend(original_trace[start_sample:end_sample]) #plots trace in new window stf.new_window(trace_artifacts_removed) return (trace_artifacts_removed)
def loadnrn( file ): """ Load a NEURON datafile and opens a new Stimfit window with a trace with the default units (e.g ms and mV) Arguments: file -- (string) file to be read """ time, trace = np.loadtxt(fname = file, skiprows = 2, unpack =1) dt = time[1] # the second temporal sampling point is the sampling stf.new_window( trace ) stf.set_sampling_interval( dt )
def loadnrn(file): """ Load a NEURON datafile and opens a new Stimfit window with a trace with the default units (e.g ms and mV) Arguments: file -- (string) file to be read """ time, trace = np.loadtxt(fname=file, skiprows=2, unpack=1) dt = time[1] # the second temporal sampling point is the sampling stf.new_window(trace) stf.set_sampling_interval(dt)
def plot_sweep(sweep_num, t_series_folder): str_sweep_num = str(sweep_num) y_vals = t_series_folder['voltage recording'].loc[ 'Sweep' + str(str_sweep_num.zfill(4))].loc[:, 'input 0'] stf.new_window(y_vals) time = t_series_folder['voltage recording'].loc[ 'Sweep' + str(str_sweep_num.zfill(4))].loc[:, 'input 0'].as_matrix() stf.set_sampling_interval((time[1] - time[0]) / 1000) return ()
def resistance( base_start, base_end, peak_start, peak_end, amplitude): """Calculates the resistance from a series of voltage clamp traces. Keyword arguments: base_start -- Starting index (zero-based) of the baseline cursors. base_end -- End index (zero-based) of the baseline cursors. peak_start -- Starting index (zero-based) of the peak cursors. peak_end -- End index (zero-based) of the peak cursors. amplitude -- Amplitude of the voltage command. Returns: The resistance. """ if not stf.check_doc(): print('Couldn\'t find an open file; aborting now.') return 0 #A temporary array to calculate the average: array = np.empty( (stf.get_size_channel(), stf.get_size_trace()) ) for n in range( 0, stf.get_size_channel() ): # Add this trace to set: array[n] = stf.get_trace( n ) # calculate average and create a new section from it: stf.new_window( np.average(set, 0) ) # set peak cursors: # -1 means all points within peak window. if not stf.set_peak_mean(-1): return 0 if not stf.set_peak_start(peak_start): return 0 if not stf.set_peak_end(peak_end): return 0 # set base cursors: if not stf.set_base_start(base_start): return 0 if not stf.set_base_end(base_end): return 0 # measure everything: stf.measure() # calculate r_seal and return: return amplitude / (stf.get_peak()-stf.get_base())
def resistance(base_start, base_end, peak_start, peak_end, amplitude): """Calculates the resistance from a series of voltage clamp traces. Keyword arguments: base_start -- Starting index (zero-based) of the baseline cursors. base_end -- End index (zero-based) of the baseline cursors. peak_start -- Starting index (zero-based) of the peak cursors. peak_end -- End index (zero-based) of the peak cursors. amplitude -- Amplitude of the voltage command. Returns: The resistance. """ if not stf.check_doc(): print('Couldn\'t find an open file; aborting now.') return 0 #A temporary array to calculate the average: array = np.empty((stf.get_size_channel(), stf.get_size_trace())) for n in range(0, stf.get_size_channel()): # Add this trace to set: array[n] = stf.get_trace(n) # calculate average and create a new section from it: stf.new_window(np.average(set, 0)) # set peak cursors: # -1 means all points within peak window. if not stf.set_peak_mean(-1): return 0 if not stf.set_peak_start(peak_start): return 0 if not stf.set_peak_end(peak_end): return 0 # set base cursors: if not stf.set_base_start(base_start): return 0 if not stf.set_base_end(base_end): return 0 # measure everything: stf.measure() # calculate r_seal and return: return amplitude / (stf.get_peak() - stf.get_base())
def rmean(binwidth, trace=-1, channel=-1): """ Calculates a running mean of a single trace Arguments: binwidth -- size of the bin in sampling points (pt). Obviously, it should be smaller than the length of the trace. trace: -- ZERO-BASED index of the trace within the channel. Note that this is one less than what is shown in the drop-down box. The default value of -1 returns the currently displayed trace. channel -- ZERO-BASED index of the channel. This is independent of whether a channel is active or not. The default value of -1 returns the currently active channel. Returns: A smoothed traced in a new stf window. """ # loads the current trace of the channel in a 1D Numpy Array sweep = stf.get_trace(trace, channel) # creates a destination python list to append the data dsweep = np.empty((len(sweep))) # running mean algorithm for i in range(len(sweep)): if (len(sweep)-i) > binwidth: # append to list the running mean of `binwidth` values # np.mean(sweep) calculates the mean of list dsweep[i] = np.mean( sweep[i:(binwidth+i)] ) else: # use all remaining points for the average: dsweep[i] = np.mean( sweep[i:] ) stf.new_window(dsweep)
def load(): # wx Widgets to create a file selection window fname = wx.FileSelector( "Import Ephus file", default_extension="Ephus XSG", default_path=".", wildcard="Matlab Ephus xsg. text import (*.xsg)|*.xsg", flags=wx.OPEN | wx.FILE_MUST_EXIST) data_sio = sio.loadmat(fname, squeeze_me=True) data = data_sio.get('data') trace = data['ephys'].item().item()[0] header = data_sio.get('header') dt = header['ephys'].item().item()[0]['sampleRate'].item() stf.new_window(trace[1:]) stf.set_sampling_interval(1000 / np.double(dt)) stf.set_xunits('ms') stf.set_yunits('pA')
def rmean(binwidth, trace=-1, channel=-1): """ Calculates a running mean of a single trace Arguments: binwidth -- size of the bin in sampling points (pt). Obviously, it should be smaller than the length of the trace. trace: -- ZERO-BASED index of the trace within the channel. Note that this is one less than what is shown in the drop-down box. The default value of -1 returns the currently displayed trace. channel -- ZERO-BASED index of the channel. This is independent of whether a channel is active or not. The default value of -1 returns the currently active channel. Returns: A smoothed traced in a new stf window. """ # loads the current trace of the channel in a 1D Numpy Array sweep = stf.get_trace(trace, channel) # creates a destination python list to append the data dsweep = np.empty((len(sweep))) # running mean algorithm for i in range(len(sweep)): if (len(sweep) - i) > binwidth: # append to list the running mean of `binwidth` values # np.mean(sweep) calculates the mean of list dsweep[i] = np.mean(sweep[i:(binwidth + i)]) else: # use all remaining points for the average: dsweep[i] = np.mean(sweep[i:]) stf.new_window(dsweep)
def loadtxt(freq=400): """ Loads an ASCII file with extension *.GoR. This file contains ratiometric fluorescent measurements (i.e Green over Red fluorescence) saved in one column. This function opens a new Stimfit window and sets the x-units to "ms" and y-units to "Delta G over R". Arguments: freq -- (float) the sampling rate (in Hz) for fluorescence acquistion. the default value is 400 Hz """ fname = wx.FileSelector("Import Ca transients" , default_extension="Ratiometric" , default_path="." , wildcard = "Ratiometric fluorescence (*.GoR)|*.GoR" , flags = wx.OPEN | wx.FILE_MUST_EXIST) stf.new_window( np.loadtxt(fname) ) stf.set_xunits('ms') stf.set_yunits('Delta G/R') stf.set_sampling_interval(1.0/freq*1000) # acquisition at 400 Hz
def loadtxt(freq=400): """ Loads an ASCII file with extension *.GoR. This file contains ratiometric fluorescent measurements (i.e Green over Red fluorescence) saved in one column. This function opens a new Stimfit window and sets the x-units to "ms" and y-units to "Delta G over R". Arguments: freq -- (float) the sampling rate (in Hz) for fluorescence acquistion. the default value is 400 Hz """ fname = wx.FileSelector("Import Ca transients", default_extension="Ratiometric", default_path=".", wildcard="Ratiometric fluorescence (*.GoR)|*.GoR", flags=wx.OPEN | wx.FILE_MUST_EXIST) stf.new_window(np.loadtxt(fname)) stf.set_xunits('ms') stf.set_yunits('Delta G/R') stf.set_sampling_interval(1.0 / freq * 1000) # acquisition at 400 Hz
def filter_1d_numpy(array_to_filter, sigma, *argv): trace_ = array_to_filter trace_filtered = scipy.ndimage.filters.gaussian_filter(trace_, sigma) if argv[0] == True: stf.new_window(trace_filtered) return (trace_filtered)
def filter_current_trace(sigma): trace_ = stf.get_trace() trace_filtered = scipy.ndimage.filters.gaussian_filter(trace_, sigma) stf.new_window(trace_filtered) return (trace_filtered)