Overview
STGNN supports time-aware prediction using temporal gap features that capture the time intervals between patient visits and to future prediction targets. This enables the model to account for varying prediction horizons and improve accuracy for both short-term and long-term forecasts.Temporal Gap Processing
Thetemporal_gap_processor.py module provides comprehensive utilities for parsing visit codes, calculating temporal gaps, and normalizing time features.
VISCODE Parsing
The system automatically parses ADNI visit codes to standardized month values:- Baseline codes:
sc,scmri,bl,init,v02,v03,v06 - ADNI-1/GO:
m03,m06,m12,m18,m24,m36,m48, etc. - ADNI-2:
v04(3m),v05(6m),v11(12m),v21(24m),v31(36m), etc. - ADNI-3:
y1,y2,y3,y4,y5
temporal_gap_processor.py:6-56 for the complete mapping.
Calculating Temporal Gaps
Usecalculate_temporal_gaps() to compute time intervals between consecutive visits:
- Out-of-order visit records (automatically sorted by time)
- Multiple subjects in a single dataframe
- Missing or irregular visit patterns
temporal_gap_processor.py:59-91
Time Normalization Methods
Four normalization strategies are available vianormalize_time_gaps():
1. Logarithmic (Default)
- Formula:
log(1 + months/12) - Range: Unbounded (typically 0-2 for 0-60 months)
- Best for: Capturing both short-term and long-term patterns
- Examples:
- 3 months → 0.22
- 6 months → 0.41
- 12 months → 0.69
- 24 months → 1.10
- 48 months → 1.61
2. Min-Max Scaling
- Formula:
clip(months / max_months, 0, 1) - Range: [0, 1]
- Best for: Linear time relationships
- Examples (max_months=60):
- 6 months → 0.10
- 12 months → 0.20
- 30 months → 0.50
- 60+ months → 1.00
3. Bucket Categorization
- Buckets:
- 0-6 months → 0.25
- 6-12 months → 0.50
- 12-24 months → 0.75
- 24+ months → 1.00
- Best for: Discrete time horizon modeling
- Use case: When prediction difficulty varies by time range
4. Raw Years
- Formula:
months / 12 - Range: Unbounded (in years)
- Best for: Interpretable units
- Examples:
- 6 months → 0.5 years
- 12 months → 1.0 years
- 36 months → 3.0 years
temporal_gap_processor.py:94-121
Usage in Training
Enable time-aware prediction with command-line arguments:Key Arguments
--use_time_features: Enable temporal gap features (default: False)--time_normalization: Normalization method -log,minmax,buckets, orraw(default:log)--single_visit_horizon: Default prediction horizon in months for subjects with only one visit (default: 6)--exclude_target_visit: Exclude the target visit from input sequences to prevent data leakage (recommended: True)
main.py:47-50 and dfc_main.py:50-53 for argument definitions.
Temporal Data Loading
TheTemporalDataLoader automatically handles time feature extraction:
- Extracts visit sequences per subject
- Parses VISCODE to months
- Computes gaps to next visit (or prediction target)
- Applies selected normalization
- Pads sequences to batch max length
main.py:236-249 and dfc_main.py:340-355
Horizon-Based Evaluation
When time features are enabled, the system evaluates performance by prediction horizon:main.py:382-463 and dfc_main.py:462-543 for implementation.
Temporal Distribution Analysis
Analyze the temporal distribution of your dataset:temporal_gap_processor.py:149-185
Best Practices
- Always use
exclude_target_visit=Truein training to prevent data leakage - Use
lognormalization for most cases - it balances short-term and long-term predictions - Set
single_visit_horizonbased on your dataset’s typical follow-up interval (6-12 months for ADNI) - Analyze temporal distribution before training to understand time range coverage
- Compare horizon-specific metrics to identify where the model struggles
Time Feature Integration
Time features are passed to the temporal model (LSTM/GRU/RNN) as part of the sequence:Implementation Files
temporal_gap_processor.py: Core time processing utilitiesTemporalDataLoader.py: Batch creation with time featuresmain.py: Static FC training with time featuresdfc_main.py: Dynamic FC training with time features