def find_sample_points_of_detected_events(whole_trace_file,
                                          extracted_events_file, sweep_num):
    """takes the window of detected events from stimfit and, for each events, runs through the full trace to pull out time (in samples) of event
	"""
    #open and load trace from whole file
    stf.file_open(whole_trace_file)
    stf.set_trace(sweep_num)
    whole_trace = stf.get_trace()
    sampling_interval = stf.get_sampling_interval()

    #open extracted events file
    stf.file_open(extracted_events_file)

    time_points = []
    for trace in range(stf.get_size_channel()):
        stf.set_trace(trace)
        trace_to_search = stf.get_trace(trace)
        # run find trace with updated search index
        # start at sample = 0 for first run through
        if len(time_points) == 0:
            sample_start = 0
        else:
            sample_start = int(time_points[len(time_points) - 1] /
                               sampling_interval)

        output_index = sub_func_find_trace(trace_to_search, whole_trace,
                                           sample_start)
        time_point = output_index * sampling_interval
        time_points.append(time_point)

    return (time_points)
    def get_time_points(self):
        #need to get specified sweep of the whole trace to an array
        stf.file_open(self.whole_trace_file)
        stf.set_trace(self.sweep_number)
        trace_array = stf.get_trace()

        #runs program to find sample indicies of selected EPSCs and converts to times
        stf.file_open(self.event_file)
        event_samples_list = find_sample_points_of_detected_events(trace_array)
        sampling_interval = stf.get_sampling_interval()
        event_times_list = [(sample * sampling_interval)
                            for sample in event_samples_list]

        return (event_times_list)
def batch_compile(file_list, summary_file_name):

    means_dict = {}

    for fname in file_list:
        stf.file_open(fname)

        file_df = compile_amplitudes_in_trace()

        means_dict[stf.get_filename()] = file_df.mean(axis=0)

    summary_df = pd.DataFrame(means_dict)
    summary_df.to_excel(str(summary_file_name) + '_amplitudes_compiled.xlsx')

    return (summary_df)
Esempio n. 4
0
def batch_fit(file_list, fit_search_params, pulse_length, fit_function):

	output_list = []
	for fname in file_list:
		print(fname);
		stf.file_open(str(fname)); 
		fit_df_exp = fitexpt.fit_experiment(fit_search_params, pulse_length, fit_function);
		
		fit_df_exp.to_csv(fname+'_fit_results.csv', sep=',');
		output_list.append(fname+'_fit_results.csv'); 
	
	
	
	
	return(output_list)
def batch_find_ISIs(dictionary_for_traces):

    output_dictionary_ISI = {}

    for whole_trace_file in dictionary_for_traces.keys():

        event_file_dictionary_ISI = {}

        for event_file in dictionary_for_traces[whole_trace_file]:

            stf.file_open(event_file)

            sweep_num = event_file[20]

            print('sweep' + sweep_num)

            ISI_index = find_sample_points_of_detected_events(
                whole_trace_file, int(sweep_num))

            event_file_dictionary_ISI[event_file] = ISI_index

        output_dictionary_ISI[whole_trace_file] = event_file_dictionary_ISI

    return (output_dictionary_ISI)
