Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated eeglab.py to account for SET files that contain trials but no… #12704

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions mne/io/eeglab/eeglab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down