extract_stimulations.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import re
  2. import pandas as pd
  3. import tools.pyrelacs as pr
  4. from tools.definitions import METADATA_FOLDER, RECORDINGS_FOLDER, OUTPUT_FOLDER, HELPER_TABLE_FOLDER, \
  5. STIMULATION_METADATA_RAW
  6. def extract_value_unit(value_unit_string):
  7. value_unit_pattern = re.compile('(\d+[e][+]\d*|[-+]?\d*\.\d+|\d+)\s*(\w+)')
  8. value, unit = value_unit_pattern.match(value_unit_string).groups()
  9. return float(value), unit
  10. def convert_to_sec(time_value, time_unit):
  11. if time_unit == "ms":
  12. time_value *= 0.001
  13. return time_value
  14. overview_of_recordings = METADATA_FOLDER+"cells.csv"
  15. test_protocol_for_pa = "SingleStimulus"
  16. print "# Extract stimulation protocols"
  17. print "Search the recordings listed in {}".format(overview_of_recordings)
  18. print "Collect all instances of the stimulation protocol {} used to test for persistent activity".format(
  19. test_protocol_for_pa)
  20. cell_recordings = pd.read_csv(overview_of_recordings, header=0)
  21. cell_recordings.columns = cell_recordings.columns.str.strip()
  22. for column in cell_recordings.columns:
  23. cell_recordings[column] = cell_recordings[column].str.strip()
  24. cells = cell_recordings["CellId"].unique()
  25. stimulation_id = []
  26. cell_ids = []
  27. recordings = []
  28. before_protocols = []
  29. after_protocols = []
  30. stimulus_lengths = []
  31. pulse_periods = []
  32. pulse_lengths = []
  33. pulse_amplitudes = []
  34. pulse_offsets = []
  35. dts = []
  36. start_indices = []
  37. end_indices = []
  38. parameter_dict={}
  39. before_stimulation_interval = 2
  40. after_stimulation_interval = 8
  41. for cell in cells:
  42. recording_counter = 0
  43. for _, recording in cell_recordings[cell_recordings["CellId"] == cell].iterrows():
  44. cell_id = cell
  45. recording_dir = RECORDINGS_FOLDER+recording["Recording"]
  46. for info, key, dat in pr.iload('%s/stimuli.dat' % (recording_dir,)):
  47. if info[1]['RePro'] != test_protocol_for_pa:
  48. continue
  49. repro_starting_idx = dat[1, 0]
  50. left_appendum, right_appendum = pr.get_trace_indices(info, before_stimulation_interval,
  51. after_stimulation_interval)
  52. dt = pr.get_sampling_interval(info)
  53. stimulation_id.append("{:s}_R{:03d}".format(cell_id, recording_counter))
  54. cell_ids.append(cell_id)
  55. recordings.append(recording_dir)
  56. available_model_parameters = map(lambda key: info[0][key], filter(lambda key: key.startswith("identifier") and key != "identifier1", info[0].keys()))
  57. parameter_values = [dat[0][key[1].index(param)+1] for param in available_model_parameters]
  58. for param, value in zip(available_model_parameters, parameter_values):
  59. try:
  60. parameter_dict[param].append(value)
  61. except KeyError as err:
  62. parameter_dict[param] = [value]
  63. dts.append(dt)
  64. stimulus_length, unit = extract_value_unit(info[1]["duration"])
  65. stimulus_length = convert_to_sec(stimulus_length, unit)
  66. pulse_period, unit = extract_value_unit(info[1]["period"])
  67. pulse_period = convert_to_sec(pulse_period, unit)
  68. pulse_length, unit = extract_value_unit(info[1]["pulseduration"])
  69. pulse_length = convert_to_sec(pulse_length, unit)
  70. pulse_amplitude, _ = extract_value_unit(info[1]["amplitude"])
  71. pulse_offset, _ = extract_value_unit(info[1]["offset"])
  72. before_protocols.append(before_stimulation_interval)
  73. after_protocols.append(after_stimulation_interval)
  74. stimulus_lengths.append(stimulus_length)
  75. pulse_periods.append(pulse_period)
  76. pulse_lengths.append(pulse_length)
  77. pulse_amplitudes.append(pulse_amplitude)
  78. pulse_offsets.append(pulse_offset)
  79. start_indices.append(repro_starting_idx - left_appendum)
  80. end_indices.append(repro_starting_idx + right_appendum)
  81. recording_counter += 1
  82. dictionary_of_stimulations = {
  83. "stimulation_id": stimulation_id,
  84. "cell_id": cell_ids,
  85. "recording": recordings,
  86. "dt": dts,
  87. "before_protocol": before_protocols,
  88. "after_protocol": after_protocols,
  89. "stimulus_length": stimulus_lengths,
  90. "pulse_period": pulse_periods,
  91. "pulse_length": pulse_lengths,
  92. "pulse_amplitude": pulse_amplitudes,
  93. "pulse_offset": pulse_offsets,
  94. "start_index": start_indices,
  95. "end_index": end_indices
  96. }
  97. dictionary_of_stimulations.update(parameter_dict)
  98. stimulus_table_saving_path = OUTPUT_FOLDER+HELPER_TABLE_FOLDER+STIMULATION_METADATA_RAW
  99. pd.DataFrame(data=dictionary_of_stimulations).set_index("stimulation_id").to_csv(path_or_buf=stimulus_table_saving_path)
  100. print "Gathered meta data for each stimulation and saved to {}".format(stimulus_table_saving_path)
  101. print ""