def return_holding_current(selected_wb, raw_wb):

    wb_ = xlrd.open_workbook(selected_wb)
    sheets = [
        str(sheet.name) for sheet in wb_.sheets()
        if 'normalized' in str(sheet.name)
    ]
    sheets_to_load_from_raw = [
        sheet.strip('normalized') + 'iled_files' for sheet in sheets
    ]
    wb_raw = xlrd.open_workbook(raw_wb)
    raw_sheets = [
        str(sheet.name) for sheet in wb_raw.sheets()
        if 'compiled_files' in str(sheet.name)
    ]
    baseline_files = []
    exp_files = []
    for raw_sheet in raw_sheets:

        try:
            df = pd.read_excel(wb_raw,
                               engine='xlrd',
                               sheetname=str(raw_sheet),
                               index_col=[0, 1])

            files_in_experiment = df.index.levels[0].values[:2]

            baseline_files.append(files_in_experiment[0])
            exp_files.append(files_in_experiment[1])
        except:
            pass

    print(baseline_files)

    baseline_files_to_load = []
    exp_files_to_load = []
    rootsearchdir = '/Users/johnmarshall/Documents/Analysis/RecordingData/'
    for fname, efname in zip(baseline_files, exp_files):
        try:
            if 'TSeries' in fname:
                dir_data = search_dir_iterative_for_extension_tupleoutput(
                    rootsearchdir, str(fname + 'vrecd_loaded.csv'))
            else:
                dir_data = search_dir_iterative_for_extension_tupleoutput(
                    rootsearchdir, fname)
                baseline_files_to_load.append(dir_data[1][1] + '/' +
                                              dir_data[1][0])
                dir_data = search_dir_iterative_for_extension_tupleoutput(
                    rootsearchdir, efname)
                exp_files_to_load.append(dir_data[1][1] + '/' + dir_data[1][0])
        except IndexError:
            print('could not find:', fname)
        pass

    print(baseline_files_to_load)
    dict_to_return = {}
    holding_current_time_series = {}
    for f, ef in zip(baseline_files_to_load, exp_files_to_load):
        currents = []
        print(f)

        if f.endswith('.abf'):
            stf.file_open(f)
        else:
            if 'TSeries' in f:
                sweeps_compiled_from_pv_tseries = pv.import_t_series_episodic(
                    f.strip('vrecd_loaded.csv'))
                pv.plot_episodic_array(sweeps_compiled_from_pv_tseries)

        baselines = []
        for sweep in range((stf.get_size_channel() - 30),
                           stf.get_size_channel()):
            stf.set_trace(sweep)
            stf.set_base_start(100, is_time=True)
            stf.set_base_end(100, is_time=True)
            baselines.append(stf.get_base())
        file_baseline = np.mean(baselines)
        currents.append(file_baseline)
        holding_current_time_series[f] = baselines

        stf.file_open(ef)
        baselines = []
        for sweep in range((stf.get_size_channel() - 15),
                           stf.get_size_channel()):
            stf.set_trace(sweep)
            stf.set_base_start(100, is_time=True)
            stf.set_base_end(100, is_time=True)
            baselines.append(stf.get_base())
        file_baseline = np.mean(baselines)
        currents.append(file_baseline)
        dict_to_return[f] = currents

    df_out = pd.DataFrame(dict_to_return)
    time_series_df = pd.DataFrame(holding_current_time_series)

    df_out.to_excel(
        '/Users/johnmarshall/Documents/Analysis/eCB_paper/holding_current.xlsx'
    )
    time_series_df.to_excel(
        '/Users/johnmarshall/Documents/Analysis/eCB_paper/holding_current_time_series.xlsx'
    )

    return (df_out)
