filter_stimulations.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import numpy
  2. import pandas as pd
  3. from tools.definitions import METADATA_FOLDER, OUTPUT_FOLDER, HELPER_TABLE_FOLDER, STIMULATION_METADATA_EXTENDED, STIMULATION_METADATA_FILTERED, \
  4. PROTOCOL_BLACKLIST
  5. from tools.helper import get_filters
  6. from tools.helper import get_pulse_times
  7. from tools.helper import get_trace, get_times
  8. path_to_extended_stimulus_file = OUTPUT_FOLDER+HELPER_TABLE_FOLDER+STIMULATION_METADATA_EXTENDED
  9. path_to_filtered_stimulus_file = OUTPUT_FOLDER+HELPER_TABLE_FOLDER+STIMULATION_METADATA_FILTERED
  10. print "# Filter stimulations suitable for analysis"
  11. print ""
  12. spiking_threshold = -10
  13. minimum_width_of_a_spike_in_time_points = 100
  14. stimulations = pd.read_csv(path_to_extended_stimulus_file, index_col="stimulation_id")
  15. # get protocols
  16. type_number_combinations = stimulations[["protocol_type", "pulse_number"]].drop_duplicates().values
  17. def detect_faulty_stimulations(stimulation):
  18. is_correct = True
  19. times = get_times(stimulation)
  20. stimulus_trace = get_trace(stimulation, "Stimulus-Current-1")
  21. pulses = get_pulse_times(stimulation)
  22. expected_amplitude_difference = 2 * numpy.abs(stimulation["pulse_amplitude"])
  23. for pulse_idx, pulse in enumerate(pulses):
  24. in_pulse = pulse["start"] + (pulse["pulse_end"] - pulse["start"]) / 2.0
  25. after_pulse = pulse["pulse_end"] + (pulse["end"] - pulse["pulse_end"]) / 2.0
  26. actual_amplitude_difference = numpy.abs(numpy.ediff1d(stimulus_trace[times.searchsorted([in_pulse, after_pulse])]))[0]
  27. correct_pulse = numpy.abs(expected_amplitude_difference -
  28. actual_amplitude_difference) <= 0.01
  29. if not correct_pulse:
  30. 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)
  31. is_correct = False
  32. break
  33. return is_correct
  34. blacklist_protocols = list(pd.read_csv(METADATA_FOLDER+PROTOCOL_BLACKLIST)["stimulation_id"].values)
  35. print "## Manual blacklist".format(METADATA_FOLDER+PROTOCOL_BLACKLIST)
  36. stimulations["filter_blacklist"] = [stim_id not in blacklist_protocols for stim_id in stimulations.index.values]
  37. print "{:d} protocols are excluded, see {:s} for justifications".format(len(blacklist_protocols), METADATA_FOLDER+PROTOCOL_BLACKLIST)
  38. print ""
  39. print "## Automatic detection of problematic stimulations"
  40. print ""
  41. stimulations["filter_protocol"] = stimulations.apply(detect_faulty_stimulations, axis=1)
  42. print ""
  43. print "{:d} protocol with stimulation problems.".format(len(numpy.where(numpy.array(stimulations["filter_protocol"].values)==False)[0]))
  44. print ""
  45. print "## Automatic detection of protocols without conductance clamp"
  46. stimulations["filter_zero_conductance"] = stimulations["g"] != 0
  47. print "{:d} protocol without conductance clamp.".format(len(numpy.where(numpy.array(stimulations["filter_zero_conductance"].values)==False)[0]))
  48. print ""
  49. stimulations.to_csv(path_or_buf=path_to_filtered_stimulus_file)
  50. print "{} / {} protocols suitable for analysis".format(len(stimulations[get_filters(stimulations)]), len(stimulations))