test_pickleio.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. Tests of the neo.io.pickleio.PickleIO class
  3. """
  4. import os
  5. import unittest
  6. import numpy as np
  7. import quantities as pq
  8. from neo.core import Block, Segment, AnalogSignal, SpikeTrain, Unit, Epoch, Event, ChannelIndex, \
  9. IrregularlySampledSignal
  10. from neo.io import PickleIO
  11. from numpy.testing import assert_array_equal
  12. from neo.test.tools import assert_arrays_equal, assert_file_contents_equal
  13. from neo.test.iotest.common_io_test import BaseTestIO
  14. NCELLS = 5
  15. class CommonTestPickleIO(BaseTestIO, unittest.TestCase):
  16. ioclass = PickleIO
  17. class TestPickleIO(unittest.TestCase):
  18. def test__issue_285(self):
  19. # Spiketrain
  20. train = SpikeTrain([3, 4, 5] * pq.s, t_stop=10.0)
  21. unit = Unit()
  22. train.unit = unit
  23. unit.spiketrains.append(train)
  24. epoch = Epoch(np.array([0, 10, 20]),
  25. np.array([2, 2, 2]),
  26. np.array(["a", "b", "c"]),
  27. units="ms")
  28. blk = Block()
  29. seg = Segment()
  30. seg.spiketrains.append(train)
  31. seg.epochs.append(epoch)
  32. epoch.segment = seg
  33. blk.segments.append(seg)
  34. reader = PickleIO(filename="blk.pkl")
  35. reader.write(blk)
  36. reader = PickleIO(filename="blk.pkl")
  37. r_blk = reader.read_block()
  38. r_seg = r_blk.segments[0]
  39. self.assertIsInstance(r_seg.spiketrains[0].unit, Unit)
  40. self.assertIsInstance(r_seg.epochs[0], Epoch)
  41. os.remove('blk.pkl')
  42. # Epoch
  43. epoch = Epoch(times=np.arange(0, 30, 10) * pq.s,
  44. durations=[10, 5, 7] * pq.ms,
  45. labels=np.array(['btn0', 'btn1', 'btn2'], dtype='U'))
  46. epoch.segment = Segment()
  47. blk = Block()
  48. seg = Segment()
  49. seg.epochs.append(epoch)
  50. blk.segments.append(seg)
  51. reader = PickleIO(filename="blk.pkl")
  52. reader.write(blk)
  53. reader = PickleIO(filename="blk.pkl")
  54. r_blk = reader.read_block()
  55. r_seg = r_blk.segments[0]
  56. self.assertIsInstance(r_seg.epochs[0].segment, Segment)
  57. os.remove('blk.pkl')
  58. # Event
  59. event = Event(np.arange(0, 30, 10) * pq.s,
  60. labels=np.array(['trig0', 'trig1', 'trig2'], dtype='U'))
  61. event.segment = Segment()
  62. blk = Block()
  63. seg = Segment()
  64. seg.events.append(event)
  65. blk.segments.append(seg)
  66. reader = PickleIO(filename="blk.pkl")
  67. reader.write(blk)
  68. reader = PickleIO(filename="blk.pkl")
  69. r_blk = reader.read_block()
  70. r_seg = r_blk.segments[0]
  71. self.assertIsInstance(r_seg.events[0].segment, Segment)
  72. os.remove('blk.pkl')
  73. # IrregularlySampledSignal
  74. signal = IrregularlySampledSignal(
  75. [0.0, 1.23, 6.78], [1, 2, 3], units='mV', time_units='ms')
  76. signal.segment = Segment()
  77. blk = Block()
  78. seg = Segment()
  79. seg.irregularlysampledsignals.append(signal)
  80. blk.segments.append(seg)
  81. blk.segments[0].block = blk
  82. reader = PickleIO(filename="blk.pkl")
  83. reader.write(blk)
  84. reader = PickleIO(filename="blk.pkl")
  85. r_blk = reader.read_block()
  86. r_seg = r_blk.segments[0]
  87. self.assertIsInstance(r_seg.irregularlysampledsignals[0].segment, Segment)
  88. os.remove('blk.pkl')
  89. if __name__ == '__main__':
  90. unittest.main()