pymovements.Gaze.measure_events_ratio#
- Gaze.measure_events_ratio(name: str, time_column: str = 'time', *, sampling_rate: float | None = None, onset_column: str = 'onset', offset_column: str = 'offset', trial_columns: list[str] | None = None) Expr[source]#
Calculate ratio of time associated with specific events.
This method computes the ratio of time that is associated with events having a specific name. It calculates the ratio from event durations (offset - onset). Overlapping intervals of events with the matching
nameare merged before their durations are summed, so each time point is counted only once and the resulting ratio never exceeds 1.0.The ratio is calculated inclusively as:
\[\frac{\sum_{i=1}^{m} (t_{\mathrm{offset},i} - t_{\mathrm{onset},i} + \Delta t)}{t_{\mathrm{max}} - t_{\mathrm{min}} + \Delta t}\]where the sum runs over the \(m\) merged (non-overlapping) intervals and \(\Delta t = 1000 / f_s\). If no sampling rate is available (neither passed as sampling_rate nor via the experiment), \(\Delta t\) is estimated as the mode of the sample time differences.
- Parameters:
name (str) – Name of events to include in the ratio calculation.
time_column (str) – Name of the timestamp column in the samples data. (default: ‘time’)
sampling_rate (float | None) – Sampling rate of the gaze data in Hz. If not provided, it will be taken from the experiment if available.
onset_column (str) – Name of the column containing event onset times (default: ‘onset’).
offset_column (str) – Name of the column containing event offset times (default: ‘offset’).
trial_columns (list[str] | None) – Names of the columns identifying trials to use for grouping the ratio calculation. Defaults to
trial_columns. Pass an empty list[]to compute a single session-level ratio without any trial grouping. (default: the gaze’strial_columns)
- Returns:
A Polars expression that calculates the event ratio. Use with select(), with_columns(), or group_by().agg() for per-trial ratios.
- Return type:
polars.Expr
Examples
Session-level ratio:
>>> import polars >>> import pymovements as pm >>> gaze = pm.Gaze( ... samples=polars.DataFrame({ ... 'time': [0, 1, 2, 3], ... 'pixel': [[0, 0], [1, 1], [2, 2], [3, 3]], ... }), ... events=pm.Events( ... name=['blink'], ... onsets=[1], ... offsets=[3], ... ), ... ) >>> gaze.samples.select(gaze.measure_events_ratio('blink')) shape: (1, 1) ┌───────────────────┐ │ event_ratio_blink │ │ --- │ │ f64 │ ╞═══════════════════╡ │ 0.75 │ └───────────────────┘
By default, the ratio is grouped by the gaze’s
trial_columns:>>> gaze = pm.Gaze( ... samples=polars.DataFrame({ ... 'time': [0, 1, 2, 3], ... 'trial': [1, 1, 2, 2], ... 'pixel': [[0, 0], [1, 1], [2, 2], [3, 3]], ... }), ... events=pm.Events( ... data=polars.DataFrame({ ... 'name': ['blink'], ... 'onset': [1], ... 'offset': [2], ... 'trial': [1], ... }), ... ), ... trial_columns=['trial'], ... ) >>> gaze.samples.group_by('trial', maintain_order=True).agg( ... gaze.measure_events_ratio('blink').first(), ... ) shape: (2, 2) ┌───────┬───────────────────┐ │ trial ┆ event_ratio_blink │ │ --- ┆ --- │ │ i64 ┆ f64 │ ╞═══════╪═══════════════════╡ │ 1 ┆ 1.0 │ │ 2 ┆ 0.0 │ └───────┴───────────────────┘
Pass
trial_columns=[]to ignore the gaze’strial_columnsand compute a single session-level scalar:>>> gaze.samples.select(gaze.measure_events_ratio('blink', trial_columns=[])).item() 0.5
Pass custom
trial_columnsto override the gaze’strial_columns:>>> gaze.samples.group_by('trial', maintain_order=True).agg( ... gaze.measure_events_ratio('blink', trial_columns=['trial']).first(), ... ) shape: (2, 2) ┌───────┬───────────────────┐ │ trial ┆ event_ratio_blink │ │ --- ┆ --- │ │ i64 ┆ f64 │ ╞═══════╪═══════════════════╡ │ 1 ┆ 1.0 │ │ 2 ┆ 0.0 │ └───────┴───────────────────┘
- Raises:
ValueError – If name is not a non-empty string.
TypeError – If time_column is not a string.
KeyError – If time_column is not present in the samples DataFrame.