test_epoch.py 23 KB

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