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

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