3
3
"""
4
4
5
5
import unittest
6
+ import warnings
6
7
7
8
import numpy as np
8
9
import quantities as pq
@@ -281,11 +282,11 @@ def test__get_epochs(self):
281
282
282
283
def test__add_epoch (self ):
283
284
starts = Event (times = [0.5 , 10.0 , 25.2 ] * pq .s )
284
- starts .annotate (event_type = 'trial start' )
285
+ starts .annotate (event_type = 'trial start' , nix_name = 'neo.event.0' )
285
286
starts .array_annotate (trial_id = [1 , 2 , 3 ])
286
287
287
288
stops = Event (times = [5.5 , 14.9 , 30.1 ] * pq .s )
288
- stops .annotate (event_type = 'trial stop' )
289
+ stops .annotate (event_type = 'trial stop' , nix_name = 'neo.event.1' )
289
290
stops .array_annotate (trial_id = [1 , 2 , 3 ])
290
291
291
292
seg = Segment ()
@@ -295,7 +296,7 @@ def test__add_epoch(self):
295
296
ep_starts = add_epoch (seg , starts , pre = - 300 * pq .ms , post = 250 * pq .ms )
296
297
297
298
assert_neo_object_is_compliant (ep_starts )
298
- assert_same_annotations (ep_starts , starts )
299
+ self . assertDictEqual (ep_starts . annotations , { 'event_type' : 'trial start' } )
299
300
assert_arrays_almost_equal (ep_starts .times , starts .times - 300 * pq .ms , 1e-12 )
300
301
assert_arrays_almost_equal (ep_starts .durations ,
301
302
(550 * pq .ms ).rescale (ep_starts .durations .units )
@@ -305,7 +306,7 @@ def test__add_epoch(self):
305
306
ep_trials = add_epoch (seg , starts , stops )
306
307
307
308
assert_neo_object_is_compliant (ep_trials )
308
- assert_same_annotations (ep_trials , starts )
309
+ self . assertDictEqual (ep_trials . annotations , { 'event_type' : 'trial start' } )
309
310
assert_arrays_almost_equal (ep_trials .times , starts .times , 1e-12 )
310
311
assert_arrays_almost_equal (ep_trials .durations , stops - starts , 1e-12 )
311
312
@@ -337,16 +338,16 @@ def test__match_events(self):
337
338
def test__cut_block_by_epochs (self ):
338
339
epoch = Epoch ([0.5 , 10.0 , 25.2 ] * pq .s , durations = [5.1 , 4.8 , 5.0 ] * pq .s ,
339
340
t_start = .1 * pq .s )
340
- epoch .annotate (epoch_type = 'a' , pick = 'me' )
341
+ epoch .annotate (epoch_type = 'a' , pick = 'me' , nix_name = 'neo.epoch.0' )
341
342
epoch .array_annotate (trial_id = [1 , 2 , 3 ])
342
343
343
344
epoch2 = Epoch ([0.6 , 9.5 , 16.8 , 34.1 ] * pq .s , durations = [4.5 , 4.8 , 5.0 , 5.0 ] * pq .s ,
344
345
t_start = .1 * pq .s )
345
- epoch2 .annotate (epoch_type = 'b' )
346
+ epoch2 .annotate (epoch_type = 'b' , nix_name = 'neo.epoch.1' )
346
347
epoch2 .array_annotate (trial_id = [1 , 2 , 3 , 4 ])
347
348
348
349
event = Event (times = [0.5 , 10.0 , 25.2 ] * pq .s , t_start = .1 * pq .s )
349
- event .annotate (event_type = 'trial start' )
350
+ event .annotate (event_type = 'trial start' , nix_name = 'neo.event.0' )
350
351
event .array_annotate (trial_id = [1 , 2 , 3 ])
351
352
352
353
anasig = AnalogSignal (np .arange (50.0 ) * pq .mV , t_start = .1 * pq .s ,
@@ -362,8 +363,8 @@ def test__cut_block_by_epochs(self):
362
363
array_annotations = {'spikenum' : np .arange (1 , 9 )})
363
364
364
365
# test without resetting the time
365
- seg = Segment ()
366
- seg2 = Segment (name = 'NoCut' )
366
+ seg = Segment (nix_name = 'neo.segment.0' )
367
+ seg2 = Segment (name = 'NoCut' , nix_name = 'neo.segment.1' )
367
368
seg .epochs = [epoch , epoch2 ]
368
369
seg .events = [event ]
369
370
seg .analogsignals = [anasig ]
@@ -374,7 +375,10 @@ def test__cut_block_by_epochs(self):
374
375
original_block .segments = [seg , seg2 ]
375
376
original_block .create_many_to_one_relationship ()
376
377
377
- block = cut_block_by_epochs (original_block , properties = {'pick' : 'me' })
378
+ with warnings .catch_warnings (record = True ) as w :
379
+ # This should raise a warning as one segment does not contain epochs
380
+ block = cut_block_by_epochs (original_block , properties = {'pick' : 'me' })
381
+ self .assertEqual (len (w ), 1 )
378
382
379
383
assert_neo_object_is_compliant (block )
380
384
self .assertEqual (len (block .segments ), 3 )
@@ -385,6 +389,10 @@ def test__cut_block_by_epochs(self):
385
389
self .assertEqual (len (block .segments [epoch_idx ].analogsignals ), 1 )
386
390
self .assertEqual (len (block .segments [epoch_idx ].irregularlysampledsignals ), 1 )
387
391
392
+ annos = block .segments [epoch_idx ].annotations
393
+ # new segment objects have different identity
394
+ self .assertNotIn ('nix_name' , annos )
395
+
388
396
if epoch_idx != 0 :
389
397
self .assertEqual (len (block .segments [epoch_idx ].epochs ), 1 )
390
398
else :
@@ -414,8 +422,8 @@ def test__cut_block_by_epochs(self):
414
422
t_stop = epoch .times [0 ] + epoch .durations [0 ]))
415
423
416
424
# test with resetting the time
417
- seg = Segment ()
418
- seg2 = Segment (name = 'NoCut' )
425
+ seg = Segment (nix_name = 'neo.segment.0' )
426
+ seg2 = Segment (name = 'NoCut' , nix_name = 'neo.segment.1' )
419
427
seg .epochs = [epoch , epoch2 ]
420
428
seg .events = [event ]
421
429
seg .analogsignals = [anasig ]
@@ -426,7 +434,10 @@ def test__cut_block_by_epochs(self):
426
434
original_block .segments = [seg , seg2 ]
427
435
original_block .create_many_to_one_relationship ()
428
436
429
- block = cut_block_by_epochs (original_block , properties = {'pick' : 'me' }, reset_time = True )
437
+ with warnings .catch_warnings (record = True ) as w :
438
+ # This should raise a warning as one segment does not contain epochs
439
+ block = cut_block_by_epochs (original_block , properties = {'pick' : 'me' }, reset_time = True )
440
+ self .assertEqual (len (w ), 1 )
430
441
431
442
assert_neo_object_is_compliant (block )
432
443
self .assertEqual (len (block .segments ), 3 )
@@ -436,6 +447,10 @@ def test__cut_block_by_epochs(self):
436
447
self .assertEqual (len (block .segments [epoch_idx ].spiketrains ), 1 )
437
448
self .assertEqual (len (block .segments [epoch_idx ].analogsignals ), 1 )
438
449
self .assertEqual (len (block .segments [epoch_idx ].irregularlysampledsignals ), 1 )
450
+
451
+ annos = block .segments [epoch_idx ].annotations
452
+ self .assertNotIn ('nix_name' , annos )
453
+
439
454
if epoch_idx != 0 :
440
455
self .assertEqual (len (block .segments [epoch_idx ].epochs ), 1 )
441
456
else :
@@ -527,14 +542,18 @@ def test__add_epoch(self):
527
542
528
543
regular_event = Event (times = loaded_event .times - 1 * loaded_event .units )
529
544
545
+ loaded_event .annotate (nix_name = 'neo.event.0' )
546
+ regular_event .annotate (nix_name = 'neo.event.1' )
547
+
530
548
seg = Segment ()
531
549
seg .events = [regular_event , proxy_event ]
532
550
533
551
# test cutting with two events one of which is a proxy
534
552
epoch = add_epoch (seg , regular_event , proxy_event )
535
553
536
554
assert_neo_object_is_compliant (epoch )
537
- assert_same_annotations (epoch , regular_event )
555
+ exp_annos = {k : v for k , v in regular_event .annotations .items () if k != 'nix_name' }
556
+ self .assertDictEqual (epoch .annotations , exp_annos )
538
557
assert_arrays_almost_equal (epoch .times , regular_event .times , 1e-12 )
539
558
assert_arrays_almost_equal (epoch .durations ,
540
559
np .ones (regular_event .shape ) * loaded_event .units , 1e-12 )
0 commit comments