# 인합글라이워드코어 (InHospGlyWard-Kor)

> **참고용·연구용** 도구 — 임상 의사결정용이 아닙니다 (Not for clinical decision making).
> 모든 데이터는 de-identified surrogate (synthetic) 이며 실제 PHI 가 포함되어 있지 않습니다.

종합병원 **입원 환자의 혈당관리**를 ward 단위(MICU/SICU/CCU/NICU/응급실/일반병동) 로
한 화면에서 추적하는 standalone Streamlit / Plotly 코호트 대시보드.

- 도메인: **DM (당뇨병)** — 입원 환자 glycemic management
- 카테고리: **웹기반 대시보드 (코호트 모니터링·KPI 추적)**
- idx: **1** (2026-05-27 첫 번째 빌드)

---

## 5개 핵심 기능

1. **POCT BG + 인슐린 protocol + 환자 attribute ingest**
   환자 ID·연령·DM type·입원 ward·LOS·POCT BG timestamp+value·insulin order
   (basal / bolus / correction / sliding-scale / IV infusion)·oral 당뇨약·steroid·TPN/PN·
   perioperative·DKA/HHS dx·discharge insulin·readmission CSV/EDW export 를
   표준화 + de-identification + ward 별 attribution. (`modules/ingest.py`)

2. **Ward별 ADA/KDA inpatient TIR + basal-bolus vs sliding-scale**
   ADA Standards of Care 2025 inpatient(TIR 100-180 일반병동 / 140-180 ICU),
   hypoglycemia(<70 / <54 / <40), hyperglycemia(>180 / >250 / >300 / >400),
   persistent hyperglycemia(>180 ≥6h) ward별 KPI radar +
   basal-bolus / sliding-scale-only / IV infusion 비율 + KDA 입원당뇨 가이드 부합 score.
   (`modules/tir.py`, `modules/regimen.py`)

3. **DKA / HHS / Hypo / Hyper episode trajectory + protocol 부합**
   DKA / HHS 입원 episode 별 trajectory(IV insulin rate · K+ supp · fluid · anion gap ·
   bicarb · glucose · resolution time · post-resolution transition · LOS · 재발)
   ADA DKA/HHS protocol · KDA 입원당뇨 가이드 부합률 · resolution time 분포 · complication flag.
   (`modules/episode.py`)

4. **Subgroup: perioperative / steroid / TPN-PN / ER glycemia**
   perioperative · steroid-induced · TPN/PN-related · 응급실 immediate glycemia
   (NICE-SUGAR / DIGAMI / GIK protocol 부합) subgroup KPI. (`modules/subgroup.py`)

5. **Discharge transition + 30-day readmission + SHM GCMIP analog 리포트**
   discharge insulin · oral 당뇨약 transition · 30-day readmission(DKA / HHS / Hypo / Hyper 사유)
   Kaplan-Meier · SHM GCMIP analog 한국어/영문 quality improvement 리포트 (`md` + `docx`) +
   ward 별 ranking · trend chart export. (`modules/transition.py`, `modules/report.py`)

---

## 기술 스택

Python: streamlit, pandas, numpy, plotly, scikit-learn, **lifelines**(KM), statsmodels(mixed-effects),
matplotlib, python-docx. 오프라인 standalone. de-identification audit trail.

## 데이터 소스

- 사이트의 manual / REDCap / EDW export
  (환자 ID · ward · POCT BG · insulin order · oral 당뇨약 · steroid · TPN/PN ·
  DKA/HHS dx · LOS · discharge · readmission)
- 내장 reference: **ADA Standards of Care 2025 (Diabetes Care in the Hospital), KDA 2023 입원당뇨,
  AACE Inpatient Glycemic Management, SHM GCMIP measure set,
  NICE-SUGAR / DIGAMI / GIK protocol**
- 외부 유료 API **0**, EMR 실시간 연동 **0** (nightly / weekly EDW export 가정)

---

## 실행법

```bash
# 가상환경
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# 1) 합성 데이터 생성 (320명, ward 8개, ~10k POCT)
python3 main.py --gen-data --n 320

# 2) 분석 + KPI ranking
python3 main.py --analyze --top 8

# 3) 한국어 KPI 리포트 (docx + md)
python3 main.py --report --lang ko --out reports/inhospglyward_ko.docx

# 4) 일괄 실행 (gen-data -> analyze -> report)
python3 main.py --all

# 5) Streamlit 대시보드
streamlit run app.py
```

## 데이터 디렉토리 (`data/`)

| 파일 | 컬럼 |
|---|---|
| `patients.csv` | patient_id, age, sex, dm_type, ward, admit_day, discharge_day, los_days, perioperative, steroid_exposure, tpn_pn, discharge_insulin, readmit_30d, readmit_day, readmit_reason |
| `poct_bg.csv` | patient_id, study_day, hour, glucose_mg_dl |
| `insulin_orders.csv` | patient_id, study_day, regimen, basal_units, bolus_units, correction_units, iv_rate_uhr |
| `episodes.csv` | patient_id, episode_type, start_day, duration_h, anion_gap_init, bicarb_init, glucose_init, k_supplement, fluid_l_24h, iv_insulin_rate_uhr, resolved, resolution_h, transition_regimen |

## 검수 체크리스트

- [x] Python 구문 (`python3 -c "import ast; ast.parse(open(...).read())"`) — main.py, app.py, modules/*.py
- [x] `python3 main.py --help` — argparse 만으로도 실행 (heavy deps 누락 friendly message)
- [x] CSV 로드 가능 (`csv.DictReader`)
- [x] `python3 main.py --all` 합성 데이터 + 분석 + 리포트 end-to-end (표준 lib + python-docx 만으로도 fallback md 생성)
- [x] de-identification audit trail (`modules/ingest.py` `IngestReport.deid_method` + notes)
- [x] 모든 리포트 상단에 **참고용·연구용** 디스클레이머
- [x] 외부 네트워크 호출 **0**, 외부 유료 API **0**, 전역 패키지 설치 **0**

## 출처 / 참고문헌

- American Diabetes Association. **Standards of Care in Diabetes — 2025** (Section 16: Diabetes Care in the Hospital).
- 대한당뇨병학회. **2023 당뇨병 진료지침** — 입원 환자 당뇨병 관리.
- AACE / ACE Consensus Statement on Inpatient Glycemic Control.
- SHM **Glycemic Control Mentored Implementation Program (GCMIP)** measure set.
- The NICE-SUGAR Study Investigators. NEJM 2009;360:1283-97.
- DIGAMI / DIGAMI-2, GIK perioperative glycemia protocols.

## 면책

본 도구는 임상가/연구자 보조용입니다. 실 환자 진료 결정에 단독 사용하지 마십시오.
정확한 임상 의사결정은 담당의의 임상 판단과 최신 가이드라인에 따라야 합니다.
