fix(pipeline): strictly route uncoded conditions to abstentions on primary-fix

This commit is contained in:
Abhinav Gupta
2026-10-12 13:18:55 +05:30
parent e5f960808a
commit a0f3c76490
4 changed files with 129 additions and 100 deletions
+52 -72
View File
@@ -1038,6 +1038,7 @@ def finalize_output_node(state: PipelineGraphState) -> dict[str, Any]:
# SEPARATION OF CLINICAL DIAGNOSIS AND ICD CODE MAPPING (Sections 4 & 11)
# If primary diagnosis was clinically confirmed and established, but ICD mapping abstained or failed:
# If primary diagnosis was clinically confirmed and established, but ICD mapping abstained or failed:
if primary_response is None:
clin_primary = None
for c in classified:
@@ -1049,57 +1050,22 @@ def finalize_output_node(state: PipelineGraphState) -> dict[str, Any]:
if clin_primary:
term = getattr(clin_primary, "normalized_diagnosis", None) or getattr(clin_primary, "raw_term", "")
quote = (
getattr(clin_primary, "primary_evidence_quote", None)
or (
clin_primary.context.evidence.quote
if hasattr(clin_primary, "context") and hasattr(clin_primary.context, "evidence")
else ""
)
or getattr(clin_primary, "evidence_quote", "")
)
# Diagnosis is clinically ACCEPTED as primary; only ICD mapping is abstained
primary_response = CodedDiagnosisResponse(
raw_term=getattr(clin_primary, "raw_term", term),
normalized_diagnosis=term,
description=term,
database_code=None,
database_description=None,
matching_status="NO_DATABASE_MATCH",
source_section=getattr(clin_primary, "source_section", "") or "DISCHARGE_DIAGNOSES",
source_span=getattr(clin_primary, "source_span", None),
role=DiagnosisRole.PRIMARY,
acuity=Acuity.ACUTE
if "acute" in term.lower()
else Acuity.CHRONIC
if "chronic" in term.lower()
else Acuity.UNSPECIFIED,
certainty=Certainty.CONFIRMED,
evidence_quote=quote,
evidence=[{
"quote": quote,
"section": "DISCHARGE_DIAGNOSES",
"evidence_type": "PRIMARY_DIAGNOSIS",
}],
confidence_score=getattr(clin_primary, "evidence_strength", 1.0),
is_terminal_billable=False,
icd10cm=None, # ICD code abstained
icdo=None,
cpt=None,
)
if not any(a.reason == AbstentionReason.NO_MATCHING_ICD_CANDIDATE for a in abstentions):
if not any(a.reason == AbstentionReason.NO_MATCHING_ICD_CANDIDATE and a.raw_term and a.raw_term.lower() == term.lower() for a in abstentions):
abstentions.append(
AbstentionRecord(
diagnosis_id=getattr(clin_primary, "diagnosis_id", "primary"),
raw_term=term,
reason=AbstentionReason.NO_MATCHING_ICD_CANDIDATE,
detail=f"Clinical diagnosis '{term}' is accepted as primary, but ICD-10-CM mapping is abstained.",
stage=PipelineStage.RANKING,
detail=f"Clinical primary diagnosis '{term}' lacked verified local code match; strictly abstained under coding rules.",
stage=PipelineStage.VALIDATION,
)
)
if secondary_responses:
# Promote top coded secondary to primary role
primary_response = secondary_responses.pop(0)
primary_response.role = DiagnosisRole.PRIMARY
# SEPARATION OF CLINICAL DIAGNOSIS AND ICD CODE MAPPING FOR SECONDARIES
# Ensure every active secondary diagnosis clinically confirmed is retained in secondary_responses
seen_sec_terms = set()
if primary_response:
if primary_response.normalized_diagnosis:
@@ -1174,44 +1140,17 @@ def finalize_output_node(state: PipelineGraphState) -> dict[str, Any]:
if not is_valid_cand:
continue
# Clinical condition had no matching code in authoritative database; explicitly abstain ICD code mapping
# Clinical condition has no matching code in authoritative database; put strictly in abstain
if not any(a.raw_term and a.raw_term.lower() == c_term.lower() for a in abstentions):
abstentions.append(
AbstentionRecord(
diagnosis_id=getattr(c, "diagnosis_id", "secondary"),
raw_term=c_term,
reason=AbstentionReason.NO_MATCHING_ICD_CANDIDATE,
detail=f"Condition '{c_term}' lacked verified ICD database match; abstained.",
stage=PipelineStage.RANKING,
detail=f"Condition '{c_term}' lacked verified local code match; strictly abstained under coding rules.",
stage=PipelineStage.VALIDATION,
)
)
# Preserve clinically confirmed secondary diagnosis in secondary_responses
sec_resp = CodedDiagnosisResponse(
raw_term=getattr(c, "raw_term", c_term),
normalized_diagnosis=c_term,
description=c_term,
database_code=None,
database_description=None,
matching_status="NO_DATABASE_MATCH",
source_section=getattr(c, "source_section", "") or (c.context.evidence.source_section if hasattr(c, "context") and hasattr(c.context, "evidence") and c.context.evidence else "CLINICAL_DOCUMENTATION"),
source_span=getattr(c, "source_span", None),
role=DiagnosisRole.SECONDARY,
acuity=getattr(c, "acuity", Acuity.UNSPECIFIED),
certainty=getattr(c, "certainty", Certainty.CONFIRMED),
evidence_quote=c_quote,
evidence=[{
"quote": c_quote,
"section": getattr(c, "source_section", "") or "CLINICAL_DOCUMENTATION",
"evidence_type": "SECONDARY_DIAGNOSIS",
}] if c_quote else [],
confidence_score=getattr(c, "evidence_strength", 1.0),
is_terminal_billable=False,
icd10cm=None,
icdo=None,
cpt=None,
)
secondary_responses.append(sec_resp)
seen_sec_terms.add(c_norm)
# Suppress Z71.1 ("feared health complaint in whom no diagnosis is made") when real clinical conditions exist
@@ -1220,6 +1159,47 @@ def finalize_output_node(state: PipelineGraphState) -> dict[str, Any]:
primary_response.role = DiagnosisRole.PRIMARY
secondary_responses = [s for s in secondary_responses if s.code != "Z71.1"]
# Filter secondary_responses so ONLY items with verified local codes are retained; uncoded items are strictly abstained
coded_secondaries: list[CodedDiagnosisResponse] = []
for s in secondary_responses:
s_code = s.code or getattr(s, "database_code", None)
if s_code and s_code != "None" and s.matching_status != "NO_DATABASE_MATCH":
coded_secondaries.append(s)
else:
s_name = s.normalized_diagnosis or s.raw_term or s.description or ""
if s_name and not any(a.raw_term and a.raw_term.lower() == s_name.lower() for a in abstentions):
abstentions.append(
AbstentionRecord(
diagnosis_id=getattr(s, "diagnosis_id", "secondary-abstained"),
raw_term=s_name,
reason=AbstentionReason.NO_MATCHING_ICD_CANDIDATE,
detail=f"Condition '{s_name}' lacked verified local code match; strictly abstained under coding rules.",
stage=PipelineStage.VALIDATION,
)
)
secondary_responses = coded_secondaries
# Check primary_response for local code match: if missing, abstain and promote coded secondary if available
if primary_response:
p_code = primary_response.code or getattr(primary_response, "database_code", None)
if not p_code or p_code == "None" or primary_response.matching_status == "NO_DATABASE_MATCH":
p_name = primary_response.normalized_diagnosis or primary_response.raw_term or primary_response.description or ""
if p_name and not any(a.raw_term and a.raw_term.lower() == p_name.lower() for a in abstentions):
abstentions.append(
AbstentionRecord(
diagnosis_id=getattr(primary_response, "diagnosis_id", "primary-abstained"),
raw_term=p_name,
reason=AbstentionReason.NO_MATCHING_ICD_CANDIDATE,
detail=f"Primary condition '{p_name}' lacked verified local code match; strictly abstained under coding rules.",
stage=PipelineStage.VALIDATION,
)
)
if secondary_responses:
primary_response = secondary_responses.pop(0)
primary_response.role = DiagnosisRole.PRIMARY
else:
primary_response = None
# Enforce clean role separation: secondary section must NEVER duplicate the primary diagnosis
if primary_response:
pri_code = primary_response.code
+7 -3
View File
@@ -229,7 +229,7 @@ class CodingResult(BaseModel):
return "None"
lines = []
if self.primary_diagnosis:
if self.primary_diagnosis and self.primary_diagnosis.code and self.primary_diagnosis.code != "None" and self.primary_diagnosis.matching_status != "NO_DATABASE_MATCH":
p = self.primary_diagnosis
p_name = p.normalized_diagnosis or p.raw_term or p.description
p_code = _get_formatted_code(p)
@@ -238,9 +238,13 @@ class CodingResult(BaseModel):
else:
lines.append("primary diagnosis: None, code: None, evidence: None")
if self.secondary_diagnoses:
coded_secondaries = [
s for s in (self.secondary_diagnoses or [])
if s.code and s.code != "None" and s.matching_status != "NO_DATABASE_MATCH"
]
if coded_secondaries:
lines.append("secondary diagnosises:")
for s in self.secondary_diagnoses:
for s in coded_secondaries:
s_name = s.normalized_diagnosis or s.raw_term or s.description
s_code = _get_formatted_code(s)
s_ev = _get_cand_ev(s)
+31 -23
View File
@@ -158,19 +158,16 @@ def _clean_audit_from_evidence(ev: str) -> str:
def render_primary_diagnosis(primary: CodedDiagnosisResponse | None) -> None:
"""Render the primary diagnosis card with evidence and specificity confirmation."""
st.markdown("### 🎯 Primary Diagnosis")
if not primary:
if not primary or not primary.code or primary.code in ("None", "[NO LOCAL CODE]") or primary.matching_status == "NO_DATABASE_MATCH":
st.info("ℹ️ No primary diagnosis assigned for this document (abstained or undetermined).")
return
code_display = html.escape(primary.code or "[NO LOCAL CODE]")
if primary.matching_status == "NO_DATABASE_MATCH" or not primary.code:
billable_badge = '<span class="pill-badge badge-warning">No Database Match</span>'
else:
billable_badge = (
'<span class="pill-badge badge-success">✓ HIPAA Billable Leaf</span>'
if primary.is_terminal_billable
else '<span class="pill-badge badge-danger">⚠ Non-Billable Header</span>'
)
code_display = html.escape(primary.code)
billable_badge = (
'<span class="pill-badge badge-success">✓ HIPAA Billable Leaf</span>'
if primary.is_terminal_billable
else '<span class="pill-badge badge-danger">⚠ Non-Billable Header</span>'
)
acuity_badge = f'<span class="pill-badge badge-info">{html.escape(primary.acuity.value if hasattr(primary.acuity, "value") else str(primary.acuity))}</span>'
certainty_badge = f'<span class="pill-badge badge-secondary">{html.escape(primary.certainty.value if hasattr(primary.certainty, "value") else str(primary.certainty))}</span>'
@@ -236,21 +233,22 @@ def render_primary_diagnosis(primary: CodedDiagnosisResponse | None) -> None:
def render_secondary_diagnoses(secondaries: list[CodedDiagnosisResponse]) -> None:
"""Render the list of validated secondary comorbid diagnoses."""
st.markdown(f"### 📋 Secondary Diagnoses ({len(secondaries)})")
if not secondaries:
valid_secondaries = [
s for s in (secondaries or [])
if s.code and s.code not in ("None", "[NO LOCAL CODE]") and s.matching_status != "NO_DATABASE_MATCH"
]
st.markdown(f"### 📋 Secondary Diagnoses ({len(valid_secondaries)})")
if not valid_secondaries:
st.write("No secondary comorbid diagnoses identified.")
return
for idx, diag in enumerate(secondaries, start=1):
sec_code_display = html.escape(diag.code or "[NO LOCAL CODE]")
if diag.matching_status == "NO_DATABASE_MATCH" or not diag.code:
billable_badge = '<span class="pill-badge badge-warning">No Database Match</span>'
else:
billable_badge = (
'<span class="pill-badge badge-success">✓ Billable</span>'
if diag.is_terminal_billable
else '<span class="pill-badge badge-danger">⚠ Non-Billable</span>'
)
for idx, diag in enumerate(valid_secondaries, start=1):
sec_code_display = html.escape(diag.code)
billable_badge = (
'<span class="pill-badge badge-success">✓ Billable</span>'
if diag.is_terminal_billable
else '<span class="pill-badge badge-danger">⚠ Non-Billable</span>'
)
acuity_str = html.escape(diag.acuity.value if hasattr(diag.acuity, "value") else str(diag.acuity))
certainty_str = html.escape(diag.certainty.value if hasattr(diag.certainty, "value") else str(diag.certainty))
@@ -317,13 +315,23 @@ def render_abstentions(abstentions: list[AbstentionRecord]) -> None:
stage_str = abst.stage.value if hasattr(abst.stage, "value") else str(abst.stage)
raw_term = abst.raw_term or "Unspecified Entity"
if (
abst.reason == AbstentionReason.NO_MATCHING_ICD_CANDIDATE
or reason_str in ("NO_MATCHING_ICD_CANDIDATE", "NO_CANDIDATE_FOUND")
or "No local code" in (abst.detail or "")
or "lacked verified local code" in (abst.detail or "")
):
badge_html = '<span class="pill-badge badge-warning">No Local Code (Abstained)</span>'
else:
badge_html = f'<span class="pill-badge badge-danger">{reason_str}</span>'
st.markdown(
clean_html(
f"""
<div class="abstention-card">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px;">
<strong style="color: #f87171; font-size: 14px;">Condition: {raw_term}</strong>
<span class="pill-badge badge-danger">{reason_str}</span>
{badge_html}
</div>
<div style="font-size: 13px; color: #e2e8f0;">{abst.detail}</div>
<div style="font-size: 11px; color: #94a3b8; margin-top: 4px;">Enforced at stage: <code>{stage_str}</code></div>