Skip to content

Commit c0487f1

Browse files
author
v01dxyz
committed
Remove hardcoded value "BAD_blink" in _interpolate_blinks (#13531)
In order to have `_interpolate_blinks` and `_annotations_starts_stops` don't have the same matching behaviour, we split `_annotations_starts_stops` into two functions: * one that does the grunt of the work using annotation indices * another one that calls the former with annotations matching the argument.
1 parent b82bf18 commit c0487f1

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

mne/annotations.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,14 +1244,25 @@ def _annotations_starts_stops(raw, kinds, name="skip_by_annotation", invert=Fals
12441244
for kind in kinds:
12451245
_validate_type(kind, "str", "All entries")
12461246

1247-
if len(raw.annotations) == 0:
1247+
idxs = [
1248+
idx
1249+
for idx, desc in enumerate(raw.annotations.description)
1250+
if any(desc.upper().startswith(kind.upper()) for kind in kinds)
1251+
]
1252+
return _annotations_starts_stops_by_idxs(
1253+
raw,
1254+
idxs,
1255+
name,
1256+
invert,
1257+
)
1258+
1259+
1260+
def _annotations_starts_stops_by_idxs(
1261+
raw, idxs, name="skip_by_annotation", invert=False
1262+
):
1263+
if len(idxs) == 0 or len(raw.annotations) == 0:
12481264
onsets, ends = np.array([], int), np.array([], int)
12491265
else:
1250-
idxs = [
1251-
idx
1252-
for idx, desc in enumerate(raw.annotations.description)
1253-
if any(desc.upper().startswith(kind.upper()) for kind in kinds)
1254-
]
12551266
# onsets are already sorted
12561267
onsets = raw.annotations.onset[idxs]
12571268
onsets = _sync_onset(raw, onsets)

mne/preprocessing/eyetracking/_pupillometry.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66

77
from ..._fiff.constants import FIFF
8-
from ...annotations import _annotations_starts_stops
8+
from ...annotations import _annotations_starts_stops_by_idxs
99
from ...io import BaseRaw
1010
from ...utils import _check_preload, _validate_type, logger, warn
1111

@@ -59,11 +59,17 @@ def interpolate_blinks(raw, buffer=0.05, match="BAD_blink", interpolate_gaze=Fal
5959
match = [match]
6060

6161
# get the blink annotations
62-
blink_annots = [annot for annot in raw.annotations if annot["description"] in match]
63-
if not blink_annots:
62+
blink_annots_idxs = [
63+
idx
64+
for idx, annot in enumerate(raw.annotations)
65+
if annot["description"] in match
66+
]
67+
if not blink_annots_idxs:
6468
warn(f"No annotations matching {match} found. Aborting.")
6569
return raw
66-
_interpolate_blinks(raw, buffer, blink_annots, interpolate_gaze=interpolate_gaze)
70+
_interpolate_blinks(
71+
raw, buffer, blink_annots_idxs, interpolate_gaze=interpolate_gaze
72+
)
6773

6874
# remove bad from the annotation description
6975
for desc in match:
@@ -73,7 +79,7 @@ def interpolate_blinks(raw, buffer=0.05, match="BAD_blink", interpolate_gaze=Fal
7379
return raw
7480

7581

76-
def _interpolate_blinks(raw, buffer, blink_annots, interpolate_gaze):
82+
def _interpolate_blinks(raw, buffer, blink_annots_idxs, interpolate_gaze):
7783
"""Interpolate eyetracking signals during blinks in-place."""
7884
logger.info("Interpolating missing data during blinks...")
7985
pre_buffer, post_buffer = buffer
@@ -88,10 +94,10 @@ def _interpolate_blinks(raw, buffer, blink_annots, interpolate_gaze):
8894
continue
8995
# Create an empty boolean mask
9096
mask = np.zeros_like(raw.times, dtype=bool)
91-
starts, ends = _annotations_starts_stops(raw, "BAD_blink")
97+
starts, ends = _annotations_starts_stops_by_idxs(raw, blink_annots_idxs)
9298
starts = np.divide(starts, raw.info["sfreq"])
9399
ends = np.divide(ends, raw.info["sfreq"])
94-
for annot, start, end in zip(blink_annots, starts, ends):
100+
for annot, start, end in zip(raw.annotations[blink_annots_idxs], starts, ends):
95101
if "ch_names" not in annot or not annot["ch_names"]:
96102
msg = f"Blink annotation missing values for 'ch_names' key: {annot}"
97103
raise ValueError(msg)

0 commit comments

Comments
 (0)