File size: 70,573 Bytes
9b1e056 d0d0725 9b1e056 3ec1273 9b1e056 3ec1273 d0d0725 9b1e056 d0d0725 9b1e056 d0d0725 9b1e056 d0d0725 9b1e056 d0d0725 3ec1273 d0d0725 3ec1273 9b1e056 d0d0725 3ec1273 d0d0725 3ec1273 d0d0725 9b1e056 d0d0725 3ec1273 9b1e056 3ec1273 9b1e056 3ec1273 d0d0725 9b1e056 d0d0725 9b1e056 3ec1273 9b1e056 d0d0725 3ec1273 d0d0725 9b1e056 d0d0725 9b1e056 3ec1273 d0d0725 9b1e056 d0d0725 9b1e056 3ec1273 d0d0725 9b1e056 3ec1273 9b1e056 3ec1273 9b1e056 3ec1273 9b1e056 3ec1273 d0d0725 3ec1273 9b1e056 3ec1273 9b1e056 3ec1273 9b1e056 3ec1273 9b1e056 d0d0725 9b1e056 3ec1273 d0d0725 3ec1273 9b1e056 3ec1273 9b1e056 3ec1273 9b1e056 3ec1273 9b1e056 d0d0725 3ec1273 d0d0725 3ec1273 9b1e056 3ec1273 9b1e056 3ec1273 9b1e056 3ec1273 9b1e056 3ec1273 9b1e056 3ec1273 9b1e056 d0d0725 9b1e056 d0d0725 3ec1273 9b1e056 3ec1273 d0d0725 9b1e056 3ec1273 9b1e056 3ec1273 d0d0725 3ec1273 d0d0725 3ec1273 9b1e056 3ec1273 9b1e056 d0d0725 3ec1273 9b1e056 3ec1273 9b1e056 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 | import gradio as gr
import pandas as pd
import numpy as np
import copy
import traceback
from inference import (
FEATURE_NAMES,
REPORTING_OUTCOMES,
OUTCOME_DESCRIPTIONS,
OUTCOMES,
SHAP_OUTCOMES,
predict_with_comparison,
predict_all_outcomes,
create_all_shap_plots,
create_all_icon_arrays,
)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# CHOICES / CONSTANTS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
AGEGPFF_CHOICES = ["<=10", "11-17", "18-29", "30-49", ">=50"]
SEX_CHOICES = ["Male", "Female"]
KPS_CHOICES = ["<90", "β₯ 90"]
DONORF_CHOICES = [
"HLA identical sibling", "HLA mismatch relative",
"Matched unrelated donor", "Mismatched unrelated donor or cord blood",
]
GRAFTYPE_CHOICES = ["Bone marrow", "Peripheral blood", "Cord blood"]
CONDGRPF_CHOICES = ["MAC", "RIC", "NMA"]
CONDGRP_FINAL_CHOICES = [
"TBI/Cy", "TBI/Cy/Flu", "TBI/Cy/Flu/TT", "TBI/Mel", "TBI/Flu",
"TBI alone (300/400/600cGy)", "Bu/Cy", "Bu/Mel", "Flu/Bu/TT",
"Flu/Bu", "Flu/Mel/TT", "Flu/Mel", "Cy/Flu", "Treosulfan",
"Cy alone", "Flud", "TLI",
]
ATGF_CHOICES = ["ATG", "Alemtuzumab", "None"]
GVHD_FINAL_CHOICES = [
"Ex-vivo T-cell depletion", "CD34 selection", "Post-CY + siro +- MMF",
"Post-CY + MMF + CNI", "CNI + MMF", "CNI + MTX", "CNI alone",
"CNI + siro", "Siro alone", "MMF + MTX", "MMF + siro", "MMF alone",
"MTX alone", "MTX + siro",
]
HLA_FINAL_CHOICES = ["8/8", "7/8", "β€ 6/8"]
RCMVPR_CHOICES = ["Negative", "Positive"]
EXCHTFPR_CHOICES = ["No", "Yes"]
VOC2YPR_CHOICES = ["No", "Yes"]
VOCFRQPR_CHOICES = ["< 3/yr", "β₯ 3/yr"]
SCATXRSN_CHOICES = [
"CNS event", "Acute chest Syndrome", "Recurrent vaso-occlusive pain",
"Recurrent priapism", "Excessive transfusion requirements/iron overload",
"Cardio-pulmonary", "Chronic transfusion", "Asymptomatic",
"Renal insufficiency", "Splenic sequestration", "Avascular necrosis",
"Hodgkin lymphoma",
]
NUM_COLS_SET = {"AGE", "NACS2YR"}
MAX_SCENARIOS = 5
GROUPED_REGIMEN_CHOICES = [
("ββ HLA IDENTICAL ββ", "__header_hla_identical__"),
("Hsieh et al 2014", "Hsieh et al 2014"),
("Krishnamurti et al 2019", "Krishnamurti et al 2019"),
("King et al 2015", "King et al 2015"),
("Walters et al 1996", "Walters et al 1996"),
("ββ HLA MISMATCHED ββ", "__header_hla_mismatched__"),
("Bolanos-Meade et al 2022 (HLA Mismatch)", "Bolanos-Meade et al 2022 (HLA Mismatch)"),
("Patel et al 2020 (HLA Mismatch)", "Patel et al 2020 (HLA Mismatch)"),
("ββ MATCHED UNRELATED ββ", "__header_matched_unrelated__"),
("L Krishnamurti et al 2019", "L Krishnamurti et al 2019"),
("Shenoy et al 2016", "Shenoy et al 2016"),
("ββ MISMATCHED UNRELATED / CORD BLOOD ββ", "__header_mismatched_cord__"),
("Bolanos-Meade et al 2022 (Mismatched/Cord)", "Bolanos-Meade et al 2022 (Mismatched/Cord)"),
("Patel et al 2020 (Mismatched/Cord)", "Patel et al 2020 (Mismatched/Cord)"),
("ββ CUSTOM ββ", "__header_custom__"),
("Custom", "Custom"),
]
HEADER_VALUES = {v for _, v in GROUPED_REGIMEN_CHOICES if v.startswith("__header_")}
PUBLISHED_PRESETS = {
"Hsieh et al 2014": {"CONDGRPF": "NMA", "CONDGRP_FINAL": "TBI alone (300/400/600cGy)", "ATGF": "Alemtuzumab", "GVHD_FINAL": "Siro alone", "HLA_FINAL": "8/8", "DONORF": "HLA identical sibling"},
"Krishnamurti et al 2019": {"CONDGRPF": "MAC", "CONDGRP_FINAL": "Flu/Bu", "ATGF": "ATG", "GVHD_FINAL": "CNI + MTX", "HLA_FINAL": "8/8", "DONORF": "HLA identical sibling"},
"King et al 2015": {"CONDGRPF": "RIC", "CONDGRP_FINAL": "Flu/Mel", "ATGF": "Alemtuzumab", "GVHD_FINAL": "CNI + MTX", "HLA_FINAL": "8/8", "DONORF": "HLA identical sibling"},
"Walters et al 1996": {"CONDGRPF": "MAC", "CONDGRP_FINAL": "Bu/Cy", "ATGF": "ATG", "GVHD_FINAL": "CNI + MTX", "HLA_FINAL": "8/8", "DONORF": "HLA identical sibling"},
"Bolanos-Meade et al 2022 (HLA Mismatch)": {"CONDGRPF": "NMA", "CONDGRP_FINAL": "TBI/Cy/Flu", "ATGF": "ATG", "GVHD_FINAL": "Post-CY + siro +- MMF", "HLA_FINAL": "7/8", "DONORF": "HLA mismatch relative"},
"Patel et al 2020 (HLA Mismatch)": {"CONDGRPF": "NMA", "CONDGRP_FINAL": "TBI/Cy/Flu/TT", "ATGF": "ATG", "GVHD_FINAL": "Post-CY + siro +- MMF", "HLA_FINAL": "7/8", "DONORF": "HLA mismatch relative"},
"L Krishnamurti et al 2019": {"CONDGRPF": "MAC", "CONDGRP_FINAL": "Flu/Bu", "ATGF": "ATG", "GVHD_FINAL": "CNI + MTX", "HLA_FINAL": "8/8", "DONORF": "Matched unrelated donor"},
"Shenoy et al 2016": {"CONDGRPF": "RIC", "CONDGRP_FINAL": "Flu/Mel", "ATGF": "Alemtuzumab", "GVHD_FINAL": "CNI + MTX", "HLA_FINAL": "8/8", "DONORF": "Matched unrelated donor"},
"Bolanos-Meade et al 2022 (Mismatched/Cord)": {"CONDGRPF": "NMA", "CONDGRP_FINAL": "TBI/Cy/Flu", "ATGF": "ATG", "GVHD_FINAL": "Post-CY + siro +- MMF", "HLA_FINAL": "7/8", "DONORF": "Mismatched unrelated donor or cord blood"},
"Patel et al 2020 (Mismatched/Cord)": {"CONDGRPF": "NMA", "CONDGRP_FINAL": "TBI/Cy/Flu/TT", "ATGF": "ATG", "GVHD_FINAL": "Post-CY + siro +- MMF", "HLA_FINAL": "7/8", "DONORF": "Mismatched unrelated donor or cord blood"},
}
MSD_DONOR = "HLA identical sibling"
MUD_DONOR = "Matched unrelated donor"
LOCKED_88_DONORS = {MSD_DONOR, MUD_DONOR}
NON_88_DONORS = {"HLA mismatch relative", "Mismatched unrelated donor or cord blood"}
MSD_REGIMENS = {"Hsieh et al 2014", "Krishnamurti et al 2019", "King et al 2015", "Walters et al 1996"}
MMUD_REGIMENS = {"Bolanos-Meade et al 2022 (Mismatched/Cord)", "Patel et al 2020 (Mismatched/Cord)",
"Bolanos-Meade et al 2022 (HLA Mismatch)", "Patel et al 2020 (HLA Mismatch)"}
PATIENT_FEATURES = ["AGE", "AGEGPFF", "SEX", "KPS", "RCMVPR"]
DONOR_FEATURES = ["DONORF", "GRAFTYPE", "HLA_FINAL", "CONDGRPF", "CONDGRP_FINAL", "ATGF", "GVHD_FINAL"]
DISEASE_FEATURES = ["NACS2YR", "EXCHTFPR", "VOC2YPR", "VOCFRQPR", "SCATXRSN"]
ALL_FEATURES = PATIENT_FEATURES + DONOR_FEATURES + DISEASE_FEATURES
# Icon array outcomes in display order
ICON_OUTCOMES = ["DEAD", "GF", "AGVHD", "CGVHD", "VOCPSHI", "STROKEHI"]
OUTCOME_TITLES = {
"DEAD": "Death",
"GF": "Graft Failure",
"AGVHD": "Acute GvHD",
"CGVHD": "Chronic GvHD",
"VOCPSHI": "Vaso-Occlusive Crisis",
"STROKEHI": "Stroke Post-HCT",
}
EVENT_COLOR = "#e53935"
NO_EVENT_COLOR = "#43a047"
SHAP_ORDER = ["DEAD", "GF", "AGVHD", "CGVHD", "VOCPSHI", "EFS", "STROKEHI", "OS"]
SHAP_LABELS = {
"DEAD": "Death",
"GF": "Graft Failure",
"AGVHD": "Acute GvHD",
"CGVHD": "Chronic GvHD",
"VOCPSHI": "Vaso-Occlusive Crisis Post-HCT",
"EFS": "Event-Free Survival",
"STROKEHI": "Stroke Post-HCT",
"OS": "Overall Survival",
}
SCENARIO_COLORS = ["#e65100", "#6a1b9a", "#1b5e20", "#0d47a1", "#b71c1c"]
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# COMPONENT FACTORY
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_FEATURE_META = {
"AGE": {"label": "Age (years)", "type": "number", "kwargs": {"minimum": 0, "maximum": 100, "step": 1}},
"AGEGPFF": {"label": "Age Group", "type": "dropdown", "choices": AGEGPFF_CHOICES, "kwargs": {"interactive": False, "info": "Auto-filled from Age"}},
"SEX": {"label": "Sex", "type": "radio", "choices": SEX_CHOICES},
"KPS": {"label": "Karnofsky Performance Status", "type": "radio", "choices": KPS_CHOICES},
"RCMVPR": {"label": "Recipient CMV Status", "type": "radio", "choices": RCMVPR_CHOICES},
"DONORF": {"label": "Donor Type", "type": "dropdown", "choices": DONORF_CHOICES},
"GRAFTYPE": {"label": "Graft Type", "type": "radio", "choices": GRAFTYPE_CHOICES},
"HLA_FINAL": {"label": "HLA Matching", "type": "radio", "choices": HLA_FINAL_CHOICES},
"CONDGRPF": {"label": "Conditioning Intensity", "type": "radio", "choices": CONDGRPF_CHOICES},
"CONDGRP_FINAL": {"label": "Conditioning Regimen", "type": "dropdown", "choices": CONDGRP_FINAL_CHOICES},
"ATGF": {"label": "Serotherapy", "type": "radio", "choices": ATGF_CHOICES},
"GVHD_FINAL": {"label": "GvHD Prophylaxis", "type": "dropdown", "choices": GVHD_FINAL_CHOICES},
"NACS2YR": {"label": "Acute Chest Syndrome Episodes (2 yrs pre-HCT)", "type": "number", "kwargs": {"minimum": 0, "maximum": 50, "step": 1}},
"EXCHTFPR": {"label": "Exchange Transfusion Pre-HCT", "type": "radio", "choices": EXCHTFPR_CHOICES},
"VOC2YPR": {"label": "Vaso-Occlusive Crisis in 2 yrs Pre-HCT", "type": "radio", "choices": VOC2YPR_CHOICES},
"VOCFRQPR": {"label": "VoC Frequency Pre-HCT", "type": "radio", "choices": VOCFRQPR_CHOICES},
"SCATXRSN": {"label": "Primary Reason for HCT", "type": "dropdown", "choices": SCATXRSN_CHOICES},
}
def make_component(feature: str, suffix: str = ""):
meta = _FEATURE_META[feature]
label = f"{meta['label']} {suffix}".strip() if suffix else meta["label"]
kind = meta["type"]
kwargs = dict(meta.get("kwargs", {}))
if kind == "number":
return gr.Number(label=label, value=None, **kwargs)
elif kind == "dropdown":
interactive = kwargs.pop("interactive", True)
info = kwargs.pop("info", None)
return gr.Dropdown(label=label, choices=meta["choices"], value=None,
interactive=interactive, info=info, **kwargs)
elif kind == "radio":
interactive = kwargs.pop("interactive", True)
info = kwargs.pop("info", None)
return gr.Radio(label=label, choices=meta["choices"], value=None,
interactive=interactive, info=info, **kwargs)
else:
raise ValueError(f"Unknown component type '{kind}' for feature '{feature}'")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# CONSTRAINT / VALIDATION HELPERS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _hla_update_for_donor(donor_value):
if not donor_value:
return gr.update(choices=HLA_FINAL_CHOICES, interactive=True)
if donor_value in LOCKED_88_DONORS:
return gr.update(choices=["8/8"], value="8/8", interactive=False)
elif donor_value in NON_88_DONORS:
return gr.update(choices=["7/8", "β€ 6/8"], value=None, interactive=True)
return gr.update(choices=HLA_FINAL_CHOICES, interactive=True)
def _validate_counterfactual_constraints(base_dict, wi_dict, label=""):
violations = []
tag = f"[{label}] " if label else ""
if base_dict.get("SEX") and wi_dict.get("SEX"):
if base_dict["SEX"] != wi_dict["SEX"]:
violations.append(f"{tag} Immutable feature: Sex cannot be changed.")
base_age, wi_age = base_dict.get("AGE"), wi_dict.get("AGE")
if base_age is not None and wi_age is not None:
try:
if float(wi_age) < float(base_age):
violations.append(f"{tag}Age cannot be decreased ({base_age} β {wi_age}).")
except (TypeError, ValueError):
pass
wi_donor = wi_dict.get("DONORF")
wi_hla = wi_dict.get("HLA_FINAL")
if wi_donor and wi_hla:
if wi_donor in LOCKED_88_DONORS and wi_hla != "8/8":
violations.append(f"{tag}HLA constraint: '{wi_donor}' requires 8/8 HLA.")
elif wi_donor in NON_88_DONORS and wi_hla == "8/8":
violations.append(f"{tag}HLA constraint: '{wi_donor}' cannot have 8/8 HLA.")
if wi_donor:
wi_gvhd = wi_dict.get("GVHD_FINAL", "")
if wi_donor == MSD_DONOR and wi_gvhd in {"Post-CY + siro +- MMF", "Post-CY + MMF + CNI"}:
violations.append(
f"{tag} Post-Cy GVHD prophylaxis is inconsistent with donor '{MSD_DONOR}'."
)
return violations
def _values_to_dict(values):
d = {}
for f, v in zip(ALL_FEATURES, values):
if f in NUM_COLS_SET:
try:
d[f] = float(v) if v not in (None, "") else None
except (TypeError, ValueError):
d[f] = None
else:
d[f] = v
return d
def _check_missing(user_vals, label=""):
missing = [f for f, v in user_vals.items()
if v is None or v == "" or (isinstance(v, float) and pd.isna(v))]
if missing:
raise ValueError(
f"{'[' + label + '] ' if label else ''}Please fill in all fields. "
f"Missing: {', '.join(missing)}"
)
def get_age_group(age):
if age is None or age == "":
return ""
try:
age = float(age)
if age <= 10: return "<=10"
elif age <= 17: return "11-17"
elif age <= 29: return "18-29"
elif age <= 49: return "30-49"
else: return ">=50"
except (ValueError, TypeError):
return ""
def vocfrqpr_from_voc2ypr(voc_status):
if voc_status == "No":
return gr.update(value="< 3/yr", interactive=False)
return gr.update(value=None, interactive=True)
def apply_grouped_preset(selected_value):
if not selected_value or selected_value in HEADER_VALUES:
return (gr.update(value=None),) + (gr.update(),) * 6
if selected_value == "Custom":
return (gr.update(),) + tuple(gr.update(interactive=True) for _ in range(6))
preset = PUBLISHED_PRESETS.get(selected_value)
if not preset:
return (gr.update(),) * 7
donor = preset["DONORF"]
hla_update = gr.update(
value=preset["HLA_FINAL"], interactive=False,
choices=(["8/8"] if donor in LOCKED_88_DONORS
else (["7/8", "β€ 6/8"] if donor in NON_88_DONORS else HLA_FINAL_CHOICES)),
)
return (
gr.update(),
gr.update(value=preset["DONORF"], interactive=False),
gr.update(value=preset["CONDGRPF"], interactive=False),
gr.update(value=preset["CONDGRP_FINAL"], interactive=False),
gr.update(value=preset["ATGF"], interactive=False),
gr.update(value=preset["GVHD_FINAL"], interactive=False),
hla_update,
)
def lock_sex(baseline_sex):
if baseline_sex:
return gr.update(value=baseline_sex, interactive=False)
return gr.update(interactive=False)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ICON ARRAY HTML RENDERERS (pure Python, no JS)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _stick_figure_svg(color, size=16):
h = round(size * 1.6)
return (
f'<svg xmlns="http://www.w3.org/2000/svg" width="{size}" height="{h}" '
f'viewBox="0 0 20 32" style="display:block;flex-shrink:0;" '
f'stroke="{color}" stroke-width="2.2" stroke-linecap="round" fill="none">'
f'<circle cx="10" cy="5" r="3.8" fill="{color}" stroke="none"/>'
f'<line x1="10" y1="9" x2="10" y2="20"/>'
f'<line x1="3" y1="13" x2="17" y2="13"/>'
f'<line x1="10" y1="20" x2="4" y2="30"/>'
f'<line x1="10" y1="20" x2="16" y2="30"/>'
f'</svg>'
)
def _render_single_icon_card(probability, outcome, panel_label="", panel_color="#1565c0"):
"""Render a single icon array card as HTML (no JS). Used for Python-driven carousel."""
title = OUTCOME_TITLES.get(outcome, OUTCOME_DESCRIPTIONS.get(outcome, outcome))
n_event = round(probability * 100)
n_no_event = 100 - n_event
pct_str = f"{probability * 100:.1f}%"
rows_parts = []
for row in range(10):
cells = ""
for col in range(10):
idx = row * 10 + col
color = EVENT_COLOR if idx < n_event else NO_EVENT_COLOR
cells += _stick_figure_svg(color, size=13)
rows_parts.append(
f'<div style="display:flex;justify-content:center;gap:1px;margin-bottom:1px;">{cells}</div>'
)
grid_html = "\n".join(rows_parts)
fig_e = _stick_figure_svg(EVENT_COLOR, size=11)
fig_ne = _stick_figure_svg(NO_EVENT_COLOR, size=11)
legend = (
f'<div style="display:inline-grid;grid-template-columns:13px 1fr 36px;'
f'align-items:center;gap:3px;row-gap:3px;">'
f'{fig_e}<span style="color:{EVENT_COLOR};font-weight:700;font-size:9px;">Event</span>'
f'<span style="color:#888;font-size:8px;">({n_event}/100)</span>'
f'{fig_ne}<span style="color:{NO_EVENT_COLOR};font-weight:700;font-size:9px;">No Event</span>'
f'<span style="color:#888;font-size:8px;">({n_no_event}/100)</span>'
f'</div>'
)
badge = (
f'<div style="background:{panel_color};color:#fff;font-size:8px;font-weight:700;'
f'border-radius:3px;padding:1px 5px;margin-bottom:2px;display:inline-block;">'
f'{panel_label}</div>'
) if panel_label else ""
return (
f'<div style="background:#fff;border:1px solid #e0e0e0;border-radius:7px;'
f'padding:10px 8px;text-align:center;font-family:\'Segoe UI\',Arial,sans-serif;'
f'box-shadow:0 2px 4px rgba(0,0,0,0.06);box-sizing:border-box;max-width:340px;'
f'margin:0 auto;display:flex;flex-direction:column;align-items:center;">'
f'{badge}'
f'<div style="min-height:30px;display:flex;align-items:center;justify-content:center;'
f'font-size:13px;font-weight:700;color:#222;line-height:1.3;margin-bottom:4px;">{title}</div>'
f'<div style="font-size:22px;font-weight:800;color:{EVENT_COLOR};'
f'line-height:1;margin-bottom:6px;">{pct_str}</div>'
f'<div style="margin-bottom:4px;">{grid_html}</div>'
f'<div>{legend}</div>'
f'</div>'
)
def _render_icon_carousel_page(probs_dict, idx):
"""Render the full carousel HTML for a given outcome index (no JS)."""
outcome = ICON_OUTCOMES[idx]
total = len(ICON_OUTCOMES)
card_html = _render_single_icon_card(probs_dict.get(outcome, 0.0), outcome)
label = OUTCOME_TITLES.get(outcome, outcome)
# Breadcrumb dots
dots = ""
for i, o in enumerate(ICON_OUTCOMES):
active = i == idx
dots += (
f'<span style="display:inline-block;width:{"10" if active else "7"}px;'
f'height:{"10" if active else "7"}px;border-radius:50%;'
f'background:{"#1565c0" if active else "#ccc"};'
f'margin:0 3px;vertical-align:middle;"></span>'
)
footnote = (
f'<div style="font-size:10px;color:#888;text-align:center;margin-top:10px;">'
f'Each figure = 1 patient out of 100. '
f'<span style="color:{EVENT_COLOR};font-weight:600;">■ Red = Event</span> '
f'<span style="color:{NO_EVENT_COLOR};font-weight:600;">■ Green = No Event</span>'
f'</div>'
)
return (
f'<div style="font-family:\'Segoe UI\',Arial,sans-serif;padding:4px 0;text-align:center;">'
f'<div style="font-size:15px;font-weight:700;color:#1565c0;margin-bottom:2px;">{label}</div>'
f'<div style="font-size:11px;color:#888;margin-bottom:10px;">{idx+1} / {total}</div>'
f'<div style="margin-bottom:2px;">{dots}</div>'
f'<div style="margin-top:10px;">{card_html}</div>'
f'{footnote}'
f'</div>'
)
def _render_comparison_icon_page(all_probs_list, labels, colors, idx):
"""Render comparison icon carousel page for a given outcome index (no JS)."""
outcome = ICON_OUTCOMES[idx]
total = len(ICON_OUTCOMES)
out_label = OUTCOME_TITLES.get(outcome, outcome)
# Dots breadcrumb
dots = ""
for i in range(total):
active = i == idx
dots += (
f'<span style="display:inline-block;width:{"10" if active else "7"}px;'
f'height:{"10" if active else "7"}px;border-radius:50%;'
f'background:{"#1565c0" if active else "#ccc"};'
f'margin:0 3px;vertical-align:middle;"></span>'
)
rows_html = ""
for probs, label, color in zip(all_probs_list, labels, colors):
prob = probs.get(outcome, 0.0)
card = _render_single_icon_card(prob, outcome, label, color)
rows_html += (
f'<div style="display:flex;align-items:flex-start;gap:10px;margin-bottom:12px;">'
f'<div style="width:80px;flex-shrink:0;font-size:9px;font-weight:700;'
f'text-align:center;border-radius:4px;padding:4px 6px;color:#fff;'
f'background:{color};white-space:normal;word-break:break-word;">{label}</div>'
f'<div style="flex:1;">{card}</div>'
f'</div>'
)
footnote = (
f'<div style="font-size:10px;color:#888;text-align:center;margin-top:4px;">'
f'Each figure = 1 patient out of 100. '
f'<span style="color:{EVENT_COLOR};font-weight:600;">■ Red = Event</span> '
f'<span style="color:{NO_EVENT_COLOR};font-weight:600;">■ Green = No Event</span>'
f'</div>'
)
return (
f'<div style="font-family:\'Segoe UI\',Arial,sans-serif;padding:4px 0;text-align:center;">'
f'<div style="font-size:15px;font-weight:700;color:#1565c0;margin-bottom:2px;">{out_label}</div>'
f'<div style="font-size:11px;color:#888;margin-bottom:6px;">{idx+1} / {total}</div>'
f'<div style="margin-bottom:10px;">{dots}</div>'
f'{rows_html}'
f'{footnote}'
f'</div>'
)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# OTHER HTML RENDERERS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _delta_color_html(delta, is_survival):
if abs(delta) < 0.0005:
color = "#888888"
elif (delta > 0 and is_survival) or (delta < 0 and not is_survival):
color = "#2e7d32"
else:
color = "#c62828"
sign = "+" if delta >= 0 else ""
return f'<span style="color:{color};font-weight:700;">{sign}{delta*100:.1f}%</span>'
def _build_comparison_table_html(base_probs, base_ci, scenario_probs_list, scenario_ci_list, scenario_labels, scenario_colors):
survival_outcomes = {"OS", "EFS"}
outcome_headers = "".join(
f"<th style='padding:8px 10px;text-align:center;border-bottom:2px solid #ccd;"
f"color:#333;font-size:11px;'>{OUTCOME_DESCRIPTIONS.get(o, o)}</th>"
for o in REPORTING_OUTCOMES if o in base_probs
)
header = (
"<div style='overflow-x:auto;'>"
"<table style='width:100%;border-collapse:collapse;"
"font-family:\"Segoe UI\",Arial,sans-serif;font-size:12px;'>"
"<thead><tr style='background:#f0f4f8;'>"
"<th style='padding:9px 12px;text-align:left;border-bottom:2px solid #ccd;"
"color:#333;min-width:120px;'>Scenario</th>"
f"{outcome_headers}"
"</tr></thead><tbody>"
)
rows = ""
baseline_cells = ""
for o in REPORTING_OUTCOMES:
if o not in base_probs:
continue
bp = base_probs[o]
blo, bhi = base_ci.get(o, (float("nan"), float("nan")))
baseline_cells += (
f"<td style='padding:7px 10px;text-align:center;'>"
f"<div style='font-weight:700;color:#1565c0;font-size:13px;'>{bp*100:.1f}%</div>"
f"<div style='color:#5c7fa8;font-size:9px;'>[{blo*100:.1f}%β{bhi*100:.1f}%]</div>"
f"</td>"
)
rows += (
f"<tr style='background:#e8f0fb;'>"
f"<td style='padding:8px 12px;font-weight:700;color:#1565c0;border-right:2px solid #ccd;'>"
f"<div style='display:flex;align-items:center;gap:6px;'>"
f"<span style='display:inline-block;width:10px;height:10px;border-radius:50%;"
f"background:#1565c0;flex-shrink:0;'></span>Baseline</div></td>"
f"{baseline_cells}</tr>"
)
for j, (sp_dict, sci_dict, s_label) in enumerate(zip(scenario_probs_list, scenario_ci_list, scenario_labels)):
sc_color = scenario_colors[j % len(scenario_colors)]
bg = "#fafbfc" if j % 2 == 0 else "#ffffff"
scenario_cells = ""
for o in REPORTING_OUTCOMES:
if o not in base_probs or o not in sp_dict:
continue
bp = base_probs[o]
wp = sp_dict[o]
wlo, whi = sci_dict.get(o, (float("nan"), float("nan")))
delta = wp - bp
is_surv = o in survival_outcomes
scenario_cells += (
f"<td style='padding:7px 10px;text-align:center;'>"
f"<div style='font-weight:700;color:{sc_color};font-size:13px;'>{wp*100:.1f}%</div>"
f"<div style='color:#888;font-size:9px;'>[{wlo*100:.1f}%β{whi*100:.1f}%]</div>"
f"<div style='font-size:10px;margin-top:1px;'>{_delta_color_html(delta, is_surv)}</div>"
f"</td>"
)
rows += (
f"<tr style='background:{bg};'>"
f"<td style='padding:8px 12px;font-weight:600;border-right:2px solid #ccd;'>"
f"<div style='display:flex;align-items:center;gap:6px;'>"
f"<span style='display:inline-block;width:10px;height:10px;border-radius:50%;"
f"background:{sc_color};flex-shrink:0;'></span>"
f"<span style='color:{sc_color};'>{s_label}</span></div></td>"
f"{scenario_cells}</tr>"
)
footer = (
"</tbody></table></div>"
"<div style='font-size:10.5px;color:#888;margin-top:6px;'>"
"Ξ from Baseline: <span style='color:#2e7d32;font-weight:600;'>Green = improvement</span> "
"<span style='color:#c62828;font-weight:600;'>Red = worsening</span> | "
"OS & EFS: higher is better; all other outcomes: lower is better."
"</div>"
)
return header + rows + footer
def _build_violation_html(violations):
if not violations:
return ""
items = "".join(f"<li style='margin-bottom:6px;'>{v}</li>" for v in violations)
return (
f'<div style="background:#fff3e0;border:2px solid #e65100;border-radius:8px;'
f'padding:14px 18px;font-family:\'Segoe UI\',Arial,sans-serif;margin-bottom:12px;">'
f'<div style="font-weight:700;font-size:14px;color:#bf360c;margin-bottom:8px;">'
f'Constraint Violations β Analysis blocked</div>'
f'<ul style="margin:0;padding-left:20px;color:#6d1f00;font-size:13px;">{items}</ul>'
f'<div style="margin-top:10px;font-size:11px;color:#888;">'
f'Please correct the above before running the comparison.</div>'
f'</div>'
)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# SHAP CAROUSEL HELPERS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _shap_counter_html(idx):
labels = [SHAP_LABELS.get(o, o) for o in SHAP_ORDER]
items = " Β· ".join(
f'<span style="font-weight:{"700" if i==idx else "400"};'
f'color:{"#1565c0" if i==idx else "#aaa"};font-size:11px;">{l}</span>'
for i, l in enumerate(labels)
)
return f'<div style="text-align:center;padding:4px 0 2px;">{items}</div>'
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# MAIN PREDICT CALLBACK (Tab 1)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def predict_gradio(*values):
try:
user_vals = _values_to_dict(values)
missing = [f for f, v in user_vals.items()
if v is None or v == "" or (isinstance(v, float) and pd.isna(v))]
if missing:
raise ValueError(f"Please fill in all fields. Missing: {', '.join(missing)}")
calibrated, _ = predict_with_comparison(user_vals)
calibrated_probs, calibrated_intervals = calibrated
rows = []
for outcome in REPORTING_OUTCOMES:
desc = OUTCOME_DESCRIPTIONS[outcome]
calib_prob = calibrated_probs[outcome]
ci_low, ci_high = calibrated_intervals[outcome]
rows.append({
"Outcome": desc,
"Probability": f"{calib_prob * 100:.1f}%",
"95% CI": f"[{ci_low * 100:.1f}% β {ci_high * 100:.1f}%]",
})
df = pd.DataFrame(rows)
shap_plots = create_all_shap_plots(user_vals, max_display=10)
# Icon carousel: start at index 0, store probs in State
icon_html = _render_icon_carousel_page(calibrated_probs, 0)
first_shap = shap_plots[SHAP_ORDER[0]]
shap_crumb = _shap_counter_html(0)
return (
df,
calibrated_probs, # stored in State for carousel navigation
0, # icon carousel index State
icon_html, # displayed icon card
shap_plots, # stored in State
0, # shap index State
first_shap, # displayed plot
shap_crumb, # breadcrumb HTML
)
except Exception as e:
print(traceback.format_exc())
raise gr.Error(f"{type(e).__name__}: {str(e)}")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# CSS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
custom_css = """
.predict-button {
background: linear-gradient(to right, #ff6b35, #ff8c42) !important;
border: none !important; color: white !important;
font-weight: bold !important; font-size: 16px !important; padding: 12px !important;
}
.predict-button:hover { background: linear-gradient(to right, #ff5722, #ff7b29) !important; }
.copy-button {
background: linear-gradient(to right, #388e3c, #66bb6a) !important;
border: none !important; color: white !important; font-weight: 600 !important;
}
.copy-button:hover { background: linear-gradient(to right, #2e7d32, #43a047) !important; }
.copy-from-predict-button {
background: linear-gradient(to right, #6a1b9a, #ab47bc) !important;
border: none !important; color: white !important; font-weight: 600 !important;
}
.copy-from-predict-button:hover { background: linear-gradient(to right, #4a148c, #8e24aa) !important; }
.counterfactual-button {
background: linear-gradient(to right, #1976d2, #42a5f5) !important;
border: none !important; color: white !important;
font-weight: bold !important; font-size: 15px !important; padding: 12px !important;
}
.counterfactual-button:hover { background: linear-gradient(to right, #1565c0, #1e88e5) !important; }
.shap-nav-button {
background: linear-gradient(to right, #37474f, #546e7a) !important;
border: none !important; color: white !important;
font-weight: bold !important; font-size: 18px !important;
padding: 8px 22px !important; border-radius: 6px !important;
}
.shap-nav-button:hover { background: linear-gradient(to right, #263238, #455a64) !important; }
.icon-nav-button {
background: linear-gradient(to right, #1565c0, #1e88e5) !important;
border: none !important; color: white !important;
font-weight: bold !important; font-size: 18px !important;
padding: 8px 22px !important; border-radius: 6px !important;
}
.icon-nav-button:hover { background: linear-gradient(to right, #0d47a1, #1565c0) !important; }
.output-dataframe table td:first-child,
.output-dataframe table th:first-child {
white-space: normal !important; word-break: break-word !important; min-width: 240px !important;
}
.constraint-info {
background: #e8f5e9; border-left: 4px solid #388e3c;
padding: 8px 14px; font-size: 12px; color: #1b5e20;
border-radius: 4px; margin-bottom: 8px;
}
.scenario-panel-0 { border-left: 4px solid #e65100 !important; }
.scenario-panel-1 { border-left: 4px solid #6a1b9a !important; }
.scenario-panel-2 { border-left: 4px solid #1b5e20 !important; }
.scenario-panel-3 { border-left: 4px solid #0d47a1 !important; }
.scenario-panel-4 { border-left: 4px solid #b71c1c !important; }
"""
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# BUILD UI
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Blocks(title="HCT Outcome Prediction Model", css=custom_css) as demo:
gr.Markdown("# HCT Outcome Prediction Model")
n_scenarios_state = gr.State(1)
with gr.Tabs():
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TAB 1 β PREDICT OUTCOMES
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("Predict Outcomes"):
gr.Markdown("Enter patient, transplant, and disease characteristics to predict outcomes.")
inputs_dict = {}
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Patient Characteristics")
for f in PATIENT_FEATURES:
inputs_dict[f] = make_component(f)
with gr.Column(scale=1):
gr.Markdown("### Transplant Characteristics")
grouped_dd = gr.Dropdown(
choices=GROUPED_REGIMEN_CHOICES, value=None,
label="Published conditioning regimen",
info="Auto-fills Donor Type, Conditioning Intensity, Regimen, Serotherapy, GVHD Prophylaxis",
)
p_donorf = inputs_dict["DONORF"] = make_component("DONORF")
inputs_dict["GRAFTYPE"] = make_component("GRAFTYPE")
p_condgrpf = inputs_dict["CONDGRPF"] = make_component("CONDGRPF")
p_condgrp_final = inputs_dict["CONDGRP_FINAL"] = make_component("CONDGRP_FINAL")
p_atgf = inputs_dict["ATGF"] = make_component("ATGF")
p_gvhd_final = inputs_dict["GVHD_FINAL"] = make_component("GVHD_FINAL")
p_hla_final = inputs_dict["HLA_FINAL"] = make_component("HLA_FINAL")
with gr.Column(scale=1):
gr.Markdown("### Disease Characteristics")
for f in DISEASE_FEATURES:
inputs_dict[f] = make_component(f)
inputs_dict["AGE"].change(get_age_group, inputs_dict["AGE"], inputs_dict["AGEGPFF"])
inputs_dict["VOC2YPR"].change(vocfrqpr_from_voc2ypr, inputs_dict["VOC2YPR"], inputs_dict["VOCFRQPR"])
p_donorf.change(fn=_hla_update_for_donor, inputs=p_donorf, outputs=p_hla_final)
grouped_dd.change(
apply_grouped_preset, grouped_dd,
[grouped_dd, p_donorf, p_condgrpf, p_condgrp_final, p_atgf, p_gvhd_final, p_hla_final],
)
inputs_list = [inputs_dict[f] for f in ALL_FEATURES]
predict_btn = gr.Button("Predict", elem_classes="predict-button", size="lg")
gr.Markdown("---")
gr.Markdown("## Prediction Results")
output_table = gr.Dataframe(
headers=["Outcome", "Probability", "95% CI"],
label="Predicted Outcomes",
elem_classes="output-dataframe",
row_count=(len(REPORTING_OUTCOMES), "dynamic"),
column_count=(3, "fixed"),
wrap=True,
)
# ββ Icon Array Carousel (Tab 1) β Python-driven βββββββββββββββββββ
gr.Markdown("---")
gr.Markdown("## Outcome Probability β Icon Arrays")
gr.Markdown("*Use the β β arrows to browse outcomes one at a time.*")
# State: store probs dict + current index
icon_probs_state = gr.State(None)
icon_idx_state = gr.State(0)
with gr.Row():
icon_prev_btn = gr.Button("β", elem_classes="icon-nav-button", size="sm", scale=0)
icon_display = gr.HTML(scale=4)
icon_next_btn = gr.Button("β", elem_classes="icon-nav-button", size="sm", scale=0)
def _on_icon_prev(idx, probs):
if probs is None:
return idx, ""
new_idx = max(0, idx - 1)
return new_idx, _render_icon_carousel_page(probs, new_idx)
def _on_icon_next(idx, probs):
if probs is None:
return idx, ""
new_idx = min(len(ICON_OUTCOMES) - 1, idx + 1)
return new_idx, _render_icon_carousel_page(probs, new_idx)
icon_prev_btn.click(
fn=_on_icon_prev,
inputs=[icon_idx_state, icon_probs_state],
outputs=[icon_idx_state, icon_display],
)
icon_next_btn.click(
fn=_on_icon_next,
inputs=[icon_idx_state, icon_probs_state],
outputs=[icon_idx_state, icon_display],
)
# ββ SHAP Carousel (Tab 1) βββββββββββββββββββββββββββββββββββββββββ
gr.Markdown("---")
gr.Markdown("## SHAP β Feature Importance")
gr.Markdown("*Use the β β buttons to browse SHAP plots one at a time.*")
shap_plots_state = gr.State(None)
shap_idx_state = gr.State(0)
with gr.Row():
shap_prev_btn = gr.Button("β", elem_classes="shap-nav-button", size="sm", scale=0)
shap_crumb = gr.HTML(value="", scale=4)
shap_next_btn = gr.Button("β", elem_classes="shap-nav-button", size="sm", scale=0)
shap_display = gr.Plot(label="SHAP Feature Importance")
def _on_shap_prev(idx, plots):
new_idx = max(0, idx - 1)
plot = plots[SHAP_ORDER[new_idx]] if plots else None
return new_idx, plot, _shap_counter_html(new_idx)
def _on_shap_next(idx, plots):
new_idx = min(len(SHAP_ORDER) - 1, idx + 1)
plot = plots[SHAP_ORDER[new_idx]] if plots else None
return new_idx, plot, _shap_counter_html(new_idx)
shap_prev_btn.click(
fn=_on_shap_prev,
inputs=[shap_idx_state, shap_plots_state],
outputs=[shap_idx_state, shap_display, shap_crumb],
)
shap_next_btn.click(
fn=_on_shap_next,
inputs=[shap_idx_state, shap_plots_state],
outputs=[shap_idx_state, shap_display, shap_crumb],
)
predict_btn.click(
fn=predict_gradio,
inputs=inputs_list,
outputs=[
output_table,
icon_probs_state, # β store probs dict
icon_idx_state, # β reset to 0
icon_display, # β show first card
shap_plots_state,
shap_idx_state,
shap_display,
shap_crumb,
],
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TAB 2 β COUNTERFACTUAL ANALYSIS
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("Counterfactual Scenarios"):
gr.Markdown(
"## Counterfactual Scenario Analysis\n"
"Enter the **baseline** patient, then choose how many counterfactual "
"scenarios you want to compare. Each scenario panel will appear below."
)
with gr.Row():
n_scenarios_slider = gr.Slider(
minimum=1, maximum=MAX_SCENARIOS, step=1, value=1,
label=f"How many counterfactual scenarios do you want to compare? (1β{MAX_SCENARIOS})",
info="Adjust this first β scenario panels will appear/disappear below.",
)
gr.Markdown("---")
# ββ BASELINE βββββββββββββββββββββββββββββββββββββββββββββββββββββ
gr.Markdown("## Baseline Patient Profile")
with gr.Row():
copy_from_predict_btn = gr.Button(
"Copy from Predict tab β Baseline",
elem_classes="copy-from-predict-button",
size="sm",
)
wi_baseline_dict = {}
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Patient Characteristics")
for f in PATIENT_FEATURES:
wi_baseline_dict[f] = make_component(f)
with gr.Column(scale=1):
gr.Markdown("### Transplant Characteristics")
wi_grouped_base = gr.Dropdown(
choices=GROUPED_REGIMEN_CHOICES, value=None,
label="Published conditioning regimen (Baseline)",
)
wb_donorf = wi_baseline_dict["DONORF"] = make_component("DONORF")
wi_baseline_dict["GRAFTYPE"] = make_component("GRAFTYPE")
wb_condgrpf = wi_baseline_dict["CONDGRPF"] = make_component("CONDGRPF")
wb_condgrp_final = wi_baseline_dict["CONDGRP_FINAL"] = make_component("CONDGRP_FINAL")
wb_atgf = wi_baseline_dict["ATGF"] = make_component("ATGF")
wb_gvhd_final = wi_baseline_dict["GVHD_FINAL"] = make_component("GVHD_FINAL")
wb_hla_final = wi_baseline_dict["HLA_FINAL"] = make_component("HLA_FINAL")
with gr.Column(scale=1):
gr.Markdown("### Disease Characteristics")
for f in DISEASE_FEATURES:
wi_baseline_dict[f] = make_component(f)
wi_baseline_dict["AGE"].change(get_age_group, wi_baseline_dict["AGE"], wi_baseline_dict["AGEGPFF"])
wi_baseline_dict["VOC2YPR"].change(vocfrqpr_from_voc2ypr, wi_baseline_dict["VOC2YPR"], wi_baseline_dict["VOCFRQPR"])
wb_donorf.change(fn=_hla_update_for_donor, inputs=wb_donorf, outputs=wb_hla_final)
wi_grouped_base.change(
apply_grouped_preset, wi_grouped_base,
[wi_grouped_base, wb_donorf, wb_condgrpf, wb_condgrp_final, wb_atgf, wb_gvhd_final, wb_hla_final],
)
wi_baseline_list = [wi_baseline_dict[f] for f in ALL_FEATURES]
def _copy_predict_to_baseline(*vals):
n = len(ALL_FEATURES)
feature_vals = vals[:n]
regimen_value = vals[n] if len(vals) > n else None
preset_outputs = apply_grouped_preset(regimen_value)
regimen_dd_upd = gr.update(value=regimen_value)
donorf_upd, condgrpf_upd, condgrp_final_upd, atgf_upd, gvhd_final_upd, hla_final_upd = preset_outputs[1:]
return (*list(feature_vals), regimen_dd_upd, donorf_upd, condgrpf_upd,
condgrp_final_upd, atgf_upd, gvhd_final_upd, hla_final_upd)
copy_from_predict_btn.click(
fn=_copy_predict_to_baseline,
inputs=inputs_list + [grouped_dd],
outputs=wi_baseline_list + [wi_grouped_base, wb_donorf, wb_condgrpf,
wb_condgrp_final, wb_atgf, wb_gvhd_final, wb_hla_final],
)
gr.Markdown("---")
# ββ SCENARIO PANELS βββββββββββββββββββββββββββββββββββββββββββββββ
scenario_dicts = []
scenario_lists = []
scenario_grouped_dds = []
scenario_rows = []
scenario_name_inputs = []
scenario_donorf_handles = []
scenario_hla_handles = []
scenario_voc2ypr_handles = []
scenario_age_handles = []
scenario_agegp_handles = []
scenario_copy_btns = []
for s_idx in range(MAX_SCENARIOS):
color = SCENARIO_COLORS[s_idx]
label = f"Scenario {s_idx + 1}"
suffix = f"({label})"
visible_init = (s_idx == 0)
with gr.Row(visible=visible_init) as s_row:
scenario_rows.append(s_row)
with gr.Column():
gr.HTML(
f'<div style="background:{color};color:#fff;font-weight:700;'
f'font-size:15px;padding:8px 14px;border-radius:6px;margin-bottom:6px;">'
f'Counterfactual {label}</div>'
)
with gr.Row():
s_name_input = gr.Textbox(
label=f"Scenario name ({label})",
value=label,
placeholder=f"e.g. {label}",
scale=2,
)
scenario_name_inputs.append(s_name_input)
copy_btn = gr.Button(
f"β¬ Copy Baseline β {label}",
elem_classes="copy-button",
size="sm",
scale=1,
)
scenario_copy_btns.append(copy_btn)
s_dict = {}
with gr.Row():
with gr.Column(scale=1):
gr.Markdown(f"#### Patient β {label}")
for f in PATIENT_FEATURES:
s_dict[f] = make_component(f, suffix)
with gr.Column(scale=1):
gr.Markdown(f"#### Transplant β {label}")
s_grouped_dd = gr.Dropdown(
choices=GROUPED_REGIMEN_CHOICES, value=None,
label=f"Published regimen ({label})",
)
scenario_grouped_dds.append(s_grouped_dd)
s_donorf = s_dict["DONORF"] = make_component("DONORF", suffix)
s_dict["GRAFTYPE"] = make_component("GRAFTYPE", suffix)
s_condgrpf = s_dict["CONDGRPF"] = make_component("CONDGRPF", suffix)
s_condgrp_final = s_dict["CONDGRP_FINAL"] = make_component("CONDGRP_FINAL", suffix)
s_atgf = s_dict["ATGF"] = make_component("ATGF", suffix)
s_gvhd_final = s_dict["GVHD_FINAL"] = make_component("GVHD_FINAL", suffix)
s_hla_final = s_dict["HLA_FINAL"] = make_component("HLA_FINAL", suffix)
with gr.Column(scale=1):
gr.Markdown(f"#### Disease β {label}")
for f in DISEASE_FEATURES:
s_dict[f] = make_component(f, suffix)
scenario_dicts.append(s_dict)
scenario_lists.append([s_dict[f] for f in ALL_FEATURES])
scenario_donorf_handles.append(s_donorf)
scenario_hla_handles.append(s_hla_final)
scenario_voc2ypr_handles.append(s_dict["VOC2YPR"])
scenario_age_handles.append(s_dict["AGE"])
scenario_agegp_handles.append(s_dict["AGEGPFF"])
s_dict["AGE"].change(get_age_group, s_dict["AGE"], s_dict["AGEGPFF"])
s_dict["VOC2YPR"].change(vocfrqpr_from_voc2ypr, s_dict["VOC2YPR"], s_dict["VOCFRQPR"])
s_donorf.change(fn=_hla_update_for_donor, inputs=s_donorf, outputs=s_hla_final)
s_grouped_dd.change(
apply_grouped_preset, s_grouped_dd,
[s_grouped_dd, s_donorf, s_condgrpf, s_condgrp_final, s_atgf, s_gvhd_final, s_hla_final],
)
wi_baseline_dict["SEX"].change(
fn=lock_sex,
inputs=wi_baseline_dict["SEX"],
outputs=s_dict["SEX"],
)
s_dict["SEX"].interactive = False
def _make_copy_fn(s_grouped_dd_ref, s_donorf_ref, s_condgrpf_ref,
s_condgrp_final_ref, s_atgf_ref, s_gvhd_final_ref,
s_hla_final_ref):
def _copy(*vals):
n = len(ALL_FEATURES)
feature_vals = vals[:n]
regimen_value = vals[n] if len(vals) > n else None
preset_outputs = apply_grouped_preset(regimen_value)
regimen_dd_upd = gr.update(value=regimen_value)
d_upd, c_upd, cr_upd, a_upd, g_upd, h_upd = preset_outputs[1:]
return (*list(feature_vals), regimen_dd_upd,
d_upd, c_upd, cr_upd, a_upd, g_upd, h_upd)
return _copy
copy_btn.click(
fn=_make_copy_fn(s_grouped_dd, s_donorf, s_condgrpf,
s_condgrp_final, s_atgf, s_gvhd_final, s_hla_final),
inputs=wi_baseline_list + [wi_grouped_base],
outputs=(scenario_lists[-1]
+ [s_grouped_dd, s_donorf, s_condgrpf,
s_condgrp_final, s_atgf, s_gvhd_final, s_hla_final]),
)
copy_btn.click(
fn=lock_sex,
inputs=wi_baseline_dict["SEX"],
outputs=s_dict["SEX"],
)
# ββ Slider β show/hide scenario panels βββββββββββββββββββββββββββ
def _update_scenario_visibility(n):
return [gr.update(visible=(i < int(n))) for i in range(MAX_SCENARIOS)]
n_scenarios_slider.change(
fn=_update_scenario_visibility,
inputs=n_scenarios_slider,
outputs=scenario_rows,
)
n_scenarios_slider.change(
fn=lambda n: int(n),
inputs=n_scenarios_slider,
outputs=n_scenarios_state,
)
# ββ RUN ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
gr.Markdown("---")
wi_run_btn = gr.Button(
"Run Counterfactual Comparison",
elem_classes="counterfactual-button",
size="lg",
)
# ββ RESULTS ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
gr.Markdown("## Comparison Results")
wi_violation_html = gr.HTML()
gr.Markdown("### Outcome Probability Table")
wi_table_html = gr.HTML()
gr.Markdown("---")
# ββ Icon Arrays β comparison, Python-driven carousel βββββββββββββ
with gr.Accordion("Outcome Icon Arrays β Baseline vs Scenarios", open=False):
gr.Markdown(
"*One row per scenario. Use the β β arrows to browse outcomes one at a time.*"
)
wi_cmp_probs_state = gr.State(None) # list of dicts
wi_cmp_labels_state = gr.State(None) # list of labels
wi_cmp_colors_state = gr.State(None) # list of colors
wi_cmp_idx_state = gr.State(0)
with gr.Row():
wi_cmp_prev_btn = gr.Button("β", elem_classes="icon-nav-button", size="sm", scale=0)
wi_icon_html = gr.HTML(scale=4)
wi_cmp_next_btn = gr.Button("β", elem_classes="icon-nav-button", size="sm", scale=0)
def _on_cmp_prev(idx, probs_list, labels, colors):
if probs_list is None:
return idx, ""
new_idx = max(0, idx - 1)
return new_idx, _render_comparison_icon_page(probs_list, labels, colors, new_idx)
def _on_cmp_next(idx, probs_list, labels, colors):
if probs_list is None:
return idx, ""
new_idx = min(len(ICON_OUTCOMES) - 1, idx + 1)
return new_idx, _render_comparison_icon_page(probs_list, labels, colors, new_idx)
wi_cmp_prev_btn.click(
fn=_on_cmp_prev,
inputs=[wi_cmp_idx_state, wi_cmp_probs_state, wi_cmp_labels_state, wi_cmp_colors_state],
outputs=[wi_cmp_idx_state, wi_icon_html],
)
wi_cmp_next_btn.click(
fn=_on_cmp_next,
inputs=[wi_cmp_idx_state, wi_cmp_probs_state, wi_cmp_labels_state, wi_cmp_colors_state],
outputs=[wi_cmp_idx_state, wi_icon_html],
)
gr.Markdown("---")
# ββ SHAP Carousels β one per scenario slot ββββββββββββββββββββββββ
with gr.Accordion("SHAP Feature Importance", open=False):
# Baseline SHAP carousel
gr.Markdown("### Baseline")
wi_shap_base_store = gr.State(None)
wi_shap_base_idx = gr.State(0)
with gr.Row():
wb_shap_prev = gr.Button("β", elem_classes="shap-nav-button", size="sm", scale=0)
wb_shap_crumb = gr.HTML(value="", scale=4)
wb_shap_next = gr.Button("β", elem_classes="shap-nav-button", size="sm", scale=0)
wb_shap_plot = gr.Plot(label="Baseline β SHAP")
def _wb_prev(idx, plots):
new_idx = max(0, idx - 1)
plot = plots[SHAP_ORDER[new_idx]] if plots else None
return new_idx, plot, _shap_counter_html(new_idx)
def _wb_next(idx, plots):
new_idx = min(len(SHAP_ORDER) - 1, idx + 1)
plot = plots[SHAP_ORDER[new_idx]] if plots else None
return new_idx, plot, _shap_counter_html(new_idx)
wb_shap_prev.click(_wb_prev, [wi_shap_base_idx, wi_shap_base_store],
[wi_shap_base_idx, wb_shap_plot, wb_shap_crumb])
wb_shap_next.click(_wb_next, [wi_shap_base_idx, wi_shap_base_store],
[wi_shap_base_idx, wb_shap_plot, wb_shap_crumb])
# Per-scenario SHAP carousels
wi_shap_scen_stores = []
wi_shap_scen_idxs = []
wi_shap_scen_plots = []
wi_shap_scen_crumbs = []
wi_shap_scen_rows = []
for s_idx in range(MAX_SCENARIOS):
with gr.Row(visible=(s_idx == 0)) as shap_row:
wi_shap_scen_rows.append(shap_row)
with gr.Column():
scen_color = SCENARIO_COLORS[s_idx]
gr.HTML(
f'<div style="font-weight:700;font-size:13px;color:{scen_color};">'
f'Scenario {s_idx+1}</div>'
)
s_store = gr.State(None)
s_idx_s = gr.State(0)
wi_shap_scen_stores.append(s_store)
wi_shap_scen_idxs.append(s_idx_s)
with gr.Row():
s_prev_btn = gr.Button("β", elem_classes="shap-nav-button", size="sm", scale=0)
s_crumb = gr.HTML(value="", scale=4)
s_next_btn = gr.Button("β", elem_classes="shap-nav-button", size="sm", scale=0)
s_plot = gr.Plot(label=f"Scenario {s_idx+1} β SHAP")
wi_shap_scen_plots.append(s_plot)
wi_shap_scen_crumbs.append(s_crumb)
def _make_prev(st, ix):
def fn(idx, plots):
new_idx = max(0, idx - 1)
plot = plots[SHAP_ORDER[new_idx]] if plots else None
return new_idx, plot, _shap_counter_html(new_idx)
return fn
def _make_next(st, ix):
def fn(idx, plots):
new_idx = min(len(SHAP_ORDER) - 1, idx + 1)
plot = plots[SHAP_ORDER[new_idx]] if plots else None
return new_idx, plot, _shap_counter_html(new_idx)
return fn
s_prev_btn.click(
_make_prev(s_store, s_idx_s),
[s_idx_s, s_store],
[s_idx_s, s_plot, s_crumb],
)
s_next_btn.click(
_make_next(s_store, s_idx_s),
[s_idx_s, s_store],
[s_idx_s, s_plot, s_crumb],
)
# Sync SHAP scenario row visibility with slider
def _update_shap_vis(n):
return [gr.update(visible=(i < int(n))) for i in range(MAX_SCENARIOS)]
n_scenarios_slider.change(
fn=_update_shap_vis,
inputs=n_scenarios_slider,
outputs=wi_shap_scen_rows,
)
# ββ RUN callback ββββββββββββββββββββββββββββββββββββββββββββββββββ
def run_counterfactual(*all_values):
n_feat = len(ALL_FEATURES)
n_scen = int(all_values[0])
base_vals = all_values[1 : 1 + n_feat]
name_offset = 1 + n_feat
scenario_names_raw = all_values[name_offset : name_offset + MAX_SCENARIOS]
feat_offset = name_offset + MAX_SCENARIOS
scenario_val_blocks = [
all_values[feat_offset + s * n_feat : feat_offset + (s + 1) * n_feat]
for s in range(MAX_SCENARIOS)
]
def _empty_outputs():
# violation, table, cmp_probs, cmp_labels, cmp_colors, cmp_idx, icon_html
# base_store, base_idx, base_plot, base_crumb
# per scenario: store, idx, plot, crumb
out = ["", "", None, None, None, 0, ""]
out += [None, 0, None, ""]
for _ in range(MAX_SCENARIOS):
out += [None, 0, None, ""]
return tuple(out)
try:
base_dict = _values_to_dict(base_vals)
_check_missing(base_dict, "Baseline")
all_violations = []
scenario_dicts_active = []
for s in range(n_scen):
sd = _values_to_dict(scenario_val_blocks[s])
_check_missing(sd, f"Scenario {s+1}")
v = _validate_counterfactual_constraints(base_dict, sd, f"Scenario {s+1}")
all_violations.extend(v)
scenario_dicts_active.append(sd)
if all_violations:
out = [_build_violation_html(all_violations), "", None, None, None, 0, ""]
out += [None, 0, None, ""]
for _ in range(MAX_SCENARIOS):
out += [None, 0, None, ""]
return tuple(out)
base_probs, base_ci = predict_all_outcomes(
base_dict, use_calibration=True, use_signed_voting=True, n_boot_ci=500
)
scen_probs_list = []
scen_ci_list = []
for sd in scenario_dicts_active:
sp, sci = predict_all_outcomes(
sd, use_calibration=True, use_signed_voting=True, n_boot_ci=500
)
scen_probs_list.append(sp)
scen_ci_list.append(sci)
scen_labels = []
for i in range(n_scen):
raw_name = scenario_names_raw[i] if i < len(scenario_names_raw) else ""
name = str(raw_name).strip() if raw_name else ""
scen_labels.append(name if name else f"Scenario {i+1}")
scen_colors = SCENARIO_COLORS[:n_scen]
table_html = _build_comparison_table_html(
base_probs, base_ci, scen_probs_list, scen_ci_list, scen_labels, scen_colors
)
# Icon carousel: store all probs + labels + colors in State
all_probs_list = [base_probs] + scen_probs_list
all_labels = ["Baseline"] + scen_labels
all_colors = ["#1565c0"] + scen_colors
icon_html_first = _render_comparison_icon_page(all_probs_list, all_labels, all_colors, 0)
# SHAP β baseline
base_shap_plots = create_all_shap_plots(base_dict, max_display=10)
first_base_plot = base_shap_plots[SHAP_ORDER[0]]
base_crumb = _shap_counter_html(0)
# SHAP β scenarios
scen_shap_data = []
for s in range(MAX_SCENARIOS):
if s < n_scen:
sp_plots = create_all_shap_plots(scenario_dicts_active[s], max_display=10)
scen_shap_data.append((sp_plots, sp_plots[SHAP_ORDER[0]], _shap_counter_html(0)))
else:
scen_shap_data.append((None, None, ""))
out = [
"", # violation html
table_html, # table
all_probs_list, # cmp_probs state
all_labels, # cmp_labels state
all_colors, # cmp_colors state
0, # cmp_idx state
icon_html_first, # icon display
base_shap_plots, # base shap store
0, # base shap idx
first_base_plot, # base shap plot
base_crumb, # base shap crumb
]
for s in range(MAX_SCENARIOS):
store, plot0, crumb0 = scen_shap_data[s]
out += [store, 0, plot0, crumb0]
return tuple(out)
except Exception as e:
print(traceback.format_exc())
raise gr.Error(f"{type(e).__name__}: {str(e)}")
# Build output list
all_run_outputs = (
[wi_violation_html, wi_table_html,
wi_cmp_probs_state, wi_cmp_labels_state, wi_cmp_colors_state,
wi_cmp_idx_state, wi_icon_html]
+ [wi_shap_base_store, wi_shap_base_idx, wb_shap_plot, wb_shap_crumb]
+ [item for s in range(MAX_SCENARIOS)
for item in (wi_shap_scen_stores[s], wi_shap_scen_idxs[s],
wi_shap_scen_plots[s], wi_shap_scen_crumbs[s])]
)
all_run_inputs = (
[n_scenarios_state]
+ wi_baseline_list
+ scenario_name_inputs
+ [feat for s_list in scenario_lists for feat in s_list]
)
wi_run_btn.click(
fn=run_counterfactual,
inputs=all_run_inputs,
outputs=all_run_outputs,
)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
demo.launch(ssr_mode=False) |