235 lines
10 KiB
Python
235 lines
10 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
models/cervix.py
|
|
This script sets up a series of data extraction models using the dspy library for pathology reports, specifically focusing on cervical 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__ = "5.1.0.0"
|
|
|
|
import dspy
|
|
from typing import Literal
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
class CervixMargin(BaseModel):
|
|
margin_category: (
|
|
Literal[
|
|
"ectocervical",
|
|
"endocervical",
|
|
"radial_circumferential",
|
|
"vaginal_cuff",
|
|
"others",
|
|
]
|
|
| None
|
|
) = Field(
|
|
None,
|
|
description="acceptable value for surgical margins in cervical 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 CervixLN(BaseModel):
|
|
lymph_node_side: Literal["right", "left", "midline"] | None = Field(
|
|
None,
|
|
description="acceptable value for lymph node side in cervical cancer. If not included in these standard sides, should be classified as None.",
|
|
)
|
|
lymph_node_category: (
|
|
Literal[
|
|
"pelvic",
|
|
"para_aortic",
|
|
"internal_iliac",
|
|
"obturator",
|
|
"external_iliac",
|
|
"common_iliac",
|
|
"parametrial",
|
|
"others",
|
|
]
|
|
| None
|
|
) = Field(
|
|
None,
|
|
description="acceptable value for lymph node categories in cervical 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 here."
|
|
)
|
|
|
|
|
|
class CervixCancerNonnested(dspy.Signature):
|
|
"""you need to extracting the value of the specified items below from the given cervical cancer 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 cervical 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[
|
|
"radical_hysterectomy",
|
|
"total_hysterectomy_bso",
|
|
"simple_hysterectomy",
|
|
"extenteration",
|
|
"others",
|
|
]
|
|
| None
|
|
) = dspy.OutputField(
|
|
desc="identify which surgery procedure was used. e.g. radical_hysterectomy"
|
|
)
|
|
surgical_technique: Literal["open", "laparoscopic", "vaginal", "others"] | None = (
|
|
dspy.OutputField(desc="identify how the surgery was taken. e.g. laparoscopic")
|
|
)
|
|
cancer_primary_site: (
|
|
Literal["12_3_clock", "3_6_clock", "6_9_clock", "9_12_clock"] | None
|
|
) = dspy.OutputField(desc="identify the primary site of cancer. e.g. sigmoid colon")
|
|
histology: (
|
|
Literal[
|
|
"squamous_cell_carcinoma_hpv_associated",
|
|
"squamous_cell_carcinoma_hpv_dependaent",
|
|
"squamous_cell_carcinoma_nos",
|
|
"adenocarcinoma_hpv_associated",
|
|
"adenocarcinoma_hpv_independent",
|
|
"adenocarcinoma_nos",
|
|
"adenosquamous_carcinoma",
|
|
"neuroendocrine_carcinoma",
|
|
"glassy_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/undiff->3"
|
|
)
|
|
tumor_size: int | None = dspy.OutputField(
|
|
desc="identify the size of the tumor in mm, rounded to integer"
|
|
)
|
|
depth_of_invasion_number: (
|
|
Literal["less_than_3", "3_to_5", "greater_than_5"] | None
|
|
) = dspy.OutputField(
|
|
desc="identify the depth of invasion of the tumor in mm, and choose from these three categories: less_than_3, 3_to_5, greater_than_5"
|
|
)
|
|
depth_of_invasion_three_tier: (
|
|
Literal["inner_third", "middle_third", "outer_third"] | None
|
|
) = dspy.OutputField(
|
|
desc="identify the depth of invasion of the tumor in three-tier system: inner_third, middle_third, outer_third"
|
|
)
|
|
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 CervixCancerStaging(dspy.Signature):
|
|
"""you need to extracting the value of the specified items below from the given pathological cancer 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 cervical 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",
|
|
"t1a1",
|
|
"t1a2",
|
|
"t1b1",
|
|
"t1b2",
|
|
"t1b3",
|
|
"t2a1",
|
|
"t2a2",
|
|
"t2b",
|
|
"t3a",
|
|
"t3b",
|
|
"t4",
|
|
]
|
|
| None
|
|
) = dspy.OutputField(desc="identify the pt category of the tumor")
|
|
pn_category: Literal["nx", "n0", "n1mi", "n1a", "n2mi", "n2a"] | 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",
|
|
"ia1",
|
|
"ia2",
|
|
"ib1",
|
|
"ib2",
|
|
"ib3",
|
|
"iia1",
|
|
"iia2",
|
|
"iib",
|
|
"iiia",
|
|
"iiib",
|
|
"iiic1",
|
|
"iiic2",
|
|
"iva",
|
|
"ivb",
|
|
]
|
|
| None
|
|
) = dspy.OutputField(desc="identify the FIGO stage group of the tumor")
|
|
ajcc_version: int | None = dspy.OutputField(
|
|
desc="identify the ajcc version of the pathological staging"
|
|
)
|
|
|
|
|
|
class CervixCancerMargins(dspy.Signature):
|
|
"""you need to extracting the value of the specified items below from the given pathological cancer 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 cervical 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[CervixMargin] | 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": "mesenteric", "margin_involved": false, "distance": null}]. If not present, just output null for every margin"""
|
|
)
|
|
|
|
|
|
class CervixCancerLN(dspy.Signature):
|
|
"""you need to extracting the value of the specified items below from the given pathological cancer 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 cervical 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[CervixLN] | None = dspy.OutputField(
|
|
desc="""return all of the involved regional lymph node. example:[{"lymph_node_category": "regional", "involved": 2, "examined": 5}, {"lymph_node_category": "mesenteric", "involved": 0, "examined": 3}]. 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"
|
|
)
|