test_epoch.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests of the neo.core.epoch.Epoch class
  4. """
  5. import unittest
  6. import numpy as np
  7. import quantities as pq
  8. import pickle
  9. import os
  10. from numpy.testing import assert_array_equal
  11. try:
  12. from IPython.lib.pretty import pretty
  13. except ImportError as err:
  14. HAVE_IPYTHON = False
  15. else:
  16. HAVE_IPYTHON = True
  17. from neo.core.epoch import Epoch
  18. from neo.core import Segment
  19. from neo.test.tools import (assert_neo_object_is_compliant,
  20. assert_arrays_equal, assert_arrays_almost_equal,
  21. assert_same_sub_schema)
  22. from neo.test.generate_datasets import (get_fake_value, get_fake_values,
  23. fake_neo, TEST_ANNOTATIONS)
  24. class Test__generate_datasets(unittest.TestCase):
  25. def setUp(self):
  26. np.random.seed(0)
  27. self.annotations = dict([(str(x), TEST_ANNOTATIONS[x]) for x in
  28. range(len(TEST_ANNOTATIONS))])
  29. def test__get_fake_values(self):
  30. self.annotations['seed'] = 0
  31. times = get_fake_value('times', pq.Quantity, seed=0, dim=1)
  32. durations = get_fake_value('durations', pq.Quantity, seed=1, dim=1)
  33. labels = get_fake_value('labels', np.ndarray, seed=2, dim=1, dtype='S')
  34. name = get_fake_value('name', str, seed=3, obj=Epoch)
  35. description = get_fake_value('description', str,
  36. seed=4, obj='Epoch')
  37. file_origin = get_fake_value('file_origin', str)
  38. attrs1 = {'name': name,
  39. 'description': description,
  40. 'file_origin': file_origin}
  41. attrs2 = attrs1.copy()
  42. attrs2.update(self.annotations)
  43. res11 = get_fake_values(Epoch, annotate=False, seed=0)
  44. res12 = get_fake_values('Epoch', annotate=False, seed=0)
  45. res21 = get_fake_values(Epoch, annotate=True, seed=0)
  46. res22 = get_fake_values('Epoch', annotate=True, seed=0)
  47. assert_arrays_equal(res11.pop('times'), times)
  48. assert_arrays_equal(res12.pop('times'), times)
  49. assert_arrays_equal(res21.pop('times'), times)
  50. assert_arrays_equal(res22.pop('times'), times)
  51. assert_arrays_equal(res11.pop('durations'), durations)
  52. assert_arrays_equal(res12.pop('durations'), durations)
  53. assert_arrays_equal(res21.pop('durations'), durations)
  54. assert_arrays_equal(res22.pop('durations'), durations)
  55. assert_arrays_equal(res11.pop('labels'), labels)
  56. assert_arrays_equal(res12.pop('labels'), labels)
  57. assert_arrays_equal(res21.pop('labels'), labels)
  58. assert_arrays_equal(res22.pop('labels'), labels)
  59. self.assertEqual(res11, attrs1)
  60. self.assertEqual(res12, attrs1)
  61. self.assertEqual(res21, attrs2)
  62. self.assertEqual(res22, attrs2)
  63. def test__fake_neo__cascade(self):
  64. self.annotations['seed'] = None
  65. obj_type = Epoch
  66. cascade = True
  67. res = fake_neo(obj_type=obj_type, cascade=cascade)
  68. self.assertTrue(isinstance(res, Epoch))
  69. assert_neo_object_is_compliant(res)
  70. self.assertEqual(res.annotations, self.annotations)
  71. def test__fake_neo__nocascade(self):
  72. self.annotations['seed'] = None
  73. obj_type = 'Epoch'
  74. cascade = False
  75. res = fake_neo(obj_type=obj_type, cascade=cascade)
  76. self.assertTrue(isinstance(res, Epoch))
  77. assert_neo_object_is_compliant(res)
  78. self.assertEqual(res.annotations, self.annotations)
  79. class TestEpoch(unittest.TestCase):
  80. def test_Epoch_creation(self):
  81. params = {'test2': 'y1', 'test3': True}
  82. epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  83. labels=np.array(['test epoch 1',
  84. 'test epoch 2',
  85. 'test epoch 3'], dtype='S'),
  86. name='test', description='tester',
  87. file_origin='test.file',
  88. test1=1, **params)
  89. epc.annotate(test1=1.1, test0=[1, 2])
  90. assert_neo_object_is_compliant(epc)
  91. assert_arrays_equal(epc.times, [1.1, 1.5, 1.7]*pq.ms)
  92. assert_arrays_equal(epc.durations, [20, 40, 60]*pq.ns)
  93. assert_arrays_equal(epc.labels, np.array(['test epoch 1',
  94. 'test epoch 2',
  95. 'test epoch 3'], dtype='S'))
  96. self.assertEqual(epc.name, 'test')
  97. self.assertEqual(epc.description, 'tester')
  98. self.assertEqual(epc.file_origin, 'test.file')
  99. self.assertEqual(epc.annotations['test0'], [1, 2])
  100. self.assertEqual(epc.annotations['test1'], 1.1)
  101. self.assertEqual(epc.annotations['test2'], 'y1')
  102. self.assertTrue(epc.annotations['test3'])
  103. def test_Epoch_repr(self):
  104. params = {'test2': 'y1', 'test3': True}
  105. epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  106. labels=np.array(['test epoch 1',
  107. 'test epoch 2',
  108. 'test epoch 3'], dtype='S'),
  109. name='test', description='tester',
  110. file_origin='test.file',
  111. test1=1, **params)
  112. epc.annotate(test1=1.1, test0=[1, 2])
  113. assert_neo_object_is_compliant(epc)
  114. targ = ('<Epoch: test epoch 1@1.1 ms for 20.0 ns, ' +
  115. 'test epoch 2@1.5 ms for 40.0 ns, ' +
  116. 'test epoch 3@1.7 ms for 60.0 ns>')
  117. res = repr(epc)
  118. self.assertEqual(targ, res)
  119. def test_Epoch_merge(self):
  120. params1 = {'test2': 'y1', 'test3': True}
  121. params2 = {'test2': 'no', 'test4': False}
  122. paramstarg = {'test2': 'yes;no',
  123. 'test3': True,
  124. 'test4': False}
  125. epc1 = Epoch([1.1, 1.5, 1.7]*pq.ms,
  126. durations=[20, 40, 60]*pq.us,
  127. labels=np.array(['test epoch 1 1',
  128. 'test epoch 1 2',
  129. 'test epoch 1 3'], dtype='S'),
  130. name='test', description='tester 1',
  131. file_origin='test.file',
  132. test1=1, **params1)
  133. epc2 = Epoch([2.1, 2.5, 2.7]*pq.us,
  134. durations=[3, 5, 7]*pq.ms,
  135. labels=np.array(['test epoch 2 1',
  136. 'test epoch 2 2',
  137. 'test epoch 2 3'], dtype='S'),
  138. name='test', description='tester 2',
  139. file_origin='test.file',
  140. test1=1, **params2)
  141. epctarg = Epoch([1.1, 1.5, 1.7, .0021, .0025, .0027]*pq.ms,
  142. durations=[20, 40, 60, 3000, 5000, 7000]*pq.ns,
  143. labels=np.array(['test epoch 1 1',
  144. 'test epoch 1 2',
  145. 'test epoch 1 3',
  146. 'test epoch 2 1',
  147. 'test epoch 2 2',
  148. 'test epoch 2 3'], dtype='S'),
  149. name='test',
  150. description='merge(tester 1, tester 2)',
  151. file_origin='test.file',
  152. test1=1, **paramstarg)
  153. assert_neo_object_is_compliant(epc1)
  154. assert_neo_object_is_compliant(epc2)
  155. assert_neo_object_is_compliant(epctarg)
  156. epcres = epc1.merge(epc2)
  157. assert_neo_object_is_compliant(epcres)
  158. assert_same_sub_schema(epctarg, epcres)
  159. def test__children(self):
  160. params = {'test2': 'y1', 'test3': True}
  161. epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  162. labels=np.array(['test epoch 1',
  163. 'test epoch 2',
  164. 'test epoch 3'], dtype='S'),
  165. name='test', description='tester',
  166. file_origin='test.file',
  167. test1=1, **params)
  168. epc.annotate(test1=1.1, test0=[1, 2])
  169. assert_neo_object_is_compliant(epc)
  170. segment = Segment(name='seg1')
  171. segment.epochs = [epc]
  172. segment.create_many_to_one_relationship()
  173. self.assertEqual(epc._single_parent_objects, ('Segment',))
  174. self.assertEqual(epc._multi_parent_objects, ())
  175. self.assertEqual(epc._single_parent_containers, ('segment',))
  176. self.assertEqual(epc._multi_parent_containers, ())
  177. self.assertEqual(epc._parent_objects, ('Segment',))
  178. self.assertEqual(epc._parent_containers, ('segment',))
  179. self.assertEqual(len(epc.parents), 1)
  180. self.assertEqual(epc.parents[0].name, 'seg1')
  181. assert_neo_object_is_compliant(epc)
  182. @unittest.skipUnless(HAVE_IPYTHON, "requires IPython")
  183. def test__pretty(self):
  184. epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  185. labels=np.array(['test epoch 1',
  186. 'test epoch 2',
  187. 'test epoch 3'], dtype='S'),
  188. name='test', description='tester',
  189. file_origin='test.file')
  190. epc.annotate(test1=1.1, test0=[1, 2])
  191. assert_neo_object_is_compliant(epc)
  192. prepr = pretty(epc)
  193. targ = ("Epoch\nname: '%s'\ndescription: '%s'\nannotations: %s" %
  194. (epc.name, epc.description, pretty(epc.annotations)))
  195. self.assertEqual(prepr, targ)
  196. def test__time_slice(self):
  197. epc = Epoch(times=[10, 20, 30] * pq.s, durations=[10, 5, 7] * pq.ms,
  198. labels=np.array(['btn0', 'btn1', 'btn2'], dtype='S'),
  199. foo='bar')
  200. epc2 = epc.time_slice(10 * pq.s, 20 * pq.s)
  201. assert_arrays_equal(epc2.times, [10, 20] * pq.s)
  202. assert_arrays_equal(epc2.durations, [10, 5] * pq.ms)
  203. assert_arrays_equal(epc2.labels, np.array(['btn0','btn1'], dtype='S'))
  204. self.assertEqual(epc.annotations, epc2.annotations)
  205. def test_time_slice2(self):
  206. params = {'test2': 'y1', 'test3': True}
  207. epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  208. labels=np.array(['test epoch 1',
  209. 'test epoch 2',
  210. 'test epoch 3'], dtype='S'),
  211. name='test', description='tester',
  212. file_origin='test.file',
  213. test1=1, **params)
  214. epc.annotate(test1=1.1, test0=[1, 2])
  215. assert_neo_object_is_compliant(epc)
  216. targ = Epoch([1.5]*pq.ms, durations=[40]*pq.ns,
  217. labels=np.array(['test epoch 2'], dtype='S'),
  218. name='test', description='tester',
  219. file_origin='test.file',
  220. test1=1, **params)
  221. targ.annotate(test1=1.1, test0=[1, 2])
  222. assert_neo_object_is_compliant(targ)
  223. t_start = 1.2
  224. t_stop = 1.6
  225. result = epc.time_slice(t_start, t_stop)
  226. assert_arrays_equal(result.times, targ.times)
  227. assert_arrays_equal(result.durations, targ.durations)
  228. assert_arrays_equal(result.labels, targ.labels)
  229. self.assertEqual(result.name, targ.name)
  230. self.assertEqual(result.description, targ.description)
  231. self.assertEqual(result.file_origin, targ.file_origin)
  232. self.assertEqual(result.annotations['test0'], targ.annotations['test0'])
  233. self.assertEqual(result.annotations['test1'], targ.annotations['test1'])
  234. self.assertEqual(result.annotations['test2'], targ.annotations['test2'])
  235. def test_time_slice_out_of_boundries(self):
  236. params = {'test2': 'y1', 'test3': True}
  237. epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  238. labels=np.array(['test epoch 1',
  239. 'test epoch 2',
  240. 'test epoch 3'], dtype='S'),
  241. name='test', description='tester',
  242. file_origin='test.file',
  243. test1=1, **params)
  244. epc.annotate(test1=1.1, test0=[1, 2])
  245. assert_neo_object_is_compliant(epc)
  246. targ = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  247. labels=np.array(['test epoch 1',
  248. 'test epoch 2',
  249. 'test epoch 3'], dtype='S'),
  250. name='test', description='tester',
  251. file_origin='test.file',
  252. test1=1, **params)
  253. targ.annotate(test1=1.1, test0=[1, 2])
  254. assert_neo_object_is_compliant(targ)
  255. t_start = 0.0001
  256. t_stop = 30
  257. result = epc.time_slice(t_start, t_stop)
  258. assert_arrays_equal(result.times, targ.times)
  259. assert_arrays_equal(result.durations, targ.durations)
  260. assert_arrays_equal(result.labels, targ.labels)
  261. self.assertEqual(result.name, targ.name)
  262. self.assertEqual(result.description, targ.description)
  263. self.assertEqual(result.file_origin, targ.file_origin)
  264. self.assertEqual(result.annotations['test0'], targ.annotations['test0'])
  265. self.assertEqual(result.annotations['test1'], targ.annotations['test1'])
  266. self.assertEqual(result.annotations['test2'], targ.annotations['test2'])
  267. def test_time_slice_empty(self):
  268. params = {'test2': 'y1', 'test3': True}
  269. epc = Epoch([]*pq.ms, durations=[]*pq.ns,
  270. labels=np.array([], dtype='S'),
  271. name='test', description='tester',
  272. file_origin='test.file',
  273. test1=1, **params)
  274. epc.annotate(test1=1.1, test0=[1, 2])
  275. assert_neo_object_is_compliant(epc)
  276. targ = Epoch([]*pq.ms, durations=[]*pq.ns,
  277. labels=np.array([], dtype='S'),
  278. name='test', description='tester',
  279. file_origin='test.file',
  280. test1=1, **params)
  281. targ.annotate(test1=1.1, test0=[1, 2])
  282. assert_neo_object_is_compliant(targ)
  283. t_start = 1.2
  284. t_stop = 1.6
  285. result = epc.time_slice(t_start, t_stop)
  286. assert_arrays_equal(result.times, targ.times)
  287. assert_arrays_equal(result.durations, targ.durations)
  288. assert_arrays_equal(result.labels, targ.labels)
  289. self.assertEqual(result.name, targ.name)
  290. self.assertEqual(result.description, targ.description)
  291. self.assertEqual(result.file_origin, targ.file_origin)
  292. self.assertEqual(result.annotations['test0'], targ.annotations['test0'])
  293. self.assertEqual(result.annotations['test1'], targ.annotations['test1'])
  294. self.assertEqual(result.annotations['test2'], targ.annotations['test2'])
  295. def test_time_slice_none_stop(self):
  296. params = {'test2': 'y1', 'test3': True}
  297. epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  298. labels=np.array(['test epoch 1',
  299. 'test epoch 2',
  300. 'test epoch 3'], dtype='S'),
  301. name='test', description='tester',
  302. file_origin='test.file',
  303. test1=1, **params)
  304. epc.annotate(test1=1.1, test0=[1, 2])
  305. assert_neo_object_is_compliant(epc)
  306. targ = Epoch([1.5, 1.7]*pq.ms, durations=[40, 60]*pq.ns,
  307. labels=np.array(['test epoch 2',
  308. 'test epoch 3'], dtype='S'),
  309. name='test', description='tester',
  310. file_origin='test.file',
  311. test1=1, **params)
  312. targ.annotate(test1=1.1, test0=[1, 2])
  313. assert_neo_object_is_compliant(targ)
  314. t_start = 1.2
  315. t_stop = None
  316. result = epc.time_slice(t_start, t_stop)
  317. assert_arrays_equal(result.times, targ.times)
  318. assert_arrays_equal(result.durations, targ.durations)
  319. assert_arrays_equal(result.labels, targ.labels)
  320. self.assertEqual(result.name, targ.name)
  321. self.assertEqual(result.description, targ.description)
  322. self.assertEqual(result.file_origin, targ.file_origin)
  323. self.assertEqual(result.annotations['test0'], targ.annotations['test0'])
  324. self.assertEqual(result.annotations['test1'], targ.annotations['test1'])
  325. self.assertEqual(result.annotations['test2'], targ.annotations['test2'])
  326. def test_time_slice_none_start(self):
  327. params = {'test2': 'y1', 'test3': True}
  328. epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  329. labels=np.array(['test epoch 1',
  330. 'test epoch 2',
  331. 'test epoch 3'], dtype='S'),
  332. name='test', description='tester',
  333. file_origin='test.file',
  334. test1=1, **params)
  335. epc.annotate(test1=1.1, test0=[1, 2])
  336. assert_neo_object_is_compliant(epc)
  337. targ = Epoch([1.1, 1.5]*pq.ms, durations=[20, 40]*pq.ns,
  338. labels=np.array(['test epoch 1', 'test epoch 2'], dtype='S'),
  339. name='test', description='tester',
  340. file_origin='test.file',
  341. test1=1, **params)
  342. targ.annotate(test1=1.1, test0=[1, 2])
  343. assert_neo_object_is_compliant(targ)
  344. t_start = None
  345. t_stop = 1.6
  346. result = epc.time_slice(t_start, t_stop)
  347. assert_arrays_equal(result.times, targ.times)
  348. assert_arrays_equal(result.durations, targ.durations)
  349. assert_arrays_equal(result.labels, targ.labels)
  350. self.assertEqual(result.name, targ.name)
  351. self.assertEqual(result.description, targ.description)
  352. self.assertEqual(result.file_origin, targ.file_origin)
  353. self.assertEqual(result.annotations['test0'], targ.annotations['test0'])
  354. self.assertEqual(result.annotations['test1'], targ.annotations['test1'])
  355. self.assertEqual(result.annotations['test2'], targ.annotations['test2'])
  356. def test_time_slice_none_both(self):
  357. params = {'test2': 'y1', 'test3': True}
  358. epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  359. labels=np.array(['test epoch 1',
  360. 'test epoch 2',
  361. 'test epoch 3'], dtype='S'),
  362. name='test', description='tester',
  363. file_origin='test.file',
  364. test1=1, **params)
  365. epc.annotate(test1=1.1, test0=[1, 2])
  366. assert_neo_object_is_compliant(epc)
  367. targ = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  368. labels=np.array(['test epoch 1',
  369. 'test epoch 2',
  370. 'test epoch 3'], dtype='S'),
  371. name='test', description='tester',
  372. file_origin='test.file',
  373. test1=1, **params)
  374. targ.annotate(test1=1.1, test0=[1, 2])
  375. assert_neo_object_is_compliant(targ)
  376. t_start = None
  377. t_stop = None
  378. result = epc.time_slice(t_start, t_stop)
  379. assert_arrays_equal(result.times, targ.times)
  380. assert_arrays_equal(result.durations, targ.durations)
  381. assert_arrays_equal(result.labels, targ.labels)
  382. self.assertEqual(result.name, targ.name)
  383. self.assertEqual(result.description, targ.description)
  384. self.assertEqual(result.file_origin, targ.file_origin)
  385. self.assertEqual(result.annotations['test0'], targ.annotations['test0'])
  386. self.assertEqual(result.annotations['test1'], targ.annotations['test1'])
  387. self.assertEqual(result.annotations['test2'], targ.annotations['test2'])
  388. def test_time_slice_differnt_units(self):
  389. params = {'test2': 'y1', 'test3': True}
  390. epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns,
  391. labels=np.array(['test epoch 1',
  392. 'test epoch 2',
  393. 'test epoch 3'], dtype='S'),
  394. name='test', description='tester',
  395. file_origin='test.file',
  396. test1=1, **params)
  397. epc.annotate(test1=1.1, test0=[1, 2])
  398. assert_neo_object_is_compliant(epc)
  399. targ = Epoch([1.5]*pq.ms, durations=[40]*pq.ns,
  400. labels=np.array(['test epoch 2'], dtype='S'),
  401. name='test', description='tester',
  402. file_origin='test.file',
  403. test1=1, **params)
  404. targ.annotate(test1=1.1, test0=[1, 2])
  405. assert_neo_object_is_compliant(targ)
  406. t_start = 0.0012 * pq.s
  407. t_stop = 0.0016 * pq.s
  408. result = epc.time_slice(t_start, t_stop)
  409. assert_arrays_equal(result.times, targ.times)
  410. assert_arrays_equal(result.durations, targ.durations)
  411. assert_arrays_equal(result.labels, targ.labels)
  412. self.assertEqual(result.name, targ.name)
  413. self.assertEqual(result.description, targ.description)
  414. self.assertEqual(result.file_origin, targ.file_origin)
  415. self.assertEqual(result.annotations['test0'], targ.annotations['test0'])
  416. self.assertEqual(result.annotations['test1'], targ.annotations['test1'])
  417. self.assertEqual(result.annotations['test2'], targ.annotations['test2'])
  418. def test_as_array(self):
  419. times = [2, 3, 4, 5]
  420. durations = [0.1, 0.2, 0.3, 0.4]
  421. epc = Epoch(times * pq.ms, durations=durations * pq.ms)
  422. epc_as_arr = epc.as_array(units='ms')
  423. self.assertIsInstance(epc_as_arr, np.ndarray)
  424. assert_array_equal(times, epc_as_arr)
  425. def test_as_quantity(self):
  426. times = [2, 3, 4, 5]
  427. durations = [0.1, 0.2, 0.3, 0.4]
  428. epc = Epoch(times * pq.ms, durations=durations * pq.ms)
  429. epc_as_q = epc.as_quantity()
  430. self.assertIsInstance(epc_as_q, pq.Quantity)
  431. assert_array_equal(times * pq.ms, epc_as_q)
  432. class TestDuplicateWithNewData(unittest.TestCase):
  433. def setUp(self):
  434. self.data = np.array([0.1, 0.5, 1.2, 3.3, 6.4, 7])
  435. self.durations = np.array([0.2, 0.4, 1.1, 2.4, 0.2, 2.0])
  436. self.quant = pq.ms
  437. self.epoch = Epoch(self.data*self.quant,
  438. durations=self.durations*self.quant)
  439. def test_duplicate_with_new_data(self):
  440. signal1 = self.epoch
  441. new_data = np.sort(np.random.uniform(0, 100, self.epoch.size)) * pq.ms
  442. signal1b = signal1.duplicate_with_new_data(new_data)
  443. assert_arrays_almost_equal(np.asarray(signal1b),
  444. np.asarray(new_data), 1e-12)
  445. assert_arrays_almost_equal(np.asarray(signal1b.durations),
  446. np.asarray(signal1.durations), 1e-12)
  447. class TestEventFunctions(unittest.TestCase):
  448. def test__pickle(self):
  449. epoch1 = Epoch(np.arange(0, 30, 10)*pq.s, labels=np.array(['t0', 't1', 't2'], dtype='S'),
  450. units='s')
  451. fobj = open('./pickle', 'wb')
  452. pickle.dump(epoch1, fobj)
  453. fobj.close()
  454. fobj = open('./pickle', 'rb')
  455. try:
  456. epoch2 = pickle.load(fobj)
  457. except ValueError:
  458. epoch2 = None
  459. fobj.close()
  460. assert_array_equal(epoch1.times, epoch2.times)
  461. os.remove('./pickle')
  462. if __name__ == "__main__":
  463. unittest.main()