diff --git a/mne/io/eeglab/eeglab.py b/mne/io/eeglab/eeglab.py index 7e006275c20..2e7784925a5 100644 --- a/mne/io/eeglab/eeglab.py +++ b/mne/io/eeglab/eeglab.py @@ -619,6 +619,38 @@ def __init__( "You should try using read_raw_eeglab function." ) + def _generate_boundary_events(n_trials): + """ + Generate boundary events for epoched data without events. + + Parameters + ---------- + n_epochs: int + Number of epochs (trials). + + Returns + ------- + events: ndarray + Array of boundary events + event_id: dict + Event id dictionary + """ + events = np.zeros((n_trials, 3), dtype=np.int64) # Explicitly use int64 + events[:, 0] = np.arange( + n_trials, dtype=np.int64 + ) # Start from 0 for sample numbers + events[:, 1] = 0 # Previous event value + events[:, 2] = 1 # Boundary marker + + event_id = {"boundary": 1} + + # Double-check the array type + assert ( + events.dtype == np.int64 + ), f"Events dtype is {events.dtype}, expected np.int64" + + return events, event_id + if events is None and eeg.trials > 1: # first extract the events and construct an event_id dict event_name, event_latencies, unique_ev = list(), list(), list() @@ -656,15 +688,21 @@ def __init__( ) # now fill up the event array - events = np.zeros((eeg.trials, 3), dtype=int) - for idx in range(0, eeg.trials): - if idx == 0: - prev_stim = 0 - elif idx > 0 and event_latencies[idx] - event_latencies[idx - 1] == 1: - prev_stim = event_id[event_name[idx - 1]] - events[idx, 0] = event_latencies[idx] - events[idx, 1] = prev_stim - events[idx, 2] = event_id[event_name[idx]] + if event_id is None and not event_name and not event_latencies: + # account for EEGLAB with trials but no events + events, event_id = _generate_boundary_events(eeg.trials) + else: + events = np.zeros((eeg.trials, 3), dtype=int) + for idx in range(0, eeg.trials): + if idx == 0: + prev_stim = 0 + elif ( + idx > 0 and event_latencies[idx] - event_latencies[idx - 1] == 1 + ): + prev_stim = event_id[event_name[idx - 1]] + events[idx, 0] = event_latencies[idx] + events[idx, 1] = prev_stim + events[idx, 2] = event_id[event_name[idx]] elif isinstance(events, (str, Path, PathLike)): events = read_events(events)