示例#1
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)
示例#2
0
def pon_batch(iv_pulses,
              pon_pulses=8,
              trace_start=0,
              subtract_base=False,
              factor=1.0):
    """Extracts p-over-n corrected traces in FPulse-generated files,
    and then creates an IV for the currently active channel.
    
    Keyword arguments:
    iv_pulses --     Number of pulses for the IV.
    pon_pulses --    Number of p-over-n correction pulses.
                     This is typically 4 (for PoN=5 in the FPulse script)
                     or 8 (for PoN=9).
    trace_start --   ZERO-BASED index of the first trace to be
                     used for the IV. Note that this is one less
                     than what is diplayed in the drop-down box.
    subtract_base -- Set to True if you want to subtract the baseline
                     at the end. You will need to set the baseline cursors
                     in the original file to appropriate positions if
                     you want to do this.
    factor --        Multiply result with an optional factor, typically
                     from some external scaling.
    Returns:
    True upon success, False otherwise.
    """

    # Extract corrected pulses:
    if (select_pon(pon_pulses) == False):
        return False

    if (stf.new_window_selected_this() == False):
        return False

    # Create IV:
    if (analyze_iv(iv_pulses, trace_start, factor) == False):
        return False

    # Subtract base:
    if (subtract_base == True):
        stf.select_all()
        if (stf.subtract_base() == False):
            return False

    return True
示例#3
0
文件: charlie.py 项目: yueqiw/stimfit
def glu_iv(pulses=13, subtract_base=True):
    """Calculates an iv from a repeated series of fast application and
    voltage pulses. 

    Keyword arguments:
    pulses        -- Number of pulses for the iv.
    subtract_base -- If True (default), baseline will be subtracted.
    
    Returns:
    True if successful.
    """

    # Some ugly definitions for the time being
    # Cursors are in ms here.
    gFitEnd = 330.6  # fit end cursor is variable
    gFSelect = 0  # Monoexp
    gDictSize = stf.leastsq_param_size(
        gFSelect) + 2  # Parameters, chisqr, peak value
    gBaseStart = 220.5  # Start and end of the baseline before the control pulse, in ms
    gBaseEnd = 223.55
    gPeakStart = 223.55  # Start and end of the peak cursors for the control pulse, in ms
    gPeakEnd = 253.55

    if (gDictSize < 0):
        print('Couldn\'t retrieve function id=%d, aborting now.' % gFSelect)
        return False

    if (not (stf.check_doc())):
        print('Couldn\'t find an open file; aborting now.')
        return False

    # analyse iv, subtract baseline if requested:
    ivtools.analyze_iv(pulses)
    if (subtract_base == True):
        if (not (stf.set_base_start(gBaseStart, True))): return False
        if (not (stf.set_base_end(gBaseEnd, True))): return False
        stf.measure()
        stf.select_all()
        stf.subtract_base()

    # set cursors:
    if (not (stf.set_peak_start(gPeakStart, True))): return False
    if (not (stf.set_peak_end(gPeakEnd, True))): return False
    if (not (stf.set_base_start(gBaseStart, True))): return False
    if (not (stf.set_base_end(gBaseEnd, True))): return False
    if (not (stf.set_fit_end(gFitEnd, True))): return False

    if (not (stf.set_peak_mean(3))): return False
    if (not (stf.set_peak_direction("both"))): return False

    # A list for dictionary keys and values:
    dict_keys = []
    dict_values = np.empty((gDictSize, stf.get_size_channel()))
    firstpass = True
    for n in range(0, stf.get_size_channel()):
        if (stf.set_trace(n) == False):
            print('Couldn\'t set a new trace; aborting now.')
            return False

        print('Analyzing trace %d of %d' % (n + 1, stf.get_size_channel()))
        # set the fit window cursors:
        if (not (stf.set_fit_start(stf.peak_index()))): return False

        # Least-squares fitting:
        p_dict = stf.leastsq(gFSelect)

        if (p_dict == 0):
            print('Couldn\'t perform a fit; aborting now.')
            return False

        # Create an empty list:
        tempdict_entry = []
        row = 0
        for k, v in p_dict.iteritems():
            if (firstpass == True):
                dict_keys.append(k)
            dict_values[row][n] = v
            row = row + 1

        if (firstpass):
            dict_keys.append("Peak amplitude")
        dict_values[row][n] = stf.get_peak() - stf.get_base()

        firstpass = False

    retDict = dict()
    # Create the dictionary for the table:
    entry = 0
    for elem in dict_keys:
        retDict[elem] = dict_values[entry].tolist()
        entry = entry + 1

    return stf.show_table_dictlist(retDict)
