How thuhci/ringtool works
1. Repository purpose and execution model
RingTool is a Python-based research toolkit for processing smart-ring physiological signals and benchmarking multiple estimation methods. The top-level entrypoint is main.py, which orchestrates runs from JSON configuration files rather than hard-coded pipelines. Most architecture diagrams should center on this config-driven execution path.
2. Core data source and file contract
The system expects a dataset directory containing per-subject .pkl files with synchronized multimodal signals and labels. Each record can include PPG channels (ir, red, bvp), IMU axes (ax, ay, az), respiratory signals, and scalar targets such as HR, RR, SpO2, and blood pressure. This dataset boundary is the main input to the pipeline.
3. Dataset loading and preprocessing boundary
The dataset package is the ingestion layer. dataset/load_dataset.py and dataset/preprocess.py likely handle reading the .pkl records, selecting channels, windowing, normalization, filtering, and feature preparation. This layer transforms raw sensor time series into model-ready tensors or signal segments, and it is shared by both physics-based and supervised branches.
4. Two major analysis branches
The architecture splits into:
- Unsupervised / physics-based methods in unsupervised/
- Supervised deep-learning methods in nets/ plus training logic in trainer/
These branches operate on the same preprocessed inputs but produce estimates through very different mechanisms, which is an important architectural boundary.
5. Physics-based signal processing subsystem
The unsupervised package contains task-specific implementations for HR, RR, and SpO2:
- unsupervised/hr/hr.py
- unsupervised/rr/rr.py
- unsupervised/spo2/spo2.py
These modules implement classical algorithms such as peak detection, FFT-based frequency estimation, and ratio-based SpO2 calculation. They are configuration-selected alternatives to ML models rather than intermediate preprocessing steps.
6. Supervised model subsystem
The nets package contains reusable neural backbones for regression on physiological signals:
- resnet.py
- inception_time.py
- transformer.py
- mamba2.py
Supporting files like base_model.py and se_layer.py provide shared model abstractions or building blocks. nets/load_model.py appears to map config choices to concrete model classes. This subsystem is the model layer in the architecture.
7. Training, validation, and evaluation orchestration
The trainer package contains the training engine; trainer/load_trainer.py suggests a factory or selector for trainer variants. From the README, the training protocol includes subject-independent cross-validation, early stopping, best-epoch selection, and regression metrics such as MAE, RMSE, MAPE, and Pearson correlation. The trainer sits between preprocessed data and model execution.
8. Experiment configuration and selection logic
The config/ tree is a large experiment matrix organized by:
- method family: physical-based, supervised, only_test
- ring type: ring1, ring2
- modality/target combinations: hr, rr, spo2, bp, plus variants like ir, irimu, irredimu
- scenario splits: all-scenes, motion-scenes, stationary-scenes
These JSON files are the main source of experiment metadata and determine which dataset slices, channels, models, and evaluation modes are run.
9. Shared constants and model/dataset taxonomy
The constants package defines canonical enums and mappings for datasets, experiments, and models. This layer prevents the rest of the code from relying on ad hoc strings and provides the repository’s internal taxonomy for ring type, target task, and supported architectures.
10. Utility and feature-engineering helpers
The utils package contains common functionality, including accel_features.py for accelerometer-derived features and general helpers in utils.py. utils/rename_dir.py suggests filesystem normalization or dataset organization support. These are supporting modules rather than primary architectural nodes, but they affect input preparation and reproducibility.
11. Notification and operational integration
The notifications package adds an optional Slack notification boundary for long-running experiments. It is not part of core signal processing, but it is part of the runtime workflow for batch training jobs launched from main.py.
12. Outputs, artifacts, and repository workflow
The repo produces multiple artifacts: logs, figures, visualizations, generated CSVs, and model outputs under folders like logs/, csv/, img/, visualizations/, and possibly nets/ or trainer/-driven checkpoints. The .github workflows and scripts enforce repository hygiene, including config naming and CSV validation, which matters for keeping the experiment matrix consistent.
13. External technology dependencies that shape architecture
The implementation is Python-centric and built around PyTorch. The README also calls out mamba-ssm and Triton, which matter because the Mamba backbone depends on them and can constrain the runtime/CUDA environment. This means the supervised branch is not just framework-agnostic Python; it has GPU/runtime coupling.
14. How to draw the main system diagram
A clear diagram should show: raw ring dataset in .pkl files feeding a preprocessing layer, then branching into either physics-based algorithms or supervised training/inference. The supervised branch should show config-driven model selection, training/evaluation, and metric reporting. Optional side paths should include Slack notifications and generated artifacts/visualizations.
Main entry points: main.py Main entry · unsupervised Physics methods · preprocess.py Preprocess · load_trainer.py Trainer
Components 18
Runtime pipeline
Config-driven execution from entrypoint through data, methods, and outputs.
- Main entry · Launches runs from JSON configs and coordinates the selected experiment path.
main.py - Ring data · Per-subject synchronized multimodal signal records and scalar targets.
- Load data · Reads subject records and constructs dataset slices for downstream processing.
dataset/load_dataset.py - Preprocess · Channel selection, filtering, windowing, normalization, and feature preparation.
dataset/preprocess.py - Physics methods · Classical estimation path for HR, RR, and SpO2 using signal processing rules.
unsupervised - HR calc · Heart-rate estimation via peak detection and frequency-domain methods.
unsupervised/hr/hr.py - RR calc · Respiration-rate estimation from respiratory or derived signals.
unsupervised/rr/rr.py - SpO2 calc · Oxygen-saturation estimation from red/IR channel relationships.
unsupervised/spo2/spo2.py - Supervised branch · Neural regression path combining model selection, training, validation, and testing.
nets - Model factory · Maps config choices to a concrete backbone implementation.
nets/load_model.py - Trainer · Runs optimization, validation, early stopping, best-epoch selection, and metrics.
trainer/load_trainer.py - Backbones · Reusable sequence regressors including ResNet, InceptionTime, Transformer, and Mamba2.
nets
Experiment matrix
JSON configurations and taxonomy that select tasks, rings, scenes, and models.
Support layers
Shared utilities, notifications, and repository hygiene.
- Helpers · Shared helpers for acceleration features, filesystem normalization, and common functions.
utils - Slack notify · Optional Slack alerts for long-running experiment jobs.
notifications/slack.py - Artifacts · Logs, CSVs, images, figures, and visualizations produced by experiments.
logs - Repo checks · Workflow and scripts that validate JSON naming and CSV outputs.
.github
Connections 19
- Main entry → Configs: loads · Main reads experiment metadata from JSON configs.
- Main entry → Taxonomy: uses · Execution relies on shared dataset and model taxonomy.
- Main entry → Load data: starts · Entrypoint invokes the data ingestion layer.
- Load data → Ring data: reads · Loads per-subject multimodal .pkl records.
- Load data → Preprocess: passes · Raw records are transformed into model-ready inputs.
- Preprocess → Physics methods: feeds · Preprocessed signals can be routed to classical estimators.
- Preprocess → Supervised branch: feeds · Preprocessed signals can also feed supervised training and inference.
- Physics methods → HR calc: selects · Task config chooses HR estimation logic.
- Physics methods → RR calc: selects · Task config chooses RR estimation logic.
- Physics methods → SpO2 calc: selects · Task config chooses SpO2 estimation logic.
- Supervised branch → Model factory: selects · Config chooses the backbone implementation.
- Model factory → Backbones: instantiates · Factory builds the concrete neural architecture.
- Supervised branch → Trainer: runs · Training engine coordinates optimization and evaluation.
- Trainer → Backbones: executes · Trainer performs forward and backward passes through the model.
- Trainer → Artifacts: writes · Metrics, logs, figures, and outputs are persisted.
- Main entry → Slack notify: notifies · Optional operational alerts for batch runs.
- Repo checks → Configs: validates · Repository checks enforce configuration naming and CSV conventions.
- Helpers → Preprocess: supports · Utility functions contribute feature engineering and data handling.
- Helpers → Trainer: supports · Shared helpers are used in training and experiment execution.