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.

Misalignment of annotations after cropping raw

See original GitHub issue

Hi,

I have an EEG dataset of one hour length covering several experiments. To mark the pauses between particular experiments as bad, I detect event codes marking their start and end point. After getting the corresponding time, I add it to the annotations variable and save it for further processing. The code is quite the same as in the example.

    raw = mne.io.read_raw_brainvision(raw_fname, montage=montage, preload=False)
    events = mne.find_events(raw, stim_channel='STI 014')

#%% Marking pauses as bad
    onset = np.array([events[idx,0] for idx in range(len(events[:, 2])) if events[idx, 2] > 80 and events[idx, 2] < 90])+1
    onset = np.insert(onset, 0, 0)
    end = np.array([events[idx,0] for idx in range(len(events[:, 2])) if events[idx, 2] > 70 and events[idx, 2] < 80])-1
    if events[0,2] < 70:
        end = np.concatenate([[events[0,0]-1], end, [len(raw)]])
    else:
        end = np.concatenate([end, [len(raw)]])

    duration = (end-onset)/raw.info['sfreq']
    onset = onset/raw.info['sfreq']
    description = ['bad']*len(onset)

    raw.annotations = mne.Annotations(onset, duration, description,
                                      orig_time=raw.info['meas_date'])

    raw.save(raw_fname+'.raw.fif.gz')

In a subsequent step, I load the EEG data, crop it and concatenate it. That results in a continous stream containing only the occurences of a certain experiment.

    raw = mne.io.read_raw_fif(filename, preload=False)
    events = mne.find_events(raw, stim_channel='STI 014')

#%% cropping - assessing the beginning of particular experiments
    event_id = experiments['resting']['event_id']
    color = experiments['resting']['color']

# indices of the start and end triggercodes are detected, +1 and -1 to take a neighboring sample
    times_start = events[:, 0][np.where(events[:, 2] == event_id['start'])]-1
    times_end = events[:, 0][np.where(events[:, 2] == event_id['end'])]+1

    old_raw = raw.copy()
    new_raw = raw.copy().crop(raw.times[times_start[0]],
                              raw.times[times_end[0]])
    for idx in range(1, len(times_start)):
        mne.concatenate_raws([new_raw,
                              raw.copy().crop(raw.times[times_start[idx]],
                                              raw.times[times_end[idx]])])
# data is loaded, now the samples are loaded to memory
    raw = new_raw
    new_raw.close()
    raw.load_data()
    events = mne.find_events(raw, stim_channel='STI 014')

That is probably not the most elegant way of doing it.

The main problem is that after cropping the annotations are not where they are supposed to be. The following pictures show what I mean.

issue_annotations_oldraw

issue_annotations_newraw

The annotations appear now at a totally different position which does not even fit to the onset values in the annotations variable:

old_raw.annotations.onset
Out[52]: 
array([    0.        ,   165.36399841,   609.80700684,   684.23101807,
        1065.14794922,  1497.01794434,  1589.45605469,  1963.6159668 ,
        2370.41699219,  2510.62597656,  2877.16308594,  3300.84399414,
        3431.56201172,  3873.2409668 ,  3942.5090332 ])

old_raw.first_samp
Out[53]: 0

new_raw.annotations.onset
Out[54]: array([ 437.35700684,  511.78101807])

new_raw.first_samp
Out[55]: 172450

I am not sure were the mistake happens. Probably, I misunderstood how the orig_time parameter works. What is your suggestion?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
agramfortcommented, Oct 25, 2017

we would need a setter for this.

it should be done in the reader.

1reaction
cbrnrcommented, Oct 25, 2017

Because people don’t know about this behavior.

Maybe we could add a note to Raw.append saying that if the individual raw objects have a first_samp > 0 everything gets mangled, therefore setting first_samp=0 is recommended in such a case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

events shift after raw.add_channels - MNE Forum - Discourse
I plot the eeg_raw before and after cropping to see whether after cropping, the events still match the signals. And the answer is...
Read more >
Annotating continuous data — MNE 1.2.2 documentation
This tutorial describes adding annotations to a Raw object, and how annotations are used in later stages of data processing.
Read more >
Introduction to Variant Calling: QC, Alignment, Deduplication ...
Basepair's variant calling pipeline identifies single nucleotide variants present within whole genome and exome data.
Read more >
Complement Genome Annotation Lift Over Using a Weighted ...
After filtering low-quality accessions (less than 3,000,000 RNA-seq reads available, alignment rate ≤90%), 697 accessions were left. The ...
Read more >
CAMERA: An integrated strategy for compound spectra ... - NCBI
To evaluate the algorithms, we compared the annotation of CAMERA against a manually ... After alignment of positively screened peak pairs, ...
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