Misalignment of annotations after cropping raw
See original GitHub issueHi,
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.
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:
- Created 6 years ago
- Comments:16 (16 by maintainers)
Top GitHub Comments
we would need a setter for this.
it should be done in the reader.
Because people don’t know about this behavior.
Maybe we could add a note to
Raw.append
saying that if the individualraw
objects have afirst_samp
> 0 everything gets mangled, therefore settingfirst_samp=0
is recommended in such a case.