Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

1
1

example.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # -*- coding: utf-8 -*-
  2. """
  3. Example code for loading and processing of a recording of the reach-
  4. to-grasp experiments conducted at the Institute de Neurosciences de la Timone
  5. by Thomas Brochier and Alexa Riehle.
  6. Authors: Julia Sprenger, Lyuba Zehl, Michael Denker
  7. Copyright (c) 2017, Institute of Neuroscience and Medicine (INM-6),
  8. Forschungszentrum Juelich, Germany
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice, this
  13. list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of the contributors
  18. may be used to endorse or promote products derived from this software without
  19. specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  21. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """
  31. # This loads the Neo and odML libraries shipped with this code. For production
  32. # use, please use the newest releases of odML and Neo.
  33. import load_local_neo_odml_elephant
  34. import os
  35. import numpy as np
  36. import matplotlib.pyplot as plt
  37. import quantities as pq
  38. from neo import Block, Segment
  39. from elephant.signal_processing import butter
  40. from reachgraspio import reachgraspio
  41. from neo.utils import cut_segment_by_epoch, add_epoch, get_events
  42. # =============================================================================
  43. # Load data
  44. #
  45. # As a first step, we partially load the data file into memory as a Neo object.
  46. # =============================================================================
  47. # Specify the path to the recording session to load, eg,
  48. # '/home/user/l101210-001'
  49. session_name = os.path.join('..', 'datasets', 'i140703-001')
  50. # session_name = os.path.join('..', 'datasets', 'l101210-001')
  51. odml_dir = os.path.join('..', 'datasets')
  52. # Open the session for reading
  53. session = reachgraspio.ReachGraspIO(session_name, odml_directory=odml_dir)
  54. # Read the complete dataset in lazy mode. Neo object will be created, but
  55. # data are not loaded in to memory.
  56. data_block = session.read_block(correct_filter_shifts=True, lazy=True)
  57. # Access the single Segment of the data block
  58. assert len(data_block.segments) == 1
  59. data_segment = data_block.segments[0]
  60. # =============================================================================
  61. # Create offline filtered LFP
  62. #
  63. # Here, we construct one offline filtered LFP from each ns5 (monkey L) or ns6
  64. # (monkey N) raw recording trace. For monkey N, this filtered LFP can be
  65. # compared to the LFPs in the ns2 file (note that monkey L contains only
  66. # behavioral signals in the ns2 file). Also, we assign telling names to each
  67. # Neo AnalogSignal, which is used for plotting later on in this script.
  68. # =============================================================================
  69. # Iterate through all analog signals and replace these lazy object by new
  70. # analog signals containing only data of channel 62 (target_channel_id) and
  71. # provide human readable name for the analog signal (LFP / raw signal type)
  72. target_channel_id = 62
  73. nsx_to_anasig_name = {2: 'LFP signal (online filtered)',
  74. 5: 'raw signal',
  75. 6: 'raw signal'}
  76. idx = 0
  77. while idx < len(data_segment.analogsignals):
  78. # remove analog signals, that don't contain target channel
  79. channel_ids = data_segment.analogsignals[idx].array_annotations['channel_ids']
  80. if target_channel_id not in channel_ids:
  81. data_segment.analogsignals.pop(idx)
  82. continue
  83. # replace analog signal with analog signal containing data
  84. target_channel_index = np.where(channel_ids == target_channel_id)[0][0]
  85. anasig = data_segment.analogsignals[idx].load(
  86. channel_indexes=[target_channel_index])
  87. data_segment.analogsignals[idx] = anasig
  88. idx += 1
  89. # replace name by label of contained signal type
  90. anasig.name = nsx_to_anasig_name[anasig.array_annotations['nsx'][0]]
  91. # load spiketrains of same channel
  92. channel_spiketrains = data_segment.filter({'channel_id': target_channel_id})
  93. loaded_spiketrains = [st.load(load_waveforms=True) for st in channel_spiketrains]
  94. data_segment.spiketrains = loaded_spiketrains
  95. # The LFP is not present in the data fils of both recording. Here, we
  96. # generate the LFP signal from the raw signal if it's not present already.
  97. if not data_segment.filter({'name': 'LFP signal (online filtered)'}):
  98. raw_signal = data_segment.filter({'name': 'raw signal'})[0]
  99. # Use the Elephant library to filter the raw analog signal
  100. f_anasig = butter(raw_signal, highpass_freq=None,
  101. lowpass_freq=250 * pq.Hz, order=4)
  102. print('filtering done.')
  103. f_anasig.name = 'LFP signal (offline filtered)'
  104. # Attach offline filtered LFP to the segment of data
  105. data_segment.analogsignals.extend(f_anasig)
  106. # =============================================================================
  107. # Construct analysis epochs
  108. #
  109. # In this step we extract and cut the data into time segments (termed analysis
  110. # epochs) that we wish to analyze. We contrast these analysis epochs to the
  111. # behavioral trials that are defined by the experiment as occurrence of a Trial
  112. # Start (TS-ON) event in the experiment. Concretely, here our analysis epochs
  113. # are constructed as a cutout of 25ms of data around the TS-ON event of all
  114. # successful behavioral trials.
  115. # =============================================================================
  116. # Get Trial Start (TS-ON) events of all successful behavioral trials
  117. # (corresponds to performance code 255, which is accessed for convenience and
  118. # better legibility in the dictionary attribute performance_codes of the
  119. # ReachGraspIO class).
  120. #
  121. # To this end, we filter all event objects of the loaded data to match the name
  122. # "TrialEvents", which is the Event object containing all Events available (see
  123. # documentation of ReachGraspIO). From this Event object we extract only events
  124. # matching "TS-ON" and the desired trial performance code (which are
  125. # annotations of the Event object).
  126. start_events = get_events(
  127. data_segment,
  128. name='TrialEvents',
  129. trial_event_labels='TS-ON',
  130. performance_in_trial=session.performance_codes['correct_trial'])
  131. print('got start events.')
  132. # Extract single Neo Event object containing all TS-ON triggers
  133. assert len(start_events) == 1
  134. start_event = start_events[0]
  135. # Construct analysis epochs from 10ms before the TS-ON of a successful
  136. # behavioral trial to 15ms after TS-ON. The name "analysis_epochs" is given to
  137. # the resulting Neo Epoch object. The object is not attached to the Neo
  138. # Segment. The parameter event2 of add_epoch() is left empty, since we are
  139. # cutting around a single event, as opposed to cutting between two events.
  140. pre = -10 * pq.ms
  141. post = 15 * pq.ms
  142. epoch = add_epoch(
  143. data_segment,
  144. event1=start_event, event2=None,
  145. pre=pre, post=post,
  146. attach_result=False,
  147. name='analysis_epochs',
  148. array_annotations=start_event.array_annotations)
  149. print('added epoch.')
  150. # Create new segments of data cut according to the analysis epochs of the
  151. # 'analysis_epochs' Neo Epoch object. The time axes of all segments are aligned
  152. # such that each segment starts at time 0 (parameter reset_times); annotations
  153. # describing the analysis epoch are carried over to the segments. A new Neo
  154. # Block named "data_cut_to_analysis_epochs" is created to capture all cut
  155. # analysis epochs. For execution time reason, we are only considering the
  156. # first 10 epochs here.
  157. cut_trial_block = Block(name="data_cut_to_analysis_epochs")
  158. cut_trial_block.segments = cut_segment_by_epoch(
  159. data_segment, epoch[:10], reset_time=True)
  160. # =============================================================================
  161. # Plot data
  162. # =============================================================================
  163. # Determine the first existing trial ID i from the Event object containing all
  164. # start events. Then, by calling the filter() function of the Neo Block
  165. # "data_cut_to_analysis_epochs" containing the data cut into the analysis
  166. # epochs, we ask to return all Segments annotated by the behavioral trial ID i.
  167. # In this case this call should return one matching analysis epoch around TS-ON
  168. # belonging to behavioral trial ID i. For monkey N, this is trial ID 1, for
  169. # monkey L this is trial ID 2 since trial ID 1 is not a correct trial.
  170. trial_id = int(np.min(start_event.array_annotations['trial_id']))
  171. trial_segments = cut_trial_block.filter(
  172. targdict={"trial_id": trial_id}, objects=Segment)
  173. assert len(trial_segments) == 1
  174. trial_segment = trial_segments[0]
  175. # Create figure
  176. fig = plt.figure(facecolor='w')
  177. time_unit = pq.CompoundUnit('1./30000*s')
  178. amplitude_unit = pq.microvolt
  179. nsx_colors = ['b', 'k', 'r']
  180. # Loop through all analog signals and plot the signal in a color corresponding
  181. # to its sampling frequency (i.e., originating from the ns2/ns5 or ns2/ns6).
  182. for i, anasig in enumerate(trial_segment.analogsignals):
  183. plt.plot(
  184. anasig.times.rescale(time_unit),
  185. anasig.squeeze().rescale(amplitude_unit),
  186. label=anasig.name,
  187. color=nsx_colors[i])
  188. # Loop through all spike trains and plot the spike time, and overlapping the
  189. # wave form of the spike used for spike sorting stored separately in the nev
  190. # file.
  191. for st in trial_segment.spiketrains:
  192. color = np.random.rand(3,)
  193. for spike_id, spike in enumerate(st):
  194. # Plot spike times
  195. plt.axvline(
  196. spike.rescale(time_unit).magnitude,
  197. color=color,
  198. label='Unit ID %i' % st.annotations['unit_id'])
  199. # Plot waveforms
  200. waveform = st.waveforms[spike_id, 0, :]
  201. waveform_times = np.arange(len(waveform))*time_unit + spike
  202. plt.plot(
  203. waveform_times.rescale(time_unit).magnitude,
  204. waveform.rescale(amplitude_unit),
  205. '--',
  206. linewidth=2,
  207. color=color,
  208. zorder=0)
  209. # Loop through all events
  210. for event in trial_segment.events:
  211. if event.name == 'TrialEvents':
  212. for ev_id, ev in enumerate(event):
  213. plt.axvline(
  214. ev.rescale(time_unit),
  215. alpha=0.2,
  216. linewidth=3,
  217. linestyle='dashed',
  218. label='event ' + event.array_annotations[
  219. 'trial_event_labels'][ev_id])
  220. # Finishing touches on the plot
  221. plt.autoscale(enable=True, axis='x', tight=True)
  222. plt.xlabel(time_unit.name)
  223. plt.ylabel(amplitude_unit.name)
  224. plt.legend(loc=4, fontsize=10)
  225. # Save plot
  226. fname = 'example_plot'
  227. for file_format in ['eps', 'png', 'pdf']:
  228. fig.savefig(fname + '.%s' % file_format, dpi=400, format=file_format)