Spaces:
Sleeping
Sleeping
File size: 15,467 Bytes
66b72b9 9cec003 66b72b9 9cec003 66b72b9 |
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 |
"""
AJCC Anatomic Staging API (Simplified)
--------------------------------------
Single-file FastAPI app that exposes TNM -> Stage mapping for:
1) Breast Cancer β AJCC 8 (Anatomic)
2) Lung Cancer (NSCLC) β AJCC 8
3) Colon / Rectal Cancer β AJCC 8
4) Stomach (Gastric Cancer) β AJCC 8
5) Pancreatic Cancer β AJCC 8
6) Liver Cancer (Hepatocellular Carcinoma) β AJCC 8
7) Melanoma β AJCC 8
8) Prostate Cancer β (Anatomic logic only)
9) Kidney (Renal Cell Carcinoma)
10) Bladder Cancer β AJCC 8
Plus a general fallback AJCC rule for any other / unknown cancer types.
This is a simplified logic based on the mapping you provided,
NOT a full official AJCC implementation.
"""
from typing import Optional, Any, Dict, List
from fastapi import FastAPI
from pydantic import BaseModel
# ---------------------------------------------------------------------
# Core TNM staging logic (previously in ajcc_staging.py)
# ---------------------------------------------------------------------
def normalize_tnm(value: Optional[str]) -> Optional[str]:
if value is None:
return None
value = value.strip()
if not value:
return None
return value.upper()
def stage_cancer(cancer_type: str, T: str, N: str, M: str) -> str:
"""
Main entry point.
cancer_type: string key identifying the cancer
T, N, M: TNM strings like 'T1', 'T2B', 'N1', 'N1MI', 'M1A', etc.
Returns: stage string like 'Stage IIA', 'Stage IIIC', 'Stage IVB', etc.
"""
cancer_type = cancer_type.strip().lower()
T = normalize_tnm(T)
N = normalize_tnm(N)
M = normalize_tnm(M)
if cancer_type == "breast":
return stage_breast(T, N, M)
elif cancer_type in ("lung", "nsclc", "non-small cell lung", "non small cell lung"):
return stage_nsclc(T, N, M)
elif cancer_type in ("colon", "rectal", "colorectal"):
return stage_colorectal(T, N, M)
elif cancer_type in ("stomach", "gastric"):
return stage_stomach(T, N, M)
elif cancer_type in ("pancreas", "pancreatic"):
return stage_pancreas(T, N, M)
elif cancer_type in ("liver", "hcc", "hepatocellular"):
return stage_liver(T, N, M)
elif cancer_type == "melanoma":
return stage_melanoma(T, N, M)
elif cancer_type == "prostate":
return stage_prostate(T, N, M)
elif cancer_type in ("kidney", "renal"):
return stage_kidney(T, N, M)
elif cancer_type == "bladder":
return stage_bladder(T, N, M)
else:
return stage_general(T, N, M)
# ---------------------------------------------------------------------
# General fallback AJCC rule (for unknown cancer types)
# ---------------------------------------------------------------------
def stage_general(T: str, N: str, M: str) -> str:
"""
General AJCC mapping:
IF T = Tis AND N = N0 AND M = M0 -> Stage 0
IF (T = T1 OR T = T2) AND N = N0 AND M = M0 -> Stage I
IF (T = T3 AND N = N0 AND M = M0)
OR ((T = T1 OR T = T2) AND N = N1 AND M = M0) -> Stage II
IF ((T = T3 OR T = T4) AND (N = N1 OR N = N2 OR N = N3) AND M = M0)
OR ((T = T1 OR T = T2 OR T = T3 OR T = T4)
AND (N = N2 OR N = N3) AND M = M0) -> Stage III
IF M = M1 -> Stage IV
"""
# Stage IV
if M == "M1":
return "Stage IV"
# Stage 0
if T == "TIS" and N == "N0" and M == "M0":
return "Stage 0"
# Stage I
if T in ("T1", "T2") and N == "N0" and M == "M0":
return "Stage I"
# Stage II
if (T == "T3" and N == "N0" and M == "M0") or (
T in ("T1", "T2") and N == "N1" and M == "M0"
):
return "Stage II"
# Stage III
if (
(T in ("T3", "T4") and N in ("N1", "N2", "N3") and M == "M0")
or (T in ("T1", "T2", "T3", "T4") and N in ("N2", "N3") and M == "M0")
):
return "Stage III"
return "Stage not mapped (general)"
# ---------------------------------------------------------------------
# 1) Breast Cancer β AJCC 8 (Anatomic)
# ---------------------------------------------------------------------
def stage_breast(T: str, N: str, M: str) -> str:
# Stage IV first
if M == "M1":
return "Stage IV"
# Stage 0
if T == "TIS" and N == "N0" and M == "M0":
return "Stage 0"
# Stage IA
if T == "T1" and N == "N0" and M == "M0":
return "Stage IA"
# Stage IB
if (T in ("T0", "T1")) and N == "N1MI" and M == "M0":
return "Stage IB"
# Stage IIA
if (
((T in ("T0", "T1")) and N == "N1" and M == "M0")
or (T == "T2" and N == "N0" and M == "M0")
):
return "Stage IIA"
# Stage IIB
if (
(T == "T2" and N == "N1" and M == "M0")
or (T == "T3" and N == "N0" and M == "M0")
):
return "Stage IIB"
# Stage IIIA
if (
(T in ("T0", "T1", "T2") and N == "N2" and M == "M0")
or (T == "T3" and N in ("N1", "N2") and M == "M0")
):
return "Stage IIIA"
# Stage IIIB
if T == "T4" and M == "M0":
return "Stage IIIB"
# Stage IIIC
if N == "N3" and M == "M0":
return "Stage IIIC"
return "Stage not mapped (breast)"
# ---------------------------------------------------------------------
# 2) Lung Cancer (NSCLC) β AJCC 8
# ---------------------------------------------------------------------
def stage_nsclc(T: str, N: str, M: str) -> str:
# Stage IV
if M in ("M1A", "M1B"):
return "Stage IVA"
if M == "M1C":
return "Stage IVB"
if M and M != "M0":
# any other M1 pattern
return "Stage IV"
# Stage 0
if T == "TIS" and N == "N0" and M == "M0":
return "Stage 0"
# Stage IA1
if T in ("T1A", "T1A(MI)", "T1AMI") and N == "N0" and M == "M0":
return "Stage IA1"
# Stage IA2
if T == "T1B" and N == "N0" and M == "M0":
return "Stage IA2"
# Stage IA3
if T == "T1C" and N == "N0" and M == "M0":
return "Stage IA3"
# Stage IB
if T == "T2A" and N == "N0" and M == "M0":
return "Stage IB"
# Stage IIA
if T == "T2B" and N == "N0" and M == "M0":
return "Stage IIA"
# Stage IIB
if (T == "T3" and N == "N0" and M == "M0") or (
T in ("T1", "T2") and N == "N1" and M == "M0"
):
return "Stage IIB"
# Stage IIIA
if (
T in ("T1", "T2") and N == "N2" and M == "M0"
) or (T == "T3" and N in ("N1", "N2") and M == "M0"):
return "Stage IIIA"
# Stage IIIB
if (
T in ("T3", "T4") and N == "N2" and M == "M0"
) or (T in ("T1", "T2") and N == "N3" and M == "M0"):
return "Stage IIIB"
# Stage IIIC
if T in ("T3", "T4") and N == "N3" and M == "M0":
return "Stage IIIC"
return "Stage not mapped (NSCLC)"
# ---------------------------------------------------------------------
# 3) Colon / Rectal Cancer β AJCC 8
# ---------------------------------------------------------------------
def stage_colorectal(T: str, N: str, M: str) -> str:
# Stage IV
if M == "M1A":
return "Stage IVA"
if M == "M1B":
return "Stage IVB"
if M == "M1C":
return "Stage IVC"
if M and M != "M0":
return "Stage IV"
# Stage 0
if T == "TIS" and N == "N0" and M == "M0":
return "Stage 0"
# Stage I
if T in ("T1", "T2") and N == "N0" and M == "M0":
return "Stage I"
# Stage II
if T == "T3" and N == "N0" and M == "M0":
return "Stage IIA"
if T == "T4A" and N == "N0" and M == "M0":
return "Stage IIB"
if T == "T4B" and N == "N0" and M == "M0":
return "Stage IIC"
# Stage IIIA
if (
T in ("T1", "T2") and N in ("N1", "N1C") and M == "M0"
) or (T == "T1" and N == "N2A" and M == "M0"):
return "Stage IIIA"
# Stage IIIB
if (
(T in ("T3", "T4A") and N in ("N1", "N1C") and M == "M0")
or (T in ("T2", "T3") and N == "N2A" and M == "M0")
or (T in ("T1", "T2") and N == "N2B" and M == "M0")
):
return "Stage IIIB"
# Stage IIIC
if (T in ("T4A", "T4B") and N in ("N2", "N2A", "N2B") and M == "M0"):
return "Stage IIIC"
return "Stage not mapped (colorectal)"
# ---------------------------------------------------------------------
# 4) Stomach (Gastric Cancer) β AJCC 8
# ---------------------------------------------------------------------
def stage_stomach(T: str, N: str, M: str) -> str:
# Stage IV
if M and M != "M0":
return "Stage IV"
# Stage I
if (T == "T1" and N in ("N0", "N1")) or (T == "T2" and N == "N0"):
return "Stage I"
# Stage II
if (T == "T1" and N == "N2") or (T == "T2" and N == "N1") or (
T == "T3" and N == "N0"
):
return "Stage II"
# Stage III
if (T == "T2" and N == "N2") or (T == "T3" and N in ("N1", "N2")) or (
T == "T4A" and N in ("N0", "N1", "N2")
) or (T == "T4B" and N in ("N0", "N1")):
return "Stage III"
return "Stage not mapped (stomach)"
# ---------------------------------------------------------------------
# 5) Pancreatic Cancer β AJCC 8
# ---------------------------------------------------------------------
def stage_pancreas(T: str, N: str, M: str) -> str:
# Stage IV
if M and M != "M0":
return "Stage IV"
# Stage I
if T in ("T1", "T2") and N == "N0" and M == "M0":
return "Stage I"
# Stage II
if (T == "T3" and N == "N0" and M == "M0") or (
T in ("T1", "T2", "T3") and N == "N1" and M == "M0"
):
return "Stage II"
# Stage III
# mapping given: T4 (unresectable arterial invasion) OR N2 (β₯4 nodes), with M0
if (T == "T4" and M == "M0") or (N == "N2" and M == "M0"):
return "Stage III"
return "Stage not mapped (pancreas)"
# ---------------------------------------------------------------------
# 6) Liver Cancer (Hepatocellular Carcinoma) β AJCC 8
# ---------------------------------------------------------------------
def stage_liver(T: str, N: str, M: str) -> str:
# Stage IV subdivided:
if M and M != "M0":
return "Stage IVB"
if N == "N1" and M == "M0":
return "Stage IVA"
# Stage I
if T == "T1" and N == "N0" and M == "M0":
return "Stage I"
# Stage II
if T == "T2" and N == "N0" and M == "M0":
return "Stage II"
# Stage III
if (T == "T3" and N == "N0" and M == "M0") or (
T == "T4" and N == "N0" and M == "M0"
):
return "Stage III"
return "Stage not mapped (liver)"
# ---------------------------------------------------------------------
# 7) Melanoma β AJCC 8
# ---------------------------------------------------------------------
def stage_melanoma(T: str, N: str, M: str) -> str:
# Stage 0
if T == "TIS" and N == "N0" and M == "M0":
return "Stage 0"
# Stage IV
if M in ("M1A", "M1B", "M1C", "M1D") or (M and M != "M0"):
if M == "M1A":
return "Stage IV (M1a)"
if M == "M1B":
return "Stage IV (M1b)"
if M == "M1C":
return "Stage IV (M1c)"
if M == "M1D":
return "Stage IV (M1d)"
return "Stage IV"
# Stage I
if T == "T1A" and N == "N0" and M == "M0":
return "Stage IA"
if T in ("T1B", "T2A") and N == "N0" and M == "M0":
return "Stage IB"
# Stage II
if T in ("T2B", "T3A") and N == "N0" and M == "M0":
return "Stage IIA"
if T in ("T3B", "T4A") and N == "N0" and M == "M0":
return "Stage IIB"
if T == "T4B" and N == "N0" and M == "M0":
return "Stage IIC"
# Stage III
if N in ("N1", "N2", "N3") and M == "M0":
return "Stage III"
return "Stage not mapped (melanoma)"
# ---------------------------------------------------------------------
# 8) Prostate Cancer β Anatomic Logic
# ---------------------------------------------------------------------
def stage_prostate(T: str, N: str, M: str) -> str:
# Stage IV
if M and M != "M0":
return "Stage IVB"
if N == "N1" and M == "M0":
return "Stage IVA"
# Stage I
if T in ("T1", "T2A") and N == "N0" and M == "M0":
return "Stage I"
# Stage II (no A/B/C separation here β depends on grade/PSA in real AJCC)
if T in ("T2B", "T2C") and N == "N0" and M == "M0":
return "Stage II"
# Stage IIIA
if T == "T3A" and N == "N0" and M == "M0":
return "Stage IIIA"
# Stage IIIB
if T in ("T3B", "T4") and N == "N0" and M == "M0":
return "Stage IIIB"
return "Stage not mapped (prostate)"
# ---------------------------------------------------------------------
# 9) Kidney (Renal Cell Carcinoma)
# ---------------------------------------------------------------------
def stage_kidney(T: str, N: str, M: str) -> str:
# Stage IV
if T == "T4" and M == "M0":
return "Stage IV"
if M and M != "M0":
return "Stage IV"
# Stage I
if T == "T1" and N == "N0" and M == "M0":
return "Stage I"
# Stage II
if T == "T2" and N == "N0" and M == "M0":
return "Stage II"
# Stage III
if (T == "T3" and N == "N0" and M == "M0") or (N == "N1" and M == "M0"):
return "Stage III"
return "Stage not mapped (kidney)"
# ---------------------------------------------------------------------
# 10) Bladder Cancer β AJCC 8
# ---------------------------------------------------------------------
def stage_bladder(T: str, N: str, M: str) -> str:
# Stage IV
if T == "T4B" and M == "M0":
return "Stage IV"
if N in ("N1", "N2", "N3") and M == "M0":
return "Stage IV"
if M and M != "M0":
return "Stage IV"
# Stage 0
if T in ("TA", "TIS") and N == "N0" and M == "M0":
return "Stage 0"
# Stage I
if T == "T1" and N == "N0" and M == "M0":
return "Stage I"
# Stage II
if T == "T2" and N == "N0" and M == "M0":
return "Stage II"
# Stage III
if T in ("T3", "T4A") and N == "N0" and M == "M0":
return "Stage III"
return "Stage not mapped (bladder)"
# ---------------------------------------------------------------------
# FastAPI Layer
# ---------------------------------------------------------------------
app = FastAPI(
title="AJCC TNM Staging API",
description=(
"Simplified AJCC anatomic staging API for multiple cancer types.\n"
"Not a full official AJCC implementation; uses custom mapping logic."
),
version="1.0.0",
)
class StagingRequest(BaseModel):
cancer_type: str
T: str
N: str
M: str
class StagingResponse(BaseModel):
cancer_type: str
T: str
N: str
M: str
stage: str
@app.get("/health", tags=["system"])
def health_check():
return {"status": "ok"}
@app.post("/stage", response_model=StagingResponse, tags=["staging"])
def get_stage(body: StagingRequest):
"""
Return AJCC stage group based on cancer type and TNM.
"""
stage = stage_cancer(
cancer_type=body.cancer_type,
T=body.T,
N=body.N,
M=body.M,
)
return StagingResponse(
cancer_type=body.cancer_type,
T=body.T,
N=body.N,
M=body.M,
stage=stage,
)
if __name__ == "__main__":
import uvicorn
uvicorn.run("ajcc_api:app", host="0.0.0.0", port=8000, reload=True)
|