Skip to content

Commit

Permalink
ENH: Merge Close flags
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-huberty committed Nov 7, 2023
1 parent a7842b2 commit 3c14357
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions pylossless/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,14 +645,26 @@ def add_pylossless_annotations(self, inds, event_type, epochs):
"""
# Concatenate epoched data back to continuous data
t_onset = epochs.events[inds, 0] / epochs.info["sfreq"]
df = pd.DataFrame(t_onset, columns=["onset"])
# We exclude the last sample from the duration because
# if the annot lasts the whole duration of the epoch
# it's end will coincide with the first sample of the
# next epoch, causing it to erroneously be rejected.
duration = np.ones_like(t_onset) / epochs.info["sfreq"] * len(epochs.times[:-1])
description = [f"bad_pylossless_{event_type}"] * len(t_onset)
df["duration"] = 1 / epochs.info["sfreq"] * len(epochs.times[:-1])
df["description"] = f"bad_pylossless_{event_type}"

# Merge close onsets to prevent a bunch of 1-second annotations of the same name
# find onsets close enough to be considered the same
df["close"] = df.sort_values("onset")["onset"].diff().le(1)
df["group"] = ~df["close"]
df["group"] = df["group"].cumsum()
# group the close onsets and merge them
df["onset"] = df.groupby("group")["onset"].transform("first")
df["duration"] = df.groupby("group")["duration"].transform("sum")
df = df.drop_duplicates(subset=["onset", "duration"])

annotations = mne.Annotations(
t_onset, duration, description, orig_time=self.raw.annotations.orig_time
df["onset"], df["duration"], df["description"], orig_time=self.raw.annotations.orig_time
)
self.raw.set_annotations(self.raw.annotations + annotations)

Expand Down

0 comments on commit 3c14357

Please sign in to comment.