Example #1
0
from ipfx.spike_features import estimate_adjusted_detection_parameters
from ipfx.feature_extractor import SpikeFeatureExtractor
from ipfx.utilities import drop_failed_sweeps
import matplotlib.pyplot as plt

# Download and access the experimental data from DANDI archive per instructions in the documentation
# Example below will use an nwb file provided with the package

nwb_file = os.path.join(os.path.dirname(os.getcwd()), "data",
                        "nwb2_H17.03.008.11.03.05.nwb")

# Create data set from the nwb file and find the short squares
data_set = create_data_set(nwb_file=nwb_file)

# Drop failed sweeps: sweeps with incomplete recording or failing QC criteria
drop_failed_sweeps(data_set)

short_square_table = data_set.filtered_sweep_table(stimuli=["Short Square"])
ssq_set = data_set.sweep_set(short_square_table.sweep_number)

# estimate the dv cutoff and threshold fraction
dv_cutoff, thresh_frac = estimate_adjusted_detection_parameters(
    ssq_set.v, ssq_set.t, 1.02, 1.021)

# detect spikes  in a given sweep number
sweep_number = 16
sweep = data_set.sweep(sweep_number)
ext = SpikeFeatureExtractor(dv_cutoff=dv_cutoff, thresh_frac=thresh_frac)
spikes = ext.process(t=sweep.t, v=sweep.v, i=sweep.i)

# and plot them
Example #2
0
def plot_data_set(
        data_set,
        clamp_mode: Optional[str] = None,
        stimuli: Optional[Collection[str]] = None,
        stimuli_exclude: Optional[Collection[str]] = None,
        show_amps: Optional[bool] = True,
        qc_sweeps: Optional[bool] = True,
        figsize=(15, 7),
):
    nwb_file_name = str(data_set._data.nwb_file)
    if qc_sweeps:
        drop_failed_sweeps(data_set)
    elif show_amps:
        data_set.sweep_info = sweep_qc_features(data_set)

    sweep_table = data_set.filtered_sweep_table(
        clamp_mode=clamp_mode,
        stimuli=stimuli,
        stimuli_exclude=stimuli_exclude)

    if len(sweep_table) == 0:
        warnings.warn("No sweeps to plot")
        return

    height_ratios, width_ratios = axes_ratios(sweep_table)

    fig, ax = plt.subplots(len(height_ratios),
                           3,
                           figsize=figsize,
                           gridspec_kw={
                               'height_ratios': height_ratios,
                               'width_ratios': width_ratios
                           })
    if len(height_ratios) == 1:
        # ensure 2d array
        ax = ax[None, :]

    for fig_row, (stimulus_code, sweep_set_table) in enumerate(
            sweep_table.groupby("stimulus_code")):
        sweep_set_table = sweep_set_table.copy().sort_values("sweep_number",
                                                             ascending=False)
        sweep_numbers = sweep_set_table["sweep_number"]
        ss = data_set.sweep_set(sweep_numbers)
        if qc_sweeps:
            ss.select_epoch('experiment')
        annot = sweep_numbers.astype(str)
        if show_amps:
            annot += sweep_set_table['stimulus_amplitude'].apply(
                ": {:.3g} pA".format)

        ax_a = ax[fig_row, 0]
        ax_i = ax[fig_row, 1]
        ax_v = ax[fig_row, 2]

        plot_waveforms(ax_i, ss.i, ss.sampling_rate, annot)
        plot_waveforms(ax_v, ss.v, ss.sampling_rate)
        ax_v.get_shared_x_axes().join(ax_i, ax_v)

        clamp_mode = sweep_set_table["clamp_mode"].values[0]
        ax_a.text(0, 0.0, "%s \n%s " % (stimulus_code, clamp_mode))
        ax_a.axis('off')

    ax[0, 0].set_title("Description")
    ax[0, 1].set_title("Current")
    ax[0, 2].set_title("Voltage")
    ax[-1, 1].set_xlabel("time (s)")
    ax[-1, 2].set_xlabel("time (s)")

    fig.suptitle("file: " + nwb_file_name, fontsize=12)

    mng = plt.get_current_fig_manager()
    if hasattr(mng, 'window'):
        mng.resize(*mng.window.maxsize())
    plt.subplots_adjust(left=0.01, right=0.98, bottom=0.02, top=0.92)