Beispiel #1
0
def monoexpfit(optimization=True, Tn=20):
    """
    Fits monoexponential function with offset to data between the fit cursors
    in the current trace of the active channel using a Chebyshev-Levenberg-
    Marquardt hybrid algorithm. Optimization requires Scipy. Setting optimization
    to False forces this function to use just the Chebyshev algorithm. The maximum
    order of the Chebyshev polynomials can be set using Tn.
    """

    # Get data
    fit_start = stf.get_fit_start()
    fit_end = stf.get_fit_end()
    y = np.double(stf.get_trace()[fit_start:fit_end])
    si = stf.get_sampling_interval()
    l = len(y)
    t = si * np.arange(0, l, 1, np.double)

    # Define monoexponential function
    def f(t, *p):
        return p[0] + p[1] * np.exp(-t / p[2])

    # Get initial values from Chebyshev transform fit
    init = chebexp(1, Tn)
    p0 = (init.get('Offset'), )
    p0 += (init.get('Amp_0'), )
    p0 += (init.get('Tau_0'), )

    # Optimize (if applicable)
    if optimization == True:
        # Optimize fit using Levenberg-Marquardt algorithm
        options = {"ftol": 2.22e-16, "xtol": 2.22e-16, "gtol": 2.22e-16}
        [p, pcov] = optimize.curve_fit(f, t, y, p0, **options)
    elif optimization == False:
        p = list(p0)
    fit = f(t, *p)

    # Calculate SSE
    SSE = np.sum((y - fit)**2)

    # Plot fit in a new window
    matrix = np.zeros((2, stf.get_size_trace())) * np.nan
    matrix[0, :] = stf.get_trace()
    matrix[1, fit_start:fit_end] = fit
    stf.new_window_matrix(matrix)

    # Create table of results
    retval = [("p0_Offset", p[0])]
    retval += [("p1_Amp_0", p[1])]
    retval += [("p2_Tau_0", p[2])]
    retval += [("SSE", SSE)]
    retval += [("dSSE", 1.0 - np.sum((y - f(t, *p0))**2) / SSE)]
    retval += [("Time fit begins", fit_start * si)]
    retval += [("Time fit ends", fit_end * si)]
    retval = dict(retval)
    stf.show_table(
        retval, "monoexpfit, Section #%i" % float(stf.get_trace_index() + 1))

    return
def plot_linear_regression(slope, intercept, length):

    x_values = range(0, length, 1)
    y_values = [((slope * x) + round(intercept)) for x in x_values]

    regression_plot = np.array([x_values, y_values])

    stf.new_window_matrix(regression_plot)

    return (x_values, y_values)
Beispiel #3
0
def plot_episodic_array(sweeps_array):
    #extracts time
    time = sweeps_array[0]

    #gets sampling interval
    sampling_interval = get_sampling_delta(time)

    #plots all traces in single window
    stf.new_window_matrix(sweeps_array[1:])

    #adjusts sampling interval
    stf.set_sampling_interval(sampling_interval)

    return ()
def csv_to_array(csv_file):

    array = np.genfromtxt(csv_file, dtype=float, delimiter=',', names=True)
    data_points = array['Input_0']

    time = array['Timems']
    sampling_interval = round(time[1] - time[0], 4)

    output_array = np.vstack((data_points, time))

    stf.new_window_matrix(output_array)

    stf.set_sampling_interval(sampling_interval)

    return (output_array)
Beispiel #5
0
def Train10AP():
    """
    An example function to perform peak measurements of a train of
    evoked fluorescence signals in the active window
    """

    # Setup
    offset = 40
    stf.set_base_start(0)
    stf.set_peak_start(offset - 2)
    stf.measure()
    base = stf.get_base()
    stf.set_peak_mean(1)
    stf.set_peak_direction("up")
    peak = []

    # Get peak measurements
    for i in range(10):
        stf.set_peak_start(offset + (i * 4) - 2)
        stf.set_peak_end(offset + (i * 4) + 2)
        stf.measure()
        peak.append(stf.get_peak())

    # Plot fit in a new window
    matrix = np.zeros((2, stf.get_size_trace())) * np.nan
    matrix[0, :] = stf.get_trace()
    for i in range(10):
        matrix[1, offset + (i * 4) - 1:offset + (i * 4) + 2] = peak[i]
    stf.new_window_matrix(matrix)

    # Create table of results
    retval = []
    for i in range(10):
        retval += [("Peak %d" % (i), peak[i] - base)]
    retval = dict(retval)
    stf.show_table(retval,
                   "Train10AP, Section #%i" % float(stf.get_trace_index() + 1))

    return
