示例#1
0
def risealign():
    """
    Shift the selected traces in the currently active channel to align to the rise. 

    """

    # Measure peak indices in the selected traces
    rtidx = []
    for i in stf.get_selected_indices():
        stf.set_trace(i)
        rtidx.append(stf.rtlow_index())

    # Find the earliest peak
    rtref = min(rtidx)

    # Align the traces
    j = 0
    shifted_traces = []
    for i in stf.get_selected_indices():
        stf.set_trace(i)
        shift = int(round(rtref - rtidx[j]))
        shifted_traces.append(np.roll(stf.get_trace(), shift))
        j += 1

    return stf.new_window_list(shifted_traces)
示例#2
0
def peakscale():
    """
    Scale the selected traces in the currently active channel to their mean peak amplitude. 

    """

    # Measure baseline in selected traces
    base = []
    for i in stf.get_selected_indices():
        stf.set_trace(i)
        base.append(stf.get_base())

    # Subtract baseline from selected traces
    stf.subtract_base()

    # Measure peak amplitudes in baseline-subtracted traces
    stf.select_all()
    peak = []
    for i in stf.get_selected_indices():
        stf.set_trace(i)
        peak.append(stf.get_peak())

    # Calculate scale factor to make peak equal to the mean peak amplitude
    scale_factor = peak / np.mean(peak)

    # Scale the traces and apply offset equal to the mean baseline
    scaled_traces = [
        stf.get_trace(i) / scale_factor[i] + np.mean(base)
        for i in stf.get_selected_indices()
    ]

    # Close window of baseline-subtracted traces
    stf.close_this()

    return stf.new_window_list(scaled_traces)
示例#3
0
def peakalign():
    """
    Shift the selected traces in the currently active channel to align the peaks. 

    """

    # Measure peak indices in the selected traces
    pidx = []
    for i in stf.get_selected_indices():
        stf.set_trace(i)
        pidx.append(stf.peak_index())

    # Find the earliest peak
    pref = min(pidx)

    # Align the traces
    j = 0
    shifted_traces = []
    for i in stf.get_selected_indices():
        stf.set_trace(i)
        shift = int(pref - pidx[j])
        shifted_traces.append(np.roll(stf.get_trace(), shift))
        j += 1

    return stf.new_window_list(shifted_traces)
示例#4
0
def median_filter(n):
    """
    Perform median smoothing filter on the selected traces. 
    Computationally this is achieved by a central simple moving 
    median over a sliding window of n points.

    The function uses reflect (or bounce) end corrections

    """

    # Check that at least one trace was selected
    if not stf.get_selected_indices():
        raise IndexError('No traces were selected')

    # Check that the number of points in the sliding window is odd
    n = int(n)
    if n % 2 != 1:
        raise ValueError('The filter rank must be an odd integer')
    elif n <= 1:
        raise ValueError('The filter rank must > 1')

    # Apply smoothing filter
    filtered_traces = []
    for i in stf.get_selected_indices():
        l = stf.get_size_trace(i)
        padded_trace = np.pad(stf.get_trace(i), (n - 1) / 2, 'reflect')
        filtered_traces.append(
            [np.median(padded_trace[j:n + j]) for j in range(l)])

    print "Window width was %g ms" % (stf.get_sampling_interval() * (n - 1))

    return stf.new_window_list(filtered_traces)
示例#5
0
def count_aps():
    """
    Shows a result table with the number of action potentials (i.e
    events whose potential is above 0 mV) in selected traces.  If
    no trace is selected, then the current trace is analyzed.

    Returns:
    False if document is not open.
    """
    if not stf.check_doc():
        print("Open file first")
        return False
  
    if len( stf.get_selected_indices() )==0: 
        sel_trace = [ stf.get_trace_index()]
    else: 
        sel_trace = stf.get_selected_indices()

    mytable = dict()
    for trace in sel_trace:
        tstart = 0
        tend = stf.get_size_trace(trace)*stf.get_sampling_interval()
        threshold = 0
        spikes = count_events(tstart, tend, threshold, True, trace, True)
        mytable["Trace %.3d" %trace] = spikes

    stf.show_table(mytable)

    return True
示例#6
0
文件: spells.py 项目: yueqiw/stimfit
def count_aps():
    """
    Shows a result table with the number of action potentials (i.e
    events whose potential is above 0 mV) in selected traces.  If
    no trace is selected, then the current trace is analyzed.

    Returns:
    False if document is not open.
    """
    if not stf.check_doc():
        print("Open file first")
        return False

    if len(stf.get_selected_indices()) == 0:
        sel_trace = [stf.get_trace_index()]
    else:
        sel_trace = stf.get_selected_indices()

    mytable = dict()
    for trace in sel_trace:
        tstart = 0
        tend = stf.get_size_trace(trace) * stf.get_sampling_interval()
        threshold = 0
        spikes = count_events(tstart, tend, threshold, True, trace, True)
        mytable["Trace %.3d" % trace] = spikes

    stf.show_table(mytable)

    return True
