question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Annotations lost when concatenating Raws / incorrect shift calculation in _combine_annotations

See original GitHub issue

Annotations 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:closed
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
agramfortcommented, Jan 22, 2019

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.

0reactions
massichcommented, Jan 22, 2019

@hekolk check #5851.

Just for completeness this is also related to #5555

Read more comments on GitHub >

github_iconTop Results From Across the Web

CONCATENATE Function - Formula, Examples, How to Use it
The CONCATENATE Function is categorized under Excel Text Functions. The function helps to join two or more strings into one string.
Read more >
How to merge rows in Excel without losing data - Ablebits
The tutorial shows how to safely merge rows in Excel in 4 different ways: merge multiple rows without losing data, combine duplicate rows, ......
Read more >
SQL Server CONCATENATE Operations with SQL Plus (+) ...
This article explores SQL Server Concatenate operations using the SQL Plus (+) operator and SQL CONCAT function.
Read more >
pandas.concat() function in Python - GeeksforGeeks
join : way to handle indexes on other axis; default = 'outer'; ignore_index: if True, do not use the index values along the...
Read more >
paste: Concatenate Strings - Rdrr.io
Concatenate Strings. Description ... A character vector of the concatenated values. ... 'plotmath' for the use of paste in plot annotation.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found