import numpy import pandas as pd from tools.definitions import METADATA_FOLDER, OUTPUT_FOLDER, HELPER_TABLE_FOLDER, STIMULATION_METADATA_EXTENDED, STIMULATION_METADATA_FILTERED, \ PROTOCOL_BLACKLIST from tools.helper import get_filters from tools.helper import get_pulse_times from tools.helper import get_trace, get_times path_to_extended_stimulus_file = OUTPUT_FOLDER+HELPER_TABLE_FOLDER+STIMULATION_METADATA_EXTENDED path_to_filtered_stimulus_file = OUTPUT_FOLDER+HELPER_TABLE_FOLDER+STIMULATION_METADATA_FILTERED print "# Filter stimulations suitable for analysis" print "" spiking_threshold = -10 minimum_width_of_a_spike_in_time_points = 100 stimulations = pd.read_csv(path_to_extended_stimulus_file, index_col="stimulation_id") # get protocols type_number_combinations = stimulations[["protocol_type", "pulse_number"]].drop_duplicates().values def detect_faulty_stimulations(stimulation): is_correct = True times = get_times(stimulation) stimulus_trace = get_trace(stimulation, "Stimulus-Current-1") pulses = get_pulse_times(stimulation) expected_amplitude_difference = 2 * numpy.abs(stimulation["pulse_amplitude"]) for pulse_idx, pulse in enumerate(pulses): in_pulse = pulse["start"] + (pulse["pulse_end"] - pulse["start"]) / 2.0 after_pulse = pulse["pulse_end"] + (pulse["end"] - pulse["pulse_end"]) / 2.0 actual_amplitude_difference = numpy.abs(numpy.ediff1d(stimulus_trace[times.searchsorted([in_pulse, after_pulse])]))[0] correct_pulse = numpy.abs(expected_amplitude_difference - actual_amplitude_difference) <= 0.01 if not correct_pulse: print "Stimulation {:s}: Wrong amplitude in pulse {:d}: expected {:.1f}nA actual {:.1f}nA.".format(stimulation.name, pulse_idx, expected_amplitude_difference, actual_amplitude_difference) is_correct = False break return is_correct blacklist_protocols = list(pd.read_csv(METADATA_FOLDER+PROTOCOL_BLACKLIST)["stimulation_id"].values) print "## Manual blacklist".format(METADATA_FOLDER+PROTOCOL_BLACKLIST) stimulations["filter_blacklist"] = [stim_id not in blacklist_protocols for stim_id in stimulations.index.values] print "{:d} protocols are excluded, see {:s} for justifications".format(len(blacklist_protocols), METADATA_FOLDER+PROTOCOL_BLACKLIST) print "" print "## Automatic detection of problematic stimulations" print "" stimulations["filter_protocol"] = stimulations.apply(detect_faulty_stimulations, axis=1) print "" print "{:d} protocol with stimulation problems.".format(len(numpy.where(numpy.array(stimulations["filter_protocol"].values)==False)[0])) print "" print "## Automatic detection of protocols without conductance clamp" stimulations["filter_zero_conductance"] = stimulations["g"] != 0 print "{:d} protocol without conductance clamp.".format(len(numpy.where(numpy.array(stimulations["filter_zero_conductance"].values)==False)[0])) print "" stimulations.to_csv(path_or_buf=path_to_filtered_stimulus_file) print "{} / {} protocols suitable for analysis".format(len(stimulations[get_filters(stimulations)]), len(stimulations))