Ejemplo n.º 1
0
def drop_failed_sweeps(
        dataset: EphysDataSet,
        stimulus_ontology: Optional[StimulusOntology] = None,
        qc_criteria: Optional[Dict] = None
) -> List[Dict]:
    """A convenience which extracts and QCs sweeps in preparation for dataset
    feature extraction. This function:
    1. extracts sweep qc features
    2. removes sweeps tagged with failure messages
    3. sets sweep states based on qc results

    Parameters
    ----------
    dataset : dataset from which to draw sweeps

    Returns
    -------
    sweep_features : a list of dictionaries, each describing a sweep
    """
    if stimulus_ontology is None:
        stimulus_ontology = StimulusOntology.default()
    if qc_criteria is None:
        qc_criteria = qcp.load_default_qc_criteria()

    sweep_features = sweep_qc_features(dataset)
    sweep_props.drop_tagged_sweeps(sweep_features)
    sweep_props.remove_sweep_feature("tags", sweep_features)
    sweep_states = qcp.qc_sweeps(
        stimulus_ontology, sweep_features, qc_criteria
    )
    sweep_props.assign_sweep_states(sweep_states, sweep_features)

    dataset.sweep_info = sweep_features
Ejemplo n.º 2
0
def extract_qc_features(data_set):
    cell_features, cell_tags = cell_qc_features(
        data_set,
        # manual_values=cell_qc_manual_values
    )
    sweep_features = sweep_qc_features(data_set)
    drop_tagged_sweeps(sweep_features)
    return cell_features, cell_tags, sweep_features
Ejemplo n.º 3
0
def run_sweep_extraction(input_nwb_file,
                         input_h5_file,
                         stimulus_ontology_file,
                         input_manual_values=None):
    """
    Parameters
    ----------
    input_nwb_file
    input_h5_file
    stimulus_ontology_file
    input_manual_values

    Returns
    -------

    """
    lu.log_pretty_header("Extract QC features", level=1)

    if input_manual_values is None:
        input_manual_values = {}

    manual_values = {}
    for mk in MANUAL_KEYS:
        if mk in input_manual_values:
            manual_values[mk] = input_manual_values[mk]

    if stimulus_ontology_file:
        mso.make_stimulus_ontology_from_lims(stimulus_ontology_file)
    else:
        stimulus_ontology_file = StimulusOntology.DEFAULT_STIMULUS_ONTOLOGY_FILE
        logging.info(
            F"Ontology is not provided, using default {StimulusOntology.DEFAULT_STIMULUS_ONTOLOGY_FILE}"
        )

    ont = StimulusOntology(ju.read(stimulus_ontology_file))
    ds = create_data_set(nwb_file=input_nwb_file,
                         h5_file=input_h5_file,
                         ontology=ont)

    cell_features, cell_tags = qcfe.cell_qc_features(ds, manual_values)

    for tag in cell_tags:
        logging.warning(tag)

    sweep_features = qcfe.sweep_qc_features(ds)

    return dict(
        cell_features=cell_features,
        cell_tags=cell_tags,
        sweep_features=sweep_features,
    )
Ejemplo n.º 4
0
def run_sweep_extraction(input_nwb_file,
                         stimulus_ontology_file=None,
                         input_manual_values=None,
                         update_ontology=True,
                         **unused_args):
    """
    Parameters
    ----------
    input_nwb_file
    stimulus_ontology_file
    input_manual_values

    Returns
    -------
    """
    log_pretty_header("Extract QC features", level=1)

    if input_manual_values is None:
        input_manual_values = {}

    manual_values = {}
    for mk in MANUAL_KEYS:
        if mk in input_manual_values:
            manual_values[mk] = input_manual_values[mk]

    if stimulus_ontology_file and update_ontology:
        make_stimulus_ontology_from_lims(stimulus_ontology_file)
    if stimulus_ontology_file is None:
        stimulus_ontology_file = \
            StimulusOntology.DEFAULT_STIMULUS_ONTOLOGY_FILE
        logging.info(f"Ontology is not provided, using default "
                     f"{StimulusOntology.DEFAULT_STIMULUS_ONTOLOGY_FILE}")

    ont = StimulusOntology(json_utilities.read(stimulus_ontology_file))
    ds = create_ephys_data_set(nwb_file=input_nwb_file, ontology=ont)

    cell_features, cell_tags = cell_qc_features(ds, manual_values)

    for tag in cell_tags:
        logging.warning(tag)

    sweep_features = sweep_qc_features(ds)

    return {
        "cell_features": cell_features,
        "cell_tags": cell_tags,
        "sweep_features": sweep_features,
    }
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
import pandas as pd
from ipfx.dataset.create import create_ephys_data_set
from ipfx.qc_feature_extractor import sweep_qc_features

import ipfx.sweep_props as sweep_props
import ipfx.qc_feature_evaluator as qcp
from ipfx.stimulus import StimulusOntology

# 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")
data_set = create_ephys_data_set(nwb_file=nwb_file)

# Compute sweep QC features
sweep_features = sweep_qc_features(data_set)

# Drop sweeps that failed to compute QC criteria
sweep_props.drop_tagged_sweeps(sweep_features)
sweep_props.remove_sweep_feature("tags", sweep_features)

stimulus_ontology = StimulusOntology.default()
qc_criteria = qcp.load_default_qc_criteria()

sweep_states = qcp.qc_sweeps(stimulus_ontology, sweep_features, qc_criteria)

# print a few sweeps and states
print(pd.DataFrame(sweep_features).head())
print(sweep_states[0:len(pd.DataFrame(sweep_features).head())])