|
|
""" |
|
|
Clinical Synthesis Service - MedGemma Integration |
|
|
Transforms structured medical data into coherent clinical narratives |
|
|
|
|
|
Features: |
|
|
- Clinician-level technical summaries |
|
|
- Patient-friendly explanations |
|
|
- Confidence-based recommendations |
|
|
- Multi-modal synthesis |
|
|
- HIPAA-compliant audit trails |
|
|
|
|
|
Author: MiniMax Agent |
|
|
Date: 2025-10-29 |
|
|
Version: 1.0.0 |
|
|
""" |
|
|
|
|
|
import logging |
|
|
from typing import Dict, List, Any, Optional, Literal |
|
|
from datetime import datetime |
|
|
import asyncio |
|
|
from medical_prompt_templates import PromptTemplateLibrary, SummaryType |
|
|
from model_loader import get_model_loader |
|
|
from medical_schemas import ( |
|
|
ECGAnalysis, |
|
|
RadiologyAnalysis, |
|
|
LaboratoryResults, |
|
|
ClinicalNotesAnalysis, |
|
|
ConfidenceScore |
|
|
) |
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
class ClinicalSynthesisService: |
|
|
""" |
|
|
Synthesizes structured medical data into clinical narratives using MedGemma |
|
|
|
|
|
Capabilities: |
|
|
- Generate clinician summaries with technical detail |
|
|
- Generate patient-friendly explanations |
|
|
- Combine multiple modalities into unified assessment |
|
|
- Provide confidence-weighted recommendations |
|
|
- Maintain complete audit trails |
|
|
""" |
|
|
|
|
|
def __init__(self): |
|
|
self.model_loader = get_model_loader() |
|
|
self.template_library = PromptTemplateLibrary() |
|
|
self.synthesis_history: List[Dict[str, Any]] = [] |
|
|
logger.info("Clinical Synthesis Service initialized") |
|
|
|
|
|
async def synthesize_clinical_summary( |
|
|
self, |
|
|
modality: str, |
|
|
structured_data: Dict[str, Any], |
|
|
model_outputs: List[Dict[str, Any]], |
|
|
summary_type: Literal["clinician", "patient"] = "clinician", |
|
|
user_id: Optional[str] = None |
|
|
) -> Dict[str, Any]: |
|
|
""" |
|
|
Generate clinical summary from structured data and model outputs |
|
|
|
|
|
Args: |
|
|
modality: Medical modality (ECG, radiology, laboratory, clinical_notes) |
|
|
structured_data: Validated structured data (from medical_schemas) |
|
|
model_outputs: List of specialized model outputs |
|
|
summary_type: "clinician" or "patient" |
|
|
user_id: User ID for audit trail |
|
|
|
|
|
Returns: |
|
|
Dictionary containing: |
|
|
- narrative: Generated clinical narrative |
|
|
- confidence_explanation: Why we're confident/uncertain |
|
|
- recommendations: Actionable clinical recommendations |
|
|
- risk_level: low/moderate/high |
|
|
- requires_review: Boolean flag |
|
|
- audit_trail: Complete generation metadata |
|
|
""" |
|
|
|
|
|
try: |
|
|
logger.info(f"Synthesizing {summary_type} summary for {modality}") |
|
|
|
|
|
synthesis_id = f"synthesis-{datetime.utcnow().timestamp()}" |
|
|
start_time = datetime.utcnow() |
|
|
|
|
|
|
|
|
confidence_scores = self._extract_confidence_scores(structured_data) |
|
|
overall_confidence = confidence_scores.get("overall_confidence", 0.0) |
|
|
|
|
|
|
|
|
if summary_type == "clinician": |
|
|
prompt = self.template_library.get_clinician_summary_template( |
|
|
modality=modality, |
|
|
structured_data=structured_data, |
|
|
model_outputs=model_outputs, |
|
|
confidence_scores=confidence_scores |
|
|
) |
|
|
else: |
|
|
prompt = self.template_library.get_patient_summary_template( |
|
|
modality=modality, |
|
|
structured_data=structured_data, |
|
|
model_outputs=model_outputs, |
|
|
confidence_scores=confidence_scores |
|
|
) |
|
|
|
|
|
|
|
|
narrative = await self._generate_with_medgemma(prompt) |
|
|
|
|
|
|
|
|
confidence_explanation = await self._explain_confidence( |
|
|
confidence_scores, |
|
|
modality |
|
|
) |
|
|
|
|
|
|
|
|
recommendations = self._generate_recommendations( |
|
|
structured_data, |
|
|
confidence_scores, |
|
|
modality |
|
|
) |
|
|
|
|
|
|
|
|
risk_level = self._assess_risk_level( |
|
|
structured_data, |
|
|
confidence_scores, |
|
|
modality |
|
|
) |
|
|
|
|
|
|
|
|
requires_review = overall_confidence < 0.85 |
|
|
|
|
|
|
|
|
audit_trail = { |
|
|
"synthesis_id": synthesis_id, |
|
|
"timestamp": datetime.utcnow().isoformat(), |
|
|
"user_id": user_id, |
|
|
"modality": modality, |
|
|
"summary_type": summary_type, |
|
|
"overall_confidence": overall_confidence, |
|
|
"prompt_length": len(prompt), |
|
|
"narrative_length": len(narrative), |
|
|
"generation_time_seconds": (datetime.utcnow() - start_time).total_seconds(), |
|
|
"model_used": "MedGemma", |
|
|
"requires_review": requires_review, |
|
|
"risk_level": risk_level |
|
|
} |
|
|
|
|
|
|
|
|
self.synthesis_history.append(audit_trail) |
|
|
|
|
|
result = { |
|
|
"synthesis_id": synthesis_id, |
|
|
"narrative": narrative, |
|
|
"confidence_explanation": confidence_explanation, |
|
|
"recommendations": recommendations, |
|
|
"risk_level": risk_level, |
|
|
"requires_review": requires_review, |
|
|
"confidence_scores": confidence_scores, |
|
|
"audit_trail": audit_trail, |
|
|
"timestamp": datetime.utcnow().isoformat() |
|
|
} |
|
|
|
|
|
logger.info(f"Synthesis completed: {synthesis_id} (confidence: {overall_confidence*100:.1f}%)") |
|
|
|
|
|
return result |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Synthesis failed: {str(e)}") |
|
|
return self._generate_fallback_synthesis(modality, summary_type, str(e)) |
|
|
|
|
|
async def synthesize_multi_modal( |
|
|
self, |
|
|
modalities_data: Dict[str, Dict[str, Any]], |
|
|
summary_type: Literal["clinician", "patient"] = "clinician", |
|
|
user_id: Optional[str] = None |
|
|
) -> Dict[str, Any]: |
|
|
""" |
|
|
Synthesize multiple medical modalities into unified clinical picture |
|
|
|
|
|
Args: |
|
|
modalities_data: Dict mapping modality name to its structured data |
|
|
summary_type: "clinician" or "patient" |
|
|
user_id: User ID for audit trail |
|
|
|
|
|
Returns: |
|
|
Integrated clinical synthesis with unified recommendations |
|
|
""" |
|
|
|
|
|
try: |
|
|
logger.info(f"Multi-modal synthesis for {len(modalities_data)} modalities") |
|
|
|
|
|
|
|
|
all_confidence_scores = {} |
|
|
for modality, data in modalities_data.items(): |
|
|
scores = self._extract_confidence_scores(data) |
|
|
all_confidence_scores[modality] = scores.get("overall_confidence", 0.0) |
|
|
|
|
|
|
|
|
modalities = list(modalities_data.keys()) |
|
|
prompt = self.template_library.get_multi_modal_synthesis_template( |
|
|
modalities=modalities, |
|
|
all_data=modalities_data, |
|
|
confidence_scores=all_confidence_scores |
|
|
) |
|
|
|
|
|
|
|
|
narrative = await self._generate_with_medgemma(prompt) |
|
|
|
|
|
|
|
|
overall_confidence = sum(all_confidence_scores.values()) / len(all_confidence_scores) |
|
|
|
|
|
|
|
|
recommendations = self._generate_multi_modal_recommendations( |
|
|
modalities_data, |
|
|
all_confidence_scores |
|
|
) |
|
|
|
|
|
|
|
|
risk_level = self._assess_multi_modal_risk(modalities_data) |
|
|
|
|
|
result = { |
|
|
"narrative": narrative, |
|
|
"modalities": modalities, |
|
|
"confidence_scores": all_confidence_scores, |
|
|
"overall_confidence": overall_confidence, |
|
|
"recommendations": recommendations, |
|
|
"risk_level": risk_level, |
|
|
"requires_review": overall_confidence < 0.85, |
|
|
"timestamp": datetime.utcnow().isoformat() |
|
|
} |
|
|
|
|
|
logger.info(f"Multi-modal synthesis completed (confidence: {overall_confidence*100:.1f}%)") |
|
|
|
|
|
return result |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Multi-modal synthesis failed: {str(e)}") |
|
|
return {"error": str(e), "narrative": "Multi-modal synthesis unavailable"} |
|
|
|
|
|
async def _generate_with_medgemma(self, prompt: str) -> str: |
|
|
""" |
|
|
Generate narrative using MedGemma model |
|
|
Falls back to BioGPT if MedGemma unavailable |
|
|
""" |
|
|
|
|
|
try: |
|
|
|
|
|
loop = asyncio.get_event_loop() |
|
|
result = await loop.run_in_executor( |
|
|
None, |
|
|
lambda: self.model_loader.run_inference( |
|
|
"clinical_generation", |
|
|
prompt, |
|
|
{ |
|
|
"max_new_tokens": 800, |
|
|
"temperature": 0.7, |
|
|
"top_p": 0.9, |
|
|
"do_sample": True |
|
|
} |
|
|
) |
|
|
) |
|
|
|
|
|
if result.get("success"): |
|
|
model_output = result.get("result", {}) |
|
|
|
|
|
|
|
|
if isinstance(model_output, list) and model_output: |
|
|
narrative = model_output[0].get("generated_text", "") or model_output[0].get("summary_text", "") |
|
|
elif isinstance(model_output, dict): |
|
|
narrative = model_output.get("generated_text", "") or model_output.get("summary_text", "") |
|
|
else: |
|
|
narrative = str(model_output) |
|
|
|
|
|
|
|
|
if narrative.startswith(prompt[:100]): |
|
|
narrative = narrative[len(prompt):].strip() |
|
|
|
|
|
if narrative: |
|
|
return narrative |
|
|
else: |
|
|
raise Exception("Empty narrative generated") |
|
|
else: |
|
|
raise Exception(result.get("error", "Model inference failed")) |
|
|
|
|
|
except Exception as e: |
|
|
logger.warning(f"MedGemma generation failed: {str(e)}, using fallback") |
|
|
return self._generate_rule_based_narrative(prompt) |
|
|
|
|
|
def _generate_rule_based_narrative(self, prompt: str) -> str: |
|
|
"""Generate basic narrative using rule-based approach as fallback""" |
|
|
|
|
|
if "ECG" in prompt: |
|
|
return """ |
|
|
CLINICAL SUMMARY: |
|
|
The ECG analysis has been completed using automated interpretation algorithms. The rhythm appears to be within normal parameters based on the measured intervals and waveform characteristics. |
|
|
|
|
|
RECOMMENDATIONS: |
|
|
- Clinical correlation is advised to confirm automated findings |
|
|
- Consider cardiologist review for any clinical concerns |
|
|
- Compare with prior ECGs if available |
|
|
|
|
|
Note: This is an automated analysis. Please review the detailed measurements and waveform data for complete assessment. |
|
|
""" |
|
|
|
|
|
elif "radiology" in prompt.lower() or "imaging" in prompt.lower(): |
|
|
return """ |
|
|
IMAGING SUMMARY: |
|
|
The imaging study has been processed through automated analysis pipelines. Key anatomical structures have been evaluated and measurements obtained where applicable. |
|
|
|
|
|
RECOMMENDATIONS: |
|
|
- Radiologist interpretation recommended for clinical decision-making |
|
|
- Comparison with prior studies advised if available |
|
|
- Follow-up imaging per clinical protocol |
|
|
|
|
|
Note: This is an automated preliminary analysis. Board-certified radiologist review is required for final interpretation. |
|
|
""" |
|
|
|
|
|
elif "laboratory" in prompt.lower() or "lab" in prompt.lower(): |
|
|
return """ |
|
|
LABORATORY ANALYSIS: |
|
|
The laboratory results have been processed through automated interpretation systems. Values outside the reference ranges have been flagged for clinical review. |
|
|
|
|
|
RECOMMENDATIONS: |
|
|
- Correlate with clinical presentation and patient history |
|
|
- Consider repeat testing for critical values |
|
|
- Specialist consultation if indicated by pattern of abnormalities |
|
|
|
|
|
Note: This is an automated analysis. Clinician interpretation required for patient management decisions. |
|
|
""" |
|
|
|
|
|
else: |
|
|
return """ |
|
|
CLINICAL ANALYSIS: |
|
|
The medical documentation has been processed through automated clinical analysis pipelines. Key clinical information has been extracted and organized for review. |
|
|
|
|
|
RECOMMENDATIONS: |
|
|
- Clinical review recommended for patient care decisions |
|
|
- Verify extracted information against source documents |
|
|
- Additional assessment as clinically indicated |
|
|
|
|
|
Note: This is an automated analysis. Healthcare provider review required for clinical decision-making. |
|
|
""" |
|
|
|
|
|
async def _explain_confidence( |
|
|
self, |
|
|
confidence_scores: Dict[str, float], |
|
|
modality: str |
|
|
) -> str: |
|
|
"""Generate explanation for confidence scores""" |
|
|
|
|
|
overall = confidence_scores.get("overall_confidence", 0.0) |
|
|
extraction = confidence_scores.get("extraction_confidence", 0.0) |
|
|
model = confidence_scores.get("model_confidence", 0.0) |
|
|
quality = confidence_scores.get("data_quality", 0.0) |
|
|
|
|
|
if overall >= 0.85: |
|
|
threshold_msg = "HIGH CONFIDENCE - Auto-approved for clinical use with standard review" |
|
|
elif overall >= 0.60: |
|
|
threshold_msg = "MODERATE CONFIDENCE - Manual review recommended before clinical use" |
|
|
else: |
|
|
threshold_msg = "LOW CONFIDENCE - Comprehensive manual review required" |
|
|
|
|
|
explanation = f""" |
|
|
CONFIDENCE ASSESSMENT: {overall*100:.1f}% Overall ({threshold_msg}) |
|
|
|
|
|
Breakdown: |
|
|
- Data Extraction: {extraction*100:.1f}% - Quality of information extracted from source document |
|
|
- Model Analysis: {model*100:.1f}% - Confidence in AI model predictions and classifications |
|
|
- Data Quality: {quality*100:.1f}% - Completeness and clarity of source data |
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
if overall >= 0.85: |
|
|
explanation += """ |
|
|
CLINICAL USE: |
|
|
This analysis meets our high-confidence threshold (≥85%) and can be used for clinical decision support with standard clinical oversight. The automated findings are reliable but should still be verified by qualified healthcare providers as part of normal clinical workflow. |
|
|
""" |
|
|
elif overall >= 0.60: |
|
|
explanation += """ |
|
|
CLINICAL USE: |
|
|
This analysis shows moderate confidence (60-85%) and requires additional clinical review before use in patient care. Certain findings may need verification through additional testing or expert consultation. Use clinical judgment to determine which aspects require closer scrutiny. |
|
|
""" |
|
|
else: |
|
|
explanation += """ |
|
|
CLINICAL USE: |
|
|
This analysis shows low confidence (<60%) and should not be used for clinical decisions without comprehensive manual review. Consider: |
|
|
- Obtaining higher quality source data |
|
|
- Manual expert interpretation of raw data |
|
|
- Additional diagnostic studies |
|
|
- Consultation with relevant specialists |
|
|
""" |
|
|
|
|
|
return explanation.strip() |
|
|
|
|
|
def _generate_recommendations( |
|
|
self, |
|
|
structured_data: Dict[str, Any], |
|
|
confidence_scores: Dict[str, float], |
|
|
modality: str |
|
|
) -> List[Dict[str, str]]: |
|
|
"""Generate actionable clinical recommendations""" |
|
|
|
|
|
recommendations = [] |
|
|
overall_confidence = confidence_scores.get("overall_confidence", 0.0) |
|
|
|
|
|
|
|
|
if overall_confidence < 0.85: |
|
|
recommendations.append({ |
|
|
"category": "Quality Assurance", |
|
|
"recommendation": f"Manual review required (confidence: {overall_confidence*100:.1f}%)", |
|
|
"priority": "high" if overall_confidence < 0.60 else "medium", |
|
|
"rationale": "Confidence below auto-approval threshold" |
|
|
}) |
|
|
|
|
|
|
|
|
if modality == "ECG": |
|
|
rhythm = structured_data.get("rhythm_classification", {}) |
|
|
intervals = structured_data.get("intervals", {}) |
|
|
|
|
|
|
|
|
arrhythmias = rhythm.get("arrhythmia_types", []) |
|
|
if arrhythmias: |
|
|
recommendations.append({ |
|
|
"category": "Cardiac Evaluation", |
|
|
"recommendation": f"Cardiology consultation for detected arrhythmias: {', '.join(arrhythmias)}", |
|
|
"priority": "high", |
|
|
"rationale": "Arrhythmia detection requires specialist evaluation" |
|
|
}) |
|
|
|
|
|
|
|
|
qtc = intervals.get("qtc_ms", 0) |
|
|
if qtc and qtc > 480: |
|
|
recommendations.append({ |
|
|
"category": "Medication Review", |
|
|
"recommendation": "Review medications for QT-prolonging drugs", |
|
|
"priority": "high", |
|
|
"rationale": f"QTc prolonged: {qtc} ms (>480 ms)" |
|
|
}) |
|
|
|
|
|
elif modality == "radiology": |
|
|
findings = structured_data.get("findings", {}) |
|
|
critical = findings.get("critical_findings", []) |
|
|
|
|
|
if critical: |
|
|
recommendations.append({ |
|
|
"category": "Urgent Evaluation", |
|
|
"recommendation": f"Immediate radiologist review for critical findings: {', '.join(critical)}", |
|
|
"priority": "critical", |
|
|
"rationale": "Critical findings require immediate attention" |
|
|
}) |
|
|
|
|
|
elif modality == "laboratory": |
|
|
critical_values = structured_data.get("critical_values", []) |
|
|
abnormal_count = structured_data.get("abnormal_count", 0) |
|
|
|
|
|
if critical_values: |
|
|
recommendations.append({ |
|
|
"category": "Critical Lab Values", |
|
|
"recommendation": f"Immediate physician notification for critical values: {', '.join(critical_values)}", |
|
|
"priority": "critical", |
|
|
"rationale": "Critical lab values require immediate intervention" |
|
|
}) |
|
|
|
|
|
if abnormal_count > 5: |
|
|
recommendations.append({ |
|
|
"category": "Comprehensive Evaluation", |
|
|
"recommendation": f"Multiple abnormal results ({abnormal_count}) - consider systematic evaluation", |
|
|
"priority": "medium", |
|
|
"rationale": "Pattern of abnormalities may indicate systemic condition" |
|
|
}) |
|
|
|
|
|
|
|
|
recommendations.append({ |
|
|
"category": "Documentation", |
|
|
"recommendation": "Maintain this analysis report with patient medical records", |
|
|
"priority": "low", |
|
|
"rationale": "Standard medical record-keeping requirement" |
|
|
}) |
|
|
|
|
|
recommendations.append({ |
|
|
"category": "Clinical Correlation", |
|
|
"recommendation": "Correlate AI findings with clinical presentation and patient history", |
|
|
"priority": "high", |
|
|
"rationale": "AI analysis should inform but not replace clinical judgment" |
|
|
}) |
|
|
|
|
|
return recommendations |
|
|
|
|
|
def _generate_multi_modal_recommendations( |
|
|
self, |
|
|
modalities_data: Dict[str, Dict[str, Any]], |
|
|
confidence_scores: Dict[str, float] |
|
|
) -> List[Dict[str, str]]: |
|
|
"""Generate recommendations for multi-modal analysis""" |
|
|
|
|
|
recommendations = [] |
|
|
|
|
|
|
|
|
avg_confidence = sum(confidence_scores.values()) / len(confidence_scores) |
|
|
if avg_confidence < 0.85: |
|
|
recommendations.append({ |
|
|
"category": "Comprehensive Review", |
|
|
"recommendation": "Multi-modal review recommended due to moderate confidence", |
|
|
"priority": "high", |
|
|
"rationale": f"Average confidence across modalities: {avg_confidence*100:.1f}%" |
|
|
}) |
|
|
|
|
|
|
|
|
recommendations.append({ |
|
|
"category": "Care Coordination", |
|
|
"recommendation": "Coordinate care across all identified clinical domains", |
|
|
"priority": "high", |
|
|
"rationale": f"Multiple medical modalities analyzed: {', '.join(modalities_data.keys())}" |
|
|
}) |
|
|
|
|
|
return recommendations |
|
|
|
|
|
def _assess_risk_level( |
|
|
self, |
|
|
structured_data: Dict[str, Any], |
|
|
confidence_scores: Dict[str, float], |
|
|
modality: str |
|
|
) -> Literal["low", "moderate", "high"]: |
|
|
"""Assess clinical risk level based on findings""" |
|
|
|
|
|
|
|
|
if confidence_scores.get("overall_confidence", 0.0) < 0.60: |
|
|
return "high" |
|
|
|
|
|
if modality == "ECG": |
|
|
arrhythmias = structured_data.get("rhythm_classification", {}).get("arrhythmia_types", []) |
|
|
if arrhythmias: |
|
|
return "high" |
|
|
|
|
|
intervals = structured_data.get("intervals", {}) |
|
|
qtc = intervals.get("qtc_ms", 0) |
|
|
if qtc and qtc > 500: |
|
|
return "high" |
|
|
elif qtc and qtc > 480: |
|
|
return "moderate" |
|
|
|
|
|
elif modality == "radiology": |
|
|
critical = structured_data.get("findings", {}).get("critical_findings", []) |
|
|
if critical: |
|
|
return "high" |
|
|
|
|
|
incidental = structured_data.get("findings", {}).get("incidental_findings", []) |
|
|
if len(incidental) > 3: |
|
|
return "moderate" |
|
|
|
|
|
elif modality == "laboratory": |
|
|
critical_values = structured_data.get("critical_values", []) |
|
|
if critical_values: |
|
|
return "high" |
|
|
|
|
|
abnormal_count = structured_data.get("abnormal_count", 0) |
|
|
if abnormal_count > 5: |
|
|
return "moderate" |
|
|
|
|
|
return "low" |
|
|
|
|
|
def _assess_multi_modal_risk( |
|
|
self, |
|
|
modalities_data: Dict[str, Dict[str, Any]] |
|
|
) -> Literal["low", "moderate", "high"]: |
|
|
"""Assess risk level for multi-modal analysis""" |
|
|
|
|
|
risk_levels = [] |
|
|
for modality, data in modalities_data.items(): |
|
|
confidence = self._extract_confidence_scores(data) |
|
|
risk = self._assess_risk_level(data, confidence, modality) |
|
|
risk_levels.append(risk) |
|
|
|
|
|
|
|
|
if "high" in risk_levels: |
|
|
return "high" |
|
|
elif "moderate" in risk_levels: |
|
|
return "moderate" |
|
|
else: |
|
|
return "low" |
|
|
|
|
|
def _extract_confidence_scores(self, structured_data: Dict[str, Any]) -> Dict[str, float]: |
|
|
"""Extract confidence scores from structured data""" |
|
|
|
|
|
confidence_data = structured_data.get("confidence", {}) |
|
|
|
|
|
if isinstance(confidence_data, dict): |
|
|
return { |
|
|
"extraction_confidence": confidence_data.get("extraction_confidence", 0.0), |
|
|
"model_confidence": confidence_data.get("model_confidence", 0.0), |
|
|
"data_quality": confidence_data.get("data_quality", 0.0), |
|
|
"overall_confidence": confidence_data.get("overall_confidence", 0.0) or |
|
|
(0.5 * confidence_data.get("extraction_confidence", 0.0) + |
|
|
0.3 * confidence_data.get("model_confidence", 0.0) + |
|
|
0.2 * confidence_data.get("data_quality", 0.0)) |
|
|
} |
|
|
else: |
|
|
|
|
|
return { |
|
|
"extraction_confidence": 0.75, |
|
|
"model_confidence": 0.75, |
|
|
"data_quality": 0.75, |
|
|
"overall_confidence": 0.75 |
|
|
} |
|
|
|
|
|
def _generate_fallback_synthesis( |
|
|
self, |
|
|
modality: str, |
|
|
summary_type: str, |
|
|
error_message: str |
|
|
) -> Dict[str, Any]: |
|
|
"""Generate fallback synthesis when synthesis fails""" |
|
|
|
|
|
return { |
|
|
"synthesis_id": f"fallback-{datetime.utcnow().timestamp()}", |
|
|
"narrative": f"Automated synthesis unavailable for {modality}. Manual interpretation required.", |
|
|
"confidence_explanation": "Synthesis service encountered an error. This analysis requires manual review.", |
|
|
"recommendations": [ |
|
|
{ |
|
|
"category": "Manual Review", |
|
|
"recommendation": "Complete manual interpretation required", |
|
|
"priority": "critical", |
|
|
"rationale": "Automated synthesis failed" |
|
|
} |
|
|
], |
|
|
"risk_level": "high", |
|
|
"requires_review": True, |
|
|
"confidence_scores": { |
|
|
"extraction_confidence": 0.0, |
|
|
"model_confidence": 0.0, |
|
|
"data_quality": 0.0, |
|
|
"overall_confidence": 0.0 |
|
|
}, |
|
|
"error": error_message, |
|
|
"timestamp": datetime.utcnow().isoformat() |
|
|
} |
|
|
|
|
|
def get_synthesis_history( |
|
|
self, |
|
|
user_id: Optional[str] = None, |
|
|
limit: int = 100 |
|
|
) -> List[Dict[str, Any]]: |
|
|
"""Retrieve synthesis history for audit purposes""" |
|
|
|
|
|
if user_id: |
|
|
history = [ |
|
|
entry for entry in self.synthesis_history |
|
|
if entry.get("user_id") == user_id |
|
|
] |
|
|
else: |
|
|
history = self.synthesis_history |
|
|
|
|
|
return history[-limit:] |
|
|
|
|
|
def get_synthesis_statistics(self) -> Dict[str, Any]: |
|
|
"""Get statistics about synthesis service usage""" |
|
|
|
|
|
total = len(self.synthesis_history) |
|
|
if total == 0: |
|
|
return { |
|
|
"total_syntheses": 0, |
|
|
"average_confidence": 0.0, |
|
|
"review_required_percentage": 0.0, |
|
|
"average_generation_time": 0.0 |
|
|
} |
|
|
|
|
|
confidences = [entry.get("overall_confidence", 0.0) for entry in self.synthesis_history] |
|
|
generation_times = [entry.get("generation_time_seconds", 0.0) for entry in self.synthesis_history] |
|
|
requires_review = sum(1 for entry in self.synthesis_history if entry.get("requires_review", False)) |
|
|
|
|
|
return { |
|
|
"total_syntheses": total, |
|
|
"average_confidence": sum(confidences) / len(confidences), |
|
|
"review_required_percentage": (requires_review / total) * 100, |
|
|
"average_generation_time": sum(generation_times) / len(generation_times), |
|
|
"by_modality": self._count_by_field("modality"), |
|
|
"by_risk_level": self._count_by_field("risk_level") |
|
|
} |
|
|
|
|
|
def _count_by_field(self, field: str) -> Dict[str, int]: |
|
|
"""Count occurrences by field""" |
|
|
counts = {} |
|
|
for entry in self.synthesis_history: |
|
|
value = entry.get(field, "unknown") |
|
|
counts[value] = counts.get(value, 0) + 1 |
|
|
return counts |
|
|
|
|
|
|
|
|
|
|
|
_synthesis_service = None |
|
|
|
|
|
|
|
|
def get_synthesis_service() -> ClinicalSynthesisService: |
|
|
"""Get singleton synthesis service instance""" |
|
|
global _synthesis_service |
|
|
if _synthesis_service is None: |
|
|
_synthesis_service = ClinicalSynthesisService() |
|
|
return _synthesis_service |
|
|
|