Spaces:
Runtime error
Runtime error
File size: 105,351 Bytes
8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 3fc6f6d 8018595 |
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 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 |
"""Generate PDF documentation for Sentinel risk models."""
import argparse
import importlib
import inspect
import re
from collections import defaultdict
from collections.abc import Iterable, Iterator
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Any, Union, get_args, get_origin
import matplotlib.pyplot as plt
from annotated_types import Ge, Gt, Le, Lt
from fpdf import FPDF
from pydantic import BaseModel
from sentinel.models import Sex
from sentinel.risk_models.base import RiskModel
from sentinel.risk_models.qcancer import (
FEMALE_CANCER_TYPES as QC_FEMALE_CANCERS,
)
from sentinel.risk_models.qcancer import (
MALE_CANCER_TYPES as QC_MALE_CANCERS,
)
from sentinel.user_input import GeneticMutation, Lifestyle, UserInput
# Constants
HERE = Path(__file__).resolve().parent
PROJECT_ROOT = HERE.parent
MODELS_DIR = PROJECT_ROOT / "src" / "sentinel" / "risk_models"
OUTPUT_DIR = PROJECT_ROOT / "docs"
CHART_FILE = OUTPUT_DIR / "cancer_coverage.png"
# Palette & typography
THEME_PRIMARY = (59, 130, 246) # Modern blue
THEME_ACCENT = (16, 185, 129) # Modern green
THEME_MUTED = (107, 114, 128) # Modern gray
TEXT_DARK = (31, 41, 55) # Darker text
TEXT_LIGHT = (255, 255, 255)
CARD_BACKGROUND = (249, 250, 251) # Very light gray
TABLE_HEADER_BACKGROUND = (75, 85, 99) # Modern dark gray
ROW_BACKGROUND_LIGHT = (255, 255, 255)
ROW_BACKGROUND_ALT = (246, 248, 255)
TABLE_BORDER = (216, 222, 233)
class LinkManager:
"""Manages PDF hyperlinks for navigation between sections."""
def __init__(self):
self.model_path_to_link_id: dict[str, int] = {}
self.next_link_id = 1
self.pending_links: list[tuple[str, float, float, float, float]] = []
def get_or_create_link_id(self, model_path: str) -> int:
"""Get or create a link ID for a model path.
Args:
model_path: The path to the model
Returns:
The link ID for the model path
"""
if model_path not in self.model_path_to_link_id:
self.model_path_to_link_id[model_path] = self.next_link_id
self.next_link_id += 1
return self.model_path_to_link_id[model_path]
def create_link_destination(self, pdf: FPDF, model_path: str) -> None:
"""Create a link destination in the PDF for a model path.
Args:
pdf: The PDF instance
model_path: The path to the model
"""
link_id = self.get_or_create_link_id(model_path)
pdf.set_link(link_id, y=pdf.get_y())
def store_link_info(
self,
model_path: str,
x_position: float,
y_position: float,
width: float,
height: float,
) -> None:
"""Store link information for later creation.
Args:
model_path: The path to the model
x_position: X position
y_position: Y position
width: Link width
height: Link height
"""
self.pending_links.append((model_path, x_position, y_position, width, height))
def create_pending_links(self, pdf: FPDF) -> None:
"""Create all pending links after destinations are created.
Args:
pdf: The PDF instance
"""
for model_path, x_position, y_position, width, height in self.pending_links:
link_id = self.get_or_create_link_id(model_path)
pdf.link(x_position, y_position, width, height, link_id)
# ---------------------------------------------------------------------------
# Metadata extraction helpers
# ---------------------------------------------------------------------------
NUMERIC_CONSTRAINT_TYPES = (Ge, Gt, Le, Lt)
def _get_enum_choices(annotation: Any) -> list[str] | None:
try:
members = getattr(annotation, "__members__", None)
if members:
return [
member.value if hasattr(member, "value") else member
for member in members.values()
]
except Exception: # pragma: no cover - defensive
return None
return None
def extract_enum_info(enum_class: type[Enum]) -> dict:
"""Extract enum values and descriptions from docstring.
Args:
enum_class: The enum class to extract information from
Returns:
dict with:
- name: Enum class name
- description: Class-level description
- values: List of (value_name, value, description) tuples
"""
name = enum_class.__name__
docstring = enum_class.__doc__ or ""
# Extract class description (first paragraph before Attributes)
description = ""
if docstring:
# Split by "Attributes:" to get the main description
parts = docstring.split("Attributes:")
if parts:
description = parts[0].strip()
# Clean up the description
description = re.sub(r"\s+", " ", description)
description = description.replace("\n", " ").strip()
# Extract individual value descriptions from Attributes section
value_descriptions = {}
if "Attributes:" in docstring:
attributes_section = docstring.split("Attributes:")[1]
# Parse lines like "VALUE_NAME: Description text"
for line in attributes_section.split("\n"):
line = line.strip()
if ":" in line and not line.startswith(" "):
parts = line.split(":", 1)
if len(parts) == 2:
value_name = parts[0].strip()
value_desc = parts[1].strip()
value_descriptions[value_name] = value_desc
# Build values list
values = []
for member_name, member_value in enum_class.__members__.items():
description_text = value_descriptions.get(member_name, "")
values.append((member_name, member_value.value, description_text))
return {"name": name, "description": description, "values": values}
def count_genomic_mutations() -> int:
"""Count the number of genomic mutation values available.
Returns:
int: Number of genetic mutation enum values available
"""
return len(GeneticMutation.__members__)
def count_lifestyle_factors() -> int:
"""Count the number of fields in the Lifestyle model.
Returns:
int: Number of fields in the Lifestyle model
"""
return len(Lifestyle.model_fields)
def count_total_input_categories() -> int:
"""Count total number of leaf field categories across UserInput.
Returns:
int: Total number of leaf field categories across all models
"""
structure = traverse_user_input_structure(UserInput)
# Count only leaf fields (where model_class is None)
return sum(1 for _, _, model_class in structure if model_class is None)
def count_total_discrete_choices() -> int:
"""Count total number of enum member values across all enums.
Returns:
int: Total number of enum member values across all enums
"""
enums_by_parent = collect_all_enums()
total = 0
for parent_enums in enums_by_parent.values():
for enum_info in parent_enums.values():
total += len(enum_info["values"])
return total
def collect_all_enums() -> dict[str, dict]:
"""Collect all enum types used in UserInput, grouped by parent model.
Returns:
dict: {parent_model_name: {enum_name: enum_info}}
"""
enums_by_parent = defaultdict(dict)
seen_enums = set()
def extract_enums_from_type(field_type: Any, parent_path: str = "") -> None:
"""Recursively extract enum types from field annotations.
Args:
field_type: The field type annotation to check
parent_path: The path to the parent model
"""
# Check if it's a direct enum
if hasattr(field_type, "__members__"):
if field_type not in seen_enums:
seen_enums.add(field_type)
enum_info = extract_enum_info(field_type)
parent_name = parent_path.split(".")[0] if parent_path else "Root"
enums_by_parent[parent_name][enum_info["name"]] = enum_info
return
# Check Union types
if hasattr(field_type, "__args__"):
for arg in field_type.__args__:
extract_enums_from_type(arg, parent_path)
# Check if it's a list or other container
origin = get_origin(field_type)
if origin is list and hasattr(field_type, "__args__"):
args = get_args(field_type)
if args:
extract_enums_from_type(args[0], parent_path)
# Traverse UserInput structure to find all enum fields
structure = traverse_user_input_structure(UserInput)
for field_path, _field_name, model_class in structure:
if model_class is not None:
# This is a nested model, check its fields
for _field_name_inner, field_info in model_class.model_fields.items():
field_type = field_info.annotation
extract_enums_from_type(field_type, field_path)
else:
# This is a leaf field, check its type
if "." in field_path:
parent_path = ".".join(field_path.split(".")[:-1])
else:
parent_path = ""
# Get the field from UserInput
current_model = UserInput
for part in field_path.split("."):
if hasattr(current_model, part):
field_info = current_model.model_fields.get(part)
if field_info:
extract_enums_from_type(field_info.annotation, parent_path)
break
return dict(enums_by_parent)
def render_enum_choices_section(
pdf: FPDF, enums_by_parent: dict, link_manager: LinkManager
) -> None:
"""Render Section 3 with enum tables grouped by parent model.
Args:
pdf: Active PDF instance
enums_by_parent: Dict of enums grouped by parent model
link_manager: Link manager for creating hyperlinks
"""
section_counter = 1
for parent_name, enums in enums_by_parent.items():
if not enums:
continue
# Add subsection heading
pdf.set_font("Helvetica", "B", 12)
pdf.set_text_color(*THEME_PRIMARY)
# Capitalize first letter and remove "Enums" suffix
formatted_parent_name = parent_name.replace("_", " ").title()
# Create link destination for this subsection
subsection_link_id = f"enum_{parent_name}"
link_manager.create_link_destination(pdf, subsection_link_id)
pdf.write(7, f"3.{section_counter} {formatted_parent_name}")
pdf.ln(8)
for enum_name, enum_info in enums.items():
# Create link destination for the enum
enum_link_id = f"enum_{enum_name}"
link_manager.create_link_destination(pdf, enum_link_id)
# Add enum heading with name and description
pdf.set_font("Helvetica", "B", 11)
pdf.set_text_color(*THEME_PRIMARY)
# Format enum name: convert CamelCase to "Camel Case" and capitalize
formatted_enum_name = " ".join(
word.capitalize()
for word in re.findall(r"[A-Z][a-z]*|[a-z]+", enum_name)
)
pdf.write(7, f"{formatted_enum_name}")
pdf.ln(6)
# Add enum description if available
if enum_info["description"]:
pdf.set_font("Helvetica", "", 9)
pdf.set_text_color(*TEXT_DARK)
pdf.multi_cell(0, 4, enum_info["description"], 0, "L")
pdf.ln(3)
# Create table for enum values
table_width = pdf.w - 2 * pdf.l_margin
value_width = table_width * 0.25
description_width = table_width * 0.75
# Table header
pdf.set_font("Helvetica", "B", 10)
pdf.set_fill_color(*TABLE_HEADER_BACKGROUND)
pdf.set_text_color(*TEXT_LIGHT)
pdf.cell(value_width, 8, "Value", 0, 0, "L", True)
pdf.cell(description_width, 8, "Description", 0, 1, "L", True)
# Table rows with alternating colors
pdf.set_font("Helvetica", "", 9)
pdf.set_text_color(*TEXT_DARK)
line_height = 5.5
for idx, (_value_name, value, description) in enumerate(
enum_info["values"]
):
# Check if we need a page break
if pdf.get_y() + 15 > pdf.h - pdf.b_margin:
pdf.add_page()
# Re-render table header on new page
pdf.set_font("Helvetica", "B", 10)
pdf.set_fill_color(*TABLE_HEADER_BACKGROUND)
pdf.set_text_color(*TEXT_LIGHT)
pdf.cell(value_width, 8, "Value", 0, 0, "L", True)
pdf.cell(description_width, 8, "Description", 0, 1, "L", True)
pdf.set_font("Helvetica", "", 9)
pdf.set_text_color(*TEXT_DARK)
# Alternate row colors
fill_color = (
ROW_BACKGROUND_LIGHT if idx % 2 == 0 else ROW_BACKGROUND_ALT
)
# Use the same table row rendering as Section 2
render_table_row(
pdf,
[str(value), description or "-"],
[value_width, description_width],
line_height,
fill_color,
)
pdf.ln(4)
section_counter += 1
def render_risk_models_section(
pdf: FPDF, models: list[RiskModel], link_manager: LinkManager
) -> None:
"""Render Section 4 with detailed risk model documentation.
For each model, display:
- Model name (as section heading)
- Description
- Reference/citation
- Cancer types covered
- Table of admissible input fields
Args:
pdf: Active PDF instance
models: List of risk model instances
link_manager: Link manager for creating hyperlinks
"""
section_counter = 1
for model in models:
# Add subsection heading for each model
pdf.set_font("Helvetica", "B", 12)
pdf.set_text_color(*THEME_PRIMARY)
# Create link destination for this model
model_link_id = f"model_{model.__class__.__name__}"
link_manager.create_link_destination(pdf, model_link_id)
model_name = getattr(model, "name", model.__class__.__name__)
# Convert underscores to spaces and capitalize each word
formatted_name = model_name.replace("_", " ").title()
pdf.write(7, f"4.{section_counter} {formatted_name}")
pdf.ln(8)
# Extract model information
model_info = extract_model_metadata(model)
# Render model description
if model_info["description"]:
pdf.set_font("Helvetica", "", 10)
pdf.set_text_color(*TEXT_DARK)
pdf.multi_cell(0, 5, model_info["description"], 0, "L")
pdf.ln(3)
# Render interpretation if available
if model_info["interpretation"]:
pdf.set_font("Helvetica", "B", 10)
pdf.set_text_color(*THEME_PRIMARY)
pdf.cell(0, 5, "Interpretation:", 0, 1)
pdf.set_font("Helvetica", "", 9)
pdf.set_text_color(*TEXT_DARK)
pdf.multi_cell(0, 4, model_info["interpretation"], 0, "L")
pdf.ln(3)
# Render reference if available
if model_info["reference"]:
pdf.set_font("Helvetica", "B", 10)
pdf.set_text_color(*THEME_PRIMARY)
pdf.cell(0, 5, "Reference:", 0, 1)
pdf.set_font("Helvetica", "I", 9)
pdf.set_text_color(*THEME_MUTED)
pdf.multi_cell(0, 4, model_info["reference"], 0, "L")
pdf.ln(3)
# Render cancer types covered
cancer_types = cancer_types_for_model(model)
if cancer_types:
pdf.set_font("Helvetica", "B", 10)
pdf.set_text_color(*THEME_PRIMARY)
pdf.cell(0, 5, "Cancer Types Covered:", 0, 1)
pdf.set_font("Helvetica", "", 9)
pdf.set_text_color(*THEME_MUTED)
cancer_types_str = ", ".join(cancer_types)
pdf.multi_cell(0, 4, cancer_types_str, 0, "L")
pdf.ln(3)
# Add subheading for input requirements
pdf.set_font("Helvetica", "B", 11)
pdf.set_text_color(*THEME_PRIMARY)
pdf.cell(0, 6, "Input Requirements", 0, 1)
pdf.ln(2)
# Render input requirements table
render_model_input_table(pdf, model, link_manager)
# Add light horizontal separator between models (except for the last one)
if section_counter < len(models):
pdf.ln(6)
pdf.set_draw_color(200, 200, 200) # Light gray color
# Shorter separator - about 60% of page width, centered
separator_width = (pdf.w - pdf.l_margin - pdf.r_margin) * 0.6
separator_start = (
pdf.l_margin
+ (pdf.w - pdf.l_margin - pdf.r_margin - separator_width) / 2
)
pdf.line(
separator_start,
pdf.get_y(),
separator_start + separator_width,
pdf.get_y(),
)
pdf.ln(6)
else:
pdf.ln(8)
section_counter += 1
def extract_model_metadata(model: RiskModel) -> dict:
"""Extract metadata from a risk model.
Args:
model: Risk model instance
Returns:
Dictionary with description, reference, interpretation, and other metadata
"""
# Get description from method
description = ""
if hasattr(model, "description") and callable(model.description):
description = model.description()
# Get interpretation from method
interpretation = ""
if hasattr(model, "interpretation") and callable(model.interpretation):
interpretation = model.interpretation()
# Get references from method
references = []
if hasattr(model, "references") and callable(model.references):
references = model.references()
# Format references as a single string
reference = ""
if references:
reference = "; ".join(references)
return {
"description": description,
"interpretation": interpretation,
"reference": reference,
}
def get_field_info_from_user_input(field_path: str) -> dict:
"""Extract description and examples from UserInput for a field path.
Args:
field_path: Full path like "demographics.age_years"
Returns:
Dict with 'description' and 'examples'
"""
# Navigate the field path
parts = field_path.split(".")
current_model = UserInput
for part in parts:
if hasattr(current_model, "model_fields"):
field_info = current_model.model_fields.get(part)
if field_info:
# Get description from field metadata
description = field_info.description or ""
# Get examples from field metadata
examples = (
field_info.json_schema_extra.get("examples", [])
if field_info.json_schema_extra
else []
)
examples_str = (
", ".join(str(example) for example in examples[:3])
if examples
else ""
)
# If this is the last part, return the info
if part == parts[-1]:
return {"description": description, "examples": examples_str}
# Otherwise, navigate deeper
current_model = field_info.annotation
return {"description": "", "examples": ""}
def has_different_constraints(model_field_type, _user_input_field_type) -> bool:
"""Check if model field type has different constraints than UserInput.
Args:
model_field_type: Field type from model's REQUIRED_INPUTS
_user_input_field_type: Field type from UserInput (unused)
Returns:
True if constraints are different, False otherwise
"""
# Check if model field type is Annotated[..., Field(...)]
# This indicates the model is redefining the format
if get_origin(model_field_type) is not None:
# It's an Annotated type, check if it contains Field metadata
args = get_args(model_field_type)
if args:
# Check if any of the metadata contains Field instances
for arg in args[1:]: # Skip the first arg (the actual type)
if hasattr(arg, "__class__") and "Field" in str(arg.__class__):
return True
return False
def extract_constraint_text(field_type) -> str:
"""Extract just the constraint text from a field type.
Args:
field_type: Field type from model's REQUIRED_INPUTS
Returns:
Constraint text like ">=0 to <=120" or "2 choices"
"""
# Check if it's an Annotated type
if get_origin(field_type) is not None:
args = get_args(field_type)
if args:
# Look for FieldInfo metadata in the arguments
for arg in args[1:]: # Skip the first arg (the actual type)
if hasattr(arg, "__class__") and "FieldInfo" in str(arg.__class__):
# Extract constraints from FieldInfo metadata
constraints = []
if hasattr(arg, "metadata") and arg.metadata:
for metadata_item in arg.metadata:
if (
hasattr(metadata_item, "ge")
and metadata_item.ge is not None
):
constraints.append(f">={metadata_item.ge}")
if (
hasattr(metadata_item, "le")
and metadata_item.le is not None
):
constraints.append(f"<={metadata_item.le}")
if (
hasattr(metadata_item, "gt")
and metadata_item.gt is not None
):
constraints.append(f">{metadata_item.gt}")
if (
hasattr(metadata_item, "lt")
and metadata_item.lt is not None
):
constraints.append(f"<{metadata_item.lt}")
if (
hasattr(metadata_item, "min_length")
and metadata_item.min_length is not None
):
constraints.append(
f"min_length={metadata_item.min_length}"
)
if (
hasattr(metadata_item, "max_length")
and metadata_item.max_length is not None
):
constraints.append(
f"max_length={metadata_item.max_length}"
)
if constraints:
return " to ".join(constraints)
# Check if it's an enum type
if hasattr(field_type, "__members__"):
return f"{len(field_type.__members__)} choices"
# Check if it's a Union type with enums
if get_origin(field_type) is Union:
args = get_args(field_type)
for arg in args:
if hasattr(arg, "__members__"):
return f"{len(arg.__members__)} choices"
return "any"
def get_user_input_field_type(field_path: str):
"""Get the field type from UserInput for a given field path.
Args:
field_path: Full path like "demographics.age_years"
Returns:
Field type from UserInput or None if not found
"""
# Navigate the field path
parts = field_path.split(".")
current_model = UserInput
for part in parts:
if hasattr(current_model, "model_fields"):
field_info = current_model.model_fields.get(part)
if field_info:
# If this is the last part, return the field type
if part == parts[-1]:
return field_info.annotation
# Otherwise, navigate deeper
current_model = field_info.annotation
return None
def render_model_input_table(
pdf: FPDF, model: RiskModel, _link_manager: LinkManager
) -> None:
"""Render input requirements table with 2 columns and hyperlinks.
Columns:
- Field Name (with hyperlinks to Section 2)
- Format (model-specific or "same as user input")
Args:
pdf: Active PDF instance
model: Risk model instance
_link_manager: Link manager for creating hyperlinks (unused)
"""
requirements = extract_model_requirements(model)
if not requirements:
pdf.set_font("Helvetica", "", 10)
pdf.cell(0, 6, "No input requirements defined for this model.", 0, 1)
pdf.ln(4)
return
# Table dimensions - only 2 columns now
table_width = pdf.w - 2 * pdf.l_margin
field_width = table_width * 0.60 # Increased since we only have 2 columns
format_width = table_width * 0.40
col_widths = [field_width, format_width]
# Table header function for reuse on page breaks
def render_table_header():
pdf.set_font("Helvetica", "B", 10)
pdf.set_fill_color(*TABLE_HEADER_BACKGROUND)
pdf.set_text_color(*TEXT_LIGHT)
pdf.cell(field_width, 8, "Field Name", 0, 0, "L", True)
pdf.cell(format_width, 8, "Format (when override)", 0, 1, "L", True)
# Add spacing before table header
pdf.ln(6)
render_table_header()
pdf.set_font("Helvetica", "", 9)
pdf.set_text_color(*TEXT_DARK)
line_height = 5.5
# Group fields by parent
grouped_fields = group_fields_by_requirements(requirements)
# Global index for alternating colors across all sub-fields
global_sub_idx = 0
for parent_name, sub_fields in grouped_fields:
# Check if we need a page break before the parent field
if pdf.get_y() + 20 > pdf.h - pdf.b_margin:
pdf.add_page()
# Add spacing after page header and before table header
pdf.ln(3)
render_table_header()
# Reset text color after page break to ensure readability
pdf.set_text_color(*TEXT_DARK)
# Render parent field name (bold) with fixed color
pdf.set_font("Helvetica", "B", 9)
pdf.set_text_color(*TEXT_DARK)
render_table_row(
pdf,
[parent_name, ""],
col_widths,
line_height,
(245, 245, 245), # Light gray for parent rows
)
# Render sub-fields (normal weight, indented) with alternating colors
pdf.set_font("Helvetica", "", 9)
for field_path, field_type, _is_required in sub_fields:
# Format field name
sub_field_name = prettify_field_name(field_path.split(".")[-1])
indented_name = f" {sub_field_name}"
# Determine format text based on whether model overrides UserInput
user_input_field_type = get_user_input_field_type(field_path)
if user_input_field_type and has_different_constraints(
field_type, user_input_field_type
):
# Extract just the constraint part from the field type
format_text = extract_constraint_text(field_type)
else:
# Show "See User Input Doc" instead of "same as user input"
format_text = "See User Input Doc"
# Get position before rendering for hyperlink
x_position = pdf.get_x()
y_position = pdf.get_y()
# Alternate colors for sub-fields using global index
fill_color = (
ROW_BACKGROUND_LIGHT if global_sub_idx % 2 == 0 else ROW_BACKGROUND_ALT
)
render_table_row(
pdf,
[indented_name, format_text],
col_widths,
line_height,
fill_color,
)
# TODO: Add hyperlinks to field names linking to Section 2
# Temporarily disabled to focus on format column functionality
# Increment global index for next sub-field
global_sub_idx += 1
pdf.ln(4)
def _format_type(annotation: Any) -> str:
origin = get_origin(annotation)
args = get_args(annotation)
if origin is list:
inner = args[0] if args else Any
return f"List[{_format_type(inner)}]"
if origin is tuple:
inner = ", ".join(_format_type(arg) for arg in args)
return f"Tuple[{inner}]"
if origin is dict:
key_type = _format_type(args[0]) if args else "Any"
value_type = _format_type(args[1]) if len(args) > 1 else "Any"
return f"Dict[{key_type}, {value_type}]"
if origin is Union and args:
# PEP604 union
return " | ".join(sorted({_format_type(arg) for arg in args}))
if isinstance(annotation, type):
try:
if issubclass(annotation, BaseModel):
return annotation.__name__
except TypeError:
pass
return annotation.__name__
if hasattr(annotation, "__name__"):
return annotation.__name__
return str(annotation)
def _collect_constraints(metadata: Iterable[Any]) -> dict[str, Any]:
constraints: dict[str, Any] = {}
for item in metadata:
if isinstance(item, NUMERIC_CONSTRAINT_TYPES):
attr = item.__class__.__name__.lower()
for key, attr_name in {
"min": "ge",
"min_exclusive": "gt",
"max": "le",
"max_exclusive": "lt",
}.items():
if hasattr(item, attr_name):
constraints[key] = getattr(item, attr_name)
elif hasattr(item, "min_length") or hasattr(item, "max_length"):
for key, attr_name in {
"min_length": "min_length",
"max_length": "max_length",
}.items():
value = getattr(item, attr_name, None)
if value is not None:
constraints[key] = value
return constraints
def parse_annotated_type(field_type: type) -> tuple[type, dict[str, Any]]:
"""Parse Annotated[Type, Field(...)] to extract base type and constraints.
Args:
field_type: The type annotation to parse
Returns:
(base_type, constraints_dict)
"""
origin = get_origin(field_type)
args = get_args(field_type)
if (
origin is not None
and hasattr(origin, "__name__")
and origin.__name__ == "Annotated"
):
# Extract base type and metadata
base_type = args[0] if args else field_type
metadata = args[1:] if len(args) > 1 else []
# Extract constraints from Field(...) metadata
constraints = {}
for item in metadata:
if hasattr(item, "__class__") and hasattr(item.__class__, "__name__"):
if item.__class__.__name__ == "FieldInfo":
# Extract Field constraints
for attr_name in [
"ge",
"gt",
"le",
"lt",
"min_length",
"max_length",
]:
if hasattr(item, attr_name):
value = getattr(item, attr_name)
if value is not None:
constraints[attr_name] = value
elif item.__class__.__name__ in ["Ge", "Gt", "Le", "Lt"]:
# Handle annotated_types constraints
for key, attr_name in {
"ge": "ge",
"gt": "gt",
"le": "le",
"lt": "lt",
}.items():
if hasattr(item, attr_name):
constraints[key] = getattr(item, attr_name)
return base_type, constraints
else:
# Handle Union types like Ethnicity | None
if (
origin is not None
and hasattr(origin, "__name__")
and origin.__name__ == "Union"
):
# Find the non-None type
non_none_types = [arg for arg in args if arg is not type(None)]
if non_none_types:
return non_none_types[0], {}
# Plain type, no constraints
return field_type, {}
def extract_model_requirements(model: RiskModel) -> list[tuple[str, type, bool]]:
"""Extract field requirements from a model's REQUIRED_INPUTS.
Args:
model: The risk model to extract requirements from
Returns:
list of (field_path, original_field_type, is_required) tuples
"""
requirements = []
for field_path, (field_type, is_required) in model.REQUIRED_INPUTS.items():
# Keep the original type annotation with constraints
requirements.append((field_path, field_type, is_required))
return requirements
def traverse_user_input_structure(
model: type[BaseModel], prefix: str = ""
) -> list[tuple[str, str, type[BaseModel] | None]]:
"""Recursively traverse UserInput to find all nested models and leaf fields.
Args:
model: The BaseModel class to traverse
prefix: Current path prefix for nested models
Returns:
list of (path, name, model_class) tuples where:
- path: Full dotted path to the field/model
- name: Display name for the field/model
- model_class: The model class (None for leaf fields)
"""
structure = []
for field_name, field_info in model.model_fields.items():
field_path = f"{prefix}.{field_name}" if prefix else field_name
field_type = field_info.annotation
# First check: Direct BaseModel type
if isinstance(field_type, type) and issubclass(field_type, BaseModel):
structure.append((field_path, prettify_field_name(field_name), field_type))
structure.extend(traverse_user_input_structure(field_type, field_path))
# Second check: Generic Union/Optional handling (works for both Union[T, None] and T | None)
elif hasattr(field_type, "__args__"):
args = field_type.__args__
non_none_types = [arg for arg in args if arg is not type(None)]
if non_none_types:
try:
first_type = non_none_types[0]
# Check if it's a Union with BaseModel
if issubclass(first_type, BaseModel):
nested_model = first_type
structure.append(
(field_path, prettify_field_name(field_name), nested_model)
)
structure.extend(
traverse_user_input_structure(nested_model, field_path)
)
# Check if it's a list
elif (
hasattr(field_type, "__origin__")
and hasattr(field_type.__origin__, "__name__")
and field_type.__origin__.__name__ == "list"
):
# Handle list types - check if it's a list of BaseModel
if issubclass(first_type, BaseModel):
list_item_model = first_type
structure.append(
(
field_path,
prettify_field_name(field_name),
list_item_model,
)
)
# Recursively traverse the list item model
structure.extend(
traverse_user_input_structure(
list_item_model, f"{field_path}[]"
)
)
else:
# List of primitive types (enum, str, int, etc.) - treat as leaf field
structure.append(
(field_path, prettify_field_name(field_name), None)
)
else:
# Leaf field (list of primitives, plain type, etc.)
structure.append(
(field_path, prettify_field_name(field_name), None)
)
except TypeError:
# Not a class, treat as leaf
structure.append(
(field_path, prettify_field_name(field_name), None)
)
else:
# No non-None types, treat as leaf
structure.append((field_path, prettify_field_name(field_name), None))
# Third check: Everything else is a leaf field
else:
structure.append((field_path, prettify_field_name(field_name), None))
return structure
def build_field_usage_map(models: list[RiskModel]) -> dict[str, list[tuple[str, bool]]]:
"""Build mapping of field_path -> [(model_name, is_required), ...]
Args:
models: List of risk model instances
Returns:
dict mapping each field path to list of (model_name, required_flag) tuples
"""
field_usage: dict[str, list[tuple[str, bool]]] = {}
for model in models:
for field_path, (_, is_required) in model.REQUIRED_INPUTS.items():
if field_path not in field_usage:
field_usage[field_path] = []
field_usage[field_path].append((model.name, is_required))
# Sort each field's usage by model name
for field_path in field_usage:
field_usage[field_path].sort(key=lambda x: x[0])
return field_usage
def extract_field_attributes(
field_info, field_type
) -> tuple[str, str, str, str, type | None]:
"""Extract field attributes directly from Field metadata.
Args:
field_info: Pydantic field info object
field_type: The field's type annotation
Returns:
tuple of (description, examples, constraints, used_by, enum_class) strings and enum class
"""
description = "-"
examples = "-"
constraints = "-"
used_by = "-"
enum_class = None
# Extract description from Field
if hasattr(field_info, "description") and field_info.description:
description = field_info.description
# Extract examples from Field - they are directly on the field_info object
if hasattr(field_info, "examples") and field_info.examples:
examples_list = field_info.examples
if isinstance(examples_list, list):
examples = ", ".join(str(ex) for ex in examples_list)
# Extract constraints from Field metadata
constraints_list = []
if hasattr(field_info, "metadata") and field_info.metadata:
for item in field_info.metadata:
if hasattr(item, "__class__") and hasattr(item.__class__, "__name__"):
class_name = item.__class__.__name__
if class_name == "Ge" and hasattr(item, "ge"):
constraints_list.append(f">={item.ge}")
elif class_name == "Gt" and hasattr(item, "gt"):
constraints_list.append(f">{item.gt}")
elif class_name == "Le" and hasattr(item, "le"):
constraints_list.append(f"<={item.le}")
elif class_name == "Lt" and hasattr(item, "lt"):
constraints_list.append(f"<{item.lt}")
elif class_name == "MinLen" and hasattr(item, "min_length"):
constraints_list.append(f"min_length={item.min_length}")
elif class_name == "MaxLen" and hasattr(item, "max_length"):
constraints_list.append(f"max_length={item.max_length}")
if constraints_list:
constraints = ", ".join(constraints_list)
# Add enum count information if the field is an enum
if hasattr(field_type, "__members__"):
enum_count = len(field_type.__members__)
enum_class = field_type
if constraints == "-":
constraints = f"{enum_count} choices"
else:
constraints = f"{constraints}, {enum_count} choices"
elif hasattr(field_type, "__args__"):
# Handle Union types - find enum in union
for arg in field_type.__args__:
if hasattr(arg, "__members__"):
enum_count = len(arg.__members__)
enum_class = arg
if constraints == "-":
constraints = f"{enum_count} choices"
else:
constraints = f"{constraints}, {enum_count} choices"
break
# Add format information for basic types if no constraints
if constraints == "-":
# Check if it's a boolean type
if field_type is bool or (
hasattr(field_type, "__args__") and bool in field_type.__args__
):
constraints = "binary"
# Check if it's a numeric type (int, float) without constraints
elif field_type in (int, float) or (
hasattr(field_type, "__args__")
and any(type_arg in field_type.__args__ for type_arg in (int, float))
):
constraints = "any number"
# Check if it's a Date type
elif (hasattr(field_type, "__name__") and field_type.__name__ == "date") or (
hasattr(field_type, "__args__")
and any(
hasattr(arg, "__name__") and arg.__name__ == "date"
for arg in field_type.__args__
)
):
constraints = "date"
return description, examples, constraints, used_by, enum_class
def format_used_by(usage_list: list[tuple[str, bool]]) -> str:
"""Format the 'Used By' column with model names and req/opt indicators.
Args:
usage_list: List of (model_name, is_required) tuples
Returns:
Formatted string like "gail (req), boadicea (opt), claus (req)"
"""
if not usage_list:
return "-"
formatted_items = []
for model_name, is_required in usage_list:
indicator = "req" if is_required else "opt"
formatted_items.append(f"{model_name} ({indicator})")
return ", ".join(formatted_items)
def render_user_input_hierarchy(
pdf: FPDF, models: list[RiskModel], link_manager: LinkManager
) -> None:
"""Render complete UserInput structure hierarchically.
Args:
pdf: Active PDF instance
models: List of risk model instances
link_manager: Link manager for creating hyperlinks
"""
# Initialize link manager for hyperlinks
link_manager = LinkManager()
# Build field usage mapping
field_usage = build_field_usage_map(models)
# Traverse the UserInput structure
structure = traverse_user_input_structure(UserInput)
# Group structure by parent path to handle mixed parent models properly
parent_to_items = defaultdict(list)
for field_path, field_name, model_class in structure:
# Get parent path (everything before last dot, or empty for top-level)
if "." in field_path:
parent_path = ".".join(field_path.split(".")[:-1])
else:
parent_path = ""
parent_to_items[parent_path].append((field_path, field_name, model_class))
# Render sections in order, ensuring each parent gets its leaf fields rendered
section_counter = 1
# Global row counter for alternating colors across all tables
global_row_counter = [0]
# Process items in the order they appear in the original structure
processed_parents = set()
for field_path, _field_name, _model_class in structure:
# Get parent path for this item
if "." in field_path:
parent_path = ".".join(field_path.split(".")[:-1])
else:
parent_path = ""
# Skip if we've already processed this parent
if parent_path in processed_parents:
continue
# Mark this parent as processed
processed_parents.add(parent_path)
# Get all items for this parent
items = parent_to_items[parent_path]
# Separate leaf fields from nested models
leaf_fields = []
nested_models = []
for item_path, item_name, item_model_class in items:
if item_model_class is not None:
# This is a nested model
nested_models.append((item_path, item_name, item_model_class))
else:
# This is a leaf field
leaf_fields.append((item_path, item_name))
# Render leaf fields for this parent if any exist
if leaf_fields and parent_path:
# Find the parent model info - look for the parent path in the structure
parent_model_info = None
for field_path, field_name, model_class in structure:
if field_path == parent_path and model_class is not None:
parent_model_info = (field_path, field_name, model_class)
break
if parent_model_info:
# Get nested models for this parent
nested_models_for_parent = []
for item_path, item_name, item_model_class in items:
if item_model_class is not None and item_path != parent_path:
nested_models_for_parent.append(
(item_path, item_name, item_model_class)
)
# Create link destination for this section
link_manager.create_link_destination(pdf, parent_path)
# Find parent info for this model (if it's not a root-level model)
parent_info_for_current = None
if parent_path: # Not root level
parent_of_current = (
".".join(parent_path.split(".")[:-1])
if "." in parent_path
else ""
)
if parent_of_current: # Has a parent
# Only create parent info if the parent actually gets rendered (has leaf fields)
parent_items = parent_to_items[parent_of_current]
parent_has_leaf_fields = any(
item_model_class is None
for _, _, item_model_class in parent_items
)
if (
parent_has_leaf_fields
): # Parent gets rendered, so we can reference it
for field_path, field_name, model_class in structure:
if (
field_path == parent_of_current
and model_class is not None
):
parent_info_for_current = (
field_path,
field_name,
model_class,
)
break
render_model_fields_table(
pdf,
parent_model_info,
leaf_fields,
field_usage,
section_counter,
link_manager,
nested_models_for_parent,
parent_info_for_current,
global_row_counter,
)
section_counter += 1
def render_model_fields_table(
pdf: FPDF,
model_info: tuple[str, str, type[BaseModel]],
fields: list[tuple[str, str]],
_field_usage: dict[str, list[tuple[str, bool]]],
section_number: int,
link_manager: LinkManager,
nested_models_info: list[tuple[str, str, type[BaseModel]]] | None = None,
parent_info: tuple[str, str, type[BaseModel]] | None = None,
global_row_counter: list[int] | None = None,
) -> None:
"""Render a table for a specific model's fields.
Args:
pdf: Active PDF instance
model_info: (path, name, model_class) tuple
fields: List of (field_path, field_name) tuples for leaf fields
_field_usage: Mapping of field paths to usage information (unused)
section_number: Section number for heading
link_manager: Link manager for hyperlinks
nested_models_info: List of nested model info tuples
parent_info: Parent model info tuple
global_row_counter: Mutable list containing the global row counter for alternating colors
"""
if not model_info or not fields:
return
_model_path, model_name, model_class = model_info
# Parent reference is now handled in the section title
# Add section heading with uniform rendering technique
pdf.set_font("Helvetica", "B", 12)
pdf.set_text_color(*THEME_PRIMARY)
if parent_info:
parent_path, parent_name, _ = parent_info
parent_link_id = link_manager.get_or_create_link_id(parent_path)
# Render main title in primary color
main_title = f"{section_number}. {model_name} "
pdf.write(7, main_title)
# Get current position for hyperlink
x_position = pdf.get_x()
y_position = pdf.get_y()
# Render parenthetical part in dark gray
parenthetical = f"(from {parent_name})"
pdf.set_text_color(64, 64, 64) # Dark gray color
pdf.write(7, parenthetical)
# Add hyperlink to the parent name within the parenthetical
text_width_before_parent = pdf.get_string_width(f"{main_title}(from ")
parent_text_width = pdf.get_string_width(parent_name)
pdf.link(x_position, y_position, parent_text_width, 7, parent_link_id)
else:
# Render single-color title
title = f"{section_number}. {model_name}"
pdf.write(7, title)
# Add proper line spacing after the title (uniform for all)
pdf.ln(8)
# Create table for this model's fields (removed "Used By" column)
table_width = pdf.w - 2 * pdf.l_margin
field_width = table_width * 0.20
desc_width = table_width * 0.45 # Reduced from 0.50 to make room for examples
examples_width = table_width * 0.20 # Increased from 0.15 to 0.20
constraints_width = table_width * 0.15 # Kept at 0.15
col_widths = [field_width, desc_width, examples_width, constraints_width]
# Table header function for reuse on page breaks
def render_table_header():
pdf.set_font("Helvetica", "B", 10)
pdf.set_fill_color(*TABLE_HEADER_BACKGROUND)
pdf.set_text_color(*TEXT_LIGHT)
pdf.cell(field_width, 8, "Field Name", 0, 0, "L", True)
pdf.cell(desc_width, 8, "Description", 0, 0, "L", True)
pdf.cell(examples_width, 8, "Examples", 0, 0, "L", True)
pdf.cell(constraints_width, 8, "Format", 0, 1, "L", True)
# Check if we need a page break before rendering the table header
# Account for table header height (8) plus some margin
if pdf.get_y() + 15 > pdf.h - pdf.b_margin:
pdf.add_page()
# Add minimal spacing before table header to avoid conflicts with headings
pdf.ln(1)
render_table_header()
# Table rows
pdf.set_font("Helvetica", "", 9)
pdf.set_text_color(*TEXT_DARK)
line_height = 5.5
for field_path, field_name in fields:
# Check if we need a page break before this row
if pdf.get_y() + 15 > pdf.h - pdf.b_margin:
pdf.add_page()
# Add spacing after page header and before table header
pdf.ln(3)
render_table_header()
# Reset text color and font after page break to ensure readability
pdf.set_text_color(*TEXT_DARK)
pdf.set_font("Helvetica", "", 9) # Reset to normal weight for table rows
# Get field info from the model
field_info = model_class.model_fields.get(field_path.split(".")[-1])
if not field_info:
continue
# Extract field attributes
description, examples, constraints, _, enum_class = extract_field_attributes(
field_info, field_info.annotation
)
# Alternate row colors using global counter
if global_row_counter is None:
# This should never happen if called correctly
current_row_index = 0
else:
current_row_index = global_row_counter[0]
# Increment global counter for next row
global_row_counter[0] += 1
fill_color = (
ROW_BACKGROUND_LIGHT if current_row_index % 2 == 0 else ROW_BACKGROUND_ALT
)
# Check if constraints contain enum choices and make them clickable
if enum_class and "choices" in constraints:
render_table_row_with_enum_link(
pdf,
[field_name, description, examples, constraints],
col_widths,
line_height,
fill_color,
enum_class,
link_manager,
)
else:
render_table_row(
pdf,
[field_name, description, examples, constraints],
col_widths,
line_height,
fill_color,
)
# Add nested model rows if any exist
if nested_models_info:
for nested_path, nested_name, _nested_model_class in nested_models_info:
# Get field info for the nested model field
field_name = nested_path.split(".")[-1]
field_info = model_class.model_fields.get(field_name)
if not field_info:
continue
# Extract field attributes
description, _, _, _, _ = extract_field_attributes(
field_info, field_info.annotation
)
# Get the section number for this nested model
nested_link_id = link_manager.get_or_create_link_id(nested_path)
# Create hyperlink text
examples = f"See Section {nested_link_id}"
constraints = "nested object"
# Check if we need a page break before this row
if pdf.get_y() + 15 > pdf.h - pdf.b_margin:
pdf.add_page()
# Add spacing after page header and before table header
pdf.ln(10)
render_table_header()
# Reset text color and font after page break
pdf.set_text_color(*TEXT_DARK)
pdf.set_font("Helvetica", "", 9)
# Use a lighter background for nested model rows, but still alternate
if global_row_counter is None:
current_row_index = 0
else:
current_row_index = global_row_counter[0]
global_row_counter[0] += 1
# Use light blue for nested models, but alternate the shade
if current_row_index % 2 == 0:
fill_color = (250, 250, 255) # Very light blue
else:
fill_color = (245, 245, 250) # Slightly darker light blue
# Create hyperlink for the nested model
x_position = pdf.get_x()
y_position = pdf.get_y()
render_table_row(
pdf,
[nested_name, description, examples, constraints],
col_widths,
line_height,
fill_color,
)
# Add the hyperlink to the examples cell
examples_x = x_position + field_width + desc_width
pdf.link(
examples_x, y_position, examples_width, line_height, nested_link_id
)
pdf.ln(6)
@dataclass(slots=True)
class FieldSpec:
"""Descriptor capturing metadata for a UserInput field."""
path: str
type_label: str
required: bool
default: Any
choices: list[str] | None
constraints: dict[str, Any]
description: str | None
def _iter_model_fields(model: type[BaseModel], prefix: str = "") -> Iterator[FieldSpec]:
"""Yield FieldSpec instances for every nested field in a model.
Args:
model (type[BaseModel]): Model class to introspect.
prefix (str): Path prefix accumulated for nested fields.
Yields:
FieldSpec: Metadata describing each discovered field.
"""
for name, field in model.model_fields.items():
path = f"{prefix}{name}" if not prefix else f"{prefix}.{name}"
annotation = field.annotation
required = field.is_required()
default = None if field.is_required() else field.default
choices = _get_enum_choices(annotation)
constraints = _collect_constraints(field.metadata)
description = getattr(field, "description", None) or None
type_label = _format_type(annotation)
yield FieldSpec(
path=path,
type_label=type_label,
required=required,
default=default,
choices=choices,
constraints=constraints,
description=description,
)
try:
if isinstance(annotation, type) and issubclass(annotation, BaseModel):
yield from _iter_model_fields(annotation, path)
continue
except TypeError:
pass
origin = get_origin(annotation)
args = get_args(annotation)
if origin in {list, Iterable, list[Any]} and args:
item = args[0]
try:
if isinstance(item, type) and issubclass(item, BaseModel):
yield from _iter_model_fields(item, f"{path}[]")
except TypeError:
pass
elif args:
for arg in args:
try:
if isinstance(arg, type) and issubclass(arg, BaseModel):
yield from _iter_model_fields(arg, path)
except TypeError:
continue
USER_INPUT_FIELD_SPECS: dict[str, FieldSpec] = {
field.path: field for field in _iter_model_fields(UserInput)
}
# ---------------------------------------------------------------------------
# Presentation helpers
# ---------------------------------------------------------------------------
def _normalise_cancer_label(label: str) -> str:
text = label.replace("_", " ").replace("-", " ")
text = text.strip().lower()
suffixes = [" cancer", " cancers"]
for suffix in suffixes:
if text.endswith(suffix):
text = text[: -len(suffix)]
break
return text.strip().title()
def _unique_qcancer_sites() -> list[str]:
"""Return the union of QCancer cancer sites across sexes.
Returns:
list[str]: Alphabetically ordered, normalised cancer site names.
"""
sites = set()
for name in (*QC_FEMALE_CANCERS, *QC_MALE_CANCERS):
sites.add(_normalise_cancer_label(name))
return sorted(sites)
def cancer_types_for_model(model: RiskModel) -> list[str]:
"""Return the normalised cancer types handled by a model.
Args:
model: Risk model instance to inspect.
Returns:
list[str]: Collection of title-cased cancer type labels.
"""
if model.name == "qcancer":
return _unique_qcancer_sites()
raw = model.cancer_type()
return [
_normalise_cancer_label(part.strip()) for part in raw.split(",") if part.strip()
]
def add_section_heading(pdf: FPDF, index: str, title: str) -> None:
"""Render a primary section heading for the PDF.
Args:
pdf: Active PDF document.
index: Section identifier prefix (e.g., "1").
title: Display title for the section.
"""
pdf.set_text_color(*TEXT_DARK)
pdf.set_font("Helvetica", "B", 16) # Reduced from 20 to 16
pdf.cell(0, 10, f"{index}. {title}", 0, 1) # Reduced height from 14 to 10
pdf.ln(3) # Reduced spacing from 4 to 3
def add_subheading(pdf: FPDF, title: str) -> None:
"""Render a muted subheading label within the document.
Args:
pdf (FPDF): Active PDF document.
title (str): Subheading text to display.
"""
pdf.set_text_color(*THEME_PRIMARY) # Changed to primary color
pdf.set_font("Helvetica", "B", 12) # Reduced from 13 to 12
pdf.cell(0, 7, title, 0, 1) # Removed .upper(), reduced height from 8 to 7
pdf.ln(8) # Increased spacing significantly to prevent conflicts with table headers
def draw_stat_card(pdf: FPDF, title: str, value: str, note: str, width: float) -> None:
"""Render a single metric card with title, value, and note.
Args:
pdf (FPDF): Active PDF instance.
title (str): Brief descriptor for the metric.
value (str): Primary value to highlight.
note (str): Supporting text beneath the value.
width (float): Width to allocate for the card (points).
"""
x_position = pdf.get_x()
y_position = pdf.get_y()
height = 32
pdf.set_fill_color(*CARD_BACKGROUND)
pdf.set_draw_color(255, 255, 255)
pdf.rect(x_position, y_position, width, height, "F")
pdf.set_text_color(*THEME_MUTED)
pdf.set_font("Helvetica", "B", 9)
pdf.set_xy(x_position + 6, y_position + 5)
pdf.cell(width - 12, 4, title.upper(), 0, 2, "L")
pdf.set_text_color(*THEME_PRIMARY)
pdf.set_font("Helvetica", "B", 18)
pdf.cell(width - 12, 10, value, 0, 2, "L")
pdf.set_text_color(*TEXT_DARK)
pdf.set_font("Helvetica", "", 10)
pdf.multi_cell(width - 12, 5, note, 0, "L")
pdf.set_xy(x_position + width + 8, y_position)
def render_summary_cards(pdf: FPDF, models: list[RiskModel]) -> None:
"""Render key summary metric cards derived from all models.
Args:
pdf (FPDF): Active PDF instance.
models (list[RiskModel]): List of instantiated risk models.
"""
stats: list[tuple[str, str, str]] = []
# Calculate total cancer type coverage instances (sum of all bars in chart)
total_coverage_instances = sum(
len(cancer_types_for_model(model)) for model in models
)
total_cancers = len(
{
cancer_type
for model in models
for cancer_type in cancer_types_for_model(model)
}
)
stats.append(
(
"Risk Models",
str(total_coverage_instances),
"Total cancer type coverage instances",
)
)
stats.append(("Cancer Types", str(total_cancers), "Unique cancer sites covered"))
# New metrics
stats.append(
(
"Genes",
str(count_genomic_mutations()),
"Genetic mutations tracked",
)
)
stats.append(
(
"Lifestyle Factors",
str(count_lifestyle_factors()),
"Lifestyle fields available",
)
)
stats.append(
(
"Input Categories",
str(count_total_input_categories()),
"Total input fields across all categories",
)
)
stats.append(
(
"Discrete Choices",
str(count_total_discrete_choices()),
"Total number of categorical values available",
)
)
available_width = pdf.w - 2 * pdf.l_margin
columns = 3 # Changed from 2 to accommodate 6 cards
gutter = 8
card_width = (
available_width - 2 * gutter
) / columns # Account for 2 gutters between 3 cards
start_x = pdf.l_margin
start_y = pdf.get_y()
max_height_in_row = 0
for idx, (title, value, note) in enumerate(stats):
col = idx % columns
row = idx // columns
x_position = start_x + col * (card_width + gutter)
y_position = start_y + row * 38 # fixed row height
pdf.set_xy(x_position, y_position)
draw_stat_card(pdf, title, value, note, card_width)
max_height_in_row = max(max_height_in_row, 36)
rows = (len(stats) + columns - 1) // columns
pdf.set_xy(pdf.l_margin, start_y + rows * (max_height_in_row + 2))
pdf.ln(4)
def add_model_heading(pdf: FPDF, section_index: int, model_name: str) -> None:
"""Render the heading for an individual model section.
Args:
pdf (FPDF): Active PDF instance.
section_index (int): Sequence number for the model section.
model_name (str): Model identifier used in headings.
"""
pdf.set_text_color(*THEME_PRIMARY)
pdf.set_font("Helvetica", "B", 15)
display_name = model_name.replace("_", " ").title()
pdf.cell(0, 10, f"2.{section_index} {display_name}", 0, 1)
pdf.set_text_color(*TEXT_DARK)
def render_model_summary(pdf: FPDF, model: RiskModel, cancer_types: list[str]) -> None:
"""Render textual summary for a risk model.
Args:
pdf (FPDF): Active PDF instance.
model (RiskModel): The risk model instance.
cancer_types (list[str]): Cancer types covered by the model.
"""
# Description
pdf.set_font("Helvetica", "B", 10)
pdf.set_text_color(*TEXT_DARK)
pdf.cell(0, 5, "Description", 0, 1)
pdf.set_font("Helvetica", "", 9)
pdf.set_text_color(*TEXT_DARK)
pdf.multi_cell(0, 4.5, model.description(), 0, "L")
pdf.ln(1)
# Interpretation
pdf.set_font("Helvetica", "B", 10)
pdf.set_text_color(*TEXT_DARK)
pdf.cell(0, 5, "Interpretation", 0, 1)
pdf.set_font("Helvetica", "", 9)
pdf.set_text_color(*TEXT_DARK)
pdf.multi_cell(0, 4.5, model.interpretation(), 0, "L")
pdf.ln(1)
# Cancer Types
pdf.set_font("Helvetica", "B", 10)
pdf.set_text_color(*TEXT_DARK)
pdf.cell(0, 5, "Cancer Types", 0, 1)
pdf.set_font("Helvetica", "", 9)
pdf.set_text_color(*TEXT_DARK)
pdf.multi_cell(0, 4.5, ", ".join(cancer_types), 0, "L")
pdf.ln(1)
# References
pdf.set_font("Helvetica", "B", 10)
pdf.set_text_color(*TEXT_DARK)
pdf.cell(0, 5, "References", 0, 1)
pdf.set_font("Helvetica", "", 8)
pdf.set_text_color(*TEXT_DARK)
references = model.references()
for ref_index, ref in enumerate(references, 1):
pdf.multi_cell(0, 4, f"{ref_index}. {ref}", 0, "L")
pdf.ln(2)
def prettify_field_name(segment: str) -> str:
"""Convert a field segment to a readable name.
Args:
segment: Raw field segment (e.g., "female_specific").
Returns:
str: Title-cased, readable field name.
"""
plain = segment.replace("[]", "")
return plain.replace("_", " ").title()
def group_fields_by_requirements(
requirements: list[tuple[str, type, bool]],
) -> list[tuple[str, list[tuple[str, type, bool]]]]:
"""Group field requirements by their parent field.
Args:
requirements: List of (field_path, field_type, is_required) tuples.
Returns:
List of (parent_name, sub_fields) tuples where sub_fields are the
child fields under that parent.
"""
groups: dict[str, list[tuple[str, type, bool]]] = {}
for field_path, field_type, is_required in requirements:
segments = field_path.split(".")
if len(segments) == 1:
# Top-level field
parent = prettify_field_name(segments[0])
if parent not in groups:
groups[parent] = []
groups[parent].append((field_path, field_type, is_required))
else:
# Nested field
parent = prettify_field_name(segments[0])
if parent not in groups:
groups[parent] = []
groups[parent].append((field_path, field_type, is_required))
return list(groups.items())
def format_field_path(path: str) -> str:
"""Format a dotted field path to highlight hierarchy.
Args:
path (str): Dotted field path (e.g., "family_history[].relation").
Returns:
str: Multi-line string emphasising hierarchy with indentation markers.
"""
segments = path.split(".")
if not segments:
return path
# Show parent labels with proper hierarchy
lines = []
for idx, segment in enumerate(segments):
cleaned = prettify_field_name(segment.strip())
if idx == 0:
lines.append(cleaned)
else:
lines.append(f" - {cleaned}")
return "\n".join(lines)
def gather_spec_details(
spec: FieldSpec | None, field_type: type | None = None, note: str = ""
) -> tuple[str, str, str, str]:
"""Prepare note, required status, unit, and constraint strings for a metadata row.
Args:
spec: Field metadata from UserInput introspection, if available.
field_type: Type annotation from model's REQUIRED_INPUTS, if available.
note: Model-specific note to include.
Returns:
tuple of (note_text, required_text, unit_text, range_text) strings for display.
"""
notes: list[str] = []
ranges: list[str] = []
units: list[str] = []
required_status = "Optional"
# Extract constraints from field_type if provided
type_constraints: dict[str, Any] = {}
if field_type is not None:
_, type_constraints = parse_annotated_type(field_type)
# Special handling for clinical observations
if note and " - " in note:
obs_name, obs_values = note.split(" - ", 1)
# Provide meaningful descriptions for clinical observations
obs_descriptions = {
"multivitamin": "Multivitamin usage status",
"aspirin": "Aspirin usage history",
"activity": "Physical activity level",
"total_meat": "Red meat consumption",
"pain_med": "NSAID/pain medication usage",
"estrogen": "Estrogen therapy history",
"diabetes": "Diabetes status",
"height": "Height measurement",
"weight": "Weight measurement",
"years_of_education": "Years of formal education",
"psa": "Prostate-specific antigen level",
"percent_free_psa": "Percent free PSA",
"pca3": "PCA3 score",
"t2_erg": "T2:ERG score",
"dre": "Digital rectal examination result",
"prior_biopsy": "Prior biopsy history",
"prior_psa": "Prior PSA screening history",
}
description = obs_descriptions.get(
obs_name, f"Clinical observation: {obs_name}"
)
notes.append(description)
# Handle special cases for numeric clinical observations
if obs_values.startswith("Numeric ("):
# Extract unit from "Numeric (unit)" format
unit_match = re.search(r"Numeric \(([^)]+)\)", obs_values)
if unit_match:
units.append(unit_match.group(1))
ranges.append("Numeric")
else:
ranges.append(obs_values)
else:
ranges.append(obs_values)
note_text = "\n".join(notes) if notes else "-"
range_text = "\n".join(ranges) if ranges else "-"
unit_text = "\n".join(units) if units else "-"
return note_text, required_status, unit_text, range_text
# Handle model-specific range constraints (e.g., "Age 45-85")
if note and any(char.isdigit() for char in note):
# Check if note contains a range pattern like "45-85" or "35-85"
range_match = re.search(r"(\d+)-(\d+)", note)
if range_match:
min_val, max_val = range_match.groups()
ranges.append(f">={min_val} to <={max_val}")
else:
# If it's just a description without range, add to notes
notes.append(note)
elif note:
# Regular note without range information
notes.append(note)
if spec:
if spec.required:
required_status = "Required"
# Only add field description if we don't already have a model-specific note
if spec.description and not note:
notes.append(spec.description)
# Special handling for specific fields
if spec and spec.path == "demographics.sex":
sex_choices = [choice.value for choice in Sex]
ranges.append(", ".join(sex_choices))
elif spec and spec.path == "demographics.ethnicity":
# CRC-PRO ethnicity choices
ranges.append("hawaiian, japanese, latino, white, black")
elif spec and spec.path == "lifestyle.smoking.pack_years":
units.append("pack-years")
ranges.append(">=0")
elif spec and spec.path == "lifestyle.alcohol.drinks_per_week":
units.append("drinks/week")
ranges.append(">=0")
elif spec and spec.path == "family_history[].cancer_type":
# Predefined cancer types
ranges.append(
"breast, cervical, colorectal, endometrial, kidney, leukemia, liver, lung, lymphoma, ovarian, pancreatic, prostate, skin, stomach, testicular, thyroid, uterine, other"
)
elif spec and spec.path == "demographics.anthropometrics.height_cm":
units.append("cm")
elif spec and spec.path == "demographics.anthropometrics.weight_kg":
units.append("kg")
elif spec and spec.path == "demographics.socioeconomic.education_level":
units.append("years")
# Add more specific field examples based on common patterns
if spec:
field_path = spec.path
if "age" in field_path:
if not ranges:
ranges.append("Age in years")
elif "height" in field_path:
if not units:
units.append("cm")
if not ranges:
ranges.append("Height measurement")
elif "weight" in field_path:
if not units:
units.append("kg")
if not ranges:
ranges.append("Weight measurement")
elif "bmi" in field_path:
if not ranges:
ranges.append("Body Mass Index")
elif "smoking" in field_path:
if not ranges:
ranges.append("Smoking-related values")
elif "alcohol" in field_path:
if not ranges:
ranges.append("Alcohol consumption values")
elif "family_history" in field_path:
if not ranges:
ranges.append("Family history information")
elif "clinical" in field_path or "test" in field_path:
if not ranges:
ranges.append("Clinical test values")
elif "symptoms" in field_path:
if not ranges:
ranges.append("Symptom descriptions")
elif "medical_history" in field_path:
if not ranges:
ranges.append("Medical history information")
# Use type constraints from REQUIRED_INPUTS if available, otherwise fall back to spec constraints
constraints_to_use = (
type_constraints if type_constraints else (spec.constraints if spec else {})
)
# Only use generic constraints if we don't have model-specific constraints
# Model-specific constraints (from note) take precedence
if not note and constraints_to_use:
# Handle min/max constraints as a single range
min_val = None
min_exclusive = False
max_val = None
max_exclusive = False
for key, value in constraints_to_use.items():
if key == "ge": # ge from Field constraint
min_val = value
elif key == "gt": # gt from Field constraint
min_val = value
min_exclusive = True
elif key == "le": # le from Field constraint
max_val = value
elif key == "lt": # lt from Field constraint
max_val = value
max_exclusive = True
elif key == "min": # Legacy min from spec
min_val = value
elif key == "min_exclusive": # Legacy min_exclusive from spec
min_val = value
min_exclusive = True
elif key == "max": # Legacy max from spec
max_val = value
elif key == "max_exclusive": # Legacy max_exclusive from spec
max_val = value
max_exclusive = True
elif key == "min_length":
ranges.append(f"Min length {value}")
elif key == "max_length":
ranges.append(f"Max length {value}")
else:
ranges.append(f"{key.replace('_', ' ').title()}: {value}")
# Format min/max as a single range
if min_val is not None or max_val is not None:
range_parts = []
if min_val is not None:
range_parts.append(f"{'>' if min_exclusive else '>='}{min_val}")
if max_val is not None:
range_parts.append(f"{'<' if max_exclusive else '<='}{max_val}")
if range_parts:
ranges.append(" to ".join(range_parts))
# Add choices from spec if available and no note
if not note and spec and spec.choices:
ranges.append(", ".join(str(choice) for choice in spec.choices))
# If we still don't have any ranges/choices, try to provide examples based on field type
if not ranges and not note:
# Get the base type from field_type if available
base_type = field_type
if field_type is not None:
base_type, _ = parse_annotated_type(field_type)
# Provide examples based on type
if base_type is bool:
ranges.append("True, False")
elif base_type is int:
ranges.append("Integer values")
elif base_type is float:
ranges.append("Decimal values")
elif base_type is str:
ranges.append("Text values")
elif hasattr(base_type, "__name__"):
# For enum types, try to get the values
if hasattr(base_type, "__members__"):
enum_values = [
str(member.value) for member in base_type.__members__.values()
]
if enum_values:
ranges.append(", ".join(enum_values))
elif base_type.__name__ in [
"Sex",
"Ethnicity",
"CancerType",
"FamilyRelation",
"RelationshipDegree",
]:
# Handle specific enum types we know about
if base_type.__name__ == "Sex":
ranges.append("male, female")
elif base_type.__name__ == "Ethnicity":
ranges.append(
"white, black, asian, hispanic, pacific_islander, other"
)
elif base_type.__name__ == "CancerType":
ranges.append(
"breast, cervical, colorectal, endometrial, kidney, leukemia, liver, lung, lymphoma, ovarian, pancreatic, prostate, skin, stomach, testicular, thyroid, uterine, other"
)
elif base_type.__name__ == "FamilyRelation":
ranges.append(
"mother, father, sister, brother, daughter, son, grandmother, grandfather, aunt, uncle, cousin, other"
)
elif base_type.__name__ == "RelationshipDegree":
ranges.append("first, second, third")
else:
ranges.append(f"{base_type.__name__} values")
elif str(base_type).startswith("typing.Union"):
# Handle Union types
ranges.append("Multiple types allowed")
else:
ranges.append("See model documentation")
note_text = "\n".join(notes) if notes else "-"
range_text = "\n".join(ranges) if ranges else "-"
unit_text = "\n".join(units) if units else "-"
return note_text, required_status, unit_text, range_text
def wrap_text_to_width(
pdf: FPDF, text: str, width: float, _line_height: float
) -> list[str]:
"""Wrap text to fit within the specified width.
Args:
pdf: FPDF instance for font metrics
text: Text to wrap
width: Maximum width in points
_line_height: Height per line (unused)
Returns:
List of wrapped lines
"""
if not text or text == "-":
return [text]
# Get current font info
font_size = pdf.font_size
font_family = pdf.font_family
# Estimate character width (rough approximation)
char_width = font_size * 0.6 # Rough estimate for most fonts
# Calculate max characters per line
max_chars = int(width / char_width)
if len(text) <= max_chars:
return [text]
# Split text into words and wrap
words = text.split()
lines = []
current_line = ""
for word in words:
test_line = current_line + (" " if current_line else "") + word
if len(test_line) <= max_chars:
current_line = test_line
else:
if current_line:
lines.append(current_line)
current_line = word
else:
# Word is too long, force break
lines.append(word[:max_chars])
current_line = word[max_chars:]
if current_line:
lines.append(current_line)
return lines
def render_table_row(
pdf: FPDF,
texts: list[str],
widths: list[float],
line_height: float,
fill_color: tuple[int, int, int],
) -> None:
"""Render a single row of the metadata table.
Args:
pdf (FPDF): Active PDF instance.
texts (list[str]): Column label/value text.
widths (list[float]): Column widths in points.
line_height (float): Base line height to use for wrapped text.
fill_color (tuple[int, int, int]): RGB tuple for row background.
"""
# Calculate the maximum height needed for this row by wrapping text
max_lines = 0
wrapped_texts = []
for text, width in zip(texts, widths, strict=False):
# Wrap text to fit the cell width
wrapped_lines = wrap_text_to_width(pdf, text, width, line_height)
wrapped_texts.append(wrapped_lines)
max_lines = max(max_lines, len(wrapped_lines))
row_height = max_lines * line_height + 2
# Check if we need a page break before rendering the row
if pdf.get_y() + row_height > pdf.h - pdf.b_margin:
pdf.add_page()
# Reset text color after page break to ensure readability
pdf.set_text_color(*TEXT_DARK)
x_position = pdf.get_x()
y_position = pdf.get_y()
# First, draw the background rectangle for the entire row
pdf.set_fill_color(*fill_color)
pdf.rect(x_position, y_position, sum(widths), row_height, "F")
# Then draw the text in each cell with consistent height
current_x = x_position
for width, wrapped_lines in zip(widths, wrapped_texts, strict=False):
pdf.set_xy(current_x, y_position)
pdf.set_fill_color(*fill_color)
# Draw each line of wrapped text
for line_index, line in enumerate(wrapped_lines):
pdf.set_xy(current_x, y_position + line_index * line_height)
pdf.cell(width, line_height, line, border=0, align="L", fill=False)
current_x += width
# Draw the border around the entire row
pdf.set_draw_color(*TABLE_BORDER)
pdf.rect(x_position, y_position, sum(widths), row_height)
pdf.set_xy(x_position, y_position + row_height)
def render_table_row_with_enum_link(
pdf: FPDF,
texts: list[str],
widths: list[float],
line_height: float,
fill_color: tuple[int, int, int],
enum_class: type,
link_manager: LinkManager,
) -> None:
"""Render a table row with clickable enum choices in the constraints column.
Args:
pdf: Active PDF instance
texts: Column texts (constraints should be in the last column)
widths: Column widths
line_height: Line height for text
fill_color: Background color for the row
enum_class: The enum class to link to
link_manager: Link manager for creating hyperlinks
"""
# Calculate the maximum height needed for this row by wrapping text
max_lines = 0
wrapped_texts = []
for text, width in zip(texts, widths, strict=False):
if not text:
wrapped_lines = [""]
else:
# Wrap text to fit within the column width
wrapped_lines = pdf.multi_cell(
width, line_height, text, border=0, align="L", split_only=True
)
wrapped_texts.append(wrapped_lines)
max_lines = max(max_lines, len(wrapped_lines))
# Calculate the total row height
row_height = max_lines * line_height
# Check if we need a page break before rendering the row
if pdf.get_y() + row_height > pdf.h - pdf.b_margin:
pdf.add_page()
# Reset text color after page break to ensure readability
pdf.set_text_color(*TEXT_DARK)
x_position = pdf.get_x()
y_position = pdf.get_y()
# First, draw the background rectangle for the entire row
pdf.set_fill_color(*fill_color)
pdf.rect(x_position, y_position, sum(widths), row_height, "F")
# Then draw the text in each cell with consistent height
current_x = x_position
for column_index, (width, wrapped_lines) in enumerate(
zip(widths, wrapped_texts, strict=False)
):
pdf.set_xy(current_x, y_position)
pdf.set_fill_color(*fill_color)
# Draw each line of wrapped text
for line_index, line in enumerate(wrapped_lines):
pdf.set_xy(current_x, y_position + line_index * line_height)
# If this is the constraints column (last column) and contains choices, make it clickable
if column_index == len(texts) - 1 and "choices" in line and enum_class:
# Store link information for later creation (after destinations are created)
enum_link_id = f"enum_{enum_class.__name__}"
link_manager.store_link_info(
enum_link_id,
pdf.get_x(),
pdf.get_y(),
pdf.get_string_width(line),
line_height,
)
# Render the text
pdf.cell(width, line_height, line, border=0, align="L", fill=False)
else:
pdf.cell(width, line_height, line, border=0, align="L", fill=False)
current_x += width
# Draw the border around the entire row
pdf.set_draw_color(*TABLE_BORDER)
pdf.rect(x_position, y_position, sum(widths), row_height)
pdf.set_xy(x_position, y_position + row_height)
def render_field_table(pdf: FPDF, model: RiskModel) -> None:
"""Render the table of UserInput fields referenced by a model.
Args:
pdf (FPDF): Active PDF instance.
model (RiskModel): The risk model to extract field requirements from.
"""
# Extract requirements from the model
requirements = extract_model_requirements(model)
if not requirements:
pdf.set_font("Helvetica", "", 10)
pdf.cell(0, 6, "No input requirements defined for this model.", 0, 1)
pdf.ln(4)
return
table_width = pdf.w - 2 * pdf.l_margin
field_width = table_width * 0.26
detail_width = table_width * 0.25
required_width = table_width * 0.10
unit_width = table_width * 0.10
range_width = table_width - field_width - detail_width - required_width - unit_width
col_widths = [field_width, detail_width, required_width, unit_width, range_width]
# Table header function for reuse on page breaks
def render_table_header():
pdf.set_font("Helvetica", "B", 10)
pdf.set_fill_color(*TABLE_HEADER_BACKGROUND)
pdf.set_text_color(*TEXT_LIGHT)
pdf.cell(field_width, 8, "Field", 0, 0, "L", True)
pdf.cell(detail_width, 8, "Details", 0, 0, "L", True)
pdf.cell(required_width, 8, "Required", 0, 0, "L", True)
pdf.cell(unit_width, 8, "Unit", 0, 0, "L", True)
pdf.cell(range_width, 8, "Choices / Range", 0, 1, "L", True)
# Add minimal spacing before table header
pdf.ln(1)
render_table_header()
pdf.set_font("Helvetica", "", 9)
pdf.set_text_color(*TEXT_DARK)
line_height = 5.5
# Group fields by parent
grouped_fields = group_fields_by_requirements(requirements)
# Define a fixed color for parent field rows (slightly darker than regular rows)
PARENT_ROW_COLOR = (245, 245, 245) # Light gray for parent rows
# Global index for alternating colors across all sub-fields
global_sub_idx = 0
for parent_name, sub_fields in grouped_fields:
# Check if we need a page break before the parent field
if pdf.get_y() + 20 > pdf.h - pdf.b_margin:
pdf.add_page()
# Add spacing after page header and before table header
pdf.ln(3)
render_table_header()
# Reset text color after page break to ensure readability
pdf.set_text_color(*TEXT_DARK)
# Render parent field name (bold) with fixed color
pdf.set_font("Helvetica", "B", 9)
pdf.set_text_color(*TEXT_DARK)
render_table_row(
pdf,
[parent_name, "", "", "", ""],
col_widths,
line_height,
PARENT_ROW_COLOR,
)
# Render sub-fields (normal weight, indented) with alternating colors
pdf.set_font("Helvetica", "", 9)
for field_path, field_type, is_required in sub_fields:
# Get the spec from UserInput introspection
spec = USER_INPUT_FIELD_SPECS.get(field_path)
# Use the field_type that was already extracted and parsed
note_text, required_text, unit_text, range_text = gather_spec_details(
spec, field_type, ""
)
# Override required status based on model requirements
required_text = "Required" if is_required else "Optional"
# Indent the sub-field name
sub_field_name = prettify_field_name(field_path.split(".")[-1])
indented_name = f" {sub_field_name}"
# Alternate colors for sub-fields using global index
fill_color = (
ROW_BACKGROUND_LIGHT if global_sub_idx % 2 == 0 else ROW_BACKGROUND_ALT
)
render_table_row(
pdf,
[indented_name, note_text, required_text, unit_text, range_text],
col_widths,
line_height,
fill_color,
)
# Increment global index for next sub-field
global_sub_idx += 1
pdf.ln(4)
# ---------------------------------------------------------------------------
# Risk model requirements are now extracted dynamically from each model's
# REQUIRED_INPUTS class attribute, eliminating the need for hardcoded hints.
# ---------------------------------------------------------------------------
class PDF(FPDF):
"""Sentinel-styled PDF document with header and footer.
Extends :class:`FPDF` to provide a branded header bar and page numbering
consistent across the generated report.
"""
def header(self):
"""Render a discrete document header on all pages except the first."""
# Skip header on first page
if self.page_no() == 1:
return
# Discrete header with title
self.set_text_color(*THEME_MUTED)
self.set_font("Helvetica", "", 10)
self.set_y(8)
self.cell(0, 6, "Sentinel Risk Model Documentation", 0, 0, "L")
# Add a subtle line
self.set_draw_color(*THEME_MUTED)
self.line(self.l_margin, 16, self.w - self.r_margin, 16)
# Add spacing after header to prevent overlap with content
self.set_y(25)
def footer(self):
"""Render the footer with page numbering."""
self.set_y(-15)
self.set_text_color(*THEME_MUTED)
self.set_font("Helvetica", "I", 8)
self.cell(0, 10, f"Page {self.page_no()}", 0, 0, "C")
def discover_risk_models() -> list[RiskModel]:
"""Discover and instantiate all risk model classes.
Returns:
list[RiskModel]: Instantiated models ordered by name.
"""
models = []
for file_path in MODELS_DIR.glob("*.py"):
if file_path.stem.startswith("_"):
continue
module_name = f"sentinel.risk_models.{file_path.stem}"
module = importlib.import_module(module_name)
for _, obj in inspect.getmembers(module, inspect.isclass):
if issubclass(obj, RiskModel) and obj is not RiskModel:
models.append(obj())
return sorted(models, key=lambda m: m.name)
def generate_coverage_chart(models: list[RiskModel]) -> None:
"""Generate and save a bar chart of cancer type coverage.
Args:
models (list[RiskModel]): Risk models to analyse for aggregate cancer coverage.
"""
coverage: dict[str, int] = defaultdict(int)
for model in models:
cancer_types = cancer_types_for_model(model)
for cancer_type in cancer_types:
coverage[cancer_type] += 1
sorted_coverage = sorted(coverage.items(), key=lambda item: item[1], reverse=True)
cancer_types, counts = zip(*sorted_coverage, strict=False)
cancer_types = list(cancer_types)
counts = list(counts)
plt.figure(figsize=(15, 6)) # Keep width, double height (15:6 ratio)
plt.bar(
cancer_types, counts, color=[color_value / 255 for color_value in THEME_PRIMARY]
)
plt.xlabel("Cancer Type", fontsize=16, fontweight="bold")
plt.ylabel("Number of Models", fontsize=16, fontweight="bold")
plt.title("Cancer Type Coverage by Risk Models", fontsize=18, fontweight="bold")
plt.xticks(
rotation=45, ha="right", fontsize=14
) # Rotate x-axis labels for better readability
plt.yticks(fontsize=14) # Set y-axis tick font size
plt.tight_layout()
plt.savefig(CHART_FILE)
plt.close()
def render_summary_page(pdf: FPDF, link_manager: "LinkManager") -> None:
"""Render a summary page with hyperlinks to all sections.
Args:
pdf: Active PDF document.
link_manager: Link manager for creating hyperlinks.
"""
# Title
pdf.set_text_color(*TEXT_DARK)
pdf.set_font("Helvetica", "B", 24)
pdf.cell(0, 15, "Sentinel Risk Model Documentation", 0, 1, "C")
pdf.ln(6)
# Subtitle
pdf.set_text_color(*THEME_MUTED)
pdf.set_font("Helvetica", "", 12)
pdf.cell(0, 8, "Comprehensive Guide to Cancer Risk Assessment Models", 0, 1, "C")
pdf.ln(8)
# Table of Contents
pdf.set_text_color(*TEXT_DARK)
pdf.set_font("Helvetica", "B", 16)
pdf.cell(0, 8, "Table of Contents", 0, 1)
pdf.ln(4)
# Section 1: Overview
pdf.set_font("Helvetica", "B", 12)
pdf.set_text_color(*THEME_PRIMARY)
# Create hyperlink for Overview section
overview_link_id = link_manager.get_or_create_link_id("overview")
x_position = pdf.get_x()
y_position = pdf.get_y()
pdf.cell(0, 7, "1. Overview", 0, 1)
pdf.link(
x_position, y_position, pdf.get_string_width("1. Overview"), 7, overview_link_id
)
pdf.set_font("Helvetica", "", 10)
pdf.set_text_color(*TEXT_DARK)
pdf.cell(0, 5, " Key metrics, cancer coverage, and model statistics", 0, 1)
pdf.ln(2)
# Section 2: User Input Structure
pdf.set_font("Helvetica", "B", 12)
pdf.set_text_color(*THEME_PRIMARY)
# Create hyperlink for User Input Structure section
user_input_link_id = link_manager.get_or_create_link_id("user_input_structure")
x_position = pdf.get_x()
y_position = pdf.get_y()
pdf.cell(0, 7, "2. User Input Structure & Requirements", 0, 1)
pdf.link(
x_position,
y_position,
pdf.get_string_width("2. User Input Structure & Requirements"),
7,
user_input_link_id,
)
pdf.set_font("Helvetica", "", 10)
pdf.set_text_color(*TEXT_DARK)
pdf.cell(0, 5, " Complete field definitions, examples, and constraints", 0, 1)
pdf.ln(2)
# Sub-sections for User Input (simplified - no hyperlinks for now)
pdf.set_font("Helvetica", "", 10)
pdf.set_text_color(*THEME_MUTED)
# Get all sections that will be rendered
structure = traverse_user_input_structure(UserInput)
parent_to_items = defaultdict(list)
for field_path, field_name, model_class in structure:
if "." in field_path:
parent_path = ".".join(field_path.split(".")[:-1])
else:
parent_path = ""
parent_to_items[parent_path].append((field_path, field_name, model_class))
section_counter = 1
processed_parents = set()
for field_path, _field_name, _model_class in structure:
if "." in field_path:
parent_path = ".".join(field_path.split(".")[:-1])
else:
parent_path = ""
if parent_path in processed_parents:
continue
processed_parents.add(parent_path)
items = parent_to_items[parent_path]
leaf_fields = []
for item_path, item_name, item_model_class in items:
if item_model_class is None:
leaf_fields.append((item_path, item_name))
if leaf_fields and parent_path:
# This section will be rendered
# Find the model name from the structure
for field_path_check, field_name_check, model_class_check in structure:
if field_path_check == parent_path and model_class_check is not None:
model_name = field_name_check.replace("_", " ").title()
pdf.cell(0, 4, f" {section_counter}. {model_name}", 0, 1)
section_counter += 1
break
# Section 3: User Input Tabular Choices
pdf.set_font("Helvetica", "B", 12)
pdf.set_text_color(*THEME_PRIMARY)
# Create hyperlink for User Input Tabular Choices section
enum_choices_link_id = link_manager.get_or_create_link_id("user_input_enums")
x_position = pdf.get_x()
y_position = pdf.get_y()
pdf.cell(0, 7, "3. User Input Tabular Choices", 0, 1)
pdf.link(
x_position,
y_position,
pdf.get_string_width("3. User Input Tabular Choices"),
7,
enum_choices_link_id,
)
pdf.set_font("Helvetica", "", 10)
pdf.set_text_color(*TEXT_DARK)
pdf.cell(0, 5, " Enum values and descriptions for all choice fields", 0, 1)
pdf.ln(2)
# Add subsections for Section 3
pdf.set_font("Helvetica", "", 10)
pdf.set_text_color(*THEME_MUTED)
# Get enum information to create subsections
enums_by_parent = collect_all_enums()
section_counter = 1
for parent_name, enums in enums_by_parent.items():
if not enums:
continue
# Format parent name
formatted_parent_name = parent_name.replace("_", " ").title()
# Create hyperlink for this subsection
subsection_link_id = f"enum_{parent_name}"
x_position = pdf.get_x()
y_position = pdf.get_y()
pdf.cell(0, 4, f" {section_counter}. {formatted_parent_name}", 0, 1)
pdf.link(
x_position,
y_position,
pdf.get_string_width(f" {section_counter}. {formatted_parent_name}"),
5,
subsection_link_id,
)
section_counter += 1
# Section 4: Risk Score Models
pdf.set_font("Helvetica", "B", 12)
pdf.set_text_color(*THEME_PRIMARY)
# Create hyperlink for Risk Score Models section
risk_models_link_id = link_manager.get_or_create_link_id("risk_models")
x_position = pdf.get_x()
y_position = pdf.get_y()
pdf.cell(0, 7, "4. Risk Score Models", 0, 1)
pdf.link(
x_position,
y_position,
pdf.get_string_width("4. Risk Score Models"),
7,
risk_models_link_id,
)
pdf.set_font("Helvetica", "", 10)
pdf.set_text_color(*TEXT_DARK)
pdf.cell(0, 5, " Detailed documentation for each risk assessment model", 0, 1)
pdf.ln(2)
pdf.ln(5)
# Footer note
pdf.set_font("Helvetica", "I", 9)
pdf.set_text_color(*THEME_MUTED)
pdf.cell(
0,
6,
"This document provides comprehensive documentation for all Sentinel risk models",
0,
1,
"C",
)
pdf.cell(
0,
6,
"and their input requirements. Use the hyperlinks above to navigate to specific sections.",
0,
1,
"C",
)
def create_pdf(models: list[RiskModel], output_path: Path) -> None:
"""Create the PDF document summarising risk models and metadata.
Args:
models (list[RiskModel]): Ordered list of risk model instances to document.
output_path (Path): Destination path where the PDF will be saved.
"""
pdf = PDF()
pdf.add_page()
pdf.set_margins(18, 20, 18)
pdf.set_auto_page_break(auto=True, margin=15)
# Initialize link manager
link_manager = LinkManager()
# --- Summary Section ---
render_summary_page(pdf, link_manager)
# --- Overview Section ---
pdf.add_page()
# Create link destination for Overview section
link_manager.create_link_destination(pdf, "overview")
# Add spacing after page header
pdf.ln(3)
add_section_heading(pdf, "1", "Overview")
add_subheading(pdf, "Key Metrics")
render_summary_cards(pdf, models)
# Coverage Chart
add_subheading(pdf, "Cancer Coverage")
pdf.image(str(CHART_FILE), x=pdf.l_margin, w=pdf.w - 2 * pdf.l_margin)
pdf.ln(12)
# --- User Input Structure & Requirements Section ---
pdf.add_page()
# Create link destination for User Input Structure section
link_manager.create_link_destination(pdf, "user_input_structure")
# Add spacing after page header
pdf.ln(3)
add_section_heading(pdf, "2", "User Input Structure & Requirements")
render_user_input_hierarchy(pdf, models, link_manager)
# --- User Input Tabular Choices Section ---
pdf.add_page()
# Create link destination for User Input Tabular Choices section
link_manager.create_link_destination(pdf, "user_input_enums")
# Add spacing after page header
pdf.ln(3)
add_section_heading(pdf, "3", "User Input Tabular Choices")
# Collect all enums and render the section
enums_by_parent = collect_all_enums()
render_enum_choices_section(pdf, enums_by_parent, link_manager)
# --- Risk Models Section ---
pdf.add_page()
# Create link destination for Risk Models section
link_manager.create_link_destination(pdf, "risk_models")
# Add spacing after page header
pdf.ln(3)
add_section_heading(pdf, "4", "Risk Score Models")
render_risk_models_section(pdf, models, link_manager)
# Create all pending links after destinations are created
link_manager.create_pending_links(pdf)
pdf.output(output_path)
print(f"Documentation successfully generated at: {output_path}")
def main():
"""Entry point for the documentation generator CLI."""
parser = argparse.ArgumentParser(
description="Generate PDF documentation for Sentinel risk models."
)
parser.add_argument(
"--output",
"-o",
type=Path,
default=OUTPUT_DIR / "risk_model_documentation.pdf",
help="Output path for the PDF file.",
)
args = parser.parse_args()
OUTPUT_DIR.mkdir(exist_ok=True)
print("Discovering risk models...")
models = discover_risk_models()
print("Generating cancer coverage chart...")
generate_coverage_chart(models)
print("Creating PDF document...")
create_pdf(models, args.output)
# Clean up the chart image
CHART_FILE.unlink()
if __name__ == "__main__":
main()
|