| name | time-series-consistency |
| description | Detect temporal distribution shifts (change points) in clinical data across years using GradientBoosting classifier + SHAP. For each year, trains a model to distinguish year-1 vs year data โ AUROC โฅ 0.8 indicates a change point. No API key required. Use for LYDUS data quality assessment of temporal consistency across data collection periods. |
| tier | community |
| category | lydus |
| parameters | {"quiq_path":{"description":"Path to QUIQ-format CSV file. Must contain data spanning multiple years with non-null Event_date.","type":"string"},"save_path":{"description":"Directory path to save output files and plots.","type":"string"}} |
Time Series Consistency
Detects temporal distribution shifts (change points) in clinical data by training a GradientBoosting classifier to distinguish consecutive year pairs. If AUROC โฅ 0.8, the distribution between year-1 and year differs significantly โ indicating a change point (e.g., coding practice change, dataset collection change).
When to Use This Skill
- After QUIQ conversion, to detect whether clinical variable distributions have shifted over time
- To identify years with sudden changes in documentation or coding patterns
- To assess longitudinal stability of data collection
- As part of LYDUS quality management assessment
SQL Support
Not applicable. Requires sklearn GradientBoostingClassifier and SHAP.
Categories and Pivot Logic
| Category | Filter | Pivot Column |
|---|
| Event | Mapping_info_1 contains event AND Mapping_info_2 contains lab_event | Variable_name (binary presence per patient-day) |
| Diagnosis | Mapping_info_1 contains diagnosis | Value (binary presence per patient-day) |
| Prescription | Mapping_info_1 contains prescription AND Mapping_info_2 contains drug | Value |
| Procedure | Mapping_info_1 contains procedure | Value |
Change Point Detection Algorithm
For each year in [min_year+1, max_year-1]:
- Select records from
year-1 (label=0) and year (label=1)
- Skip if: no data, class imbalance (
min_class / total < 0.25), or too few samples
- Train
GradientBoostingClassifier (50/50 train/test split, stratified)
- Compute AUROC on test set
- AUROC โฅ 0.8 โ change point; save SHAP bar plot
Time Series Consistency (%) = (total_years โ change_points) / total_years ร 100
Output
| File | Description |
|---|
time_series_consistency_total.txt | Overall Consistency (%), Total Time Points, Change Points |
time_series_consistency_summary.csv | Per-category: Total_time_point, Change_point, Time Series Consistency (%) |
{Category} - AUROC plot.png | Year vs AUROC line plot with change points in red |
{Category} ({Year}) - SHAP Plot.png | SHAP bar plot for each detected change point |
How to Run
import pandas as pd
from scripts.time_series_consistency import get_time_series_consistency
quiq = pd.read_csv("/path/to/quiq.csv")
save_path = "/path/to/output"
total_results, summary = get_time_series_consistency(quiq, save_path)
valid = summary.dropna(subset=['Total_time_point'])
total_tp = valid['Total_time_point'].sum()
change_pt = valid['Change_point'].sum()
consistency = round((total_tp - change_pt) / total_tp * 100, 2)
print(f"Time Series Consistency (%) = {consistency}")
print(summary)
As a script with config
quiq_path: /path/to/quiq.csv
save_path: /path/to/output
python scripts/time_series_consistency.py --config config.yaml
Critical Notes
-
Multi-year data required โ needs at least 3 distinct years per category (min_year+1 to max_year-1). Single-year or two-year data returns no results.
-
์๋ณธ ์ฝ๋ ๋ฒ๊ทธ ์์ (Prescription ํํฐ):
- ์๋ณธ:
Mapping_info_1.str.contains('drug') โ Mapping_info_1='prescription'์ด๋ผ ํญ์ ๋น ๊ฒฐ๊ณผ
- ์์ :
Mapping_info_2.str.contains('drug') โ ์ฒ๋ฐฉ ์ฝ๋ฌผ๋ช
ํ๋ง ์ฌ๋ฐ๋ฅด๊ฒ ํํฐ
-
Change point โ data error โ AUROC โฅ 0.8์ ์ฐ๋๊ฐ ๋ถํฌ ์ฐจ์ด๊ฐ ํฌ๋ค๋ ์๋ฏธ. ์ค์ ์์ ๋ณํ(์ ๊ท ์ฝ๋ฌผ, ICD ๊ฐ์ ๋ฑ)๊ฐ ์์ธ์ผ ์ ์์ผ๋ฏ๋ก ๋งฅ๋ฝ ํด์ ํ์.
-
SettingWithCopyWarning ์์ โ ํํฐ๋ ์ฌ๋ผ์ด์ค์ ์ง์ ์ปฌ๋ผ ์ถ๊ฐํ๋ ์๋ณธ ์ฝ๋ โ ๋ชจ๋ .copy() ํ ์ฒ๋ฆฌ.
-
๋ฆฌํฉํฐ๋ง โ 4๊ฐ ์นดํ
๊ณ ๋ฆฌ์ ๊ฑฐ์ ๋์ผํ ๋ธ๋ก โ _run_category + _build_pivot_and_years + _detect_change_points ํจ์๋ก ํตํฉ.
-
Dependencies โ scikit-learn, shap, pandas, numpy, matplotlib
References
- LYDUS ํ์ง๊ด๋ฆฌ ํ๋ก๊ทธ๋จ ํ์ฉ ๊ฐ์ด๋๋ผ์ธ (๋น๊ณต๊ฐ ๋ด๋ถ ๋ฌธ์)
- Original Python implementation: LYDUS_Time_Series_Consistency.py (์ด์ฑ๋ฏผ ์์ฑ)
- Lundberg, S.M., Lee, S-I. (2017). A Unified Approach to Interpreting Model Predictions. NeurIPS.