Skip to content

Commit 68d4458

Browse files
author
sprenger
committed
[utils] introduce utility function for annotation cleaning and adjust tests
1 parent b5f0c0e commit 68d4458

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

neo/test/utils/test_misc.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ def test__cut_block_by_epochs(self):
386386
self.assertEqual(len(block.segments[epoch_idx].irregularlysampledsignals), 1)
387387

388388
annos = block.segments[epoch_idx].annotations
389-
self.assertIn('nix_name', annos)
390-
self.assertTrue(annos['nix_name'].startswith('neo.segment.'))
389+
# new segment objects have different identity
390+
self.assertNotIn('nix_name', annos)
391391

392392
if epoch_idx != 0:
393393
self.assertEqual(len(block.segments[epoch_idx].epochs), 1)
@@ -442,8 +442,7 @@ def test__cut_block_by_epochs(self):
442442
self.assertEqual(len(block.segments[epoch_idx].irregularlysampledsignals), 1)
443443

444444
annos = block.segments[epoch_idx].annotations
445-
self.assertIn('nix_name', annos)
446-
self.assertTrue(annos['nix_name'].startswith('neo.segment.'))
445+
self.assertNotIn('nix_name', annos)
447446

448447
if epoch_idx != 0:
449448
self.assertEqual(len(block.segments[epoch_idx].epochs), 1)

neo/utils/misc.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import neo
1313

14-
ignore_annotations = ['nix_name']
14+
reserved_annotations = ['nix_name']
1515

1616
def get_events(container, **properties):
1717
"""
@@ -348,12 +348,8 @@ def add_epoch(
348348

349349
ep = neo.Epoch(times=times, durations=durations, **kwargs)
350350

351-
annos = {k: v for k, v in event1.annotations.items()
352-
if k not in ignore_annotations}
353-
array_annos = {k: v for k, v in event1.array_annotations.items()
354-
if k not in ignore_annotations}
355-
ep.annotate(**copy.copy(annos))
356-
ep.array_annotate(**copy.copy(array_annos))
351+
ep.annotate(**clean_annotations(event1.annotations))
352+
ep.array_annotate(**clean_annotations(event1.array_annotations))
357353

358354
if attach_result:
359355
segment.epochs.append(ep)
@@ -550,21 +546,34 @@ def cut_segment_by_epoch(seg, epoch, reset_time=False):
550546
epoch.times[ep_id] + epoch.durations[ep_id],
551547
reset_time=reset_time)
552548

553-
annos = {k: v for k, v in epoch.annotations.items()
554-
if k not in ignore_annotations}
555-
subseg.annotate(**copy.copy(annos))
549+
subseg.annotations = clean_annotations(subseg.annotations)
550+
subseg.annotate(**clean_annotations(epoch.annotations))
556551

557552
# Add array-annotations of Epoch
558-
for key, val in epoch.array_annotations.items():
559-
if key in ignore_annotations:
560-
continue
553+
for key, val in clean_annotations(epoch.array_annotations).items():
561554
if len(val):
562555
subseg.annotations[key] = copy.copy(val[ep_id])
563556

564557
segments.append(subseg)
565558

566559
return segments
567560

561+
def clean_annotations(dictionary):
562+
"""
563+
Remove reserved keys from an annotation dictionary.
564+
565+
Parameters
566+
----------
567+
dictionary: dict
568+
annotation dictionary to be cleaned
569+
570+
Returns:
571+
--------
572+
dict
573+
A cleaned version of the annotations
574+
"""
575+
return {k: v for k, v in dictionary.items() if k not in reserved_annotations}
576+
568577

569578
def is_block_rawio_compatible(block, return_problems=False):
570579
"""

0 commit comments

Comments
 (0)