compute_reading_measures#

pymovements.measure.reading.compute_reading_measures(fixations: DataFrame, aois: DataFrame | dict[Any, DataFrame], *, word_index_column: str = 'word_idx', word_column: str = 'word', event_name: str = 'fixation', group_columns: list[str] | None = None) → DataFrame[source]#

Compute reading measures from fixation sequences.

This function expects fixations annotated with AOI data. See map_to_aois() for further details.

The fixations are annotated with run- and pass-level information (see annotate_fixations()) and each reading measure is then aggregated per word and joined onto the word table derived from aois. Fixation order is taken from the onset column; if it is absent, the row order of fixations is used.

Independent reading sequences are kept apart via the group_columns. The first group column identifies the recording (trial role), the remaining group columns describe the stimulus layout (page role):

  • If both inputs carry a group column, it is used for grouping and preserved in the output.

  • If aois is a dict, its keys are the values of the first group column and each entry is the AOI table of that group. The fixations must then carry the first group column and every fixation group must have an entry in the dict. Dict entries without any matching fixations are kept in the output as fully skipped words (all measures zero, skipped = 1) and a warning names their keys.

  • If the fixations carry the first group column but a plain aois frame does not, the AOI table is broadcast to every fixation group and a warning is issued.

  • Any other one-sided group column raises a ValueError, as the join would otherwise silently produce all-zero measures.

  • If neither input carries a group column, the fixations are treated as a single sequence.

Fixation onsets that are not sorted within a group trigger a warning, as this usually means independent reading sequences were concatenated without a separating group column.

The returned word_index preserves the word indexing of aois (zero-based, one-based, or any other start), so word indices are not shifted. Fixations whose word index has no entry in the AOI table (for example sentinel values like -1 for out-of-text fixations) are treated like fixations with a null word index: they are excluded from the fixation sequence and do not affect any measure.

The landing position LP is the one-based character position of the first fixation within the word (the word’s first character is 1), computed from the char_idx columns of fixations and AOI table. Words that were never fixated get 0. If either input has no char_idx column, or the word’s first fixation carries a null char_idx value, LP is null.

Parameters:
  • fixations (pl.DataFrame) – DataFrame with fixation data, containing the column specified by word_index_column and a duration column.

  • aois (pl.DataFrame | dict[Any, pl.DataFrame]) – DataFrame with AOI data, containing the columns specified by word_index_column and word_column. Alternatively a dict mapping values of the first group column to such DataFrames.

  • word_index_column (str) – Shared column name in fixations and aois that corresponds to the word index of the text. (default: 'word_idx')

  • word_column (str) – Column in aois with the content within each AOI. (default: 'word')

  • event_name (str) – Name of the fixation events to compute measures from. Rows of fixations with a different name are dropped. If fixations has no name column, all rows are used. (default: 'fixation')

  • group_columns (list[str] | None) – Column names used to partition the data into independent reading sequences. The first column takes the trial role, the remaining columns take the page role (see above). If None or empty, the whole input is treated as a single reading sequence. (default: None)

Returns:

DataFrame with computed reading measures, one row per word, prefixed by the group columns that were present in the input.

Return type:

pl.DataFrame

Raises:

ValueError – If group_columns contains reserved column names, if a group column is present in only one of the inputs (except for the broadcast case described above), if its dtypes do not match, or if the aois dict is inconsistent with the fixation groups.

Notes

The output contains one row per word. Group columns present in the input are prepended, followed by these columns in order:

Column

Description

word_index

index of the word as given in the AOI definitions. The indexing of aois is preserved, whether zero-based, one-based, or any other scheme.

word

the word (AOI) within the text.

FFD

First Fixation Duration: duration of the first fixation on the word during the first pass, see first_fixation_duration().

SFD

Single Fixation Duration: equal to FFD if the word received exactly one fixation during the first pass, 0 otherwise.

FD

Fixation Duration: duration of the first fixation on the word, regardless of reading pass, see first_duration().

FPRT

