Files
cancer-pipeline/models/pancreas.py
2025-11-30 16:46:59 -05:00

216 lines
9.5 KiB
Python

# -*- coding: utf-8 -*-
"""
models/pancreas.py
This script sets up a series of data extraction models using the dspy library for pathology reports, specifically focusing on pancreas cancer. It includes model loading, signature definitions for various cancer types, and functions to convert model predictions into structured JSON formats.
author: Hong-Kai (Walther) Chen, Po-Yen Tzeng and Kai-Po Chang @ Med NLP Lab, China Medical University
date: 2025-10-13
"""
__version__ = "1.0.0"
__date__ = "2025-10-13"
__author__ = ["Hong-Kai (Walther) Chen", "Po-Yen Tzeng", "Kai-Po Chang"]
__copyright__ = "Copyright 2025, Med NLP Lab, China Medical University"
__license__ = "MIT"
__ajcc_version__ = 8
__cap_version__ = "4.2.0.2"
import dspy
from typing import Literal
from pydantic import BaseModel, Field
class PancreasMargin(BaseModel):
margin_category: (
Literal[
"distal_pancreatic",
"proximal_pancreatic",
"pancreatic_neck",
"uncinate",
"bile_duct",
"proximal_gastric",
"proximal_duodenal",
"distal_intestinal",
"outmost",
"anterior_outmost",
"posterior_outmost",
"others",
]
| None
) = Field(
None,
description="acceptable value for surgical margins in pancreas cancer. If not included in these standard margins, should be classified as others.",
)
margin_involved: bool
distance: int | None = Field(
None,
description="If margin is involved, return 0. If margin is uninvolved/free, try your best to find the distance at both microscopic and macroscopic(gross) description, and specify the distance from tumor to margin in mm, rounded to integer. If the margin is uninvolved/free and, after your best effort, the distance is still not specified, return null",
)
description: str | None
class PancreasLN(BaseModel):
lymph_node_category: (
Literal[
"regional_pancreatic",
"regional_gastric",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"others",
]
| None
) = Field(
None,
description="acceptable value for lymph node categories in pancreas cancer. If not included in these standard lymph node 'station' number, should be classified as others.",
)
involved: int
examined: int
station_name: str | None = Field(
None, description="specify the name of the lymph node station/group here."
)
class PancreasCancerNonnested(dspy.Signature):
"""you need to extract the value of the specified items below from the given pancreas cancer excision report. DO NOT JUST RETURN NULL. IF SOME ITEM IS NOT PRESENT, RETURN NULL FOR THAT ITEM, BUT TRY YOUR BEST TO FILL IN THE OTHERS."""
report: list = dspy.InputField(
desc="this is a pathological report for pancreas cancer excision, separated into paragraphs."
)
report_jsonized: dict = dspy.InputField(
desc="this is a roughly structured json summary of the pathological report, which is generated by another model."
)
procedure: (
Literal[
"partial_pancreatectomy",
"ssppd",
"pppd",
"whipple_procedure",
"distal_pancreatectomy",
"total_pancreatectomy",
"others",
]
| None
) = dspy.OutputField(
desc="identify which surgery procedure was used. e.g. partial pancreatectomy, pylorus-preserving pancreaticoduodenectomy(PPPD), Subtotal stomach-preserving pancreaticoduodenectomy (SSPPD), Whipple procedure, etc."
)
tumor_site: (
Literal["head", "neck", "body", "tail", "uncinate_process", "others"] | None
) = dspy.OutputField(
desc="identify the primary site of cancer. e.g. head of pancreas"
)
histology: (
Literal[
"ductal_adenocarcinoma_nos",
"ipmn_with_carcinoma",
"itpn_with_carcinoma",
"acinar_cell_carcinoma",
"solid_pseudopapillary_neoplasm",
"undifferentiated_carcinoma",
"others",
]
| None
) = dspy.OutputField(
desc="identify the histological type of the cancer. e.g. invasive carcinoma of no special type"
)
tumor_size: int | None = dspy.OutputField(
desc="identify the size of the tumor in mm, rounded, if multiple tumors are present, please provide the size of the largest tumor"
)
tumor_extension: (
Literal[
"within_pancreas",
"peripancreatic_soft_tissue",
"adjacent_organs_structures",
"others",
]
| None
) = dspy.OutputField(
desc="identify the extent of tumor extension. e.g. within pancreas"
)
lymphovascular_invasion: bool | None = dspy.OutputField(
desc="check whether or not lymphovascular invasion is present"
)
perineural_invasion: bool | None = dspy.OutputField(
desc="check whether or not perineural invasion is present"
)
distant_metastasis: bool | None = dspy.OutputField(
desc="check whether or not distant metastasis is present"
)
treatment_effect: str | None = dspy.OutputField(
desc='check the treatment effect of the cancer. If you see "No known presurgical therapy", return None'
)
class PancreasCancerStaging(dspy.Signature):
"""you need to extract the value of the specified items below from the given pancreas cancer excision report. DO NOT JUST RETURN NULL. IF SOME ITEM IS NOT PRESENT, RETURN NULL FOR THAT ITEM, BUT TRY YOUR BEST TO FILL IN THE OTHERS."""
report: list = dspy.InputField(
desc="this is a pathological report for pancreas cancer excision, separated into paragraphs."
)
report_jsonized: dict = dspy.InputField(
desc="this is a roughly structured json summary of the pathological report, which is generated by another model."
)
tnm_descriptor: Literal["y", "r", "m"] | None = dspy.OutputField(
desc='identify the tnm descriptor of the tumor. e.g., "y" (post-therapy), "r", etc.'
)
pt_category: Literal["tx", "tis", "t1a", "t1b", "t1c", "t2", "t3", "t4"] | None = (
dspy.OutputField(desc="identify the pt category of the tumor")
)
pn_category: Literal["nx", "n0", "n1", "n2"] | None = dspy.OutputField(
desc="identify the pn category of the tumor"
)
pm_category: Literal["mx", "m1"] | None = dspy.OutputField(
desc="identify the pm category of the tumor. if you see cM0 or cM1, etc., code as mx, since pathological M category is not available"
)
overall_stage: Literal["ia", "ib", "iia", "iib", "iii", "iv"] | None = (
dspy.OutputField(desc="identify the overall stage of the tumor")
)
ajcc_version: int | None = dspy.OutputField(
desc="identify the ajcc version of the pathological staging"
)
class PancreasCancerMargins(dspy.Signature):
"""you need to extract the value of the specified items below from the given pancreas cancer excision report. DO NOT JUST RETURN NULL. IF SOME ITEM IS NOT PRESENT, RETURN NULL FOR THAT ITEM, BUT TRY YOUR BEST TO FILL IN THE OTHERS."""
report: list = dspy.InputField(
desc="this is a pathological report for pancreas cancer excision, separated into paragraphs."
)
report_jsonized: dict = dspy.InputField(
desc="this is a roughly structured json summary of the pathological report, which is generated by another model."
)
margins: list[PancreasMargin] | None = dspy.OutputField(
desc="""return all of the possible involved margins and its distance from cancer. example:[{"margin_category": "proximal_duodenal", "margin_involved": true, "distance": null}, {"margin_category": "anterior_outmost", "margin_involved": false, "distance": 10}, {"margin_category": "radial", "margin_involved": false, "distance": null}]. If not present, just output null for every margin"""
)
class PancreasCancerLN(dspy.Signature):
"""you need to extract the value of the specified items below from the given pancreas cancer excision report. DO NOT JUST RETURN NULL. IF SOME ITEM IS NOT PRESENT, RETURN NULL FOR THAT ITEM, BUT TRY YOUR BEST TO FILL IN THE OTHERS."""
report: list = dspy.InputField(
desc="this is a pathological report for pancreas cancer excision, separated into paragraphs."
)
report_jsonized: dict = dspy.InputField(
desc="this is a roughly structured json summary of the pathological report, which is generated by another model."
)
regional_lymph_node: list[PancreasLN] | None = dspy.OutputField(
desc="""return all of the involved regional lymph node. example:[{"lymph_node_category": "1", "involved": 2, "examined": 5, "station_name": "station 1"}, {"lymph_node_category": "2", "involved": 0, "examined": 3, "station_name": "station 2"}, ...]. If not present, just output null for every lymph node"""
)
extranodal_extension: bool | None = dspy.OutputField(
desc="check whether or not extranodal extension is present"
)
maximal_ln_size: int | None = dspy.OutputField(
desc="check the maximal size of node metastatic tumor in mm, rounded to integer. if not, return None"
)