intanrawio.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. """
  2. Support for intan tech rhd and rhs files.
  3. This 2 formats are more or less the same but:
  4. * some variance in headers.
  5. * rhs amplifier is more complexe because the optional DC channel
  6. RHS supported version 1.0
  7. RHD supported version 1.0 1.1 1.2 1.3 2.0
  8. See:
  9. * http://intantech.com/files/Intan_RHD2000_data_file_formats.pdf
  10. * http://intantech.com/files/Intan_RHS2000_data_file_formats.pdf
  11. Author: Samuel Garcia
  12. """
  13. # from __future__ import unicode_literals is not compatible with numpy.dtype both py2 py3
  14. from .baserawio import (BaseRawIO, _signal_channel_dtype, _unit_channel_dtype,
  15. _event_channel_dtype)
  16. import numpy as np
  17. from collections import OrderedDict
  18. from distutils.version import LooseVersion as V
  19. class IntanRawIO(BaseRawIO):
  20. """
  21. """
  22. extensions = ['rhd', 'rhs']
  23. rawmode = 'one-file'
  24. def __init__(self, filename=''):
  25. BaseRawIO.__init__(self)
  26. self.filename = filename
  27. def _source_name(self):
  28. return self.filename
  29. def _parse_header(self):
  30. if self.filename.endswith('.rhs'):
  31. self._global_info, self._ordered_channels, data_dtype,\
  32. header_size, self._block_size = read_rhs(self.filename)
  33. elif self.filename.endswith('.rhd'):
  34. self._global_info, self._ordered_channels, data_dtype,\
  35. header_size, self._block_size = read_rhd(self.filename)
  36. # memmap raw data with the complicated structured dtype
  37. self._raw_data = np.memmap(self.filename, dtype=data_dtype, mode='r', offset=header_size)
  38. # check timestamp continuity
  39. timestamp = self._raw_data['timestamp'].flatten()
  40. assert np.all(np.diff(timestamp) == 1), 'timestamp have gaps'
  41. # signals
  42. sig_channels = []
  43. for c, chan_info in enumerate(self._ordered_channels):
  44. name = chan_info['native_channel_name']
  45. chan_id = c # the chan_id have no meaning in intan
  46. if chan_info['signal_type'] == 20:
  47. # exception for temperature
  48. sig_dtype = 'int16'
  49. else:
  50. sig_dtype = 'uint16'
  51. group_id = 0
  52. sig_channels.append((name, chan_id, chan_info['sampling_rate'],
  53. sig_dtype, chan_info['units'], chan_info['gain'],
  54. chan_info['offset'], chan_info['signal_type']))
  55. sig_channels = np.array(sig_channels, dtype=_signal_channel_dtype)
  56. self._max_sampling_rate = np.max(sig_channels['sampling_rate'])
  57. self._max_sigs_length = self._raw_data.size * self._block_size
  58. # No events
  59. event_channels = []
  60. event_channels = np.array(event_channels, dtype=_event_channel_dtype)
  61. # No spikes
  62. unit_channels = []
  63. unit_channels = np.array(unit_channels, dtype=_unit_channel_dtype)
  64. # fille into header dict
  65. self.header = {}
  66. self.header['nb_block'] = 1
  67. self.header['nb_segment'] = [1]
  68. self.header['signal_channels'] = sig_channels
  69. self.header['unit_channels'] = unit_channels
  70. self.header['event_channels'] = event_channels
  71. self._generate_minimal_annotations()
  72. def _segment_t_start(self, block_index, seg_index):
  73. return 0.
  74. def _segment_t_stop(self, block_index, seg_index):
  75. t_stop = self._max_sigs_length / self._max_sampling_rate
  76. return t_stop
  77. def _get_signal_size(self, block_index, seg_index, channel_indexes):
  78. assert channel_indexes is not None, 'channel_indexes cannot be None, several signal size'
  79. assert np.unique(self.header['signal_channels'][channel_indexes]['group_id']).size == 1
  80. channel_names = self.header['signal_channels'][channel_indexes]['name']
  81. chan_name = channel_names[0]
  82. size = self._raw_data[chan_name].size
  83. return size
  84. def _get_signal_t_start(self, block_index, seg_index, channel_indexes):
  85. return 0.
  86. def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, channel_indexes):
  87. if i_start is None:
  88. i_start = 0
  89. if i_stop is None:
  90. i_stop = self._get_signal_size(block_index, seg_index, channel_indexes)
  91. if channel_indexes is None:
  92. channel_indexes = slice(None)
  93. channel_names = self.header['signal_channels'][channel_indexes]['name']
  94. shape = self._raw_data[channel_names[0]].shape
  95. # some channel (temperature) have 1D field so shape 1D
  96. # because 1 sample per block
  97. if len(shape) == 2:
  98. # this is the general case with 2D
  99. block_size = shape[1]
  100. block_start = i_start // block_size
  101. block_stop = i_stop // block_size + 1
  102. sl0 = i_start % block_size
  103. sl1 = sl0 + (i_stop - i_start)
  104. sigs_chunk = np.zeros((i_stop - i_start, len(channel_names)), dtype='uint16')
  105. for i, chan_name in enumerate(channel_names):
  106. data_chan = self._raw_data[chan_name]
  107. if len(shape) == 1:
  108. sigs_chunk[:, i] = data_chan[i_start:i_stop]
  109. else:
  110. sigs_chunk[:, i] = data_chan[block_start:block_stop].flatten()[sl0:sl1]
  111. return sigs_chunk
  112. def read_qstring(f):
  113. length = np.fromfile(f, dtype='uint32', count=1)[0]
  114. if length == 0xFFFFFFFF or length == 0:
  115. return ''
  116. txt = f.read(length).decode('utf-16')
  117. return txt
  118. def read_variable_header(f, header):
  119. info = {}
  120. for field_name, field_type in header:
  121. if field_type == 'QString':
  122. field_value = read_qstring(f)
  123. else:
  124. field_value = np.fromfile(f, dtype=field_type, count=1)[0]
  125. info[field_name] = field_value
  126. return info
  127. ###############
  128. # RHS ZONE
  129. rhs_global_header = [
  130. ('magic_number', 'uint32'), # 0xD69127AC
  131. ('major_version', 'int16'),
  132. ('minor_version', 'int16'),
  133. ('sampling_rate', 'float32'),
  134. ('dsp_enabled', 'int16'),
  135. ('actual_dsp_cutoff_frequency', 'float32'),
  136. ('actual_lower_bandwidth', 'float32'),
  137. ('actual_lower_settle_bandwidth', 'float32'),
  138. ('actual_upper_bandwidth', 'float32'),
  139. ('desired_dsp_cutoff_frequency', 'float32'),
  140. ('desired_lower_bandwidth', 'float32'),
  141. ('desired_lower_settle_bandwidth', 'float32'),
  142. ('desired_upper_bandwidth', 'float32'),
  143. ('notch_filter_mode', 'int16'),
  144. ('desired_impedance_test_frequency', 'float32'),
  145. ('actual_impedance_test_frequency', 'float32'),
  146. ('amp_settle_mode', 'int16'),
  147. ('charge_recovery_mode', 'int16'),
  148. ('stim_step_size', 'float32'),
  149. ('recovery_current_limit', 'float32'),
  150. ('recovery_target_voltage', 'float32'),
  151. ('note1', 'QString'),
  152. ('note2', 'QString'),
  153. ('note3', 'QString'),
  154. ('dc_amplifier_data_saved', 'int16'),
  155. ('board_mode', 'int16'),
  156. ('ref_channel_name', 'QString'),
  157. ('nb_signal_group', 'int16'),
  158. ]
  159. rhs_signal_group_header = [
  160. ('signal_group_name', 'QString'),
  161. ('signal_group_prefix', 'QString'),
  162. ('signal_group_enabled', 'int16'),
  163. ('channel_num', 'int16'),
  164. ('amplified_channel_num', 'int16'),
  165. ]
  166. rhs_signal_channel_header = [
  167. ('native_channel_name', 'QString'),
  168. ('custom_channel_name', 'QString'),
  169. ('native_order', 'int16'),
  170. ('custom_order', 'int16'),
  171. ('signal_type', 'int16'),
  172. ('channel_enabled', 'int16'),
  173. ('chip_channel_num', 'int16'),
  174. ('command_stream', 'int16'),
  175. ('board_stream_num', 'int16'),
  176. ('spike_scope_trigger_mode', 'int16'),
  177. ('spike_scope_voltage_thresh', 'int16'),
  178. ('spike_scope_digital_trigger_channel', 'int16'),
  179. ('spike_scope_digital_edge_polarity', 'int16'),
  180. ('electrode_impedance_magnitude', 'float32'),
  181. ('electrode_impedance_phase', 'float32'),
  182. ]
  183. def read_rhs(filename):
  184. BLOCK_SIZE = 128 # sample per block
  185. with open(filename, mode='rb') as f:
  186. global_info = read_variable_header(f, rhs_global_header)
  187. channels_by_type = {k: [] for k in [0, 3, 4, 5, 6]}
  188. for g in range(global_info['nb_signal_group']):
  189. group_info = read_variable_header(f, rhs_signal_group_header)
  190. if bool(group_info['signal_group_enabled']):
  191. for c in range(group_info['channel_num']):
  192. chan_info = read_variable_header(f, rhs_signal_channel_header)
  193. assert chan_info['signal_type'] not in (1, 2)
  194. if bool(chan_info['channel_enabled']):
  195. channels_by_type[chan_info['signal_type']].append(chan_info)
  196. header_size = f.tell()
  197. sr = global_info['sampling_rate']
  198. # construct dtype by re-ordering channels by types
  199. ordered_channels = []
  200. data_dtype = [('timestamp', 'int32', BLOCK_SIZE)]
  201. # 0: RHS2000 amplifier channel.
  202. for chan_info in channels_by_type[0]:
  203. name = chan_info['native_channel_name']
  204. chan_info['sampling_rate'] = sr
  205. chan_info['units'] = 'uV'
  206. chan_info['gain'] = 0.195
  207. chan_info['offset'] = -32768 * 0.195
  208. ordered_channels.append(chan_info)
  209. data_dtype += [(name, 'uint16', BLOCK_SIZE)]
  210. if bool(global_info['dc_amplifier_data_saved']):
  211. for chan_info in channels_by_type[0]:
  212. name = chan_info['native_channel_name']
  213. chan_info_dc = dict(chan_info)
  214. chan_info_dc['native_channel_name'] = name + '_DC'
  215. chan_info_dc['sampling_rate'] = sr
  216. chan_info_dc['units'] = 'mV'
  217. chan_info_dc['gain'] = 19.23
  218. chan_info_dc['offset'] = -512 * 19.23
  219. chan_info_dc['signal_type'] = 10 # put it in another group
  220. ordered_channels.append(chan_info_dc)
  221. data_dtype += [(name + '_DC', 'uint16', BLOCK_SIZE)]
  222. for chan_info in channels_by_type[0]:
  223. name = chan_info['native_channel_name']
  224. chan_info_stim = dict(chan_info)
  225. chan_info_stim['native_channel_name'] = name + '_STIM'
  226. chan_info_stim['sampling_rate'] = sr
  227. # stim channel are coplicated because they are coded
  228. # with bits, they do not fit the gain/offset rawio strategy
  229. chan_info_stim['units'] = ''
  230. chan_info_stim['gain'] = 1.
  231. chan_info_stim['offset'] = 0.
  232. chan_info_stim['signal_type'] = 11 # put it in another group
  233. ordered_channels.append(chan_info_stim)
  234. data_dtype += [(name + '_STIM', 'uint16', BLOCK_SIZE)]
  235. # 3: Analog input channel.
  236. # 4: Analog output channel.
  237. for sig_type in [3, 4, ]:
  238. for chan_info in channels_by_type[sig_type]:
  239. name = chan_info['native_channel_name']
  240. chan_info['sampling_rate'] = sr
  241. chan_info['units'] = 'V'
  242. chan_info['gain'] = 0.0003125
  243. chan_info['offset'] = -32768 * 0.0003125
  244. ordered_channels.append(chan_info)
  245. data_dtype += [(name, 'uint16', BLOCK_SIZE)]
  246. # 5: Digital input channel.
  247. # 6: Digital output channel.
  248. for sig_type in [5, 6]:
  249. # at the moment theses channel are not in sig channel list
  250. # but they are in the raw memamp
  251. if len(channels_by_type[sig_type]) > 0:
  252. name = {5: 'DIGITAL-IN', 6: 'DIGITAL-OUT'}[sig_type]
  253. data_dtype += [(name, 'uint16', BLOCK_SIZE)]
  254. return global_info, ordered_channels, data_dtype, header_size, BLOCK_SIZE
  255. ###############
  256. # RHD ZONE
  257. rhd_global_header_base = [
  258. ('magic_number', 'uint32'), # 0xC6912702
  259. ('major_version', 'int16'),
  260. ('minor_version', 'int16'),
  261. ]
  262. rhd_global_header_part1 = [
  263. ('sampling_rate', 'float32'),
  264. ('dsp_enabled', 'int16'),
  265. ('actual_dsp_cutoff_frequency', 'float32'),
  266. ('actual_lower_bandwidth', 'float32'),
  267. ('actual_upper_bandwidth', 'float32'),
  268. ('desired_dsp_cutoff_frequency', 'float32'),
  269. ('desired_lower_bandwidth', 'float32'),
  270. ('desired_upper_bandwidth', 'float32'),
  271. ('notch_filter_mode', 'int16'),
  272. ('desired_impedance_test_frequency', 'float32'),
  273. ('actual_impedance_test_frequency', 'float32'),
  274. ('note1', 'QString'),
  275. ('note2', 'QString'),
  276. ('note3', 'QString'),
  277. ]
  278. rhd_global_header_v11 = [
  279. ('num_temp_sensor_channels', 'int16'),
  280. ]
  281. rhd_global_header_v13 = [
  282. ('eval_board_mode', 'int16'),
  283. ]
  284. rhd_global_header_v20 = [
  285. ('reference_channel', 'QString'),
  286. ]
  287. rhd_global_header_final = [
  288. ('nb_signal_group', 'int16'),
  289. ]
  290. rhd_signal_group_header = [
  291. ('signal_group_name', 'QString'),
  292. ('signal_group_prefix', 'QString'),
  293. ('signal_group_enabled', 'int16'),
  294. ('channel_num', 'int16'),
  295. ('amplified_channel_num', 'int16'),
  296. ]
  297. rhd_signal_channel_header = [
  298. ('native_channel_name', 'QString'),
  299. ('custom_channel_name', 'QString'),
  300. ('native_order', 'int16'),
  301. ('custom_order', 'int16'),
  302. ('signal_type', 'int16'),
  303. ('channel_enabled', 'int16'),
  304. ('chip_channel_num', 'int16'),
  305. ('board_stream_num', 'int16'),
  306. ('spike_scope_trigger_mode', 'int16'),
  307. ('spike_scope_voltage_thresh', 'int16'),
  308. ('spike_scope_digital_trigger_channel', 'int16'),
  309. ('spike_scope_digital_edge_polarity', 'int16'),
  310. ('electrode_impedance_magnitude', 'float32'),
  311. ('electrode_impedance_phase', 'float32'),
  312. ]
  313. def read_rhd(filename):
  314. with open(filename, mode='rb') as f:
  315. global_info = read_variable_header(f, rhd_global_header_base)
  316. version = V('{major_version}.{minor_version}'.format(**global_info))
  317. # the header size depend on the version :-(
  318. header = list(rhd_global_header_part1) # make a copy
  319. if version >= '1.1':
  320. header = header + rhd_global_header_v11
  321. else:
  322. global_info['num_temp_sensor_channels'] = 0
  323. if version >= '1.3':
  324. header = header + rhd_global_header_v13
  325. else:
  326. global_info['eval_board_mode'] = 0
  327. if version >= '2.0':
  328. header = header + rhd_global_header_v20
  329. else:
  330. global_info['reference_channel'] = ''
  331. header = header + rhd_global_header_final
  332. global_info.update(read_variable_header(f, header))
  333. # read channel group and channel header
  334. channels_by_type = {k: [] for k in [0, 1, 2, 3, 4, 5]}
  335. for g in range(global_info['nb_signal_group']):
  336. group_info = read_variable_header(f, rhd_signal_group_header)
  337. if bool(group_info['signal_group_enabled']):
  338. for c in range(group_info['channel_num']):
  339. chan_info = read_variable_header(f, rhd_signal_channel_header)
  340. if bool(chan_info['channel_enabled']):
  341. channels_by_type[chan_info['signal_type']].append(chan_info)
  342. header_size = f.tell()
  343. sr = global_info['sampling_rate']
  344. # construct the data block dtype and reorder channels
  345. if version >= '2.0':
  346. BLOCK_SIZE = 128
  347. else:
  348. BLOCK_SIZE = 60 # 256 channels
  349. ordered_channels = []
  350. if version >= '1.2':
  351. data_dtype = [('timestamp', 'int32', BLOCK_SIZE)]
  352. else:
  353. data_dtype = [('timestamp', 'uint32', BLOCK_SIZE)]
  354. # 0: RHD2000 amplifier channel
  355. for chan_info in channels_by_type[0]:
  356. name = chan_info['native_channel_name']
  357. chan_info['sampling_rate'] = sr
  358. chan_info['units'] = 'uV'
  359. chan_info['gain'] = 0.195
  360. chan_info['offset'] = -32768 * 0.195
  361. ordered_channels.append(chan_info)
  362. data_dtype += [(name, 'uint16', BLOCK_SIZE)]
  363. # 1: RHD2000 auxiliary input channel
  364. for chan_info in channels_by_type[1]:
  365. name = chan_info['native_channel_name']
  366. chan_info['sampling_rate'] = sr / 4.
  367. chan_info['units'] = 'V'
  368. chan_info['gain'] = 0.0000374
  369. chan_info['offset'] = 0.
  370. ordered_channels.append(chan_info)
  371. data_dtype += [(name, 'uint16', BLOCK_SIZE // 4)]
  372. # 2: RHD2000 supply voltage channel
  373. for chan_info in channels_by_type[2]:
  374. name = chan_info['native_channel_name']
  375. chan_info['sampling_rate'] = sr / BLOCK_SIZE
  376. chan_info['units'] = 'V'
  377. chan_info['gain'] = 0.0000748
  378. chan_info['offset'] = 0.
  379. ordered_channels.append(chan_info)
  380. data_dtype += [(name, 'uint16')]
  381. # temperature is not an official channel in the header
  382. for i in range(global_info['num_temp_sensor_channels']):
  383. name = 'temperature_{}'.format(i)
  384. chan_info = {'native_channel_name': name, 'signal_type': 20}
  385. chan_info['sampling_rate'] = sr / BLOCK_SIZE
  386. chan_info['units'] = 'Celsius'
  387. chan_info['gain'] = 0.001
  388. chan_info['offset'] = 0.
  389. ordered_channels.append(chan_info)
  390. data_dtype += [(name, 'int16')]
  391. # 3: USB board ADC input channel
  392. for chan_info in channels_by_type[3]:
  393. name = chan_info['native_channel_name']
  394. chan_info['sampling_rate'] = sr
  395. chan_info['units'] = 'V'
  396. if global_info['eval_board_mode'] == 0:
  397. chan_info['gain'] = 0.000050354
  398. chan_info['offset'] = 0.
  399. elif global_info['eval_board_mode'] == 1:
  400. chan_info['gain'] = 0.00015259
  401. chan_info['offset'] = -32768 * 0.00015259
  402. elif global_info['eval_board_mode'] == 13:
  403. chan_info['gain'] = 0.0003125
  404. chan_info['offset'] = -32768 * 0.0003125
  405. ordered_channels.append(chan_info)
  406. data_dtype += [(name, 'uint16', BLOCK_SIZE)]
  407. # 4: USB board digital input channel
  408. # 5: USB board digital output channel
  409. for sig_type in [4, 5]:
  410. # at the moment theses channel are not in sig channel list
  411. # but they are in the raw memamp
  412. if len(channels_by_type[sig_type]) > 0:
  413. name = {4: 'DIGITAL-IN', 5: 'DIGITAL-OUT'}[sig_type]
  414. data_dtype += [(name, 'uint16', BLOCK_SIZE)]
  415. return global_info, ordered_channels, data_dtype, header_size, BLOCK_SIZE