pymovements.gaze.from_csv#
- pymovements.gaze.from_csv(file: str | Path, experiment: Experiment | None = None, *, trial_columns: list[str] | None = None, time_column: str | None = None, time_unit: str | None = 'ms', pixel_columns: list[str] | None = None, position_columns: list[str] | None = None, velocity_columns: list[str] | None = None, acceleration_columns: list[str] | None = None, distance_column: str | None = None, column_map: dict[str, str] | None = None, add_columns: dict[str, str] | None = None, column_dtypes: dict[str, Any] | None = None, **read_csv_kwargs: Any) GazeDataFrame #
Initialize a
pymovements.gaze.gaze_dataframe.GazeDataFrame
.- Parameters:
file (str | Path) – Path of gaze file.
experiment (Experiment | None) – The experiment definition. (default: None)
trial_columns (list[str] | None) – The name of the trial columns in the input data frame. If the list is empty or None, the input data frame is assumed to contain only one trial. If the list is not empty, the input data frame is assumed to contain multiple trials and the transformation methods will be applied to each trial separately. (default: None)
time_column (str | None) – The name of the timestamp column in the input data frame. (default: None)
time_unit (str | None) – The unit of the timestamps in the timespamp column in the input data frame. Supported units are ‘s’ for seconds, ‘ms’ for milliseconds and ‘step’ for steps. If the unit is ‘step’ the experiment definition must be specified. All timestamps will be converted to milliseconds. (default: ‘ms’)
pixel_columns (list[str] | None) – The name of the pixel position columns in the input data frame. These columns will be nested into the column
pixel
. If the list is empty or None, the nestedpixel
column will not be created. (default: None)position_columns (list[str] | None) – The name of the dva position columns in the input data frame. These columns will be nested into the column
position
. If the list is empty or None, the nestedposition
column will not be created. (default: None)velocity_columns (list[str] | None) – The name of the velocity columns in the input data frame. These columns will be nested into the column
velocity
. If the list is empty or None, the nestedvelocity
column will not be created. (default: None)acceleration_columns (list[str] | None) – The name of the acceleration columns in the input data frame. These columns will be nested into the column
acceleration
. If the list is empty or None, the nestedacceleration
column will not be created. (default: None)distance_column (str | None) – The name of the eye-to-screen distance column in the input data frame. If specified, the column will be used for pixel to dva transformations. If not specified, the constant eye-to-screen distance will be taken from the experiment definition. (default: None)
column_map (dict[str, str] | None) – The keys are the columns to read, the values are the names to which they should be renamed. (default: None)
add_columns (dict[str, str] | None) – Dictionary containing columns to add to loaded data frame. (default: None)
column_dtypes (dict[str, Any] | None) – Dictionary containing types for columns. (default: None)
**read_csv_kwargs (Any) – Additional keyword arguments to be passed to
polars.read_csv()
to read in the csv. These can include custom separators, a subset of columns, or specific data types for columns.
- Returns:
The gaze data frame read from the csv file.
- Return type:
Notes
About using the arguments
pixel_columns
,position_columns
,velocity_columns
, andacceleration_columns
:By passing a list of columns as any of these arguments, these columns will be merged into a single column with the corresponding name , e.g. using pixel_columns will merge the respective columns into the column pixel.
The supported number of component columns with the expected order are:
zero columns: No nested component column will be created.
two columns: monocular data; expected order: x-component, y-component
four columns: binocular data; expected order: x-component left eye, y-component left eye, x-component right eye, y-component right eye,
six columns: binocular data with additional cyclopian data; expected order: x-component left eye, y-component left eye, x-component right eye, y-component right eye, x-component cyclopian eye, y-component cyclopian eye,
Examples
First let’s assume a CSV file stored tests/files/monocular_example.csv with the following content: shape: (10, 3) ┌──────┬────────────┬────────────┐ │ time ┆ x_left_pix ┆ y_left_pix │ │ — ┆ — ┆ — │ │ i64 ┆ i64 ┆ i64 │ ╞══════╪════════════╪════════════╡ │ 0 ┆ 0 ┆ 0 │ │ 1 ┆ 0 ┆ 0 │ │ 2 ┆ 0 ┆ 0 │ │ 3 ┆ 0 ┆ 0 │ │ … ┆ … ┆ … │ │ 6 ┆ 0 ┆ 0 │ │ 7 ┆ 0 ┆ 0 │ │ 8 ┆ 0 ┆ 0 │ │ 9 ┆ 0 ┆ 0 │ └──────┴────────────┴────────────┘
We can now load the data into a
GazeDataFrame
by specyfing the experimental setting and the names of the pixel position columns. We can specify a custom separator for the csv file by passing it as a keyword argument topolars.read_csv()
:>>> from pymovements.gaze.io import from_csv >>> gaze = from_csv( ... file='tests/files/monocular_example.csv', ... time_column = 'time', ... time_unit='ms', ... pixel_columns = ['x_left_pix','y_left_pix'], ... separator = ',', ... ) >>> gaze.frame shape: (10, 2) ┌──────┬───────────┐ │ time ┆ pixel │ │ --- ┆ --- │ │ i64 ┆ list[i64] │ ╞══════╪═══════════╡ │ 0 ┆ [0, 0] │ │ 1 ┆ [0, 0] │ │ 2 ┆ [0, 0] │ │ 3 ┆ [0, 0] │ │ … ┆ … │ │ 6 ┆ [0, 0] │ │ 7 ┆ [0, 0] │ │ 8 ┆ [0, 0] │ │ 9 ┆ [0, 0] │ └──────┴───────────┘
Please be aware that data types are inferred from a fixed number of rows. To ensure correct data types, you can pass a dictionary of column names and data types to the dtypes keyword argument of
polars.read_csv()
:>>> from pymovements.gaze.io import from_csv >>> import polars as pl >>> gaze = from_csv( ... file='tests/files/monocular_example.csv', ... time_column = 'time', ... time_unit='ms', ... pixel_columns = ['x_left_pix','y_left_pix'], ... dtypes = {'time': pl.Int64, 'x_left_pix': pl.Int64, 'y_left_pix': pl.Int64}, ... ) >>> gaze.frame shape: (10, 2) ┌──────┬───────────┐ │ time ┆ pixel │ │ --- ┆ --- │ │ i64 ┆ list[i64] │ ╞══════╪═══════════╡ │ 0 ┆ [0, 0] │ │ 1 ┆ [0, 0] │ │ 2 ┆ [0, 0] │ │ 3 ┆ [0, 0] │ │ … ┆ … │ │ 6 ┆ [0, 0] │ │ 7 ┆ [0, 0] │ │ 8 ┆ [0, 0] │ │ 9 ┆ [0, 0] │ └──────┴───────────┘