test_blackrockrawio.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests of neo.rawio.examplerawio
  4. """
  5. # needed for python 3 compatibility
  6. from __future__ import unicode_literals, print_function, division, absolute_import
  7. import unittest
  8. from neo.rawio.blackrockrawio import BlackrockRawIO
  9. from neo.rawio.tests.common_rawio_test import BaseTestRawIO
  10. import numpy as np
  11. from numpy.testing import assert_equal
  12. try:
  13. import scipy.io
  14. HAVE_SCIPY = True
  15. except ImportError:
  16. HAVE_SCIPY = False
  17. class TestBlackrockRawIO(BaseTestRawIO, unittest.TestCase, ):
  18. rawioclass = BlackrockRawIO
  19. entities_to_test = ['FileSpec2.3001']
  20. files_to_download = [
  21. 'FileSpec2.3001.nev',
  22. 'FileSpec2.3001.ns5',
  23. 'FileSpec2.3001.ccf',
  24. 'FileSpec2.3001.mat',
  25. 'blackrock_2_1/l101210-001.mat',
  26. 'blackrock_2_1/l101210-001_nev-02_ns5.mat',
  27. 'blackrock_2_1/l101210-001.ns2',
  28. 'blackrock_2_1/l101210-001.ns5',
  29. 'blackrock_2_1/l101210-001.nev',
  30. 'blackrock_2_1/l101210-001-02.nev']
  31. @unittest.skipUnless(HAVE_SCIPY, "requires scipy")
  32. def test_compare_blackrockio_with_matlabloader(self):
  33. """
  34. This test compares the output of ReachGraspIO.read_block() with the
  35. output generated by a Matlab implementation of a Blackrock file reader
  36. provided by the company. The output for comparison is provided in a
  37. .mat file created by the script create_data_matlab_blackrock.m.
  38. The function tests LFPs, spike times, and digital events on channels
  39. 80-83 and spike waveforms on channel 82, unit 1.
  40. For details on the file contents, refer to FileSpec2.3.txt
  41. Ported to the rawio API by Samuel Garcia.
  42. """
  43. # Load data from Matlab generated files
  44. ml = scipy.io.loadmat(self.get_filename_path('FileSpec2.3001.mat'))
  45. lfp_ml = ml['lfp'] # (channel x time) LFP matrix
  46. ts_ml = ml['ts'] # spike time stamps
  47. elec_ml = ml['el'] # spike electrodes
  48. unit_ml = ml['un'] # spike unit IDs
  49. wf_ml = ml['wf'] # waveform unit 1 channel 1
  50. mts_ml = ml['mts'] # marker time stamps
  51. mid_ml = ml['mid'] # marker IDs
  52. # Load data in channels 1-3 from original data files using the Neo
  53. # BlackrockIO
  54. reader = BlackrockRawIO(filename=self.get_filename_path('FileSpec2.3001'))
  55. reader.parse_header()
  56. # Check if analog data on channels 1-8 are equal
  57. self.assertGreater(reader.signal_channels_count(), 0)
  58. for c in range(0, 8):
  59. raw_sigs = reader.get_analogsignal_chunk(channel_indexes=[c])
  60. raw_sigs = raw_sigs.flatten()
  61. assert_equal(raw_sigs[:-1], lfp_ml[c, :])
  62. # Check if spikes in channels are equal
  63. nb_unit = reader.unit_channels_count()
  64. for unit_index in range(nb_unit):
  65. unit_name = reader.header['unit_channels'][unit_index]['name']
  66. # name is chXX#YY where XX is channel_id and YY is unit_id
  67. channel_id, unit_id = unit_name.split('#')
  68. channel_id = int(channel_id.replace('ch', ''))
  69. unit_id = int(unit_id)
  70. matlab_spikes = ts_ml[(elec_ml == channel_id) & (unit_ml == unit_id)]
  71. io_spikes = reader.get_spike_timestamps(unit_index=unit_index)
  72. assert_equal(io_spikes, matlab_spikes)
  73. # Check waveforms of channel 1, unit 0
  74. if channel_id == 1 and unit_id == 0:
  75. io_waveforms = reader.get_spike_raw_waveforms(unit_index=unit_index)
  76. io_waveforms = io_waveforms[:, 0, :] # remove dim 1
  77. assert_equal(io_waveforms, wf_ml)
  78. # Check if digital input port events are equal
  79. nb_ev_chan = reader.event_channels_count()
  80. # ~ print(reader.header['event_channels'])
  81. for ev_chan in range(nb_ev_chan):
  82. name = reader.header['event_channels']['name'][ev_chan]
  83. # ~ print(name)
  84. all_timestamps, _, labels = reader.get_event_timestamps(
  85. event_channel_index=ev_chan)
  86. if name == 'digital_input_port':
  87. for label in np.unique(labels):
  88. python_digievents = all_timestamps[labels == label]
  89. matlab_digievents = mts_ml[mid_ml == int(label)]
  90. assert_equal(python_digievents, matlab_digievents)
  91. elif name == 'comments':
  92. pass
  93. # TODO: Save comments to Matlab file.
  94. @unittest.skipUnless(HAVE_SCIPY, "requires scipy")
  95. def test_compare_blackrockio_with_matlabloader_v21(self):
  96. """
  97. This test compares the output of ReachGraspIO.read_block() with the
  98. output generated by a Matlab implementation of a Blackrock file reader
  99. provided by the company. The output for comparison is provided in a
  100. .mat file created by the script create_data_matlab_blackrock.m.
  101. The function tests LFPs, spike times, and digital events.
  102. Ported to the rawio API by Samuel Garcia.
  103. """
  104. dirname = self.get_filename_path('blackrock_2_1/l101210-001')
  105. # First run with parameters for ns5, then run with correct parameters for ns2
  106. parameters = [('blackrock_2_1/l101210-001_nev-02_ns5.mat',
  107. {'nsx_to_load': 5, 'nev_override': '-'.join([dirname, '02'])}, 96),
  108. ('blackrock_2_1/l101210-001.mat', {'nsx_to_load': 2}, 6)]
  109. for param in parameters:
  110. # Load data from Matlab generated files
  111. ml = scipy.io.loadmat(self.get_filename_path(filename=param[0]))
  112. lfp_ml = ml['lfp'] # (channel x time) LFP matrix
  113. ts_ml = ml['ts'] # spike time stamps
  114. elec_ml = ml['el'] # spike electrodes
  115. unit_ml = ml['un'] # spike unit IDs
  116. wf_ml = ml['wf'] # waveforms
  117. mts_ml = ml['mts'] # marker time stamps
  118. mid_ml = ml['mid'] # marker IDs
  119. # Load data from original data files using the Neo BlackrockIO
  120. reader = BlackrockRawIO(dirname, **param[1])
  121. reader.parse_header()
  122. # Check if analog data are equal
  123. self.assertGreater(reader.signal_channels_count(), 0)
  124. for c in range(0, param[2]):
  125. raw_sigs = reader.get_analogsignal_chunk(channel_indexes=[c])
  126. raw_sigs = raw_sigs.flatten()
  127. assert_equal(raw_sigs[:], lfp_ml[c, :])
  128. # Check if spikes in channels are equal
  129. nb_unit = reader.unit_channels_count()
  130. for unit_index in range(nb_unit):
  131. unit_name = reader.header['unit_channels'][unit_index]['name']
  132. # name is chXX#YY where XX is channel_id and YY is unit_id
  133. channel_id, unit_id = unit_name.split('#')
  134. channel_id = int(channel_id.replace('ch', ''))
  135. unit_id = int(unit_id)
  136. matlab_spikes = ts_ml[(elec_ml == channel_id) & (unit_ml == unit_id)]
  137. io_spikes = reader.get_spike_timestamps(unit_index=unit_index)
  138. assert_equal(io_spikes, matlab_spikes)
  139. # Check all waveforms
  140. io_waveforms = reader.get_spike_raw_waveforms(unit_index=unit_index)
  141. io_waveforms = io_waveforms[:, 0, :] # remove dim 1
  142. matlab_wf = wf_ml[np.nonzero(
  143. np.logical_and(elec_ml == channel_id, unit_ml == unit_id)), :][0]
  144. assert_equal(io_waveforms, matlab_wf)
  145. # Check if digital input port events are equal
  146. nb_ev_chan = reader.event_channels_count()
  147. # ~ print(reader.header['event_channels'])
  148. for ev_chan in range(nb_ev_chan):
  149. name = reader.header['event_channels']['name'][ev_chan]
  150. # ~ print(name)
  151. if name == 'digital_input_port':
  152. all_timestamps, _, labels = reader.get_event_timestamps(
  153. event_channel_index=ev_chan)
  154. for label in np.unique(labels):
  155. python_digievents = all_timestamps[labels == label]
  156. matlab_digievents = mts_ml[mid_ml == int(label)]
  157. assert_equal(python_digievents, matlab_digievents)
  158. if __name__ == '__main__':
  159. unittest.main()