Skip to content

Commit

Permalink
FIX: eric suggestions and fix test
Browse files Browse the repository at this point in the history
Co-authored-by: Eric Larson <[email protected]>
  • Loading branch information
scott-huberty and larsoner committed Jan 27, 2024
1 parent 00ab0db commit bcb7994
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions mne/preprocessing/eyetracking/tests/test_eyetracking.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import numpy as np
import pytest
from numpy.testing import assert_allclose

import mne
from mne._fiff.constants import FIFF
from mne.utils import _record_warnings


def test_set_channel_types_eyetrack(eyetrack_raw):
Expand All @@ -22,7 +24,7 @@ def test_convert_units(eyetrack_raw, eyetrack_cal):
mne.preprocessing.eyetracking.convert_units(raw, cal, "radians")
assert raw.info["chs"][0]["unit"] == FIFF.FIFF_UNIT_RAD
# Gaze was to center of screen, so x-coord and y-coord should now be 0 radians
np.testing.assert_allclose(raw.get_data(picks=[0, 1]), 0)
assert_allclose(raw.get_data(picks=[0, 1]), 0)

# Should raise an error if we try to convert to radians again
with pytest.raises(ValueError, match="Data must be in"):
Expand All @@ -39,29 +41,32 @@ def test_convert_units(eyetrack_raw, eyetrack_cal):
mne.preprocessing.eyetracking.convert_units(raw, cal, "pixels")

# Finally, check that we raise other errors or warnings when we should
with pytest.warns(UserWarning, match="Could not"):
raw_misc = raw.copy()
# warn if no eyegaze channels found
raw_misc = raw.copy()
with _record_warnings(): # channel units change warning
raw_misc.set_channel_types({ch: "misc" for ch in raw_misc.ch_names})
with pytest.warns(UserWarning, match="Could not"):
mne.preprocessing.eyetracking.convert_units(raw_misc, cal, "radians")

# raise an error if the calibration is missing a key
bad_cal = cal.copy()
bad_cal.pop("screen_size")
bad_cal["screen_distance"] = None
with pytest.raises(KeyError, match="Calibration object must have the following"):
bad_cal = cal.copy()
bad_cal.pop("screen_size")
bad_cal["screen_distance"] = None
mne.preprocessing.eyetracking.convert_units(raw, bad_cal, "radians")

with pytest.raises(UserWarning, match="Some visual angle values"):
cal_tmp = cal.copy()
cal_tmp["screen_distance"] = 0.1
raw_tmp = raw.copy()
raw_tmp._data[0, :10] = 1900 # gaze to extremity of screen
np.testing.assert_allclose(raw_tmp.get_data(picks=[0])[..., :10], 1900)
# warn if visual angle is too large
cal_tmp = cal.copy()
cal_tmp["screen_distance"] = 0.1
raw_tmp = raw.copy()
raw_tmp._data[0, :10] = 1900 # gaze to extremity of screen
with pytest.warns(UserWarning, match="Some visual angle values"):
mne.preprocessing.eyetracking.convert_units(raw_tmp, cal_tmp, "radians")
np.testing.assert_allclose(raw_tmp.get_data(picks=[0])[..., :10], 1.09867434)

# raise an error if channel locations not set
raw_missing = raw.copy()
raw_missing.info["chs"][0]["loc"] = np.zeros(12)
with pytest.raises(ValueError, match="loc array not set"):
raw_missing = raw.copy()
raw_missing.info["chs"][0]["loc"] = np.zeros(12)
mne.preprocessing.eyetracking.convert_units(raw_missing, cal, "radians")


Expand Down

0 comments on commit bcb7994

Please sign in to comment.