Update app.py
Browse files
app.py
CHANGED
|
@@ -105,6 +105,45 @@ def process_files(thuoc_file, xetnghiem_file, thuoc_plt_file, icd_plt_file):
|
|
| 105 |
drug_info = final_filtered_thuoc.sort_values(['makcb', 'ngaychidinh']).drop_duplicates('makcb')[['makcb', 'tenhh', 'hoatchat']]
|
| 106 |
final_report = pd.merge(final_report, drug_info, on='makcb', how='left')
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
# Tính num_no_nadir chỉ cho bệnh nhân đang nằm viện (Ngày ra NaN) và thiếu nadir
|
| 109 |
status_df = df_xetnghiem[['Mã KCB', 'Ngày ra']].drop_duplicates('Mã KCB')
|
| 110 |
status_df['Mã KCB'] = status_df['Mã KCB'].astype(str).str.replace(r'\.0$', '', regex=True).str.strip()
|
|
@@ -121,7 +160,7 @@ def process_files(thuoc_file, xetnghiem_file, thuoc_plt_file, icd_plt_file):
|
|
| 121 |
output_text += f"Số bệnh nhân cần can thiệp: {num_signal}\n" # Giả định signal là cần can thiệp
|
| 122 |
output_text += f"Số bệnh nhân cần khuyến cáo làm thêm xét nghiệm PLT: {num_no_nadir}\n"
|
| 123 |
|
| 124 |
-
# Xuất file
|
| 125 |
output_dir = "/tmp/outputs"
|
| 126 |
os.makedirs(output_dir, exist_ok=True)
|
| 127 |
|
|
@@ -133,16 +172,22 @@ def process_files(thuoc_file, xetnghiem_file, thuoc_plt_file, icd_plt_file):
|
|
| 133 |
check_icd[['makcb', 'hoten', 'maicd', 'chandoan']].to_excel(file2, index=False)
|
| 134 |
|
| 135 |
file3 = os.path.join(output_dir, '3_Check_PLT_Duoi_100.xlsx')
|
|
|
|
|
|
|
|
|
|
| 136 |
check_plt_low.to_excel(file3, index=False)
|
| 137 |
|
| 138 |
file4 = os.path.join(output_dir, '4_Check_Giao_Thoa_Thuoc_PLT.xlsx')
|
| 139 |
check_inter = df_plt[df_plt['Mã KCB'].isin(inter_ids)].copy()
|
|
|
|
|
|
|
|
|
|
| 140 |
check_inter.to_excel(file4, index=False)
|
| 141 |
|
| 142 |
file5 = os.path.join(output_dir, '5_Check_Baseline_Nadir_Full.xlsx')
|
| 143 |
final_analysis.to_excel(file5, index=False)
|
| 144 |
|
| 145 |
-
# Realtime report
|
| 146 |
def export_realtime_reports(df_report, df_xn, include_priority=True, include_recommendations=True):
|
| 147 |
today = pd.Timestamp.now(tz='Asia/Ho_Chi_Minh').normalize() # Sử dụng ngày hiện tại động với múi giờ VN
|
| 148 |
status_df = df_xn[['Mã KCB', 'Ngày vào', 'Ngày ra']].drop_duplicates('Mã KCB')
|
|
@@ -156,7 +201,8 @@ def process_files(thuoc_file, xetnghiem_file, thuoc_plt_file, icd_plt_file):
|
|
| 156 |
|
| 157 |
filerealtime = os.path.join(output_dir, 'Danh_Sach_Can_Thiep_DLS_2026.xlsx')
|
| 158 |
with pd.ExcelWriter(filerealtime) as writer:
|
| 159 |
-
cols = ['makcb', 'Họ tên', 'tenhh', 'hoatchat', 'PLT_baseline', 'Ngày_PLT_baseline', '
|
|
|
|
| 160 |
if include_priority: cols += ['Ngày vào', 'So_Ngay_Nam_Vien', 'Muc_Do_Cap_Bach']
|
| 161 |
if include_recommendations: cols += ['Mau_Cau_Khuyen_Cao']
|
| 162 |
|
|
@@ -179,9 +225,12 @@ def process_files(thuoc_file, xetnghiem_file, thuoc_plt_file, icd_plt_file):
|
|
| 179 |
thuoc = row['tenhh'] if pd.notna(row['tenhh']) else row['hoatchat']
|
| 180 |
plt_nen = row['PLT_baseline']
|
| 181 |
ngay = row['So_Ngay_Nam_Vien']
|
|
|
|
|
|
|
|
|
|
| 182 |
if "sụt giảm" in row['Signal'] or "Trầm trọng" in row['Signal']:
|
| 183 |
-
return (f"CẢNH BÁO ADR: Tiểu cầu bệnh nhân sụt giảm rõ rệt từ {plt_nen} G/L xuống còn {
|
| 184 |
-
f"sau khi dùng {thuoc}. Đề xuất bác sĩ xem xét tạm ngưng hoặc đổi thuốc nếu không có nguyên nhân khác.")
|
| 185 |
else:
|
| 186 |
return (f"Bệnh nhân đã điều trị {ngay} ngày và đang sử dụng {thuoc} (có nguy cơ giảm PLT). "
|
| 187 |
f"Kết quả PLT nền thấp ({plt_nen} G/L) và chưa có xét nghiệm kiểm tra lại. "
|
|
|
|
| 105 |
drug_info = final_filtered_thuoc.sort_values(['makcb', 'ngaychidinh']).drop_duplicates('makcb')[['makcb', 'tenhh', 'hoatchat']]
|
| 106 |
final_report = pd.merge(final_report, drug_info, on='makcb', how='left')
|
| 107 |
|
| 108 |
+
# Thêm phân loại CTCAE v5.0
|
| 109 |
+
def identify_ctcae_grade(plt_value):
|
| 110 |
+
if pd.isna(plt_value):
|
| 111 |
+
return "N/A"
|
| 112 |
+
if plt_value >= 150:
|
| 113 |
+
return "Bình thường"
|
| 114 |
+
elif 75 <= plt_value < 150:
|
| 115 |
+
return "Độ 1 (Nhẹ)"
|
| 116 |
+
elif 50 <= plt_value < 75:
|
| 117 |
+
return "Độ 2 (Trung bình)"
|
| 118 |
+
elif 25 <= plt_value < 50:
|
| 119 |
+
return "Độ 3 (Nặng)"
|
| 120 |
+
elif plt_value < 25:
|
| 121 |
+
return "Độ 4 (Đe dọa tính mạng)"
|
| 122 |
+
else:
|
| 123 |
+
return "Độ 5 (Tử vong)" # Nếu có dữ liệu tử vong, cần điều chỉnh thêm
|
| 124 |
+
|
| 125 |
+
final_analysis['CTCAE_Baseline'] = final_analysis['PLT_baseline'].apply(identify_ctcae_grade)
|
| 126 |
+
final_analysis['CTCAE_Nadir'] = final_analysis['PLT_nadir'].apply(identify_ctcae_grade)
|
| 127 |
+
final_report['CTCAE_Baseline'] = final_report['PLT_baseline'].apply(identify_ctcae_grade)
|
| 128 |
+
final_report['CTCAE_Nadir'] = final_report['PLT_nadir'].apply(identify_ctcae_grade)
|
| 129 |
+
|
| 130 |
+
def assess_danger(ctcae_grade):
|
| 131 |
+
if "Độ 1" in ctcae_grade:
|
| 132 |
+
return "Không nguy hiểm, theo dõi"
|
| 133 |
+
elif "Độ 2" in ctcae_grade:
|
| 134 |
+
return "Có nguy cơ, theo dõi chặt"
|
| 135 |
+
elif "Độ 3" in ctcae_grade:
|
| 136 |
+
return "Nguy hiểm, can thiệp"
|
| 137 |
+
elif "Độ 4" in ctcae_grade or "Độ 5" in ctcae_grade:
|
| 138 |
+
return "Rất nguy hiểm, cấp cứu"
|
| 139 |
+
else:
|
| 140 |
+
return "Không xác định"
|
| 141 |
+
|
| 142 |
+
final_analysis['Nguy_Hiem_Baseline'] = final_analysis['CTCAE_Baseline'].apply(assess_danger)
|
| 143 |
+
final_analysis['Nguy_Hiem_Nadir'] = final_analysis['CTCAE_Nadir'].apply(assess_danger)
|
| 144 |
+
final_report['Nguy_Hiem_Baseline'] = final_report['CTCAE_Baseline'].apply(assess_danger)
|
| 145 |
+
final_report['Nguy_Hiem_Nadir'] = final_report['CTCAE_Nadir'].apply(assess_danger)
|
| 146 |
+
|
| 147 |
# Tính num_no_nadir chỉ cho bệnh nhân đang nằm viện (Ngày ra NaN) và thiếu nadir
|
| 148 |
status_df = df_xetnghiem[['Mã KCB', 'Ngày ra']].drop_duplicates('Mã KCB')
|
| 149 |
status_df['Mã KCB'] = status_df['Mã KCB'].astype(str).str.replace(r'\.0$', '', regex=True).str.strip()
|
|
|
|
| 160 |
output_text += f"Số bệnh nhân cần can thiệp: {num_signal}\n" # Giả định signal là cần can thiệp
|
| 161 |
output_text += f"Số bệnh nhân cần khuyến cáo làm thêm xét nghiệm PLT: {num_no_nadir}\n"
|
| 162 |
|
| 163 |
+
# Xuất file với cột mới
|
| 164 |
output_dir = "/tmp/outputs"
|
| 165 |
os.makedirs(output_dir, exist_ok=True)
|
| 166 |
|
|
|
|
| 172 |
check_icd[['makcb', 'hoten', 'maicd', 'chandoan']].to_excel(file2, index=False)
|
| 173 |
|
| 174 |
file3 = os.path.join(output_dir, '3_Check_PLT_Duoi_100.xlsx')
|
| 175 |
+
# Thêm CTCAE cho file 3
|
| 176 |
+
check_plt_low['CTCAE_Grade'] = check_plt_low['Kết quả XN'].apply(identify_ctcae_grade)
|
| 177 |
+
check_plt_low['Nguy_Hiem'] = check_plt_low['CTCAE_Grade'].apply(assess_danger)
|
| 178 |
check_plt_low.to_excel(file3, index=False)
|
| 179 |
|
| 180 |
file4 = os.path.join(output_dir, '4_Check_Giao_Thoa_Thuoc_PLT.xlsx')
|
| 181 |
check_inter = df_plt[df_plt['Mã KCB'].isin(inter_ids)].copy()
|
| 182 |
+
# Thêm CTCAE cho file 4
|
| 183 |
+
check_inter['CTCAE_Grade'] = check_inter['Kết quả XN'].apply(identify_ctcae_grade)
|
| 184 |
+
check_inter['Nguy_Hiem'] = check_inter['CTCAE_Grade'].apply(assess_danger)
|
| 185 |
check_inter.to_excel(file4, index=False)
|
| 186 |
|
| 187 |
file5 = os.path.join(output_dir, '5_Check_Baseline_Nadir_Full.xlsx')
|
| 188 |
final_analysis.to_excel(file5, index=False)
|
| 189 |
|
| 190 |
+
# Realtime report với cột mới
|
| 191 |
def export_realtime_reports(df_report, df_xn, include_priority=True, include_recommendations=True):
|
| 192 |
today = pd.Timestamp.now(tz='Asia/Ho_Chi_Minh').normalize() # Sử dụng ngày hiện tại động với múi giờ VN
|
| 193 |
status_df = df_xn[['Mã KCB', 'Ngày vào', 'Ngày ra']].drop_duplicates('Mã KCB')
|
|
|
|
| 201 |
|
| 202 |
filerealtime = os.path.join(output_dir, 'Danh_Sach_Can_Thiep_DLS_2026.xlsx')
|
| 203 |
with pd.ExcelWriter(filerealtime) as writer:
|
| 204 |
+
cols = ['makcb', 'Họ tên', 'tenhh', 'hoatchat', 'PLT_baseline', 'Ngày_PLT_baseline', 'PLT_nadir', 'Ngày_PLT_nadir',
|
| 205 |
+
'CTCAE_Baseline', 'Nguy_Hiem_Baseline', 'CTCAE_Nadir', 'Nguy_Hiem_Nadir', 'Signal']
|
| 206 |
if include_priority: cols += ['Ngày vào', 'So_Ngay_Nam_Vien', 'Muc_Do_Cap_Bach']
|
| 207 |
if include_recommendations: cols += ['Mau_Cau_Khuyen_Cao']
|
| 208 |
|
|
|
|
| 225 |
thuoc = row['tenhh'] if pd.notna(row['tenhh']) else row['hoatchat']
|
| 226 |
plt_nen = row['PLT_baseline']
|
| 227 |
ngay = row['So_Ngay_Nam_Vien']
|
| 228 |
+
nadir = row.get('PLT_nadir', 'N/A')
|
| 229 |
+
ctcae_nadir = row['CTCAE_Nadir']
|
| 230 |
+
nguy_hiem_nadir = row['Nguy_Hiem_Nadir']
|
| 231 |
if "sụt giảm" in row['Signal'] or "Trầm trọng" in row['Signal']:
|
| 232 |
+
return (f"CẢNH BÁO ADR: Tiểu cầu bệnh nhân sụt giảm rõ rệt từ {plt_nen} G/L xuống còn {nadir} G/L "
|
| 233 |
+
f"({ctcae_nadir} - {nguy_hiem_nadir}) sau khi dùng {thuoc}. Đề xuất bác sĩ xem xét tạm ngưng hoặc đổi thuốc nếu không có nguyên nhân khác.")
|
| 234 |
else:
|
| 235 |
return (f"Bệnh nhân đã điều trị {ngay} ngày và đang sử dụng {thuoc} (có nguy cơ giảm PLT). "
|
| 236 |
f"Kết quả PLT nền thấp ({plt_nen} G/L) và chưa có xét nghiệm kiểm tra lại. "
|