analyse_firing_rates.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import warnings
  2. import pandas as pd
  3. from tools.helper import get_trace, get_times, get_pulse_times, get_filters
  4. from tools.definitions import OUTPUT_FOLDER, HELPER_TABLE_FOLDER, STIMULATION_METADATA_FILTERED
  5. from tools.voltage_trace_analyzer import VoltageTraceAnalyzer
  6. path_to_filtered_stimulus_file = OUTPUT_FOLDER+HELPER_TABLE_FOLDER+STIMULATION_METADATA_FILTERED
  7. print "# Analyse firing during and after pulses"
  8. print ""
  9. spiking_threshold = -10
  10. minimum_width_of_a_spike_in_time_points = 100
  11. pulse_mean_string = "p_{:d}_ISI_mean"
  12. after_pulse_mean_string = "ap_{:d}_ISI_mean"
  13. stimulations = pd.read_csv(path_to_filtered_stimulus_file, index_col="stimulation_id")
  14. stimulations = stimulations[get_filters(stimulations)]
  15. # get protocols
  16. type_number_combinations = stimulations[["protocol_type", "pulse_number"]].drop_duplicates().values
  17. def get_firing_rates(stimulation):
  18. times = get_times(stimulation)
  19. voltage_trace = get_trace(stimulation, "V-1")
  20. v_analyser = VoltageTraceAnalyzer(times, voltage_trace, spiking_threshold, minimum_width_of_a_spike_in_time_points)
  21. transient_period = 2
  22. pulses = get_pulse_times(stimulation)
  23. # Suppress RuntimeWarnings generated by empty ISI arrays
  24. with warnings.catch_warnings():
  25. warnings.simplefilter("ignore", category=RuntimeWarning)
  26. for idx, pulse in enumerate(pulses):
  27. during_pulse_column = pulse_mean_string.format(idx + 1)
  28. after_pulse_column = after_pulse_mean_string.format(idx + 1)
  29. stimulation[during_pulse_column] = v_analyser.get_interspike_interval(pulse["start"]+transient_period, pulse["pulse_end"]).mean()
  30. stimulation[after_pulse_column] = v_analyser.get_interspike_interval(pulse["pulse_end"]+transient_period, pulse["end"]).mean()
  31. stimulation["isi_before"] = v_analyser.get_interspike_interval(-stimulation["before_protocol"]+transient_period, 0).mean()
  32. stimulation["isi_after"] = v_analyser.get_interspike_interval(stimulation["stimulus_length"]+transient_period, stimulation[
  33. "stimulus_length"] + stimulation["after_protocol"]).mean()
  34. return stimulation
  35. for type_number in type_number_combinations:
  36. protocol_type = type_number[0]
  37. number = type_number[1]
  38. # Determine the protocol
  39. protocol_id = "{}-{:d}".format(protocol_type, number)
  40. stimulation_of_current_protocol = stimulations[(stimulations["protocol_type"] == protocol_type) & (stimulations[
  41. "pulse_number"] == number)]
  42. print "{}: {} stimulations".format(protocol_id, len(stimulation_of_current_protocol))
  43. # Analyse firing rates
  44. stimulation_with_firing_info = stimulation_of_current_protocol.apply(get_firing_rates, axis=1)
  45. # Save firing rates
  46. firing_info_columns = ["protocol_type", "pulse_number", "isi_before", "isi_after"] + \
  47. [pulse_mean_string.format(pulse_idx) for pulse_idx in range(1, number + 1)] + \
  48. [after_pulse_mean_string.format(pulse_idx) for pulse_idx in range(1, number + 1)]
  49. path_to_firing_rate_file = OUTPUT_FOLDER+HELPER_TABLE_FOLDER+"{:s}.csv".format(protocol_id)
  50. stimulation_with_firing_info[firing_info_columns].to_csv(path_to_firing_rate_file)
  51. print "Firing rates saved in {:s}".format(path_to_firing_rate_file)
  52. print ""