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.

test_hdf5io.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests of neo.io.hdf5io_new
  4. """
  5. import unittest
  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'], dtype='|S5'))
  59. self.assertEqual(ev.units, second)
  60. assert_array_equal(ev.magnitude, np.arange(0, 30, 10))
  61. self.assertEqual(ev.segment, segment)
  62. self.assertEqual(len(segment.epochs), 1)
  63. ep = segment.epochs[0]
  64. assert_array_equal(ep.labels,
  65. np.array(['btn0', 'btn1', 'btn2'], dtype='|S4'))
  66. assert_array_equal(ep.durations.magnitude,
  67. np.array([10, 5, 7]))
  68. self.assertEqual(ep.units, second)
  69. assert_array_equal(ep.magnitude, np.arange(0, 30, 10))
  70. self.assertEqual(ep.segment, segment)
  71. self.assertEqual(len(segment.irregularlysampledsignals), 2)
  72. iss0 = segment.irregularlysampledsignals[0]
  73. self.assertEqual(iss0.shape, (3, 2))
  74. assert_array_equal(iss0.times,
  75. [0.01, 0.03, 0.12] * second)
  76. assert_array_equal(iss0.magnitude,
  77. np.array([[4, 3],
  78. [5, 4],
  79. [6, 3]]))
  80. self.assertEqual(iss0.units, nA)
  81. self.assertEqual(iss0.segment, segment)
  82. iss1 = segment.irregularlysampledsignals[1]
  83. self.assertEqual(iss1.shape, (3, 1))
  84. assert_array_equal(iss1.times,
  85. [0.02, 0.05, 0.15] * second)
  86. self.assertEqual(iss1.units, nA)
  87. assert_array_equal(iss1.magnitude,
  88. np.array([[3], [4], [3]]))
  89. # tests of Block #2, which is constructed from "singleton"
  90. # (single-channel) objects, so is potentially tricky to convert to the
  91. # version 0.5 API
  92. block1 = blocks[1]
  93. self.assertEqual(block1.name, "block2")
  94. for segment in block1.segments:
  95. self.assertEqual(len(segment.analogsignals), 2)
  96. as0 = segment.analogsignals[0]
  97. self.assertEqual(as0.shape, (1000, 4))
  98. self.assertEqual(as0.sampling_rate, 1 * kHz)
  99. self.assertEqual(as0.units, mV)
  100. self.assertEqual(as0.segment, segment)
  101. self.assertEqual(len(segment.spiketrains), 7)
  102. st = segment.spiketrains[-1]
  103. self.assertEqual(st.units, ms)
  104. self.assertEqual(st.t_stop, 1000 * ms)
  105. self.assertEqual(st.t_start, 0 * ms)
  106. self.assertEqual(st.segment, segment)
  107. self.assertEqual(len(segment.events), 0)
  108. self.assertEqual(len(segment.epochs), 0)
  109. self.assertEqual(len(block1.channel_indexes), 3)
  110. ci0 = block1.channel_indexes[0]
  111. self.assertEqual(ci0.name, "electrode1")
  112. self.assertEqual(len(ci0.analogsignals), 1)
  113. as00 = ci0.analogsignals[0]
  114. self.assertEqual(as00.segment, segment)
  115. self.assertEqual(as00.shape, (1000, 4))
  116. self.assertEqual(id(as00), id(segment.analogsignals[0]))
  117. self.assertEqual(as00.mean(), segment.analogsignals[0].mean())
  118. self.assertEqual(as00.channel_index, ci0)
  119. assert_array_equal(ci0.index, np.array([0, 1, 2, 3]))
  120. assert_array_equal(ci0.channel_ids, np.array([0, 1, 2, 3]))
  121. self.assertEqual(len(ci0.units), 2)
  122. self.assertEqual(len(ci0.units[0].spiketrains), 2)
  123. self.assertEqual(id(ci0.units[0].spiketrains[0]),
  124. id(block1.segments[0].spiketrains[0]))
  125. self.assertEqual(id(ci0.units[0].spiketrains[1]),
  126. id(block1.segments[1].spiketrains[0]))
  127. self.assertEqual(id(ci0.units[1].spiketrains[0]),
  128. id(block1.segments[0].spiketrains[1]))
  129. ci1 = block1.channel_indexes[1]
  130. self.assertEqual(ci1.name, "electrode2")
  131. self.assertEqual(len(ci1.analogsignals), 1)
  132. as10 = ci1.analogsignals[0]
  133. self.assertEqual(as10.segment, segment)
  134. self.assertEqual(as10.shape, (1000, 4))
  135. self.assertEqual(id(as10), id(segment.analogsignals[1]))
  136. self.assertEqual(as10.mean(), segment.analogsignals[1].mean())
  137. self.assertEqual(as10.channel_index, ci1)
  138. assert_array_equal(ci1.index, np.array([0, 1, 2, 3]))
  139. assert_array_equal(ci1.channel_ids, np.array([4, 5, 6, 7]))
  140. self.assertEqual(len(ci1.units), 5)
  141. self.assertEqual(id(ci1.units[0].spiketrains[0]),
  142. id(block1.segments[0].spiketrains[2]))
  143. self.assertEqual(id(ci1.units[3].spiketrains[1]),
  144. id(block1.segments[1].spiketrains[5]))
  145. ci2 = block1.channel_indexes[2]
  146. self.assertEqual(ci2.name, "my_favourite_channels")
  147. self.assertEqual(len(ci2.analogsignals), 1)
  148. self.assertEqual(id(ci2.analogsignals[0]), id(as00))
  149. assert_array_equal(ci2.index, np.array([1, 3]))
  150. assert_array_equal(ci2.channel_ids, np.array([1, 3]))