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 fromaois. Fixation order is taken from theonsetcolumn; if it is absent, the row order offixationsis 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
aoisis 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
aoisframe 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_indexpreserves the word indexing ofaois(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-1for 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
LPis the one-based character position of the first fixation within the word (the word’s first character is 1), computed from thechar_idxcolumns of fixations and AOI table. Words that were never fixated get 0. If either input has nochar_idxcolumn, or the word’s first fixation carries a nullchar_idxvalue,LPis null.- Parameters:
fixations (pl.DataFrame) – DataFrame with fixation data, containing the column specified by
word_index_columnand adurationcolumn.aois (pl.DataFrame | dict[Any, pl.DataFrame]) – DataFrame with AOI data, containing the columns specified by
word_index_columnandword_column. Alternatively a dict mapping values of the first group column to such DataFrames.word_index_column (str) – Shared column name in
fixationsandaoisthat corresponds to the word index of the text. (default:'word_idx')word_column (str) – Column in
aoiswith the content within each AOI. (default:'word')event_name (str) – Name of the fixation events to compute measures from. Rows of
fixationswith a differentnameare dropped. Iffixationshas nonamecolumn, 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
Noneor 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_columnscontains 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 theaoisdict 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_indexindex of the word as given in the AOI definitions. The indexing of
aoisis preserved, whether zero-based, one-based, or any other scheme.wordthe word (AOI) within the text.
FFDFirst Fixation Duration: duration of the first fixation on the word during the first pass, see
first_fixation_duration().SFDSingle Fixation Duration: equal to
FFDif the word received exactly one fixation during the first pass, 0 otherwise.FDFixation Duration: duration of the first fixation on the word, regardless of reading pass, see
first_duration().FPRTFirst Pass Reading Time: sum of all fixation durations on the word during the first pass, see
first_pass_reading_time().FPFCFirst Pass Fixation Count: number of fixations on the word during the first pass, see
first_pass_fixation_count().FRTFirst Reading Time: total dwell time from first entering the word until first leaving it, see
first_reading_time().TFTTotal Fixation Time: total fixation time on the word (
FPRT + RRT).RRTRereading Time: sum of fixation durations on the word after the first pass, see
rereading_time().RPD_incRegression-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_excRegression-Path Duration, exclusive: time spent on regressed words within the regression-path window, excluding fixations on the word itself, see
regression_path_duration_exclusive().RBRTRight-Bounded Reading Time: sum of fixation durations on the word before any word to its right is fixated, see
right_bounded_reading_time().FixFixation Indicator: 1 if the word was fixated (
TFT > 0).skippedSkipping Indicator: 1 if the word was never fixated (
TFC == 0).FPFFirst Pass Fixation Indicator: 1 if the word was fixated during the first pass (
FPRT > 0).RRRereading Indicator: 1 if the word was reread (
RRT > 0).FPRegRegression Indicator: 1 if a regression was initiated from the word (
RPD_exc > 0).TRC_outTotal Regression Count, outgoing: number of regressions going out of the word, see
regression_count_out().TRC_inTotal Regression Count, ingoing: number of regressions landing on the word, see
regression_count_in().SL_inSaccade 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_outSaccade 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().LPLanding 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 nochar_idxvalue, seelanding_position().TFCTotal 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 bymap_to_aois()already carry the word index and duration columns.