示例#7
0
def normalize():
    """
    Normalize to the peak amplitude of the selected trace and 
    scale all other traces in the currently active channel by 
    the same factor. 

    Ensure that you subtract the baseline before normalizing
    """

    # Find index of the selected trace
    idx = stf.get_selected_indices()
    if len(idx) > 1:
        raise ValueError('More than one trace was selected')
    elif len(idx) < 1:
        raise ValueError('Select one trace to subtract from the others')

    # Measure peak amplitude in the selected trace
    stf.set_trace(idx[0])
    refval = np.abs(stf.get_peak())

    # Apply normalization
    scaled_traces = [
        stf.get_trace(i) / refval for i in range(stf.get_size_channel())
    ]

    return stf.new_window_list(scaled_traces)
示例#8
0
    def plot_screen(self):
        import stf

        tsl = []
        try:
            l = stf.get_selected_indices()
            for idx in l:
                tsl.append(
                    stfio_plot.Timeseries(stf.get_trace(idx),
                                          stf.get_sampling_interval(),
                                          yunits=stf.get_yunits(),
                                          color='0.2'))
                fit = stf.get_fit(idx)
                if fit is not None:
                    self.axes.plot(fit[0],
                                   fit[1],
                                   color='0.4',
                                   alpha=0.5,
                                   lw=5.0)
        except:
            pass

        tsl.append(
            stfio_plot.Timeseries(stf.get_trace(),
                                  stf.get_sampling_interval(),
                                  yunits=stf.get_yunits()))
        if stf.get_size_recording() > 1:
            tsl2 = [
                stfio_plot.Timeseries(
                    stf.get_trace(trace=-1,
                                  channel=stf.get_channel_index(False)),
                    stf.get_sampling_interval(),
                    yunits=stf.get_yunits(
                        trace=-1, channel=stf.get_channel_index(False)),
                    color='r',
                    linestyle='-r')
            ]
            stfio_plot.plot_traces(tsl,
                                   traces2=tsl2,
                                   ax=self.axes,
                                   textcolor2='r',
                                   xmin=stf.plot_xmin(),
                                   xmax=stf.plot_xmax(),
                                   ymin=stf.plot_ymin(),
                                   ymax=stf.plot_ymax(),
                                   y2min=stf.plot_y2min(),
                                   y2max=stf.plot_y2max())
        else:
            stfio_plot.plot_traces(tsl,
                                   ax=self.axes,
                                   xmin=stf.plot_xmin(),
                                   xmax=stf.plot_xmax(),
                                   ymin=stf.plot_ymin(),
                                   ymax=stf.plot_ymax())
        fit = stf.get_fit()
        if fit is not None:
            self.axes.plot(fit[0], fit[1], color='0.2', alpha=0.5, lw=5.0)
示例#9
0
def subtract_trace():
    """
    Subtract the selected trace from all traces in the currently active channel

    """

    # Find index of the selected trace to subtract from all the other traces
    idx = stf.get_selected_indices()
    if len(idx) > 1:
        raise ValueError('More than one trace was selected')
    elif len(idx) < 1:
        raise ValueError('Select one trace to subtract from the others')

    # Apply subtraction
    subtracted_traces = [
        stf.get_trace(i) - stf.get_trace(idx[0])
        for i in range(stf.get_size_channel())
    ]

    return stf.new_window_list(subtracted_traces)
示例#10
0
 def plot_screen(self):
     import stf
     
     tsl = []
     try:
         l = stf.get_selected_indices()
         for idx in l:
             tsl.append(stfio_plot.Timeseries(stf.get_trace(idx), 
                                              stf.get_sampling_interval(),
                                              yunits = stf.get_yunits(),
                                              color='0.2'))
             fit = stf.get_fit(idx)
             if fit is not None:
                 self.axes.plot(fit[0], fit[1], color='0.4', alpha=0.5, lw=5.0)
     except:
         pass
     
     tsl.append(stfio_plot.Timeseries(stf.get_trace(),
                                      stf.get_sampling_interval(),
                                      yunits = stf.get_yunits()))
     if stf.get_size_recording()>1:
         tsl2 = [stfio_plot.Timeseries(stf.get_trace(trace=-1, channel=stf.get_channel_index(False)),
                                       stf.get_sampling_interval(),
                                       yunits = stf.get_yunits(trace=-1, channel=stf.get_channel_index(False)),
                                       color='r', linestyle='-r')]
         stfio_plot.plot_traces(tsl, traces2=tsl2, ax=self.axes, textcolor2 = 'r',
                                xmin=stf.plot_xmin(), xmax=stf.plot_xmax(),
                                ymin=stf.plot_ymin(), ymax=stf.plot_ymax(), 
                                y2min=stf.plot_y2min(), y2max=stf.plot_y2max())
     else:
         stfio_plot.plot_traces(tsl, ax=self.axes,
                                xmin=stf.plot_xmin(), xmax=stf.plot_xmax(),
                                ymin=stf.plot_ymin(), ymax=stf.plot_ymax())
     fit = stf.get_fit()
     if fit is not None:
         self.axes.plot(fit[0], fit[1], color='0.2', alpha=0.5, lw=5.0)