I want to load single epochs exported from Brainstorm (data shared by collaborator) and concatenate them in MNE.
So far I am able to create single epochs, but when I try to concatenate them I am receiving
raise ValueError("Epochs must have same times")
Epochs times are for example [0, 4.99s], [5, 9.99] et cetera - 5s subsequent chunks.
My code is below
def load_single_mat_epoch(mat_file):
# Load the .mat file
mat_data = sio.loadmat(mat_file)
# Extract relevant data
data = mat_data["Value"] # Transpose to shape (1, 68, 3000)
data = data[np.newaxis, :, :]
times = mat_data["Time"].flatten()
print(f"File: {Path(mat_file).name}, Time array length: {len(times)}")
region_struct = mat_data["Atlas"][0, 0]["Scouts"]["Label"].ravel().tolist()
ch_names = [item.item() for item in region_struct]
sfreq = 600.0
info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types="misc")
# Create events array for this single epoch
start_idx = int(times[0]*sfreq)
events = np.array([[start_idx, 0, 1]])
return mne.EpochsArray(data, info, events, tmin=times[0])
def build_mne_epochs_from_matlab_files(file_paths):
"""Build MNE Epochs object from multiple MATLAB files."""
epochs_list = []
for file_path in file_paths:
try:
epoch = load_single_mat_epoch(file_path)
epochs_list.append(epoch)
except Exception as e:
print(f"Error loading {file_path}: {e}")
if not epochs_list:
raise ValueError("No valid epochs were loaded.")
# Concatenate all loaded epochs
# return mne.concatenate_epochs(epochs_list, add_offset=False, on_mismatch="warn")
return epochs_list
Should I remove the times imported from Matlab? How I can retain this information about timing in an other way?