Annotations lost when concatenating Raws / incorrect shift calculation in _combine_annotations
See original GitHub issueAnnotations are lost when concatenating (BrainVision) raws (on master
).
The bug seems to be in mne.annotations._combine_annotations
( https://github.com/mne-tools/mne-python/blob/master/mne/annotations.py#L362 ).
When concatenating annotations, the time shift calculation seems to be incorrect since the annotations of the second raw are shifted both by the length of the first raw (one_n_sampes
) and the difference in the start times of the recording (meas_date - two.orig_time
). Hence, the annotations of the second raw are shifted “too far” and removed when cropping the annotations in Raw.set_annotations
(some annotations might be kept if the lengths of the Raws differs).
I’m not sure if I’m missing something, but since annotations are supposed to be used instead of stim channels, this bug seems rather critical to me.
Steps and/or code to reproduce
On a high level, the bug manifests itself as
import mne
import numpy as np
# vhdr_files = ...
#%% load first raw
raw1 = mne.io.read_raw_brainvision(vhdr_files[0])
events1, _ = mne.events_from_annotations(raw1)
print(len(events1))
# 977
#%% load second raw
raw2 = mne.io.read_raw_brainvision(vhdr_files[1])
events2, _ = mne.events_from_annotations(raw2)
print(len(events2))
# 975
#%% combine raw
raw1.append(raw2)
events_comb, _ = mne.events_from_annotations(raw1)
print(len(events_comb))
# 979
np.testing.assert_equal(events1, events_comb[:len(events1), :])
All but two events of the second recording are missing.
On a lower level, the shifting becomes obvious:
orig_time1 = 0
duration1 = 10.
sfreq = 1000
annot1 = mne.Annotations(onset=[0, 1], duration=[0,0], description=['test', 'test'],
orig_time=orig_time1)
annot2 = mne.Annotations(onset=[0, 1], duration=[0,0], description=['test', 'test'],
orig_time=orig_time1 + duration1)
#%%
# this call corresponds to the one in Raw.append
annot_combined = mne.annotations._combine_annotations(
annot1, annot2, one_n_samples=duration1 * sfreq,
one_first_samp=0, two_first_samp=0, sfreq=sfreq,
meas_date=orig_time1)
print(annot_combined.onset)
# [ 0. 1. 20. 21.]
Expected results
The onsets of the annotations should be [0., 1., 10. ,11.]
.
Actual results
The onsets are [ 0. 1. 20. 21.]
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (8 by maintainers)
Top GitHub Comments
it should not fail to concat raws with different meas_date as indeed it’s a really frequent usecase. mne inserts a bad annotation to notifiy there is file boundary and it should just work ie the orig_time of the appended raw file should be adjust to just work.
@hekolk check #5851.
Just for completeness this is also related to #5555