I manually annotated bad segments in patient EEG data.
Next, I cropped the early and late parts of the resting state data, as they were most noisy.
I assumed that the annotations in the remaining time window would be shifted accordingly. Luckily, my colleague compared the pre/post cropping annotations and noted that they didn`t get shifted. There is probably a good reason for this behaviour, which I am curious to hear about.
I suggest a warning that cautions the user that annotations are not updated automatically when cropping signals containing annotations. I think that can be easily implemented and might prevent users from potentially rejecting good segments and keeping bad segments.
Then again, perhaps we are already shifting onset when cropping, and this is exactly what’s causing your issue? Could you please provide a reproducible example that demonstrates the issue?
import os
import mne
sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = os.path.join(
sample_data_folder, "MEG", "sample", "sample_audvis_filt-0-40_raw.fif"
)
raw = mne.io.read_raw_fif(sample_data_raw_file, verbose=False)
# define bad data onsets and durations
onset = [40., 220., 260.]
duration = [5., 7., 10.]
description = ['bad', 'bad', 'bad']
bad_annot = mne.Annotations(
onset, duration, description, orig_time=raw.info["meas_date"]
)
raw.set_annotations(bad_annot)
# that's the bad segments we want to annotate
raw.plot()
# save pre crop annotations
annotations_pre_crop = raw.annotations
# crop the data
raw_cropped = raw.copy().crop(60,250)
# let's look at post cropping annotations
annotations_post_crop = raw_cropped.annotations
# seems that the annotations do get updated somewhere
raw_cropped.plot()
# loop over annotations and print onset times
for annot in annotations_pre_crop:
print(annot['onset'])
for annot in annotations_post_crop:
print(annot['onset'])
# annotations do not get updated (onset times are out of cropped window)
The raw.plot() shows that the bad segments are shifted and align with where we would want them. The annotations dict however shows the pre cropping onsets. I do think that the annotations do get correctly shifted, the annotations object might just not be updated accordingly. This is mainly an issue if you share data like in my case and your colleague would like to use the annotations for further processing.
Hello there!
I’ve been looking into this too and I think the critical lines for this are:
When dealing with orig_time = None, the onsets of all annots are corrected, since they are relative to first_time. This is fine.
Then they are passed to
Which also does some cropping and reintroduces first_time:
It seems like the correction for first_time is doesn’t quite work as it’s supposed to when not having orig_time set, but maybe I am wrong. I am trying to write an MWE now