From 09e3539dd02e38cb91b8a48875fcdd44d99ece50 Mon Sep 17 00:00:00 2001 From: "Daniel G, Krakowczyk" Date: Thu, 14 Sep 2023 10:11:42 +0200 Subject: [PATCH] feat: Add events to gaze integration functions --- src/pymovements/gaze/integration.py | 5 ++++ tests/gaze/integration_numpy_test.py | 2 +- tests/gaze/integration_pandas_test.py | 43 +++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/pymovements/gaze/integration.py b/src/pymovements/gaze/integration.py index 87671e335..d075739d7 100644 --- a/src/pymovements/gaze/integration.py +++ b/src/pymovements/gaze/integration.py @@ -262,6 +262,8 @@ def from_numpy( def from_pandas( data: pd.DataFrame, experiment: Experiment | None = None, + events: pm.EventDataFrame | None = None, + *, time_column: str | None = None, pixel_columns: list[str] | None = None, position_columns: list[str] | None = None, @@ -276,6 +278,8 @@ def from_pandas( Data represented as a pandas DataFrame. experiment : Experiment The experiment definition. + events: EventDataFrame + A dataframe of events in the gaze signal. time_column: The name of the timestamp column in the input data frame. pixel_columns: @@ -295,6 +299,7 @@ def from_pandas( return GazeDataFrame( data=df, experiment=experiment, + events=events, time_column=time_column, pixel_columns=pixel_columns, position_columns=position_columns, diff --git a/tests/gaze/integration_numpy_test.py b/tests/gaze/integration_numpy_test.py index c94ab66f6..ed61afedf 100644 --- a/tests/gaze/integration_numpy_test.py +++ b/tests/gaze/integration_numpy_test.py @@ -189,7 +189,7 @@ def test_from_numpy_all_none(): @pytest.mark.parametrize( - ('events'), + 'events', [ pytest.param( None, diff --git a/tests/gaze/integration_pandas_test.py b/tests/gaze/integration_pandas_test.py index ae7f34bde..082c3b85b 100644 --- a/tests/gaze/integration_pandas_test.py +++ b/tests/gaze/integration_pandas_test.py @@ -20,6 +20,7 @@ """Test from gaze.from_pandas.""" import pandas as pd import polars as pl +import pytest from polars.testing import assert_frame_equal import pymovements as pm @@ -71,3 +72,45 @@ def test_from_pandas_explicit_columns(): }) assert_frame_equal(gaze.frame, expected) + + +@pytest.mark.parametrize( + ('df', 'events'), + [ + pytest.param( + pd.DataFrame(), + None, + id='events_none', + ), + + pytest.param( + pd.DataFrame(), + pm.EventDataFrame(), + id='events_empty', + ), + + pytest.param( + pd.DataFrame(), + pm.EventDataFrame(name='fixation', onsets=[123], offsets=[345]), + id='fixation', + ), + + pytest.param( + pd.DataFrame(), + pm.EventDataFrame(name='saccade', onsets=[34123], offsets=[67345]), + id='saccade', + ), + + ], +) +def test_from_pandas_events(df, events): + if events is None: + expected_events = pm.EventDataFrame().frame + else: + expected_events = events.frame + + gaze = pm.gaze.from_pandas(data=df, events=events) + + assert_frame_equal(gaze.events.frame, expected_events) + # We don't want the events point to the same reference. + assert gaze.events.frame is not expected_events