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

218 lines
9.7 KiB
Python

# -*- coding: utf-8 -*-
"""
models/esophagus.py
This script sets up a series of data extraction models using the dspy library for pathology reports, specifically focusing on esophageal 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.1"
import dspy
from typing import Literal
from pydantic import BaseModel, Field
class EsophagusMargin(BaseModel):
margin_category: (
Literal["proximal", "distal", "radial", "lateral", "deep", "others"] | None
) = Field(
None,
description="acceptable value for surgical margins in esophageal 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 EsophagusLN(BaseModel):
lymph_node_category: (
Literal[
"regional_esophageal",
"regional_gastric",
"thoracic_1",
"thoracic_1r",
"thoracic_1l",
"thoracic_4",
"thoracic_4r",
"thoracic_4l",
"thoracic_7",
"thoracic_8u",
"thoracic_8m",
"thoracic_8l",
"thoracic_8",
"thoracic_9",
"thoracic_9r",
"thoracic_9l",
"thoracic_10",
"thoracic_10r",
"thoracic_10l",
"abdomen_106",
"abdomen_1",
"abdomen_2",
"abdomen_3",
"abdomen_4",
"abdomen_5",
"abdomen_6",
"abdomen_7",
"abdomen_8",
"abdomen_9",
"abdomen_10",
"others",
]
| None
) = Field(
None,
description="acceptable value for lymph node categories (i.e. stations or groups) in esophageal cancer. Default to thoracic if not mentioned. 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 EsophagusCancerNonnested(dspy.Signature):
"""you need to extract the value of the specified items below from the given esophagus 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 esophagus 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[
"endoscopic_resection", "esophagectomy", "esophagogastrectomy", "others"
]
| None
) = dspy.OutputField(
desc="identify which surgery procedure was used. e.g. polypectomy"
)
surgical_technique: (
Literal["open", "thoracoscopic", "robotic", "hybrid", "endoscopic", "others"]
| None
) = dspy.OutputField(desc="identify how the surgery was taken. e.g. thoracoscopic")
cancer_primary_site: (
Literal[
"upper_third", "middle_third", "lower_third", "gastroesophageal_junction"
]
| None
) = dspy.OutputField(desc="identify the primary site of cancer. e.g. upper_third")
histology: (
Literal[
"squamous_cell_carcinoma",
"adenocarcinoma",
"adenoid_cystic_carcinoma",
"mucoepidermoid_carcinoma",
"basaloid_squamous_cell_carcinoma",
"small_cell_carcinoma",
"large_cell_carcinoma",
"others",
]
| None
) = dspy.OutputField(
desc="identify the histological type of the cancer. e.g. squamous cell carcinoma"
)
grade: Literal[1, 2, 3] | None = dspy.OutputField(
desc="identify the grade of the cancer, well->1, moderate->2, poor->3"
)
tumor_extent: (
Literal[
"mucosa",
"submucosa",
"muscularis_propria",
"adventitia",
"adjacent_structures",
]
| None
) = dspy.OutputField(
desc="identify how deep the tumor invades. e.g. muscularis_propria"
)
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 EsophagusCancerStaging(dspy.Signature):
"""you need to extract the value of the specified items below from the given esophagus 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 esophagus 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", "t1a", "t1b", "t2", "t3", "t4a", "t4b"] | None = (
dspy.OutputField(desc="identify the pt category of the tumor")
)
pn_category: Literal["nx", "n0", "n1", "n2", "n3"] | None = dspy.OutputField(
desc="identify the pn category of the tumor"
)
pm_category: Literal["mx", "m0", "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"
)
stage_group: (
Literal["0", "i", "ia", "ib", "ic", "iia", "iib", "iiia", "iiib", "iva", "ivb"]
| None
) = dspy.OutputField(desc="identify the stage group of the tumor")
ajcc_version: int | None = dspy.OutputField(
desc="identify the ajcc version of the pathological staging"
)
class EsophagusCancerMargins(dspy.Signature):
"""you need to extract the value of the specified items below from the given esophagus 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 esophagus 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[EsophagusMargin] | None = dspy.OutputField(
desc="""return all of the possible involved margins and its distance from cancer. example:[{"margin_category": "proximal", "margin_involved": true, "distance": 5}, {"margin_category": "distal", "margin_involved": false, "distance": null}, {"margin_category": "radial", "margin_involved": false, "distance": null}]. If not present, just output null for every margin"""
)
class EsophagusCancerLN(dspy.Signature):
"""you need to extract the value of the specified items below from the given esophagus 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 esophagus 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[EsophagusLN] | None = dspy.OutputField(
desc="""return all of the involved regional lymph node. example:[{"lymph_node_category": "thoracic_1", "involved": 2, "examined": 5}, ...]. 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; if no lymph node metastasis, should be None"
)
maximal_ln_size: int | None = dspy.OutputField(
desc="check the maximal size of node metastatic tumor in mm, rounded to integer; if no lymph node metastasis, should be None"
)