[7f9fb8]: / mne / preprocessing / eyetracking / tests / test_pupillometry.py

Download this file

74 lines (65 with data), 3.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import numpy as np
import pytest
from mne import create_info
from mne.annotations import _annotations_starts_stops
from mne.datasets.testing import data_path, requires_testing_data
from mne.io import RawArray, read_raw_eyelink
from mne.preprocessing.eyetracking import interpolate_blinks
fname = data_path(download=False) / "eyetrack" / "test_eyelink.asc"
pytest.importorskip("pandas")
@requires_testing_data
@pytest.mark.parametrize(
"buffer, match, cause_error, interpolate_gaze, crop",
[
(0.025, "BAD_blink", False, False, False),
(0.025, "BAD_blink", False, True, True),
((0.025, 0.025), ["random_annot"], False, False, False),
(0.025, "BAD_blink", True, False, False),
],
)
def test_interpolate_blinks(buffer, match, cause_error, interpolate_gaze, crop):
"""Test interpolating pupil data during blinks."""
raw = read_raw_eyelink(fname, create_annotations=["blinks"], find_overlaps=True)
if crop:
raw.crop(tmin=2)
assert raw.first_time == 2.0
# Create a dummy stim channel
# this will hit a certain line in the interpolate_blinks function
info = create_info(["STI"], raw.info["sfreq"], ["stim"])
stim_data = np.zeros((1, len(raw.times)))
stim_raw = RawArray(stim_data, info)
raw.add_channels([stim_raw], force_update_info=True)
# Get the indices of the first blink
blink_starts, blink_ends = _annotations_starts_stops(raw, "BAD_blink")
blink_starts = np.divide(blink_starts, raw.info["sfreq"])
blink_ends = np.divide(blink_ends, raw.info["sfreq"])
first_blink_start = blink_starts[0]
first_blink_end = blink_ends[0]
if match == ["random_annot"]:
msg = "No annotations matching"
with pytest.warns(RuntimeWarning, match=msg):
interpolate_blinks(raw, buffer=buffer, match=match)
return
if cause_error:
# Make an annotation without ch_names info
raw.annotations.append(onset=1, duration=1, description="BAD_blink")
with pytest.raises(ValueError):
interpolate_blinks(raw, buffer=buffer, match=match)
return
else:
interpolate_blinks(
raw, buffer=buffer, match=match, interpolate_gaze=interpolate_gaze
)
# Now get the data and check that the blinks are interpolated
data, times = raw.get_data(return_times=True)
# Get the indices of the first blink
blink_ind = np.where((times >= first_blink_start) & (times <= first_blink_end))[0]
# pupil data during blinks are zero, check that interpolated data are not zeros
assert not np.any(data[2, blink_ind] == 0) # left eye
assert not np.any(data[5, blink_ind] == 0) # right eye
if interpolate_gaze:
assert not np.isnan(data[0, blink_ind]).any() # left eye
assert not np.isnan(data[1, blink_ind]).any() # right eye