How to keep consistant timing after dividing a Raw File

Hello everyone,

I have one unique raw file which contains EEG data for different conditions in bdf format.

I want to be able to separate each condition in its dedicated .fif file.

For now, I have been able to use my unique raw file and save smaller .fif thanks to crop method.

However, later in the pipeline, I want to be able to retrieve all the events of the .fif file specific to a condition. So, I apply mne.find_events() on this condition file but this outputs events onset based on the original longer bdf file which thus does not correspond to correct times.

In practice: I have my original bdf file ranging from 0 to 913sec. I have one of the condition with first event at timepoint 582703 and last event at timepoint 681316 (which corresponds to 96.3s duration for that condition with my sampling frequency at 1024Hz).
Later, when I read that smaller file its range does not start with 0 but EEG.times do start at 0 and when using mne.find_events() on that smaller condition fif file the first event is still at timepoint 582703 which is out of the time duration of the file (582703/1024 = 569sec).

How can I get my events according to the new duration of the file and not the original .bdf file.

Thank you very much for any help.

The only solution I came with so far is not based on dedicated MNE methods but consists of extracting events from the original bdf file, croping the data thanks to tmin and tmax and then filtering the original events list inside the cropped time range and then substracting the tmin from all the events time points.

If you have any solution that use MNE dedicated function or more straightforward please do not hesitate to share your solution.

Thanks.

Hello this will works for me

import datetime
import mne
from mne.utils import logger

def get_info(inst,task=None):
df = inst.annotations.to_data_frame(time_format=None)
msg= f"β€œβ€
β†’ raw {task}
β†’ orig_time : {inst.annotations.orig_time}
β†’ first_time: {inst.first_time}
β†’ first_samp: {inst.first_samp}
β†’ t0: {inst.times[0]}
β†’ Annotations\n{df.head()}β€œ)
β€œβ€β€
logger.info(msg)

tmin_idx = 582703
tmax_idx = 681316

tmin = raw.times[tmin_idx]
tmax = raw.times[tmax_idx]

– crop the raw

raw_crop = raw.copy().crop(tmin=tmin,tmax=tmax)

– set raw.first_samp = 0 & raw.first_time = 0

raw_crop._cropped_samp = 0

– get annotations from raw and crop

ant_crop = raw.annotations.copy().crop(tmin=tmin, tmax=tmax, use_orig_time=False)

– reset onsets to raw.first_time == 0

ant_crop.onset -= tmin

– set annotations in raw_crop

raw_crop.set_annotations(ant_crop)

– update info[β€˜meas_date’]

raw_crop.set_meas_date(raw.info[β€˜meas_date’] + datetime.timedelta(seconds=tmin))

– check

get_info(raw_crop,task=β€˜crop’)

– show raw

raw_crop.plot()

1 Like