snikhilesh commited on
Commit
38b1019
Β·
verified Β·
1 Parent(s): 15ee17a

Deploy test_prompt_templates.py to backend/ directory

Browse files
Files changed (1) hide show
  1. backend/test_prompt_templates.py +419 -0
backend/test_prompt_templates.py ADDED
@@ -0,0 +1,419 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Unit Tests for Medical Prompt Templates
3
+ Tests prompt generation logic without requiring ML model dependencies
4
+
5
+ Author: MiniMax Agent
6
+ Date: 2025-10-29
7
+ """
8
+
9
+ import sys
10
+ sys.path.insert(0, '/workspace/medical-ai-platform/backend')
11
+
12
+ from medical_prompt_templates import PromptTemplateLibrary, SummaryType
13
+
14
+
15
+ def create_sample_ecg_data():
16
+ """Sample ECG data for testing"""
17
+ return {
18
+ "metadata": {
19
+ "document_id": "ecg-test-001",
20
+ "facility": "Test Hospital",
21
+ "document_date": "2025-10-29"
22
+ },
23
+ "intervals": {
24
+ "pr_ms": 165.0,
25
+ "qrs_ms": 92.0,
26
+ "qt_ms": 390.0,
27
+ "qtc_ms": 425.0,
28
+ "rr_ms": 850.0
29
+ },
30
+ "rhythm_classification": {
31
+ "primary_rhythm": "Normal Sinus Rhythm",
32
+ "heart_rate_bpm": 71,
33
+ "heart_rate_regularity": "regular",
34
+ "arrhythmia_types": []
35
+ },
36
+ "arrhythmia_probabilities": {
37
+ "normal_rhythm": 0.92,
38
+ "atrial_fibrillation": 0.02
39
+ },
40
+ "derived_features": {
41
+ "st_elevation_mm": {},
42
+ "axis_deviation": "normal",
43
+ "t_wave_abnormalities": []
44
+ }
45
+ }
46
+
47
+
48
+ def create_sample_model_outputs():
49
+ """Sample model outputs"""
50
+ return [
51
+ {
52
+ "model_name": "Bio_ClinicalBERT",
53
+ "domain": "clinical_notes",
54
+ "result": {"summary": "Analysis complete", "confidence": 0.87}
55
+ }
56
+ ]
57
+
58
+
59
+ def test_ecg_clinician_prompt():
60
+ """Test ECG clinician prompt generation"""
61
+ print("\n" + "="*80)
62
+ print("TEST 1: ECG Clinician Prompt Generation")
63
+ print("="*80)
64
+
65
+ lib = PromptTemplateLibrary()
66
+ ecg_data = create_sample_ecg_data()
67
+ model_outputs = create_sample_model_outputs()
68
+ confidence = {"overall_confidence": 0.89}
69
+
70
+ prompt = lib.get_clinician_summary_template(
71
+ modality="ECG",
72
+ structured_data=ecg_data,
73
+ model_outputs=model_outputs,
74
+ confidence_scores=confidence
75
+ )
76
+
77
+ # Validate prompt contains key elements
78
+ assert "ECG" in prompt, "Prompt should mention ECG"
79
+ assert "Heart Rate: 71 bpm" in prompt, "Prompt should include heart rate"
80
+ assert "Normal Sinus Rhythm" in prompt, "Prompt should include rhythm"
81
+ assert "ANALYSIS CONFIDENCE: 89.0%" in prompt, "Prompt should include confidence"
82
+ assert "TECHNICAL SUMMARY" in prompt, "Prompt should have technical summary section"
83
+ assert "RECOMMENDATIONS" in prompt, "Prompt should have recommendations section"
84
+
85
+ print(f"βœ“ Prompt generated: {len(prompt)} characters")
86
+ print(f"βœ“ Contains all required sections")
87
+ print(f"\nSample excerpt:\n{prompt[:300]}...\n")
88
+
89
+ return True
90
+
91
+
92
+ def test_ecg_patient_prompt():
93
+ """Test ECG patient-friendly prompt generation"""
94
+ print("\n" + "="*80)
95
+ print("TEST 2: ECG Patient Prompt Generation")
96
+ print("="*80)
97
+
98
+ lib = PromptTemplateLibrary()
99
+ ecg_data = create_sample_ecg_data()
100
+ model_outputs = create_sample_model_outputs()
101
+ confidence = {"overall_confidence": 0.89}
102
+
103
+ prompt = lib.get_patient_summary_template(
104
+ modality="ECG",
105
+ structured_data=ecg_data,
106
+ model_outputs=model_outputs,
107
+ confidence_scores=confidence
108
+ )
109
+
110
+ # Validate patient-friendly language
111
+ assert "YOUR ECG RESULTS" in prompt, "Should use patient-friendly heading"
112
+ assert "simple" in prompt.lower(), "Should mention simple language"
113
+ assert "WHAT WE FOUND" in prompt, "Should have clear sections"
114
+ assert "NEXT STEPS" in prompt, "Should include next steps"
115
+
116
+ print(f"βœ“ Prompt generated: {len(prompt)} characters")
117
+ print(f"βœ“ Patient-friendly language detected")
118
+ print(f"\nSample excerpt:\n{prompt[:300]}...\n")
119
+
120
+ return True
121
+
122
+
123
+ def test_radiology_prompts():
124
+ """Test radiology prompt generation"""
125
+ print("\n" + "="*80)
126
+ print("TEST 3: Radiology Prompt Generation")
127
+ print("="*80)
128
+
129
+ lib = PromptTemplateLibrary()
130
+ rad_data = {
131
+ "metadata": {"document_id": "rad-001"},
132
+ "image_references": [
133
+ {"modality": "CT", "body_part": "Chest"}
134
+ ],
135
+ "findings": {
136
+ "findings_text": "Clear lungs bilaterally",
137
+ "impression_text": "No acute abnormality",
138
+ "critical_findings": [],
139
+ "incidental_findings": []
140
+ },
141
+ "metrics": {"organ_volumes": {}, "lesion_measurements": []}
142
+ }
143
+ model_outputs = create_sample_model_outputs()
144
+ confidence = {"overall_confidence": 0.85}
145
+
146
+ # Test clinician prompt
147
+ clinician_prompt = lib.get_clinician_summary_template(
148
+ modality="radiology",
149
+ structured_data=rad_data,
150
+ model_outputs=model_outputs,
151
+ confidence_scores=confidence
152
+ )
153
+
154
+ assert "IMAGING STUDY DETAILS" in clinician_prompt
155
+ assert "CT" in clinician_prompt
156
+ assert "Chest" in clinician_prompt
157
+
158
+ # Test patient prompt
159
+ patient_prompt = lib.get_patient_summary_template(
160
+ modality="radiology",
161
+ structured_data=rad_data,
162
+ model_outputs=model_outputs,
163
+ confidence_scores=confidence
164
+ )
165
+
166
+ assert "YOUR IMAGING STUDY" in patient_prompt
167
+ assert "Type of Scan" in patient_prompt
168
+
169
+ print(f"βœ“ Clinician prompt: {len(clinician_prompt)} characters")
170
+ print(f"βœ“ Patient prompt: {len(patient_prompt)} characters")
171
+
172
+ return True
173
+
174
+
175
+ def test_laboratory_prompts():
176
+ """Test laboratory prompt generation"""
177
+ print("\n" + "="*80)
178
+ print("TEST 4: Laboratory Prompt Generation")
179
+ print("="*80)
180
+
181
+ lib = PromptTemplateLibrary()
182
+ lab_data = {
183
+ "metadata": {"document_id": "lab-001"},
184
+ "tests": [
185
+ {
186
+ "test_name": "Glucose",
187
+ "value": 105.0,
188
+ "unit": "mg/dL",
189
+ "reference_range_low": 70.0,
190
+ "reference_range_high": 99.0,
191
+ "flags": ["H"]
192
+ }
193
+ ],
194
+ "abnormal_count": 1,
195
+ "critical_values": [],
196
+ "panel_name": "Basic Metabolic Panel",
197
+ "collection_date": "2025-10-29"
198
+ }
199
+ model_outputs = create_sample_model_outputs()
200
+ confidence = {"overall_confidence": 0.92}
201
+
202
+ # Test clinician prompt
203
+ clinician_prompt = lib.get_clinician_summary_template(
204
+ modality="laboratory",
205
+ structured_data=lab_data,
206
+ model_outputs=model_outputs,
207
+ confidence_scores=confidence
208
+ )
209
+
210
+ assert "LABORATORY PANEL" in clinician_prompt
211
+ assert "Glucose" in clinician_prompt
212
+ assert "105.0" in clinician_prompt
213
+ assert "SUMMARY OF KEY FINDINGS" in clinician_prompt
214
+
215
+ # Test patient prompt
216
+ patient_prompt = lib.get_patient_summary_template(
217
+ modality="laboratory",
218
+ structured_data=lab_data,
219
+ model_outputs=model_outputs,
220
+ confidence_scores=confidence
221
+ )
222
+
223
+ assert "YOUR LAB RESULTS" in patient_prompt
224
+ assert "everyday language" in patient_prompt.lower()
225
+
226
+ print(f"βœ“ Clinician prompt: {len(clinician_prompt)} characters")
227
+ print(f"βœ“ Patient prompt: {len(patient_prompt)} characters")
228
+
229
+ return True
230
+
231
+
232
+ def test_clinical_notes_prompts():
233
+ """Test clinical notes prompt generation"""
234
+ print("\n" + "="*80)
235
+ print("TEST 5: Clinical Notes Prompt Generation")
236
+ print("="*80)
237
+
238
+ lib = PromptTemplateLibrary()
239
+ notes_data = {
240
+ "metadata": {"document_id": "note-001"},
241
+ "note_type": "progress_note",
242
+ "sections": [
243
+ {
244
+ "section_type": "chief_complaint",
245
+ "content": "Patient presents with chest pain"
246
+ }
247
+ ],
248
+ "entities": [],
249
+ "diagnoses": ["Chest pain, unspecified"],
250
+ "medications": ["Aspirin 81mg daily"]
251
+ }
252
+ model_outputs = create_sample_model_outputs()
253
+ confidence = {"overall_confidence": 0.87}
254
+
255
+ # Test clinician prompt
256
+ clinician_prompt = lib.get_clinician_summary_template(
257
+ modality="clinical_notes",
258
+ structured_data=notes_data,
259
+ model_outputs=model_outputs,
260
+ confidence_scores=confidence
261
+ )
262
+
263
+ assert "CLINICAL SECTIONS" in clinician_prompt
264
+ assert "ASSESSMENT" in clinician_prompt
265
+ assert "chest pain" in clinician_prompt.lower()
266
+
267
+ # Test patient prompt
268
+ patient_prompt = lib.get_patient_summary_template(
269
+ modality="clinical_notes",
270
+ structured_data=notes_data,
271
+ model_outputs=model_outputs,
272
+ confidence_scores=confidence
273
+ )
274
+
275
+ assert "REASON FOR YOUR VISIT" in patient_prompt
276
+ assert "TREATMENT PLAN" in patient_prompt
277
+
278
+ print(f"βœ“ Clinician prompt: {len(clinician_prompt)} characters")
279
+ print(f"βœ“ Patient prompt: {len(patient_prompt)} characters")
280
+
281
+ return True
282
+
283
+
284
+ def test_multi_modal_prompt():
285
+ """Test multi-modal synthesis prompt"""
286
+ print("\n" + "="*80)
287
+ print("TEST 6: Multi-Modal Synthesis Prompt")
288
+ print("="*80)
289
+
290
+ lib = PromptTemplateLibrary()
291
+
292
+ modalities = ["ECG", "radiology", "laboratory"]
293
+ all_data = {
294
+ "ECG": create_sample_ecg_data(),
295
+ "radiology": {"metadata": {"document_id": "rad-001"}},
296
+ "laboratory": {"metadata": {"document_id": "lab-001"}}
297
+ }
298
+ confidence_scores = {
299
+ "ECG": 0.89,
300
+ "radiology": 0.85,
301
+ "laboratory": 0.92
302
+ }
303
+
304
+ prompt = lib.get_multi_modal_synthesis_template(
305
+ modalities=modalities,
306
+ all_data=all_data,
307
+ confidence_scores=confidence_scores
308
+ )
309
+
310
+ assert "multiple medical documents" in prompt.lower()
311
+ assert "ECG" in prompt
312
+ assert "INTEGRATED CLINICAL PICTURE" in prompt
313
+ assert "COORDINATED CARE PLAN" in prompt
314
+
315
+ print(f"βœ“ Multi-modal prompt: {len(prompt)} characters")
316
+ print(f"βœ“ Includes all {len(modalities)} modalities")
317
+
318
+ return True
319
+
320
+
321
+ def test_confidence_explanation_prompt():
322
+ """Test confidence explanation prompt"""
323
+ print("\n" + "="*80)
324
+ print("TEST 7: Confidence Explanation Prompt")
325
+ print("="*80)
326
+
327
+ lib = PromptTemplateLibrary()
328
+
329
+ # Test high confidence
330
+ high_conf = {
331
+ "overall_confidence": 0.92,
332
+ "extraction_confidence": 0.94,
333
+ "model_confidence": 0.91,
334
+ "data_quality": 0.95
335
+ }
336
+
337
+ prompt_high = lib.get_confidence_explanation_template(
338
+ confidence_scores=high_conf,
339
+ modality="ECG"
340
+ )
341
+
342
+ assert "92.0%" in prompt_high
343
+ assert "AUTO-APPROVED" in prompt_high
344
+
345
+ # Test low confidence
346
+ low_conf = {
347
+ "overall_confidence": 0.55,
348
+ "extraction_confidence": 0.55,
349
+ "model_confidence": 0.50,
350
+ "data_quality": 0.58
351
+ }
352
+
353
+ prompt_low = lib.get_confidence_explanation_template(
354
+ confidence_scores=low_conf,
355
+ modality="ECG"
356
+ )
357
+
358
+ assert "55.0%" in prompt_low
359
+ assert "MANUAL REVIEW REQUIRED" in prompt_low
360
+
361
+ print(f"βœ“ High confidence prompt generated")
362
+ print(f"βœ“ Low confidence prompt generated")
363
+ print(f"βœ“ Threshold detection working correctly")
364
+
365
+ return True
366
+
367
+
368
+ def run_prompt_template_tests():
369
+ """Run all prompt template tests"""
370
+ print("\n" + "="*80)
371
+ print("MEDICAL PROMPT TEMPLATES - UNIT TEST SUITE")
372
+ print("Testing Prompt Generation Logic")
373
+ print("="*80)
374
+
375
+ tests = [
376
+ ("ECG Clinician Prompt", test_ecg_clinician_prompt),
377
+ ("ECG Patient Prompt", test_ecg_patient_prompt),
378
+ ("Radiology Prompts", test_radiology_prompts),
379
+ ("Laboratory Prompts", test_laboratory_prompts),
380
+ ("Clinical Notes Prompts", test_clinical_notes_prompts),
381
+ ("Multi-Modal Prompt", test_multi_modal_prompt),
382
+ ("Confidence Explanation", test_confidence_explanation_prompt)
383
+ ]
384
+
385
+ results = []
386
+
387
+ for test_name, test_func in tests:
388
+ try:
389
+ success = test_func()
390
+ results.append((test_name, "PASS" if success else "FAIL"))
391
+ print(f"βœ“ {test_name}: PASS")
392
+ except AssertionError as e:
393
+ print(f"βœ— {test_name}: FAIL - {str(e)}")
394
+ results.append((test_name, "FAIL"))
395
+ except Exception as e:
396
+ print(f"βœ— {test_name}: ERROR - {str(e)}")
397
+ import traceback
398
+ traceback.print_exc()
399
+ results.append((test_name, "ERROR"))
400
+
401
+ # Print summary
402
+ print("\n" + "="*80)
403
+ print("TEST SUMMARY")
404
+ print("="*80)
405
+ for test_name, status in results:
406
+ status_symbol = "βœ“" if status == "PASS" else "βœ—"
407
+ print(f"{status_symbol} {test_name}: {status}")
408
+
409
+ passed = sum(1 for _, status in results if status == "PASS")
410
+ total = len(results)
411
+ print(f"\nTotal: {passed}/{total} tests passed ({passed/total*100:.1f}%)")
412
+ print("="*80)
413
+
414
+ return passed == total
415
+
416
+
417
+ if __name__ == "__main__":
418
+ success = run_prompt_template_tests()
419
+ exit(0 if success else 1)