Skip to content

Commit

Permalink
deal with edge cases for events/annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jackz314 committed Aug 5, 2022
1 parent 5d90ff4 commit 1b4b647
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion eeglabio/epochs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import numpy as np
from numpy.core.records import fromarrays
from scipy.io import savemat
Expand Down Expand Up @@ -82,9 +84,9 @@ def export_set(fname, data, sfreq, events, tmin, tmax, ch_names, event_id=None,
ev_types = [event_type_d[ev[2]] for ev in events]
else:
ev_types = [str(ev[2]) for ev in events]
ev_types = np.array(ev_types)

# EEGLAB latency, in units of data sample points
# ev_lat = [int(n) for n in self.events[:, 0]]
# ensure same int type (int64) as duration
ev_lat = events[:, 0].astype(np.int64)

Expand All @@ -93,6 +95,10 @@ def export_set(fname, data, sfreq, events, tmin, tmax, ch_names, event_id=None,

# indices of epochs each event belongs to
ev_epoch = ev_lat // data.shape[1] + 1
if len(ev_epoch) > 0 and max(ev_epoch) > trials: # probably due to shifted/wrong events latency
# attempt to fix it by reverting to simple arange
ev_epoch = np.arange(1, trials + 1)
warnings.warn("Invalid event latencies, ignoring for export.")

# merge annotations into events array
if annotations is not None:
Expand Down
2 changes: 1 addition & 1 deletion eeglabio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def export_mne_epochs(inst, fname):
else:
cart_coords = None

if len(inst.annotations) > 0:
if inst.annotations is not None and len(inst.annotations) > 0:
annot = [inst.annotations.description, inst.annotations.onset,
inst.annotations.duration]
else:
Expand Down

0 comments on commit 1b4b647

Please sign in to comment.