| name | fidelity |
| description | Calculate structured fidelity for clinical variables in a QUIQ-format table. Measures how frequently each clinical concept (event, diagnosis, prescription, procedure) appears per patient. Use for data quality assessment of clinical record completeness and recording pattern consistency. |
| tier | community |
| category | lydus |
| parameters | {"quiq_path":{"description":"Path to QUIQ-format CSV file (output of quiq skill).","type":"string"},"save_path":{"description":"Directory path to save output files (fidelity_total.txt, fidelity_summary.csv).","type":"string"}} |
Fidelity
Calculates structured fidelity for clinical variables in a QUIQ-format table. Measures the average per-patient recording frequency for each clinical concept, grouped by category (Mapping_info_1).
When to Use This Skill
- After QUIQ conversion, to assess how faithfully clinical events are recorded per patient
- To identify variables with unusually high or low recording frequency
- As part of LYDUS quality management assessment
Categories and Filtering
| Category | Mapping_info_1 | Is_categorical | Grouped by Value |
|---|
| Event | contains event | any | โ (์ ์ฒด ๋น๋๋ง) |
| Diagnosis | contains diagnosis | = 1 | โ
|
| Prescription | contains prescription | = 1 | โ
|
| Procedure | contains procedure | = 1 | โ
|
- Event๋ ๊ฐ ๊ตฌ๋ถ ์์ด ํ์๋ณ ๋ฐ์ ํ์๋ง ์ง๊ณ
- ๋๋จธ์ง 3๊ฐ ์นดํ
๊ณ ๋ฆฌ๋
(Variable_name, Value) ์กฐํฉ๋ณ๋ก ์ง๊ณ
Metrics
(Original_table_name, Variable_name[, Value]) ๊ทธ๋ฃน๋ณ:
| ์ปฌ๋ผ | ์๋ฏธ |
|---|
Patient_num | ํด๋น ํญ๋ชฉ์ ๊ฐ์ง ํ์ ์ |
Mean | ํ์๋ณ ํ๊ท ๊ธฐ๋ก ํ์ |
Std | ํ์๋ณ ๊ธฐ๋ก ํ์์ ํ์คํธ์ฐจ |
Weighted Fidelity = ฮฃ(Patient_num ร Mean) / ฮฃ(Patient_num)
Output
| File | Description |
|---|
fidelity_total.txt | Weighted Fidelity (์ ์ฒด ๊ฐ์ค ํ๊ท ๋น๋) |
fidelity_summary.csv | ์นดํ
๊ณ ๋ฆฌ๋ณ ์ ์ฒด ๊ฒฐ๊ณผ |
How to Run
import os
import duckdb
skill_dir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(skill_dir, "scripts/duckdb.sql")) as f:
sql = f.read()
quiq_csv = "/path/to/quiq_3patients.csv"
sql = sql.replace("{quiq_csv}", quiq_csv)
df = duckdb.sql(sql).df()
total_num = df["Patient_num"].sum()
weighted_fidelity = round(
(df["Patient_num"] * df["Mean"]).sum() / total_num, 2
)
print(f"Weighted Fidelity = {weighted_fidelity}")
save_path = "/path/to/output"
os.makedirs(save_path, exist_ok=True)
df.to_csv(f"{save_path}/fidelity_summary.csv", index=False, encoding="utf-8-sig")
with open(f"{save_path}/fidelity_total.txt", "w") as f:
f.write(f"Weighted Fidelity = {weighted_fidelity}\n")
print(f"Saved {len(df):,} rows โ {save_path}")
As a script with config
quiq_path: /path/to/quiq.csv
save_path: /path/to/output
python scripts/fidelity.py --config config.yaml
Critical Notes
-
Event Value = NULL โ Event ์นดํ
๊ณ ๋ฆฌ๋ ๊ฐ ์ข
๋ฅ์ ๋ฌด๊ดํ๊ฒ ๋ฐ์ ๋น๋๋ง ์ง๊ณํ๋ฏ๋ก Value ์ปฌ๋ผ์ด NULL.
-
Is_categorical ํ์
โ ์๋ณธ ์ฝ๋์์ df['Is_categorical'] == 1 ๋น๊ต ์ ํ์
๋ณํ ์์. ์คํฌ ๋ฒ์ ์์ pd.to_numeric(..., errors='coerce') ๋ฐ SQL TRY_CAST ๋ก ์์ .
-
Std = NULL โ ํ์๊ฐ 1๋ช
์ธ ๊ทธ๋ฃน์ ํ์คํธ์ฐจ๊ฐ NULL (์ํ ํ์คํธ์ฐจ ddof=1).
-
Weighted Fidelity ํด์ โ ๊ฐ์ด ํด์๋ก ํ์ 1์ธ๋น ํ๊ท ์ ์ผ๋ก ๋ ๋ง์ ๊ธฐ๋ก์ด ์์. ์ ์ ๋ฒ์๋ ๋ฐ์ดํฐ์
๊ณผ ๊ธฐ๊ด์ ๋ฐ๋ผ ๋ค๋ฆ.
References
- LYDUS ํ์ง๊ด๋ฆฌ ํ๋ก๊ทธ๋จ ํ์ฉ ๊ฐ์ด๋๋ผ์ธ (๋น๊ณต๊ฐ ๋ด๋ถ ๋ฌธ์)
- Original Python implementation: LYDUS_Fidelity.py (์ด์ฑ๋ฏผ ์์ฑ)