kollar-ts
v0.2.0
Published
TypeScript port of kollaR - Eye tracking data analysis library with I-VT, I-DT, I2MC, and Adaptive fixation detection algorithms
Downloads
124
Maintainers
Readme
kollar-ts
TypeScript port of kollaR - Eye tracking data analysis library
Overview
kollar-ts is a TypeScript port of the R package kollaR by Johan Lundin Kleberg. It provides scientific algorithms for analyzing eye tracking data, including fixation detection, saccade analysis, and Area of Interest (AOI) analysis.
Features
4 Fixation Detection Algorithms:
- I-VT: Velocity-based algorithm
- I-DT: Dispersion-based algorithm
- I2MC: Two-means clustering algorithm
- Adaptive: Adaptive velocity threshold algorithm
Preprocessing Pipeline:
- Gap interpolation with margin-based approach
- Moving average smoothing
- RMS (precision) calculation
- Downsampling for robustness
Analysis Tools:
- Area of Interest (AOI) testing
- Sample-to-sample precision metrics (RMSD)
- Velocity profile analysis
- Fixation merging and trimming
Full TypeScript Support:
- Strict type checking
- Comprehensive type definitions
- IDE autocomplete support
Installation
npm install kollar-tsQuick Start
import { preprocessGaze, calculateRMS } from 'kollar-ts';
// Your raw gaze data from eye tracker
const rawData = [
{ timestamp: 0, x: 512, y: 384 },
{ timestamp: 1.67, x: 513, y: 385 },
// ... more samples
];
// Preprocess: interpolate gaps and smooth
const processed = preprocessGaze(rawData, {
maxGapMs: 75, // Interpolate gaps up to 75ms
marginMs: 5, // Use 5ms margin for interpolation
filterMs: 15, // 15ms moving average window
});
// Calculate precision metric
const rms = calculateRMS(processed, 'x', 'y', 40);
console.log(`Sample-to-sample precision: ${rms.toFixed(3)} degrees`);Fixation Detection
import { algorithmIVT, algorithmIDT, algorithmI2MC, algorithmAdaptive } from 'kollar-ts';
// I-VT: Velocity-based detection
const ivtResult = algorithmIVT(processed, {
velocityThreshold: 35, // degrees/second
minFixationDuration: 40, // milliseconds
});
// I-DT: Dispersion-based detection
const idtResult = algorithmIDT(processed, {
dispersionThreshold: 1.0, // degrees
minDuration: 40, // milliseconds
});
// I2MC: Clustering-based detection (robust to noise)
const i2mcResult = algorithmI2MC(processed, {
windowLengthMs: 200,
downsamplingFactors: [2, 5, 10],
});
// Adaptive: Data-driven threshold
const adaptiveResult = algorithmAdaptive(processed, {
onsetThresholdSd: 3,
saveVelocityProfiles: true,
});
console.log(`Detected ${ivtResult.fixations.length} fixations`);Development Status
Version 0.1.0 - Ready for initial release! ✅
✅ Complete Features
- [x] Type System: Full TypeScript support with strict mode
- [x] Preprocessing Pipeline: Interpolation, smoothing, RMS calculation, downsampling
- [x] Fixation Detection Algorithms:
- [x] I-VT (Velocity-based)
- [x] I-DT (Dispersion-based)
- [x] I2MC (Clustering-based)
- [x] Adaptive (Data-driven thresholds)
- [x] Fixation Utilities: Summarize, merge, trim
- [x] AOI Analysis: Classification, dwell time, latency, transitions
- [x] Comprehensive Testing: 257 tests with 91.64% coverage
- [x] Working Examples: 7 complete examples demonstrating all features (including real data demo)
- [x] Documentation: Complete API documentation and usage guides
Project Structure
kollar-ts/
├── src/
│ ├── types/ # TypeScript type definitions
│ ├── utils/ # Utility functions (math, stats, RLE, rolling)
│ ├── preprocessing/ # Data preprocessing pipeline
│ ├── core/ # Core fixation detection algorithms
│ ├── analysis/ # AOI and analysis functions
│ └── index.ts # Main exports
├── test/
│ ├── unit/ # Unit tests
│ ├── integration/ # Integration tests
│ └── validation/ # Validation against R
├── examples/ # Usage examples
├── docs/ # Documentation
├── LICENSE # GPL-3.0 license
└── CITATION.md # Citation informationAttribution
This TypeScript implementation is a port of the R package kollaR by Johan Lundin Kleberg.
Original R Package
- Author: Johan Lundin Kleberg
- Email: [email protected]
- Organization: Stockholm University
- Repository: https://github.com/drjohanlk/kollaR
- License: GPL-3.0
Research Citations
The algorithms in this library are based on peer-reviewed research:
I-VT and I-DT Algorithms
Salvucci, D. D., & Goldberg, J. H. (2000). Identifying fixations and saccades in eye-tracking protocols. Proceedings of the 2000 symposium on Eye tracking research & applications, 71-78. DOI: 10.1145/355017.355028
I2MC Algorithm
Hessels, R. S., Niehorster, D. C., Kemner, C., & Hooge, I. T. C. (2017). Noise-robust fixation detection in eye movement data: Identification by two-means clustering (I2MC). Behavior Research Methods, 49(5), 1802-1823. DOI: 10.3758/s13428-016-0822-1
Adaptive Velocity Threshold
Nyström, M., & Holmqvist, K. (2010). An adaptive algorithm for fixation, saccade, and glissade detection in eyetracking data. Behavior Research Methods, 42(1), 188-204. DOI: 10.3758/BRM.42.1.188
Please cite both the original R package and the relevant research papers when using this library.
See CITATION.md for complete citation information.
API Documentation
Preprocessing
import { preprocessGaze, calculateRMS } from 'kollar-ts';
// Main preprocessing pipeline
const processed = preprocessGaze(rawData, {
maxGapMs: 75, // Max gap to interpolate (ms)
marginMs: 5, // Margin for interpolation (ms)
filterMs: 15, // Moving average window (ms)
xcol: 'x', // X coordinate column name
ycol: 'y', // Y coordinate column name
naIgnore: true, // Ignore NAs when filtering
});
// Calculate precision (RMS)
const rms = calculateRMS(processed, 'x', 'y', 40);Utilities
import { mean, median, sd, mad } from 'kollar-ts/utils';
import { rollmean, rollmedian } from 'kollar-ts/utils';
import { rle, findRuns } from 'kollar-ts/utils';
// Statistical functions
const m = mean([1, 2, 3, null, 4], true); // true = remove NAs
const med = median([1, 2, 10, 4, 5], true);
// Rolling operations
const smoothed = rollmean(data, 5, 'center'); // 5-sample window
// Run-length encoding
const encoded = rle([1, 1, 2, 2, 2, 3]);
// { lengths: [2, 3, 1], values: [1, 2, 3] }Examples
The examples/ directory contains complete, runnable examples demonstrating all features:
Algorithm Examples
- examples/i-vt.ts - I-VT velocity-based fixation detection
- examples/i-dt.ts - I-DT dispersion-based fixation detection
- examples/i2mc.ts - I2MC clustering-based algorithm (robust to noise)
- examples/adaptive.ts - Adaptive data-driven threshold algorithm
- examples/aoi-analysis.ts - Area of Interest (AOI) analysis and gaze transitions
Real Data Demo
examples/real-data-demo.ts - Complete pipeline demonstration using real eye tracking data
# Run the real data demo
npx tsx examples/real-data-demo.tsThis example shows:
- Loading real gaze data from JSON
- Preprocessing (interpolation, smoothing, RMS calculation)
- Running all 4 fixation detection algorithms
- Comparing algorithm results
- Performance analysis and spatial distribution
The demo uses sample data representative of Tobii Pro Spectrum recordings at 1200 Hz. For full datasets from the original R package, see examples/data/README.md for export instructions.
Development
# Install dependencies
npm install
# Build
npm run build
# Run tests
npm test
# Watch mode for tests
npm run test:watch
# Type checking
npm run typecheck
# Linting
npm run lintContributing
Contributions are welcome! This project maintains scientific accuracy as its top priority. All algorithm implementations must match the R package behavior and be validated against R outputs.
Guidelines
- Scientific Accuracy: All algorithms must be validated against R implementation
- Testing: Comprehensive unit and validation tests required
- Documentation: TSDoc comments for all public APIs
- Type Safety: Strict TypeScript mode, no
anytypes - Code Style: Follow existing patterns, use Prettier
License
GPL-3.0 - This project uses the same license as the original R package to ensure it remains open and accessible to the research community.
See LICENSE for full license text.
Roadmap
Phase 1: Foundation ✅ COMPLETE
- [x] Project setup and configuration
- [x] Type definitions
- [x] Utility functions
- [x] Preprocessing pipeline
Phase 2: Core Algorithms ✅ COMPLETE
- [x] Fixation utility functions
- [x] I-DT algorithm
- [x] I-VT algorithm
- [x] I2MC algorithm
- [x] Adaptive algorithm
Phase 3: Analysis & Validation ✅ COMPLETE
- [x] AOI analysis (11 functions)
- [x] Comprehensive unit tests (257 tests)
- [x] 91.64% test coverage
- [x] Integration testing
Phase 4: Documentation & Examples ✅ COMPLETE
- [x] Complete examples (6 examples)
- [x] Usage guides
- [x] API documentation
- [x] README and guides
Phase 5: Publication 🚀 READY
- [ ] npm publication (ready to publish)
- [ ] GitHub release (v0.1.0)
- [ ] Research community announcement
Future Enhancements (Post v0.1.0)
- [ ] R validation script integration
- [ ] Performance benchmarks vs R
- [ ] Algorithm-specific documentation pages
- [ ] CI/CD pipeline (GitHub Actions)
Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Original R Package: https://github.com/drjohanlk/kollaR
Citation
If you use kollar-ts in your research, please cite both this TypeScript implementation and the original R package. See CITATION.md for complete citation information.
Acknowledgments
This TypeScript port was created by H.Kaan Koyukan.
Special thanks to:
- Johan Lundin Kleberg for the original R implementation
- All researchers who developed and validated these eye tracking algorithms
- The eye tracking research community
Note: This is a scientific software project. Precision and accuracy are paramount. All algorithm implementations are validated against the original R package to ensure research reliability.