Esempio n. 7
0
def figure():
    sd_factor = 5.0
    # to yield a low total number of false positive and negative events:
    deconv_th = 4.0
    matching_th = 2.5
    deconv_min_int = 5.0
    matching_min_int = 5.0

    module_dir = os.path.dirname(__file__)

    import stf
    if not stf.file_open("%s/dat/events.h5" % module_dir):
        sys.stderr.write("Couldn't open %s/dat/events.h5; aborting now.\n" %
                         module_dir)
        return
    dt = stf.get_sampling_interval()
    trace = stf.get_trace() * 1e3
    plot_start_t = 55310.0
    plot_end_t = 55640.0
    plot_hi_start_t = 55489.0
    plot_hi_end_t = 55511.0
    plot_start_i = int(plot_start_t / dt)
    plot_end_i = int(plot_end_t / dt)
    plot_hi_start_i = int(plot_hi_start_t / dt)
    plot_hi_end_i = int(plot_hi_end_t / dt)
    plot_trace = trace[plot_start_i:plot_end_i]
    plot_hi_trace = trace[plot_hi_start_i:plot_hi_end_i]
    trange = np.arange(len(plot_trace)) * dt
    trange_hi = np.arange(len(plot_hi_trace)) * dt
    templ, templ_epscs, spiketimes = template(sd_factor=sd_factor)
    plot_templ = templ * 1e3
    templ_epscs *= 1e3
    rec_threshold = trace.mean() - trace.std() * sd_factor
    t_templ = np.arange(templ_epscs.shape[1]) * dt

    # subtract baseline and normalize template:
    templ -= templ[0]
    if np.abs(templ.min()) > np.abs(templ.max()):
        templ /= np.abs(templ.min())
    else:
        templ /= templ.max()
    deconv_amps, deconv_onsets, deconv_crit, \
        matching_amps, matching_onsets, matching_crit = \
        events(-templ, deconv_th=deconv_th, matching_th=matching_th,
               deconv_min_int=deconv_min_int, matching_min_int=matching_min_int)

    theoretical_ieis = np.diff(spiketimes)
    theoretical_peaks_t = spiketimes  # + np.argmax(templ)*dt
    theoretical_peaks_t_plot = theoretical_peaks_t[
        (theoretical_peaks_t > plot_start_i * dt)
        & (theoretical_peaks_t < plot_end_i * dt)] - plot_start_i * dt + 1.0
    theoretical_peaks_t_plot_hi = theoretical_peaks_t[
        (theoretical_peaks_t > plot_hi_start_i * dt)
        & (theoretical_peaks_t < plot_hi_end_i *
           dt)] - plot_hi_start_i * dt + 1.0

    deconv_peaks_t = deconv_onsets  # + np.argmax(templ)*dt
    deconv_peaks_t_plot = deconv_peaks_t[
        (deconv_peaks_t > plot_start_i * dt)
        & (deconv_peaks_t < plot_end_i * dt)] - plot_start_i * dt
    deconv_peaks_t_plot_hi = deconv_peaks_t[
        (deconv_peaks_t > plot_hi_start_i * dt)
        & (deconv_peaks_t < plot_hi_end_i * dt)] - plot_hi_start_i * dt
    matching_peaks_t = matching_onsets  # + np.argmax(templ)*dt
    matching_peaks_t_plot = matching_peaks_t[
        (matching_peaks_t > plot_start_i * dt)
        & (matching_peaks_t < plot_end_i * dt)] - plot_start_i * dt
    matching_peaks_t_plot_hi = matching_peaks_t[
        (matching_peaks_t > plot_hi_start_i * dt)
        & (matching_peaks_t < plot_hi_end_i * dt)] - plot_hi_start_i * dt

    deconv_correct = np.zeros((deconv_peaks_t.shape[0]))
    matching_correct = np.zeros((matching_peaks_t.shape[0]))
    for theor in theoretical_peaks_t:
        if (np.abs(deconv_peaks_t - theor)).min() < deconv_min_int:
            deconv_correct[(np.abs(deconv_peaks_t - theor)).argmin()] = True
        if (np.abs(matching_peaks_t - theor)).min() < matching_min_int:
            matching_correct[(np.abs(matching_peaks_t -
                                     theor)).argmin()] = True

    total_events = spiketimes.shape[0]
    deconv_TP = deconv_correct.sum() / deconv_correct.shape[0]
    deconv_FP = (deconv_correct.shape[0] -
                 deconv_correct.sum()) / deconv_correct.shape[0]
    deconv_FN = (total_events - deconv_correct.sum()) / total_events
    sys.stdout.write("True positives deconv: %.2f\n" % (deconv_TP * 100.0))
    sys.stdout.write("False positives deconv: %.2f\n" % (deconv_FP * 100.0))
    sys.stdout.write("False negatives deconv: %.2f\n" % (deconv_FN * 100.0))
    matching_TP = matching_correct.sum() / matching_correct.shape[0]
    matching_FP = (matching_correct.shape[0] -
                   matching_correct.sum()) / matching_correct.shape[0]
    matching_FN = (total_events - matching_correct.sum()) / total_events
    sys.stdout.write("True positives matching: %.2f\n" % (matching_TP * 100.0))
    sys.stdout.write("False positives matching: %.2f\n" %
                     (matching_FP * 100.0))
    sys.stdout.write("False negatives matching: %.2f\n" %
                     (matching_FN * 100.0))

    gs = gridspec.GridSpec(11, 13)
    fig = plt.figure(figsize=(16, 12))

    ax = stfio_plot.StandardAxis(fig, gs[:5, :6], hasx=False, hasy=False)
    ax.plot(trange, plot_trace, '-k', lw=2)
    ax.plot(theoretical_peaks_t_plot,
            theoretical_peaks_t_plot**0 * np.max(plot_trace),
            'v',
            ms=12,
            mew=2.0,
            mec='k',
            mfc='None')
    ax.axhline(rec_threshold, ls='--', color='r', lw=2.0)
    stfio_plot.plot_scalebars(ax, xunits="ms", yunits="pA")

    ax_templ = stfio_plot.StandardAxis(fig,
                                       gs[:5, 7:],
                                       hasx=False,
                                       hasy=False,
                                       sharey=ax)
    for epsc in templ_epscs:
        ax_templ.plot(t_templ, -epsc, '-', color='0.5', alpha=0.5)
    ax_templ.plot(t_templ, -templ_epscs.mean(axis=0), '-k', lw=2)
    ax_templ.plot(t_templ[-plot_templ.shape[0]:],
                  -plot_templ,
                  '-r',
                  lw=4,
                  alpha=0.5)
    stfio_plot.plot_scalebars(ax_templ, xunits="ms", yunits="pA", sb_yoff=0.1)

    ax_matching = stfio_plot.StandardAxis(fig,
                                          gs[5:7, :6],
                                          hasx=False,
                                          hasy=False,
                                          sharex=ax)
    ax_matching.plot(trange, matching_crit[plot_start_i:plot_end_i], '-g')
    stfio_plot.plot_scalebars(ax_matching, xunits="ms", yunits="SD", nox=True)
    ax_matching.axhline(matching_th, ls='--', color='r', lw=2.0)
    ax_matching.plot(theoretical_peaks_t_plot,
                     theoretical_peaks_t_plot**0 * 1.25 *
                     np.max(matching_crit[plot_start_i:plot_end_i]),
                     'v',
                     ms=12,
                     mew=2.0,
                     mec='k',
                     mfc='None')
    ax_matching.plot(matching_peaks_t_plot,
                     matching_peaks_t_plot**0 *
                     np.max(matching_crit[plot_start_i:plot_end_i]),
                     'v',
                     ms=12,
                     mew=2.0,
                     mec='g',
                     mfc='None')
    ax_matching.set_ylim(None,
                         1.37 * np.max(matching_crit[plot_start_i:plot_end_i]))
    ax_matching.set_title(r"Template matching")

    ax_deconv = stfio_plot.StandardAxis(fig,
                                        gs[7:9, :6],
                                        hasx=False,
                                        hasy=False,
                                        sharex=ax)
    ax_deconv.plot(trange, deconv_crit[plot_start_i:plot_end_i], '-b')
    stfio_plot.plot_scalebars(ax_deconv, xunits="ms", yunits="SD")
    ax_deconv.axhline(deconv_th, ls='--', color='r', lw=2.0)
    ax_deconv.plot(theoretical_peaks_t_plot,
                   theoretical_peaks_t_plot**0 * 1.2 *
                   np.max(deconv_crit[plot_start_i:plot_end_i]),
                   'v',
                   ms=12,
                   mew=2.0,
                   mec='k',
                   mfc='None')
    ax_deconv.plot(deconv_peaks_t_plot,
                   deconv_peaks_t_plot**0 *
                   np.max(deconv_crit[plot_start_i:plot_end_i]),
                   'v',
                   ms=12,
                   mew=2.0,
                   mec='b',
                   mfc='None')
    ax_deconv.set_ylim(None,
                       1.3 * np.max(deconv_crit[plot_start_i:plot_end_i]))
    ax_deconv.set_title(r"Deconvolution")

    ax_hi = stfio_plot.StandardAxis(fig, gs[9:11, 2:5], hasx=False, hasy=False)
    ax_hi.plot(trange_hi, plot_hi_trace, '-k', lw=2)
    ax_hi.plot(theoretical_peaks_t_plot_hi,
               theoretical_peaks_t_plot_hi * 0 + 30.0,
               'v',
               ms=12,
               mew=2.0,
               mec='k',
               mfc='None')
    ax_hi.plot(matching_peaks_t_plot_hi,
               matching_peaks_t_plot_hi * 0 + 20.0,
               'v',
               ms=12,
               mew=2.0,
               mec='g',
               mfc='None')
    ax_hi.plot(deconv_peaks_t_plot_hi,
               deconv_peaks_t_plot_hi * 0 + 10.0,
               'v',
               ms=12,
               mew=2.0,
               mec='b',
               mfc='None')
    stfio_plot.plot_scalebars(ax_hi, xunits="ms", yunits="pA")

    xA = plot_hi_start_t - plot_start_t
    yA = deconv_crit[plot_start_i:plot_end_i].min()
    con = ConnectionPatch(xyA=(xA, yA),
                          xyB=(0, 1.0),
                          coordsA="data",
                          coordsB="axes fraction",
                          axesA=ax_deconv,
                          axesB=ax_hi,
                          arrowstyle="-",
                          linewidth=1,
                          color="k")
    ax_deconv.add_artist(con)
    xA += (plot_hi_end_t - plot_hi_start_t) * 0.9
    con = ConnectionPatch(xyA=(xA, yA),
                          xyB=(0.9, 1.0),
                          coordsA="data",
                          coordsB="axes fraction",
                          axesA=ax_deconv,
                          axesB=ax_hi,
                          arrowstyle="-",
                          linewidth=1,
                          color="k")
    ax_deconv.add_artist(con)

    ax_bars_matching = stfio_plot.StandardAxis(fig, gs[5:10, 7:9])
    matching_bars_FP = Bardata(matching_FP * 1e2,
                               title="False positives",
                               color='g')
    matching_bars_FN = Bardata(matching_FN * 1e2,
                               title="False negatives",
                               color='g')
    bargraph([matching_bars_FP, matching_bars_FN],
             ax_bars_matching,
             ylabel=r'Rate ($\%$)')
    ax_bars_matching.set_title(r"Template matching")

    ax_bars_deconv = stfio_plot.StandardAxis(fig,
                                             gs[5:10, 10:12],
                                             hasy=False,
                                             sharey=ax_bars_matching)
    deconv_bars_FP = Bardata(deconv_FP * 1e2,
                             title="False positives",
                             color='b')
    deconv_bars_FN = Bardata(deconv_FN * 1e2,
                             title="False negatives",
                             color='b')
    bargraph([deconv_bars_FP, deconv_bars_FN],
             ax_bars_deconv,
             ylabel=r'Error rate $\%$')
    ax_bars_deconv.set_title(r"Deconvolution")

    fig.text(0.09,
             0.9,
             "A",
             size='x-large',
             weight='bold',
             ha='left',
             va='top')
    fig.text(0.53,
             0.9,
             "B",
             size='x-large',
             weight='bold',
             ha='left',
             va='top')
    fig.text(0.09,
             0.58,
             "C",
             size='x-large',
             weight='bold',
             ha='left',
             va='top')
    fig.text(0.53,
             0.58,
             "D",
             size='x-large',
             weight='bold',
             ha='left',
             va='top')

    plt.savefig("%s/../../manuscript/figures/Fig5/Fig5.svg" % module_dir)

    fig = plt.figure()
    ieis_ax = fig.add_subplot(111)
    ieis_ax.hist(
        [np.diff(deconv_onsets),
         np.diff(matching_onsets), theoretical_ieis],
        bins=len(theoretical_ieis) / 1.0,
        cumulative=True,
        normed=True,
        histtype='step')
    ieis_ax.set_xlabel("Interevent intervals (ms)")
    ieis_ax.set_ylabel("Cumulative probability")
    ieis_ax.set_xlim(0, 800.0)
    ieis_ax.set_ylim(0, 1.0)