Upload db.py with huggingface_hub
Browse files
db.py
CHANGED
|
@@ -383,47 +383,72 @@ def get_patient_reports(chart_no):
|
|
| 383 |
def get_mfi_comparison(chart_no, pra_class=None):
|
| 384 |
"""
|
| 385 |
取得同一病人跨日期的 MFI 比較資料。
|
|
|
|
| 386 |
回傳:
|
| 387 |
-
dates: [
|
| 388 |
-
antigens: [{antigen, allele, mfi_by_date: {
|
|
|
|
| 389 |
"""
|
| 390 |
conn = get_conn()
|
| 391 |
patient = conn.execute('SELECT id FROM patients WHERE chart_no=?', (chart_no,)).fetchone()
|
| 392 |
if not patient:
|
| 393 |
conn.close()
|
| 394 |
-
return [], []
|
| 395 |
|
| 396 |
-
|
| 397 |
-
SELECT
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
JOIN antibody_strength a ON a.report_id = r.id
|
| 401 |
-
WHERE r.patient_id = ? AND COALESCE(r.is_deleted, 0) = 0
|
| 402 |
'''
|
| 403 |
-
|
| 404 |
if pra_class:
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
|
|
|
| 408 |
|
| 409 |
-
|
| 410 |
-
|
|
|
|
|
|
|
| 411 |
|
| 412 |
-
|
|
|
|
| 413 |
dates = []
|
| 414 |
pra_by_date = {}
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
for r in rows:
|
| 418 |
d = r['report_date']
|
| 419 |
-
if d
|
| 420 |
-
|
| 421 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
|
|
|
|
|
|
|
|
|
|
| 423 |
ag = r['antigen']
|
| 424 |
if ag not in antigen_data:
|
| 425 |
antigen_data[ag] = {'antigen': ag, 'allele': r['allele'], 'mfi_by_date': {}}
|
| 426 |
-
antigen_data[ag]['mfi_by_date'][
|
| 427 |
'max_mfi': r['max_mfi'],
|
| 428 |
'mean_mfi': r['mean_mfi'],
|
| 429 |
'no_of_beads': r['no_of_beads'],
|
|
|
|
| 383 |
def get_mfi_comparison(chart_no, pra_class=None):
|
| 384 |
"""
|
| 385 |
取得同一病人跨日期的 MFI 比較資料。
|
| 386 |
+
同一天有多筆 report 時,label 加上 (A)/(B)/... 後綴(按 report_id 升冪)。
|
| 387 |
回傳:
|
| 388 |
+
dates: [label1, label2, ...] # 顯示用字串,同日多筆會帶後綴
|
| 389 |
+
antigens: [{antigen, allele, mfi_by_date: {label: {...}, ...}}, ...]
|
| 390 |
+
pra_by_date: {label: pra_percent}
|
| 391 |
"""
|
| 392 |
conn = get_conn()
|
| 393 |
patient = conn.execute('SELECT id FROM patients WHERE chart_no=?', (chart_no,)).fetchone()
|
| 394 |
if not patient:
|
| 395 |
conn.close()
|
| 396 |
+
return [], [], {}
|
| 397 |
|
| 398 |
+
rep_query = '''
|
| 399 |
+
SELECT id, report_date, pra_percent
|
| 400 |
+
FROM reports
|
| 401 |
+
WHERE patient_id = ? AND COALESCE(is_deleted, 0) = 0
|
|
|
|
|
|
|
| 402 |
'''
|
| 403 |
+
rep_params = [patient['id']]
|
| 404 |
if pra_class:
|
| 405 |
+
rep_query += ' AND pra_class = ?'
|
| 406 |
+
rep_params.append(pra_class)
|
| 407 |
+
rep_query += ' ORDER BY report_date, id'
|
| 408 |
+
reports = conn.execute(rep_query, rep_params).fetchall()
|
| 409 |
|
| 410 |
+
from collections import defaultdict
|
| 411 |
+
date_count = defaultdict(int)
|
| 412 |
+
for r in reports:
|
| 413 |
+
date_count[r['report_date']] += 1
|
| 414 |
|
| 415 |
+
date_seen = defaultdict(int)
|
| 416 |
+
labels_by_rid = {}
|
| 417 |
dates = []
|
| 418 |
pra_by_date = {}
|
| 419 |
+
for r in reports:
|
|
|
|
|
|
|
| 420 |
d = r['report_date']
|
| 421 |
+
if date_count[d] == 1:
|
| 422 |
+
label = d
|
| 423 |
+
else:
|
| 424 |
+
suffix = chr(ord('A') + date_seen[d])
|
| 425 |
+
label = f'{d} ({suffix})'
|
| 426 |
+
date_seen[d] += 1
|
| 427 |
+
labels_by_rid[r['id']] = label
|
| 428 |
+
dates.append(label)
|
| 429 |
+
pra_by_date[label] = r['pra_percent']
|
| 430 |
+
|
| 431 |
+
if not reports:
|
| 432 |
+
conn.close()
|
| 433 |
+
return [], [], {}
|
| 434 |
+
|
| 435 |
+
rep_ids = [r['id'] for r in reports]
|
| 436 |
+
placeholders = ','.join('?' * len(rep_ids))
|
| 437 |
+
ab_rows = conn.execute(f'''
|
| 438 |
+
SELECT report_id, antigen, allele, max_mfi, mean_mfi, no_of_beads
|
| 439 |
+
FROM antibody_strength
|
| 440 |
+
WHERE report_id IN ({placeholders})
|
| 441 |
+
ORDER BY antigen
|
| 442 |
+
''', rep_ids).fetchall()
|
| 443 |
+
conn.close()
|
| 444 |
|
| 445 |
+
antigen_data = {}
|
| 446 |
+
for r in ab_rows:
|
| 447 |
+
label = labels_by_rid[r['report_id']]
|
| 448 |
ag = r['antigen']
|
| 449 |
if ag not in antigen_data:
|
| 450 |
antigen_data[ag] = {'antigen': ag, 'allele': r['allele'], 'mfi_by_date': {}}
|
| 451 |
+
antigen_data[ag]['mfi_by_date'][label] = {
|
| 452 |
'max_mfi': r['max_mfi'],
|
| 453 |
'mean_mfi': r['mean_mfi'],
|
| 454 |
'no_of_beads': r['no_of_beads'],
|