Basic Fixation Extraction

This example demonstrates how to extract fixations from raw gaze data using the I-DT (Identification by Dispersion-Threshold) algorithm, combined with smoothing filters.

 1"""
 2Basic Fixation Extraction Example
 3=================================
 4
 5This example demonstrates how to extract fixations from raw gaze data using the I-DT
 6(Identification by Dispersion-Threshold) algorithm. We use a subset of gaze data provided
 7in the library.
 8"""
 9
10import pandas as pd
11from sklearn.pipeline import Pipeline
12
13from eyefeatures.preprocessing.fixation_extraction import IDT
14from eyefeatures.preprocessing.smoothing import SavGolFilter, WienerFilter
15
16# Load sample data
17gazes_df = pd.read_csv("data/gazes/gazes_subset.csv")
18print("Loaded gazes data:", gazes_df.shape)
19
20# column names
21x, y, t = "norm_pos_x", "norm_pos_y", "gaze_timestamp"
22pk = ["Participant", "tekst"]
23
24# Initialize IDT algorithm
25fixation_extractor = IDT(
26    x=x,
27    y=y,
28    t=t,
29    min_duration=0.08,  # seconds
30    max_duration=2.0,  # seconds
31    max_dispersion=0.05,  # Distance units (normalized data)
32    pk=pk,
33)
34
35# Initialize smoothing algorithms
36w_filter = WienerFilter(x=x, y=y, t=t, pk=pk, K="auto")
37sg_filter = SavGolFilter(x=x, y=y, t=t, pk=pk, window_length=11)
38
39# Initialize a pipeline gazes -> fixations
40pipe = Pipeline(
41    steps=[
42        ("w_filter", w_filter),  # Wiener
43        ("sg_filter", sg_filter),  # Savitzkiy-Golay
44        ("fixation_extractor", fixation_extractor),  # IDT
45    ]
46)
47
48# Run the pipeline
49fixations_smooth = pipe.fit_transform(gazes_df)
50
51print("\nResulting Fixations DataFrame:")
52print(fixations_smooth.head())
53print(f"\nShape: {fixations_smooth.shape}")
54print("\nColumns:", list(fixations_smooth.columns))