extract_stimulations.py 4.5 KB

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