- Implement soft dimensional compatibility and 3-way attribute logic (SUPPORTED, CONTRADICTED, UNKNOWN) - Replace binary hardwall rejection with nuanced compatibility scoring - Isolate context per diagnosis to prevent cross-condition contamination (MD Sec 41) - Generalize disease families (liver sarcoma, net, asthma, acl) and category priors - Add regex word boundary acuity checking and laterality alignment scoring in ranker - Fix candidate sort by sorting on unconstrained multi-factor raw score before output clamping - Preserve zero-modification immutability of local Database_2.xlsx - Pass 100% of full regression test suite (158/158 tests) and ruff linting
12 KiB
12 KiB
PRODUCTION CLINICAL EVIDENCE EXTRACTION & DATABASE CODING ARCHITECTURE
1. System Philosophy & Absolute Non-Negotiables
The system operates strictly as a clinical evidence extraction and database matching engine. It establishes two completely independent, non-overlapping domains:
CLINICAL DOCUMENT APPROVED EXCEL DATABASE
(Single Authority for Evidence) (Single Authority for Codes)
┌───────────────────────────────────┐ ┌───────────────────────────────┐
│ • WHAT diagnosis exists │ │ • WHICH codes are available │
│ • WHETHER it is affirmed/active │ │ • Official descriptions │
│ • WHETHER primary or secondary │ │ • Permitted specificities │
│ • WHAT specificity is evidenced │ │ • Active vs inactive status │
└─────────────────┬─────────────────┘ └───────────────┬───────────────┘
│ │
▼ ▼
================================================================================
ABSOLUTE PIPELINE ORDER
DIAGNOSIS ──▶ VALIDATION ──▶ ROLE ──▶ DATABASE RETRIEVAL ──▶ COMPATIBILITY ──▶ CODE
================================================================================
Absolute System Invariants
- INVARIANT 1: No code without an established clinical diagnosis.
- INVARIANT 2: No diagnosis without explicit textual evidence.
- INVARIANT 3: No final diagnosis from metadata, section headers, or administrative templates.
- INVARIANT 4: No final diagnosis from medication or prescription names alone.
- INVARIANT 5: No ruled-out, negated, or unmanaged historical diagnosis in final output.
- INVARIANT 6: Code specificity may NEVER exceed evidence specificity (
CODE_SPECIFICITY <= EVIDENCE_SPECIFICITY). - INVARIANT 7: The database retriever is a mapper, NEVER a diagnosis detector. A retrieved code must NEVER create or alter a diagnosis.
- INVARIANT 8: Candidate independence: each candidate possesses an isolated retrieval context. Zero shared top-K; zero cross-candidate rank contamination.
- INVARIANT 9: Primary / Secondary role classification occurs strictly after the complete candidate inventory is constructed and deduplicated.
- INVARIANT 10: Duplicate clinical concepts merge into exactly ONE canonical diagnosis object.
- INVARIANT 11: The database is immutable and read-only. No codes may be added, altered, or inferred.
- INVARIANT 12: Every final ICD code must exist in
Database/with verifiable provenance. - INVARIANT 13: Every accepted code must be compatible with the evidenced attributes.
- INVARIANT 14: Ambiguity or missing attribute specificity results in deterministic ABSTENTION (
NO_DATABASE_MATCH). - INVARIANT 15: Zero hardcoded cases, filenames, patients, or benchmark-specific overrides.
- INVARIANT 16: The architecture generalizes across unseen clinical documents and specialty domains.
2. End-to-End Typed Pipeline Workflow
graph TD
DOC[Clinical Document / Discharge Summary] --> PARSE[Node 1: Document Parser & Validator]
PARSE --> SECT[Node 2: Section-Aware Segmenter]
SECT --> EXTRACT[Node 3: Clinical Evidence Extraction]
EXTRACT --> ASSERT[Node 4: Assertion, Negation & Temporality Engine]
ASSERT --> GATE[Node 5: Hard Clinical Candidate Gate - 20 Checks]
GATE --> DECOMP[Node 6: Compound Decomposition & Attribute Layer]
DECOMP --> DEDUP[Node 7: Normalization & Deterministic Deduplication]
DEDUP --> INVENT[Node 8: Complete Candidate Inventory]
INVENT --> ROLE[Node 9: Global Primary / Secondary Role Classifier]
ROLE --> RETR[Node 10: Isolated Staged Database Retrieval]
RETR --> COMPAT[Node 11: Database Compatibility Validator - Reverse Attributes]
COMPAT --> CONSIST[Node 12: Final Deterministic Consistency Gate]
CONSIST --> JSON[Node 13: Structured Auditable JSON Payload]
3. Core Architectural Modules & Components
3.1 Document Ingestion & Section Segmentation (ingestion/, clinical_extractor.py)
- Segment text into 20+ recognized clinical sections (Discharge Diagnoses, Admitting Diagnoses, Reason for Admission, Chief Complaints, HPI, PMH, Hospital Course, Procedures, Investigations, Microbiology, Medications, Follow-up, Allergies, Metadata).
- Every extracted fact retains:
source_section: Exact clinical section namesource_text: Verbatim sentencesource_span: Character index tuple(start, end)
3.2 Assertion, Negation & Temporality Engine (clinical_extractor.py, classifier.py)
- Negation detection: Identifies affirmative evidence vs explicit negations (
"ruled out","no evidence of","negative for"). - Temporality classifier: Separates
CURRENT,HISTORICAL,FUTURE, andUNCLEAR. - Pre-existing PMH comorbidity resolution: Pre-existing conditions (e.g., T2DM, HTN, CKD) are cross-referenced with inpatient management evidence (medications, bedside monitoring, lab evaluation). If actively managed, marked
CURRENT; if unmanaged history (e.g., history of falling), markedHISTORICALand excluded.
3.3 Hard Clinical Candidate Gate (clinical_gate.py)
Deterministic 20-point validation gate. Any candidate failing any check is rejected immediately prior to retrieval:
- Meaningful clinical condition
- Supported by explicit clinical evidence
- Not negated or ruled out
- Not hypothetical or differential
- Not a section header (e.g.,
"BIOMARKERS & RECEPTOR STATUS") - Not metadata or template artifact (e.g.,
"Classification of Fracture: Not Applicable") - Not an isolated anatomical location (e.g.,
"Left foot","Chest") - Not a pharmaceutical dosage or medication name (e.g.,
"Cremaffin","Alex lozenges") - Not a treatment instruction or directive (e.g.,
"Watch for reactions","Follow up") - Not a procedure or surgical intervention unless documented as a postoperative diagnosis
- Not an isolated laboratory value or vital sign measurement
- Not a symptom unless symptom coding is appropriate under CMS Guideline I.B.4
- Not a disease attribute (e.g.,
"Stage IV","Grade III") - Not a staging classification
- Not molecular or genetic markers (e.g.,
"BRCA pathogenic") - Not receptor status (e.g.,
"Triple negative","PD-L1 positive") - Not pathology classification alone
- Not unmanaged historical condition
- Does not contain unsupported specificity
- Has sufficient evidence to distinguish it from nearby concepts
3.4 Compound Diagnosis Decomposer & Attribute Layer
- Orthopedic Decomposition: Decomposes complex fracture/sprain narratives:
"Multiple fractures of left lateral malleolus, tarsals and fifth metatarsal with deltoid and calcaneofibular ligament sprains"into atomic candidates:left lateral malleolus fractureleft tarsal fractureleft fifth metatarsal fractureleft deltoid ligament sprainleft calcaneofibular ligament sprainDistributes shared laterality (left) and fracture entity.
- Oncology Representation (
CancerConcept): Encapsulatesprimary_site,laterality,histology,stage,grade,receptor_status,molecular_attributes,metastatic_status, andmetastatic_sites.- Attributes (
Triple negative,BRCA,PD-L1,Stage IV) are attached to the primary neoplasm, NEVER sent to ICD retrieval as standalone diseases. - Linked metastatic manifestations (e.g.,
pleural metastasis) are routed to Chapter 2 secondary neoplasms (C77-C79), preventing generic symptom misclassification.
- Attributes (
3.5 Deduplication & Complete Candidate Inventory
- Semantic deduplication merges duplicate mentions across different sections into ONE canonical candidate with merged evidence spans.
- Classification occurs globally over the complete candidate inventory, preventing sequential replacement bugs.
3.6 Primary vs. Secondary Role Classification (classifier.py)
- Evaluates UHDDS inpatient hierarchy:
- Condition occasioning admission established after study
- Condition driving major therapeutic or surgical intervention
- Condition dominating hospital course
- Provider explicit designation (
PRIMARY DIAGNOSIS:,PRINCIPAL DIAGNOSIS:) receives overwhelming priority (+10.0). - Chronicity penalties are strictly forbidden from overriding explicit provider role documentation.
- Exactly ONE primary diagnosis is selected; secondary diagnoses must be active, managed, or documented comorbidities.
3.7 Constrained Database Retrieval (retrieval/hybrid.py, lexical.py)
- Isolated context per candidate: no shared candidate pool.
- Staged filtering before semantic ranking:
- Concept / family match
- Anatomical site filtering
- Laterality filtering
- Etiology filtering
- Active code filtering (
active_yesno = 1)
- Lexical BM25 + dense FAISS embeddings with clinical tokenization.
3.8 Database Compatibility Validator (reverse_attributes.py, ranker.py)
- Enforces
CODE_SPECIFICITY <= EVIDENCE_SPECIFICITY. - Reverse attribute extraction: Extracts every qualifier from the candidate database description (e.g.,
displaced,bilateral,with bleeding,type 2,streptococcal). - If the candidate code requires a positive attribute that is not evidenced in the clinical record:
- The code is disqualified.
- If no compatible codes exist, the system triggers
NO_DATABASE_MATCHand abstains.
3.9 Final Deterministic Consistency Gate (validation/deterministic.py, nodes.py)
- Validates the entire output bundle against official coding rules:
- HIPAA valid terminal leaf codes
- CMS Excludes1 and Excludes2 constraints
- Code existence in canonical database
- Provenance audit trail verification
4. Final Output Payload Schema
{
"document_id": "case-sample-01",
"status": "COMPLETED",
"primary_diagnosis": {
"raw_term": "Metastatic breast cancer",
"normalized_diagnosis": "Malignant neoplasm of unspecified site of female breast",
"code": "C50.919",
"description": "Malignant neoplasm of unspecified site of unspecified female breast",
"role": "PRIMARY",
"acuity": "CHRONIC",
"certainty": "CONFIRMED",
"evidence_quote": "Patient with known metastatic breast cancer presented for restaging and systemic therapy adjustment.",
"confidence_score": 0.95,
"matching_status": "MATCHED",
"database_code": "C50.919",
"database_description": "Malignant neoplasm of unspecified site of unspecified female breast",
"database_source": "Database_2.xlsx::ICD 10-1::row_14321",
"source_section": "PRINCIPAL_DIAGNOSIS"
},
"secondary_diagnoses": [
{
"raw_term": "Pleural metastasis",
"normalized_diagnosis": "Secondary malignant neoplasm of pleura",
"code": "C78.2",
"description": "Secondary malignant neoplasm of pleura",
"role": "SECONDARY",
"acuity": "CHRONIC",
"certainty": "CONFIRMED",
"evidence_quote": "CT chest confirmed progressive malignant pleural metastasis secondary to breast carcinoma.",
"confidence_score": 0.92,
"matching_status": "MATCHED",
"database_code": "C78.2",
"database_description": "Secondary malignant neoplasm of pleura",
"database_source": "Database_2.xlsx::ICD 10-1::row_18920",
"source_section": "SECONDARY_DIAGNOSES"
}
],
"abstentions": [],
"excluded_candidates": [
{
"text": "Triple negative",
"reason": "ATTRIBUTE"
},
{
"text": "BRCA pathogenic mutation",
"reason": "ATTRIBUTE"
},
{
"text": "PD-L1 positive",
"reason": "ATTRIBUTE"
},
{
"text": "BIOMARKERS & RECEPTOR STATUS",
"reason": "METADATA"
}
],
"validation": {
"evidence_grounded": true,
"database_grounded": true,
"unsupported_specificity": false,
"hallucinated_codes": false,
"noise_capture": false,
"duplicate_candidates": false,
"primary_secondary_validated": true
}
}