| name | instance-diversity |
| description | Calculate instance diversity and Gini-Simpson index per (variable, value) combination in a QUIQ-format table. Measures how diversely distributed patients are across occurrences of each clinical value. Use for data quality assessment of recording pattern diversity in event, diagnosis, prescription, and procedure variables. |
| 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 (instance_diversity_total.txt, instance_diversity_summary.csv).","type":"string"}} |
Instance Diversity
Calculates instance diversity and Gini-Simpson index per (Original_table_name, Variable_name, Value) combination in a QUIQ-format table. Measures how evenly distributed patients are in terms of how many times they contribute to each clinical value.
When to Use This Skill
- After QUIQ conversion, to assess whether specific clinical values are dominated by a few patients (low diversity) or spread across many patients (high diversity)
- To detect recording bias โ e.g., one patient contributing disproportionately many records for a given value
- As part of LYDUS quality management assessment
Filtering Logic
| Category | Mapping_info_1 | Mapping_info_2 | Is_categorical |
|---|
| Event | contains event | any | = 1 |
| Diagnosis | contains diagnosis | any | = 1 |
| Prescription | contains prescription | contains drug | = 1 |
| Procedure | contains procedure | any | = 1 |
Metrics
For each (Original_table_name, Variable_name, Value) group:
- Per-patient occurrence count: group by
(ClassKey, Patient_id) โ Instance_Count per patient
- Instance Diversity (%):
num_patients / total_occurrences ร 100
- ๋์์๋ก ํ์๋น ํ๊ท ๋ฐ์ ํ์๊ฐ ์ ์ (์ฌ๋ฌ ํ์์ ๋ถ์ฐ)
- Gini-Simpson Index (%):
(1 - ฮฃpยฒ) ร 100
p = patients_with_count_k / total_occurrences
- ํ์๋ณ ๋ฐ์ ํ์ ๋ถํฌ์ Simpson ๋ค์์ฑ
instance_diversity == 1 (๋ชจ๋ ํ์ 1ํ ๋ฐ์) โ score = 100%
Weighted averages: weighted by total occurrence count across all ClassKey groups.
Output
| File | Description |
|---|
instance_diversity_total.txt | Weighted Instance Diversity (%), Weighted Gini-Simpson Index (%) |
instance_diversity_summary.csv | Per-(table, variable, value): num_patients, total_num, Instance Diversity (%), Gini-Simpson Index (%) |
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_items = df["total_num"].sum()
weighted_instance = round((df["total_num"] * df["Instance Diversity (%)"]).sum() / total_items, 2)
weighted_simpson = round((df["total_num"] * df["Gini-Simpson Index (%)"]).sum() / total_items, 2)
print(f"Weighted Instance Diversity (%) = {weighted_instance}")
print(f"Weighted Gini-Simpson Index (%) = {weighted_simpson}")
save_path = "/path/to/output"
os.makedirs(save_path, exist_ok=True)
df.to_csv(f"{save_path}/instance_diversity_summary.csv", index=False, encoding="utf-8-sig")
with open(f"{save_path}/instance_diversity_total.txt", "w") as f:
f.write(f"Weighted Instance Diversity (%) = {weighted_instance}\n")
f.write(f"Weighted Gini-Simpson Index (%) = {weighted_simpson}\n")
print(f"Saved {len(df):,} rows โ ")
As a script with config
quiq_path: /path/to/quiq.csv
save_path: /path/to/output
python scripts/instance_diversity.py --config config.yaml
Critical Notes
-
Class Diversity vs Instance Diversity โ class diversity๋ ํ ๋ณ์ ๋ด ๊ฐ๋ค์ ๋ถํฌ๋ฅผ ์ธก์ ; instance diversity๋ ํน์ ๊ฐ์ ๊ธฐ์ฌํ๋ ํ์๋ค์ ๋ฐ์ ๋น๋ ๋ถํฌ๋ฅผ ์ธก์ .
-
Prescription ํํฐ โ Mapping_info_2 LIKE '%drug%' ์กฐ๊ฑด์ด ์ถ๊ฐ๋จ (์ฒ๋ฐฉ ์ ๋ณด ์ค ์ฝ๋ฌผ๋ช
๋ง ๋์).
-
์๋ณธ ์คํ ์์ โ 'Weighted Instance Diveristy (%)' โ 'Weighted Instance Diversity (%)'
-
None ์ฒ๋ฆฌ โ ํ์๊ฐ ์๊ฑฐ๋ ์ด ๋ฐ์ ์๊ฐ 0์ธ ClassKey๋ None์ผ๋ก ๋ฐํ๋จ. ๊ฐ์ค ํ๊ท ๊ณ์ฐ ์ 0์ผ๋ก ์ฒ๋ฆฌ.
References
- LYDUS ํ์ง๊ด๋ฆฌ ํ๋ก๊ทธ๋จ ํ์ฉ ๊ฐ์ด๋๋ผ์ธ (๋น๊ณต๊ฐ ๋ด๋ถ ๋ฌธ์)
- Original Python implementation: LYDUS_Instance_Diversity.py (์ด์ฑ๋ฏผ ์์ฑ)
- Simpson, E.H. (1949). Measurement of Diversity. Nature, 163, 688.