test_axographio.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. """
  2. Tests of neo.io.axographio
  3. """
  4. from datetime import datetime
  5. import unittest
  6. from neo.io import AxographIO
  7. from neo.test.iotest.common_io_test import BaseTestIO
  8. import numpy as np
  9. from numpy.testing import assert_equal
  10. import quantities as pq
  11. class TestAxographIO(BaseTestIO, unittest.TestCase):
  12. files_to_test = [
  13. 'AxoGraph_Graph_File', # version 1 file, provided with AxoGraph
  14. 'AxoGraph_Digitized_File', # version 2 file, provided with AxoGraph
  15. 'AxoGraph_X_File.axgx', # version 5 file, provided with AxoGraph
  16. 'File_axograph.axgd', # version 6 file
  17. 'episodic.axgd',
  18. 'events_and_epochs.axgx',
  19. 'written-by-axographio-with-linearsequence.axgx',
  20. 'written-by-axographio-without-linearsequence.axgx',
  21. 'corrupt-comment.axgx',
  22. ]
  23. files_to_download = files_to_test
  24. ioclass = AxographIO
  25. def test_version_1(self):
  26. """Test reading a version 1 AxoGraph file"""
  27. filename = self.get_filename_path('AxoGraph_Graph_File')
  28. reader = AxographIO(filename=filename)
  29. blk = reader.read_block(signal_group_mode='split-all')
  30. assert_equal(blk.annotations['format_ver'], 1)
  31. names = [sig.name for sig in blk.segments[0].analogsignals]
  32. assert_equal(names, ['Current', 'Current'])
  33. sig = blk.segments[0].analogsignals[0][:5]
  34. arr = sig.as_array('pA')
  35. target = np.array([[-5.5078130],
  36. [-3.1171880],
  37. [+1.6640626],
  38. [+1.6640626],
  39. [+4.0546880]], dtype=np.float32)
  40. assert_equal(arr, target)
  41. assert_equal(sig.t_start, 0.0005000000237487257 * pq.s)
  42. assert_equal(sig.sampling_period, 0.0005000010132789612 * pq.s)
  43. def test_version_2(self):
  44. """Test reading a version 2 AxoGraph file"""
  45. filename = self.get_filename_path('AxoGraph_Digitized_File')
  46. reader = AxographIO(filename=filename)
  47. blk = reader.read_block(signal_group_mode='split-all')
  48. assert_equal(blk.annotations['format_ver'], 2)
  49. names = [sig.name for sig in blk.segments[0].analogsignals]
  50. assert_equal(names, ['Current', 'Voltage', 'Column4', 'Column5',
  51. 'Column6', 'Column7', 'Column8', 'Column9',
  52. 'Column10', 'Column11', 'Column12', 'Column13',
  53. 'Column14', 'Column15', 'Column16', 'Column17',
  54. 'Column18', 'Column19', 'Column20', 'Column21',
  55. 'Column22', 'Column23', 'Column24', 'Column25',
  56. 'Column26', 'Column27', 'Column28', 'Column29'])
  57. sig = blk.segments[0].analogsignals[0][:5]
  58. arr = sig.as_array('pA')
  59. target = np.array([[0.3125],
  60. [9.6875],
  61. [9.6875],
  62. [9.6875],
  63. [9.3750]], dtype=np.float32)
  64. assert_equal(arr, target)
  65. assert_equal(sig.t_start, 0.00009999999747378752 * pq.s)
  66. assert_equal(sig.sampling_period, 0.00009999999747378750 * pq.s)
  67. def test_version_5(self):
  68. """Test reading a version 5 AxoGraph file"""
  69. filename = self.get_filename_path('AxoGraph_X_File.axgx')
  70. reader = AxographIO(filename=filename)
  71. blk = reader.read_block(signal_group_mode='split-all')
  72. assert_equal(blk.annotations['format_ver'], 5)
  73. names = [sig.name for sig in blk.segments[0].analogsignals]
  74. assert_equal(names, ['Current', '', '', '', '', ''])
  75. sig = blk.segments[0].analogsignals[0][:5]
  76. arr = sig.as_array('pA')
  77. target = np.array([[+3.0846775],
  78. [-2.5403225],
  79. [-1.2903225],
  80. [+6.8346770],
  81. [-5.0403230]], dtype=np.float32)
  82. assert_equal(arr, target)
  83. assert_equal(sig.t_start, 0.00005 * pq.s)
  84. assert_equal(sig.sampling_period, 0.00005 * pq.s)
  85. def test_version_6(self):
  86. """Test reading a version 6 AxoGraph file"""
  87. filename = self.get_filename_path('File_axograph.axgd')
  88. reader = AxographIO(filename=filename)
  89. blk = reader.read_block(signal_group_mode='split-all')
  90. assert_equal(blk.annotations['format_ver'], 6)
  91. names = [sig.name for sig in blk.segments[0].analogsignals]
  92. assert_equal(names, ['Membrane Voltage-1'])
  93. sig = blk.segments[0].analogsignals[0][:5]
  94. arr = sig.as_array('mV')
  95. target = np.array([[-60.731834],
  96. [-60.701313],
  97. [-60.670795],
  98. [-60.701313],
  99. [-60.731834]], dtype=np.float32)
  100. assert_equal(arr, target)
  101. assert_equal(sig.t_start, 0.00002 * pq.s)
  102. assert_equal(sig.sampling_period, 0.00002 * pq.s)
  103. def test_file_written_by_axographio_package_with_linearsequence(self):
  104. """Test reading file written by axographio package with linearsequence time column"""
  105. filename = self.get_filename_path('written-by-axographio-with-linearsequence.axgx')
  106. reader = AxographIO(filename=filename)
  107. blk = reader.read_block(signal_group_mode='split-all')
  108. assert_equal(blk.annotations['format_ver'], 6)
  109. names = [sig.name for sig in blk.segments[0].analogsignals]
  110. assert_equal(names, ['Data 1', 'Data 2'])
  111. sig = blk.segments[0].analogsignals[0][:5]
  112. arr = sig.as_array('mV')
  113. target = np.array([[0.000000],
  114. [9.999833],
  115. [19.998667],
  116. [29.995500],
  117. [39.989334]], dtype=np.float32)
  118. assert_equal(arr, target)
  119. assert_equal(sig.t_start, 0 * pq.s)
  120. assert_equal(sig.sampling_period, 0.01 * pq.s)
  121. def test_file_written_by_axographio_package_without_linearsequence(self):
  122. """Test reading file written by axographio package without linearsequence time column"""
  123. filename = self.get_filename_path('written-by-axographio-without-linearsequence.axgx')
  124. reader = AxographIO(filename=filename)
  125. blk = reader.read_block(signal_group_mode='split-all')
  126. assert_equal(blk.annotations['format_ver'], 6)
  127. names = [sig.name for sig in blk.segments[0].analogsignals]
  128. assert_equal(names, ['Data 1', 'Data 2'])
  129. sig = blk.segments[0].analogsignals[0][:5]
  130. arr = sig.as_array('mV')
  131. target = np.array([[0.000000],
  132. [9.999833],
  133. [19.998667],
  134. [29.995500],
  135. [39.989334]], dtype=np.float32)
  136. assert_equal(arr, target)
  137. assert_equal(sig.t_start, 0 * pq.s)
  138. assert_equal(sig.sampling_period, 0.009999999999999787 * pq.s)
  139. def test_file_with_corrupt_comment(self):
  140. """Test reading a file with a corrupt comment"""
  141. filename = self.get_filename_path('corrupt-comment.axgx')
  142. reader = AxographIO(filename=filename)
  143. blk = reader.read_block(signal_group_mode='split-all')
  144. assert_equal(blk.annotations['format_ver'], 6)
  145. names = [sig.name for sig in blk.segments[0].analogsignals]
  146. assert_equal(names, ['Data 1', 'Data 2'])
  147. sig = blk.segments[0].analogsignals[0][:5]
  148. arr = sig.as_array('mV')
  149. target = np.array([[0.000000],
  150. [9.999833],
  151. [19.998667],
  152. [29.995500],
  153. [39.989334]], dtype=np.float32)
  154. assert_equal(arr, target)
  155. assert_equal(sig.t_start, 0 * pq.s)
  156. assert_equal(sig.sampling_period, 0.01 * pq.s)
  157. def test_multi_segment(self):
  158. """Test reading an episodic file into multiple Segments"""
  159. filename = self.get_filename_path('episodic.axgd')
  160. reader = AxographIO(filename=filename)
  161. blk = reader.read_block(signal_group_mode='split-all')
  162. assert_equal(len(blk.segments), 30)
  163. assert_equal(len(blk.groups), 2)
  164. assert_equal(len(blk.segments[0].analogsignals), 2)
  165. names = [sig.name for sig in blk.segments[0].analogsignals]
  166. assert_equal(names, ['CAP', 'STIM'])
  167. sig = blk.segments[0].analogsignals[0][:5]
  168. arr = sig.as_array('V')
  169. target = np.array([[1.37500e-06],
  170. [1.53125e-06],
  171. [1.34375e-06],
  172. [1.09375e-06],
  173. [1.21875e-06]], dtype=np.float32)
  174. assert_equal(arr, target)
  175. def test_force_single_segment(self):
  176. """Test reading an episodic file into one Segment"""
  177. filename = self.get_filename_path('episodic.axgd')
  178. reader = AxographIO(filename=filename, force_single_segment=True)
  179. blk = reader.read_block(signal_group_mode='split-all')
  180. assert_equal(len(blk.segments), 1)
  181. assert_equal(len(blk.groups), 60)
  182. assert_equal(len(blk.segments[0].analogsignals), 60)
  183. names = [sig.name for sig in blk.segments[0].analogsignals]
  184. assert_equal(names, ['CAP', 'STIM'] * 30)
  185. sig = blk.segments[0].analogsignals[0][:5]
  186. arr = sig.as_array('V')
  187. target = np.array([[1.37500e-06],
  188. [1.53125e-06],
  189. [1.34375e-06],
  190. [1.09375e-06],
  191. [1.21875e-06]], dtype=np.float32)
  192. assert_equal(arr, target)
  193. def test_group_by_same_units(self):
  194. """Test reading with group-by-same-units"""
  195. filename = self.get_filename_path('episodic.axgd')
  196. reader = AxographIO(filename=filename)
  197. blk = reader.read_block(signal_group_mode='group-by-same-units')
  198. assert_equal(len(blk.segments), 30)
  199. assert_equal(len(blk.groups), 1)
  200. assert_equal(len(blk.segments[0].analogsignals), 1)
  201. names = [sig.name for sig in blk.segments[0].analogsignals]
  202. assert_equal(names, ['Channel bundle (CAP,STIM) '])
  203. sig = blk.segments[0].analogsignals[0][:5]
  204. arr = sig.as_array('V')
  205. target = np.array([[1.37500e-06, 3.43750e-03],
  206. [1.53125e-06, 2.81250e-03],
  207. [1.34375e-06, 1.87500e-03],
  208. [1.09375e-06, 1.56250e-03],
  209. [1.21875e-06, 1.56250e-03]], dtype=np.float32)
  210. assert_equal(arr, target)
  211. def test_events_and_epochs(self):
  212. """Test loading events and epochs"""
  213. filename = self.get_filename_path('events_and_epochs.axgx')
  214. reader = AxographIO(filename=filename)
  215. blk = reader.read_block(signal_group_mode='split-all')
  216. event = blk.segments[0].events[0]
  217. assert_equal(event.times, [5999, 5999, 23499, 23499,
  218. 26499, 26499, 35999]
  219. * blk.segments[0].analogsignals[0].sampling_period)
  220. assert_equal(event.labels, ['Stop', 'Start', 'Stop', 'Start',
  221. 'Stop', 'Start', 'Stop'])
  222. epoch = blk.segments[0].epochs[0]
  223. assert_equal(epoch.times, np.array([0.1, 4]) * pq.s)
  224. assert_equal(epoch.durations, np.array([1.4, 2]) * pq.s)
  225. assert_equal(epoch.labels, ['test interval 1', 'test interval 2'])
  226. def test_rec_datetime(self):
  227. """Test parsing the recording datetime from notes"""
  228. # parsing of rec_datetime differs depending on acquisition mode
  229. # file obtained in episodic acquisition mode has date and time on
  230. # separate lines of notes
  231. filename = self.get_filename_path('episodic.axgd')
  232. reader = AxographIO(filename=filename)
  233. blk = reader.read_block(signal_group_mode='split-all')
  234. assert_equal(blk.rec_datetime, datetime(2018, 6, 7, 15, 11, 36))
  235. # file obtained in continuous acquisition mode has date and time in
  236. # single line of notes
  237. filename = self.get_filename_path('events_and_epochs.axgx')
  238. reader = AxographIO(filename=filename)
  239. blk = reader.read_block(signal_group_mode='split-all')
  240. assert_equal(blk.rec_datetime, datetime(2019, 5, 25, 20, 16, 25))
  241. if __name__ == "__main__":
  242. unittest.main()