Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/dev/14413.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :class:`~mne.preprocessing.eyetracking.Calibration` to accept array-like inputs, by `Ishaan Maheshwari`_.
15 changes: 13 additions & 2 deletions mne/preprocessing/eyetracking/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def __init__(
screen_distance=None,
screen_resolution=None,
):
if positions is not None:
positions = np.asarray(positions, dtype=float)
if offsets is not None:
offsets = np.asarray(offsets, dtype=float)
if gaze is not None:
gaze = np.asarray(gaze, dtype=float)
super().__init__(
onset=onset,
model=model,
Expand Down Expand Up @@ -137,8 +143,13 @@ def plot(self, show_offsets=True, axes=None, show=True):
from ...viz.utils import plt_show

msg = "positions and gaze keys must both be 2D numpy arrays."
assert isinstance(self["positions"], np.ndarray), msg
assert isinstance(self["gaze"], np.ndarray), msg
if (
not isinstance(self["positions"], np.ndarray)
or not isinstance(self["gaze"], np.ndarray)
or self["positions"].ndim != 2
or self["gaze"].ndim != 2
):
raise ValueError(msg)

if axes is not None:
from matplotlib.axes import Axes
Expand Down
101 changes: 41 additions & 60 deletions mne/preprocessing/eyetracking/tests/test_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,75 +33,46 @@
)


@pytest.mark.parametrize(
(
"onset, model, eye, avg_error, max_error, positions, offsets, gaze,"
" screen_size, screen_distance, screen_resolution"
),
[
(
0,
"H3",
"right",
0.5,
1.0,
POSITIONS,
OFFSETS,
GAZES,
(0.531, 0.298),
0.065,
(1920, 1080),
),
(None, None, None, None, None, None, None, None, None, None, None),
],
)
def test_create_calibration(
onset,
model,
eye,
avg_error,
max_error,
positions,
offsets,
gaze,
screen_size,
screen_distance,
screen_resolution,
):
@pytest.mark.parametrize("input_type", ["array", "list", None])
def test_create_calibration(input_type):
"""Test creating a Calibration object."""
kwargs = dict(
onset=onset,
model=model,
eye=eye,
avg_error=avg_error,
max_error=max_error,
positions=positions,
offsets=offsets,
gaze=gaze,
screen_size=screen_size,
screen_distance=screen_distance,
screen_resolution=screen_resolution,
kwargs_template = dict(
onset=0,
model="H3",
eye="right",
avg_error=0.5,
max_error=1.0,
positions=POSITIONS,
offsets=OFFSETS,
gaze=GAZES,
screen_size=(0.531, 0.298),
screen_distance=0.065,
screen_resolution=(1920, 1080),
)
kwargs = kwargs_template.copy()
if input_type is None:
kwargs = {k: None for k in kwargs}
elif input_type == "list":
for key in ("positions", "offsets", "gaze"):
kwargs[key] = kwargs[key].tolist()

cal = Calibration(**kwargs)
assert cal["onset"] == onset
assert cal["model"] == model
assert cal["eye"] == eye
assert cal["avg_error"] == avg_error
assert cal["max_error"] == max_error
if positions is not None:

for key in ("onset", "model", "eye", "avg_error", "max_error"):
assert cal[key] == kwargs[key]
if input_type is not None:
assert isinstance(cal["positions"], np.ndarray)
assert np.array_equal(cal["positions"], np.array(POSITIONS))
else:
assert cal["positions"] is None
if offsets is not None:
assert isinstance(cal["offsets"], np.ndarray)
assert np.array_equal(cal["offsets"], np.array(OFFSETS))
if gaze is not None:
assert isinstance(cal["gaze"], np.ndarray)
assert np.array_equal(cal["gaze"], np.array(GAZES))
assert cal["screen_size"] == screen_size
assert cal["screen_distance"] == screen_distance
assert cal["screen_resolution"] == screen_resolution
else:
assert cal["positions"] is None

assert cal["screen_size"] == kwargs["screen_size"]
assert cal["screen_distance"] == kwargs["screen_distance"]
assert cal["screen_resolution"] == kwargs["screen_resolution"]
# test copy method
copied_obj = cal.copy()
# Check if the copied object is an instance of Calibration
Expand All @@ -115,6 +86,16 @@ def test_create_calibration(
if cal["onset"] is not None:
assert repr(cal) == EXPECTED_REPR # test __repr__

# test plot
if cal["positions"] is not None:
import matplotlib.pyplot as plt

fig = cal.plot(show=False)
plt.close(fig)
else:
with pytest.raises(ValueError, match="positions and gaze keys"):
cal.plot(show=False)


@requires_testing_data
@pytest.mark.parametrize("fname", [(fname)])
Expand Down
Loading