test_hdf5io.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. """
  2. Tests of neo.io.hdf5io_new
  3. """
  4. import unittest
  5. import sys
  6. import numpy as np
  7. from numpy.testing import assert_array_equal
  8. from quantities import kHz, mV, ms, second, nA
  9. try:
  10. import h5py
  11. HAVE_H5PY = True
  12. except ImportError:
  13. HAVE_H5PY = False
  14. from neo.io.hdf5io import NeoHdf5IO
  15. from neo.test.iotest.common_io_test import BaseTestIO
  16. from neo.test.iotest.tools import get_test_file_full_path
  17. @unittest.skipUnless(HAVE_H5PY, "requires h5py")
  18. class ReadOldNeoHdf5IOTest(BaseTestIO, unittest.TestCase):
  19. """
  20. Test that data generated by NeoHdf5IO in Neo versions 0.3, 0.4 are
  21. read correctly.
  22. """
  23. ioclass = NeoHdf5IO
  24. files_to_test = ["neo_hdf5_example.h5"]
  25. files_to_download = files_to_test
  26. def test_read_with_merge(self):
  27. test_file = get_test_file_full_path(self.ioclass, filename=self.files_to_test[0],
  28. directory=self.local_test_dir, clean=False)
  29. io = NeoHdf5IO(test_file)
  30. blocks = io.read_all_blocks(merge_singles=True)
  31. # general tests, true for both blocks
  32. for block in blocks:
  33. for segment in block.segments:
  34. self.assertEqual(segment.block, block)
  35. # tests of Block #1, which is constructed from "array" (multi-channel)
  36. # objects, so should be straightforward to convert to the version 0.5 API
  37. block0 = blocks[0]
  38. self.assertEqual(block0.name, "block1")
  39. self.assertEqual(block0.index, 1234)
  40. self.assertEqual(block0.annotations["foo"], "bar")
  41. self.assertEqual(len(block0.segments), 3)
  42. for segment in block0.segments:
  43. self.assertEqual(len(segment.analogsignals), 2)
  44. as0 = segment.analogsignals[0]
  45. self.assertEqual(as0.shape, (1000, 4))
  46. self.assertEqual(as0.sampling_rate, 1 * kHz)
  47. self.assertEqual(as0.units, mV)
  48. self.assertEqual(as0.segment, segment)
  49. self.assertEqual(len(segment.spiketrains), 4)
  50. st = segment.spiketrains[-1]
  51. self.assertEqual(st.units, ms)
  52. self.assertEqual(st.t_stop, 1000 * ms)
  53. self.assertEqual(st.t_start, 0 * ms)
  54. self.assertEqual(st.segment, segment)
  55. self.assertEqual(len(segment.events), 1)
  56. ev = segment.events[0]
  57. assert_array_equal(ev.labels,
  58. np.array(['trig0', 'trig1', 'trig2'],
  59. dtype=(sys.byteorder == 'little' and '<' or '>') + 'U5'))
  60. self.assertEqual(ev.units, second)
  61. assert_array_equal(ev.magnitude, np.arange(0, 30, 10))
  62. self.assertEqual(ev.segment, segment)
  63. self.assertEqual(len(segment.epochs), 1)
  64. ep = segment.epochs[0]
  65. assert_array_equal(ep.labels,
  66. np.array(['btn0', 'btn1', 'btn2'],
  67. dtype=(sys.byteorder == 'little' and '<' or '>') + 'U4'))
  68. assert_array_equal(ep.durations.magnitude,
  69. np.array([10, 5, 7]))
  70. self.assertEqual(ep.units, second)
  71. assert_array_equal(ep.magnitude, np.arange(0, 30, 10))
  72. self.assertEqual(ep.segment, segment)
  73. self.assertEqual(len(segment.irregularlysampledsignals), 2)
  74. iss0 = segment.irregularlysampledsignals[0]
  75. self.assertEqual(iss0.shape, (3, 2))
  76. assert_array_equal(iss0.times,
  77. [0.01, 0.03, 0.12] * second)
  78. assert_array_equal(iss0.magnitude,
  79. np.array([[4, 3],
  80. [5, 4],
  81. [6, 3]]))
  82. self.assertEqual(iss0.units, nA)
  83. self.assertEqual(iss0.segment, segment)
  84. iss1 = segment.irregularlysampledsignals[1]
  85. self.assertEqual(iss1.shape, (3, 1))
  86. assert_array_equal(iss1.times,
  87. [0.02, 0.05, 0.15] * second)
  88. self.assertEqual(iss1.units, nA)
  89. assert_array_equal(iss1.magnitude,
  90. np.array([[3], [4], [3]]))
  91. # tests of Block #2, which is constructed from "singleton"
  92. # (single-channel) objects, so is potentially tricky to convert to the
  93. # version 0.5 API
  94. block1 = blocks[1]
  95. self.assertEqual(block1.name, "block2")
  96. for segment in block1.segments:
  97. self.assertEqual(len(segment.analogsignals), 2)
  98. as0 = segment.analogsignals[0]
  99. self.assertEqual(as0.shape, (1000, 4))
  100. self.assertEqual(as0.sampling_rate, 1 * kHz)
  101. self.assertEqual(as0.units, mV)
  102. self.assertEqual(as0.segment, segment)
  103. self.assertEqual(len(segment.spiketrains), 7)
  104. st = segment.spiketrains[-1]
  105. self.assertEqual(st.units, ms)
  106. self.assertEqual(st.t_stop, 1000 * ms)
  107. self.assertEqual(st.t_start, 0 * ms)
  108. self.assertEqual(st.segment, segment)
  109. self.assertEqual(len(segment.events), 0)
  110. self.assertEqual(len(segment.epochs), 0)
  111. self.assertEqual(len(block1.channel_indexes), 3)
  112. ci0 = block1.channel_indexes[0]
  113. self.assertEqual(ci0.name, "electrode1")
  114. self.assertEqual(len(ci0.analogsignals), 1)
  115. as00 = ci0.analogsignals[0]
  116. self.assertEqual(as00.segment, segment)
  117. self.assertEqual(as00.shape, (1000, 4))
  118. self.assertEqual(id(as00), id(segment.analogsignals[0]))
  119. self.assertEqual(as00.mean(), segment.analogsignals[0].mean())
  120. self.assertEqual(as00.channel_index, ci0)
  121. assert_array_equal(ci0.index, np.array([0, 1, 2, 3]))
  122. assert_array_equal(ci0.channel_ids, np.array([0, 1, 2, 3]))
  123. self.assertEqual(len(ci0.units), 2)
  124. self.assertEqual(len(ci0.units[0].spiketrains), 2)
  125. self.assertEqual(id(ci0.units[0].spiketrains[0]),
  126. id(block1.segments[0].spiketrains[0]))
  127. self.assertEqual(id(ci0.units[0].spiketrains[1]),
  128. id(block1.segments[1].spiketrains[0]))
  129. self.assertEqual(id(ci0.units[1].spiketrains[0]),
  130. id(block1.segments[0].spiketrains[1]))
  131. ci1 = block1.channel_indexes[1]
  132. self.assertEqual(ci1.name, "electrode2")
  133. self.assertEqual(len(ci1.analogsignals), 1)
  134. as10 = ci1.analogsignals[0]
  135. self.assertEqual(as10.segment, segment)
  136. self.assertEqual(as10.shape, (1000, 4))
  137. self.assertEqual(id(as10), id(segment.analogsignals[1]))
  138. self.assertEqual(as10.mean(), segment.analogsignals[1].mean())
  139. self.assertEqual(as10.channel_index, ci1)
  140. assert_array_equal(ci1.index, np.array([0, 1, 2, 3]))
  141. assert_array_equal(ci1.channel_ids, np.array([4, 5, 6, 7]))
  142. self.assertEqual(len(ci1.units), 5)
  143. self.assertEqual(id(ci1.units[0].spiketrains[0]),
  144. id(block1.segments[0].spiketrains[2]))
  145. self.assertEqual(id(ci1.units[3].spiketrains[1]),
  146. id(block1.segments[1].spiketrains[5]))
  147. ci2 = block1.channel_indexes[2]
  148. self.assertEqual(ci2.name, "my_favourite_channels")
  149. self.assertEqual(len(ci2.analogsignals), 1)
  150. self.assertEqual(id(ci2.analogsignals[0]), id(as00))
  151. assert_array_equal(ci2.index, np.array([1, 3]))
  152. assert_array_equal(ci2.channel_ids, np.array([1, 3]))