Error when cropping raw object

Hi!
For some reason the last event is removed when I cropped my data, which to my understanding should not be possible, as I’m defining the tmax according to the last event. I also tried using tmax=None, but also here it’s removed.

Furthermore, I’m having a lot of issues when using events later in my code, as the events are not updated accordingly when the data is cropped.

Is there a bug or am I doing something wrong?

def crop_data(self, raw_intensity):
event_dict_trans = {val: int(key) for key, val in self.annotation_names.items()}
events, event_dict = mne.events_from_annotations(raw_intensity, event_dict_trans)
sfreq = raw_intensity.info["sfreq"]
new_tmin = max(events[0][0] / sfreq - 10, 0) #Always ensure the tmin is non-negative.
new_tmax = events[-1][0] / sfreq + self.stimulus_duration[str(events[-1][2])] + 3
raw_intensity = raw_intensity.crop(tmin=new_tmin, tmax=new_tmax)
return raw_intensity

MNE version: 1.10.2

Windows: 11

Hello,

Please can you provide some example events/annotations where you see this problematic behaviour. That will make it much easier for us to test whether this is a bug in MNE or something wrong with that code.

Cheers,
Thomas

1 Like

Hej Thomas,

Of course — sorry for not doing that in the first place!

Below is an example where I print the first and last times before and after cropping, as well as the events before and after cropping.

Before:

new_tmin
np.float64(170.289536)
new_tmax
np.float64(675.2640055615234)
raw_intensity
<RawNIRX | NIRX, 32 x 6957 (683.9 s), ~1.7 MiB, data loaded>
raw_intensity._first_time
0.0
raw_intensity._last_time
np.float64(683.802624)
events
array([[1834,    0,    2],
       [2244,    0,    4],
       [2758,    0,    2],
       [3724,    0,    5],
       [4362,    0,    2],
       [4878,    0,    6],
       [5505,    0,    2],
       [6201,    0,    7]])

After:

events, event_dict = mne.events_from_annotations(raw_intensity, event_dict_trans)
raw_intensity
<RawNIRX | NIRX, 32 x 5138 (505.1 s), ~1.3 MiB, data loaded>
raw_intensity._first_time
np.float64(170.262528)
raw_intensity._last_time
np.float64(675.250176)
events
array([[1834,    0,    2],
       [2244,    0,    4],
       [2758,    0,    2],
       [3724,    0,    5],
       [4362,    0,    2],
       [4878,    0,    6],
       [5505,    0,    2]])

he problems encountered later arise, for example, when I want to apply a baseline correction to the raw object and use the event indices to compute the mean of a control period to subtract it from the following active period. I then get index errors because the indices are set relative to the start of the entire recording.

For example, after cropping, the shape of the data is:

np.shape(raw_intensity.pick('S1_D1 760').get_data())
(1, 5138)

Which obviously makes it difficult to index the last event shown above.

Does this give enough insight?

Best,
Nikolai

1 Like

So in my attempt to replicate what’s happening, I don’t see the last event being dropped.

What I don’t have though is the stimulus_duration information which you use to crop the data. Can you double check that this has not been set to a negative number. That could cause you to get the wrong new_tmax.

My code:

import numpy as np
import mne

n_samples = 6957
dur = 683.802624
sfreq = n_samples / dur

data = np.random.RandomState(0).standard_normal((1, n_samples))
info = mne.create_info(sfreq=sfreq, ch_names=["ch1"], ch_types=["eeg"])
raw = mne.io.RawArray(data, info)

events = np.array(
    [
        [1834, 0, 2],
        [2244, 0, 4],
        [2758, 0, 2],
        [3724, 0, 5],
        [4362, 0, 2],
        [4878, 0, 6],
        [5505, 0, 2],
        [6201, 0, 7],
    ]
)
annots = mne.annotations_from_events(events, sfreq)
raw.set_annotations(annots)


def crop_data(raw):
    events, _ = mne.events_from_annotations(raw)
    new_tmin = max(events[0][0] / sfreq - 10, 0)
    new_tmax = events[-1][0] / sfreq + 3  # <-- stimulus_duration missing here
    return raw.copy().crop(tmin=new_tmin, tmax=new_tmax)


cropped_raw = crop_data(raw)
cropped_events, _ = mne.events_from_annotations(cropped_raw)

assert len(events) == len(cropped_events)