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_pickleio.py 3.4 KB

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