Beispiel #6
0
    def psc_analysis(self):

        self.psc_data = self.metadata[self.metadata.recording_type == 'psc']
        self.psc_data['ch1_self_psc'] = [False for _ in self.psc_data]
        self.psc_data['ch1_other_psc'] = [False for _ in self.psc_data]
        self.psc_data['ch2_self_psc'] = [False for _ in self.psc_data]
        self.psc_data['ch2_other_psc'] = [False for _ in self.psc_data]
        self.psc_data['ch1_self_tau'] = [False for _ in self.psc_data]
        self.psc_data['ch1_other_tau'] = [False for _ in self.psc_data]
        self.psc_data['ch2_self_tau'] = [False for _ in self.psc_data]
        self.psc_data['ch2_other_tau'] = [False for _ in self.psc_data]

        for idx, abffiles in enumerate(self.psc_data):
            # Initialize Channel 1
            data = AbfFile(abffiles, self.path)
            stf.new_window_matrix(data.channel1.signal)

            # Cell 1 to Self
            advance_var = input("Cell 1: Select the traces to use, set the Peak, Baseline, and fit cursors for self.\n"
                                "If no connection exists, type 'no connection'\n"
                                "Type 'y' to advance to the next trace:\t")

            while advance_var is not 'y' or advance_var is not 'no connection':
                advance_var = input("You must type 'y' or 'no connection' to advance:\t")

            if advance_var == 'no connection':
                self.psc_data['ch1_self_psc'][idx] = None
                self.psc_data['ch1_self_tau'][idx] = None
            else:
                peaks = np.array(
                    [np.min(trace[stf.get_peak_start():stf.get_peak_end()]) for trace in data.channel1.signal]
                )
                bases = np.array(
                    [np.mean(trace[stf.get_base_start():stf.get_base_end()]) for trace in data.channel1.signal]
                )
                peak = np.mean(peaks - bases)
                fit = stf.leastsq(0)
                self.psc_data['ch1_self_psc'][idx] = peak
                self.psc_data['ch1_self_tau'][idx] = fit['Tau_0']

            # Cell 2 to Cell 1
            advance_var = input("Great, now do the same for Cell 2 onto Cell 1.\n"
                                "Type 'y' to advance to the next trace:\t")

            while advance_var is not 'y' or advance_var is not 'no connection':
                advance_var = input("You must type 'y' or 'no connection' to advance:\t")

            if advance_var == 'no connection':
                self.psc_data['ch2_other_psc'][idx] = None
                self.psc_data['ch2_other_tau'][idx] = None
            else:
                peaks = np.array(
                    [np.min(trace[stf.get_peak_start():stf.get_peak_end()]) for trace in data.channel1.signal]
                )
                bases = np.array(
                    [np.mean(trace[stf.get_base_start():stf.get_base_end()]) for trace in data.channel1.signal]
                )
                peak = np.mean(peaks - bases)
                fit = stf.leastsq(0)
                self.psc_data['ch2_other_psc'][idx] = peak
                self.psc_data['ch2_other_tau'][idx] = fit['Tau_0']

            # Initialize Channel 2
            data = AbfFile(abffiles, self.path)
            stf.new_window_matrix(data.channel1.signal)

            # Cell 2 to Self
            advance_var = input("Cell 2: Select the traces to use, set the Peak, Baseline, and fit cursors for self.\n"
                                "If no connection exists, type 'no connection'\n"
                                "Type 'y' to advance to the next trace:\t")

            while advance_var is not 'y' or advance_var is not 'no connection':
                advance_var = input("You must type 'y' or 'no connection' to advance:\t")

            if advance_var == 'no connection':
                self.psc_data['ch2_self_psc'][idx] = None
                self.psc_data['ch2_self_tau'][idx] = None
            else:
                peaks = np.array([np.min(trace[stf.get_peak_start():stf.get_peak_end()]) for trace in data.channel1.signal])
                bases = np.array(
                    [np.mean(trace[stf.get_base_start():stf.get_base_end()]) for trace in data.channel1.signal])
                peak = np.mean(peaks - bases)
                fit = stf.leastsq(0)
                self.psc_data['ch2_self_psc'][idx] = peak
                self.psc_data['ch2_self_tau'][idx] = fit['Tau_0']

            # Cell 2 to Cell 1
            advance_var = input("Great, now do the same for Cell 2 onto Cell 1.\n"
                                "Type 'y' to advance to the next trace:\t")

            while advance_var is not 'y' or advance_var is not 'no connection':
                advance_var = input("You must type 'y' or 'no connection' to advance:\t")

            if advance_var == 'no connection':
                self.psc_data['ch1_other_psc'][idx] = None
                self.psc_data['ch1_other_tau'][idx] = None
            else:
                peaks = np.array([np.min(trace[stf.get_peak_start():stf.get_peak_end()]) for trace in data.channel1.signal])
                bases = np.array(
                    [np.mean(trace[stf.get_base_start():stf.get_base_end()]) for trace in data.channel1.signal])
                peak = np.mean(peaks - bases)
                fit = stf.leastsq(0)
                self.psc_data['ch1_other_psc'][idx] = peak
                self.psc_data['ch1_other_tau'][idx] = fit['Tau_0']

        self.psc_data.to_csv(os.path.join(self.path, '.stimpy', 'psc_data.csv'))