Unable to set annotations of epochs in raw EEG data

Hello guys! I’m working with a raw file obtained with BrainVision that has 63 channels of EEG, without any sort of annotations or events and I’m trying to mark events by hand.

The first at 10 seconds, the second at 15, then 25, then 30 and so on, always intercalated by 10 and then 5 seconds. After this I’ve tried to create epochs which are limited by each pair of consecutive events (p.e. first epoch from 0 to 10 and second from 10 to 15).

My code to do so is as follows:

# Define the pattern of event intervals
event_intervals = [10, 5, 10, 5]  # Intervals between events 

# Calculate the number of events in the signal
total_duration = raw.times[-1]  # Total duration of the signal
num_events = len(event_intervals)

# Create events 
onset = 0
while onset < total_duration:
    for interval in event_intervals:
        if onset >= total_duration:
            break
        events.append([int(onset * raw.info['sfreq']), 0, 1])  # Event ID is set to 1 for all events
        onset += interval

# Create epochs 
epochs = []
for i in range(len(events) - 1):
    tmin = events[i][0] / raw.info['sfreq']
    tmax = events[i+1][0] / raw.info['sfreq']
    epoch = mne.Epochs(raw, events[i:i+2], event_id={'event': 1}, tmin=tmin, tmax=tmax, baseline=None, preload=True)
    epochs.append(epoch)

# Mark events in the raw data
event_desc = ['Event'] * len(events)
onset_times = [event[0] / raw.info['sfreq'] for event in events]
raw.set_annotations(mne.Annotations(onset = onset_times, duration = [0.] * len(events), description = event_desc))

# Mark epochs in the raw data
epoch_desc = ['Epoch'] * len(epochs)
for i, epoch in enumerate(epochs):
    tmin = epoch.times[0]
    tmax = epoch.times[-1]
    raw.set_annotations(mne.Annotations([tmin], [tmax], epoch_desc[i]))

When i print the timings of the events, it seem’s like it’s working, however, when i try to set annotations in the data, I get the following warning message (repeated over several lines) and consequent result:

Warning:

<ipython-input-61-d9a4eb3df21d>:92: RuntimeWarning: Limited 1 annotation(s) that were expanding outside the data range.
  raw.set_annotations(mne.Annotations([tmin], [tmax], epoch_desc[i]))

Result when calling raw.annotations:
<Annotations | 1 segment: Epoch (1)>

When i then plot the raw file with the annotations, it doesn’t have any event or epoch marked on it.
Do you guys understand what i am doing wrong?
Thanks for the help!

  • MNE version: 1.4
  • operating system: Windows 10