示例#4
0
def glu_iv( pulses = 13, subtract_base=True ):
    """Calculates an iv from a repeated series of fast application and
    voltage pulses. 

    Keyword arguments:
    pulses        -- Number of pulses for the iv.
    subtract_base -- If True (default), baseline will be subtracted.
    
    Returns:
    True if successful.
    """

    # Some ugly definitions for the time being
    # Cursors are in ms here.
    gFitEnd = 330.6 # fit end cursor is variable
    gFSelect  =  0 # Monoexp
    gDictSize =  stf.leastsq_param_size( gFSelect ) + 2 # Parameters, chisqr, peak value
    gBaseStart  = 220.5 # Start and end of the baseline before the control pulse, in ms
    gBaseEnd    = 223.55
    gPeakStart  = 223.55 # Start and end of the peak cursors for the control pulse, in ms
    gPeakEnd = 253.55 
    
    if ( gDictSize < 0 ):
        print('Couldn\'t retrieve function id=%d, aborting now.'%gFSelect)
        return False        
    
    if ( not(stf.check_doc()) ):
        print('Couldn\'t find an open file; aborting now.')
        return False
    
    # analyse iv, subtract baseline if requested:
    ivtools.analyze_iv( pulses )
    if ( subtract_base == True ):
        if ( not(stf.set_base_start( gBaseStart, True )) ): return False
        if ( not(stf.set_base_end( gBaseEnd, True )) ): return False
        stf.measure()
        stf.select_all()
        stf.subtract_base()
    
    # set cursors:
    if ( not(stf.set_peak_start( gPeakStart, True )) ): return False
    if ( not(stf.set_peak_end( gPeakEnd, True )) ): return False
    if ( not(stf.set_base_start( gBaseStart, True )) ): return False
    if ( not(stf.set_base_end( gBaseEnd, True )) ): return False
    if ( not(stf.set_fit_end( gFitEnd, True )) ): return False
    
    if ( not(stf.set_peak_mean( 3 )) ): return False
    if ( not(stf.set_peak_direction( "both" )) ): return False

    # A list for dictionary keys and values:
    dict_keys = []
    dict_values = np.empty( (gDictSize, stf.get_size_channel()) )
    firstpass = True
    for n in range( 0, stf.get_size_channel() ):
        if ( stf.set_trace( n ) == False ):
            print('Couldn\'t set a new trace; aborting now.')
            return False
        
        print('Analyzing trace %d of %d'%( n+1, stf.get_size_channel() ) )
        # set the fit window cursors:
        if ( not(stf.set_fit_start( stf.peak_index() )) ): return False
        
        # Least-squares fitting:
        p_dict = stf.leastsq( gFSelect )
        
        if ( p_dict == 0 ):
            print('Couldn\'t perform a fit; aborting now.')
            return False
            
        # Create an empty list:
        tempdict_entry = []
        row = 0
        for k, v in p_dict.iteritems():
            if ( firstpass == True ):
                dict_keys.append( k )
            dict_values[row][n] = v 
            row = row+1
        
        if ( firstpass ):
            dict_keys.append( "Peak amplitude" )
        dict_values[row][n] = stf.get_peak()-stf.get_base()
        
        firstpass = False
    
    retDict = dict()
    # Create the dictionary for the table:
    entry = 0
    for elem in dict_keys:
        retDict[ elem ] = dict_values[entry].tolist()
        entry = entry+1
   
    return stf.show_table_dictlist( retDict )