First Pass Reading Time: sum of all fixation durations on the word during the first pass, see first_pass_reading_time().

FPFC

First Pass Fixation Count: number of fixations on the word during the first pass, see first_pass_fixation_count().

FRT

First Reading Time: total dwell time from first entering the word until first leaving it, see first_reading_time().

TFT

Total Fixation Time: total fixation time on the word (FPRT + RRT).

RRT

Rereading Time: sum of fixation durations on the word after the first pass, see rereading_time().

RPD_inc

Regression-Path Duration, inclusive: sum of all fixation durations from first entering the word until the first fixation to its right, including fixations on the word itself, see regression_path_duration_inclusive().

RPD_exc

Regression-Path Duration, exclusive: time spent on regressed words within the regression-path window, excluding fixations on the word itself, see regression_path_duration_exclusive().

RBRT

Right-Bounded Reading Time: sum of fixation durations on the word before any word to its right is fixated, see right_bounded_reading_time().

Fix

Fixation Indicator: 1 if the word was fixated (TFT > 0).

skipped

Skipping Indicator: 1 if the word was never fixated (TFC == 0).

FPF

First Pass Fixation Indicator: 1 if the word was fixated during the first pass (FPRT > 0).

RR

Rereading Indicator: 1 if the word was reread (RRT > 0).

FPReg

Regression Indicator: 1 if a regression was initiated from the word (RPD_exc > 0).

TRC_out

Total Regression Count, outgoing: number of regressions going out of the word, see regression_count_out().

TRC_in

Total Regression Count, ingoing: number of regressions landing on the word, see regression_count_in().

SL_in

Saccade Length, ingoing: signed word distance between the current word and the previously fixated word at the first fixation on the current word, see saccade_length_in().

SL_out

Saccade Length, outgoing: signed word distance from the current word to the next fixated word, measured at the last fixation of the first run, see saccade_length_out().

LP

Landing Position: one-based character position of the first fixation within the word (the word’s first character is 1), 0 if the word was never fixated, null when either input has no character-level (char_idx) data or the word’s first fixation carries no char_idx value, see landing_position().

TFC

Total Fixation Count: total number of fixations on the word, see total_fixation_count().

Examples

Four fixations over a three-word text, with a regression from the second word back to the first. The fixation table needs the fixated word index and the fixation duration, the AOI table maps word indices to words:

>>> import polars as pl
>>> from pymovements.measure.reading import compute_reading_measures
>>> fixations = pl.DataFrame({
...     'word_idx': [0, 1, 0, 2],
...     'duration': [220, 180, 140, 250],
... })
>>> aois = pl.DataFrame({
...     'word_idx': [0, 1, 2],
...     'word': ['The', 'quick', 'fox'],
... })
>>> measures = compute_reading_measures(fixations, aois)
>>> measures.select('word_index', 'word', 'FFD', 'TFT', 'TFC', 'TRC_in', 'skipped')
shape: (3, 7)
┌────────────┬───────┬─────┬─────┬─────┬────────┬─────────┐
│ word_index ┆ word  ┆ FFD ┆ TFT ┆ TFC ┆ TRC_in ┆ skipped │
│ ---        ┆ ---   ┆ --- ┆ --- ┆ --- ┆ ---    ┆ ---     │
│ i64        ┆ str   ┆ i64 ┆ i64 ┆ u64 ┆ u64    ┆ i64     │
╞════════════╪═══════╪═════╪═════╪═════╪════════╪═════════╡
│ 0          ┆ The   ┆ 220 ┆ 360 ┆ 2   ┆ 1      ┆ 0       │
│ 1          ┆ quick ┆ 180 ┆ 180 ┆ 1   ┆ 0      ┆ 0       │
│ 2          ┆ fox   ┆ 250 ┆ 250 ┆ 1   ┆ 0      ┆ 0       │
└────────────┴───────┴─────┴─────┴─────┴────────┴─────────┘

Fixations recorded across several trials or pages are kept apart with group_columns; fixations produced by map_to_aois() already carry the word index and duration columns.