File size: 8,924 Bytes
a963d65 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
#!/usr/bin/env python3
"""
Download Medical Files for Testing
Simple script to download DICOM and other medical files for testing FhirFlame
"""
import os
import requests
import time
from pathlib import Path
from typing import List
class MedicalFileDownloader:
"""Simple downloader for medical test files"""
def __init__(self):
self.download_dir = Path("tests/medical_files")
self.download_dir.mkdir(parents=True, exist_ok=True)
# Sample medical files (these are publicly available test files)
self.file_sources = {
"dicom_samples": [
# These would be actual DICOM file URLs - using placeholders for now
"https://www.rubomedical.com/dicom_files/CT_small.dcm",
"https://www.rubomedical.com/dicom_files/MR_small.dcm",
"https://www.rubomedical.com/dicom_files/US_small.dcm",
"https://www.rubomedical.com/dicom_files/XA_small.dcm",
],
"text_reports": [
# Medical text documents for testing
"sample_discharge_summary.txt",
"sample_lab_report.txt",
"sample_radiology_report.txt"
]
}
def download_file(self, url: str, filename: str) -> bool:
"""Download a single file"""
try:
file_path = self.download_dir / filename
# Skip if file already exists
if file_path.exists():
print(f"βοΈ Skipping {filename} (already exists)")
return True
print(f"π₯ Downloading {filename}...")
# Try to download the file
response = requests.get(url, timeout=30, stream=True)
if response.status_code == 200:
with open(file_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
file_size = os.path.getsize(file_path)
print(f"β
Downloaded {filename} ({file_size} bytes)")
return True
else:
print(f"β Failed to download {filename}: HTTP {response.status_code}")
return False
except Exception as e:
print(f"β Error downloading {filename}: {e}")
return False
def create_sample_medical_files(self) -> List[str]:
"""Create sample medical text files for testing"""
sample_files = []
# Sample discharge summary
discharge_summary = """
DISCHARGE SUMMARY
Patient: John Smith
DOB: 1975-03-15
MRN: MR123456789
Admission Date: 2024-01-15
Discharge Date: 2024-01-18
CHIEF COMPLAINT:
Chest pain and shortness of breath
HISTORY OF PRESENT ILLNESS:
45-year-old male presents with acute onset chest pain radiating to left arm.
Associated with diaphoresis and nausea. No prior cardiac history.
VITAL SIGNS:
Blood Pressure: 145/95 mmHg
Heart Rate: 102 bpm
Temperature: 98.6Β°F
Oxygen Saturation: 96% on room air
ASSESSMENT AND PLAN:
1. Acute coronary syndrome - rule out myocardial infarction
2. Hypertension - new diagnosis
3. Start aspirin 325mg daily
4. Lisinopril 10mg daily for blood pressure control
5. Atorvastatin 40mg daily
MEDICATIONS PRESCRIBED:
- Aspirin 325mg daily
- Lisinopril 10mg daily
- Atorvastatin 40mg daily
- Nitroglycerin 0.4mg sublingual PRN chest pain
FOLLOW-UP:
Cardiology in 1 week
Primary care in 2 weeks
"""
# Sample lab report
lab_report = """
LABORATORY REPORT
Patient: Maria Rodriguez
DOB: 1962-08-22
MRN: MR987654321
Collection Date: 2024-01-20
COMPLETE BLOOD COUNT:
White Blood Cell Count: 7.2 K/uL (Normal: 4.0-11.0)
Red Blood Cell Count: 4.5 M/uL (Normal: 4.0-5.2)
Hemoglobin: 13.8 g/dL (Normal: 12.0-15.5)
Hematocrit: 41.2% (Normal: 36.0-46.0)
Platelet Count: 285 K/uL (Normal: 150-450)
COMPREHENSIVE METABOLIC PANEL:
Glucose: 126 mg/dL (High - Normal: 70-100)
BUN: 18 mg/dL (Normal: 7-20)
Creatinine: 1.0 mg/dL (Normal: 0.6-1.2)
eGFR: >60 (Normal)
Sodium: 140 mEq/L (Normal: 136-145)
Potassium: 4.2 mEq/L (Normal: 3.5-5.1)
Chloride: 102 mEq/L (Normal: 98-107)
LIPID PANEL:
Total Cholesterol: 220 mg/dL (High - Optimal: <200)
LDL Cholesterol: 145 mg/dL (High - Optimal: <100)
HDL Cholesterol: 45 mg/dL (Low - Normal: >40)
Triglycerides: 150 mg/dL (Normal: <150)
HEMOGLOBIN A1C:
HbA1c: 6.8% (Elevated - Target: <7% for diabetics)
"""
# Sample radiology report
radiology_report = """
RADIOLOGY REPORT
Patient: Robert Wilson
DOB: 1980-12-10
MRN: MR456789123
Exam Date: 2024-01-22
Exam Type: Chest X-Ray PA and Lateral
CLINICAL INDICATION:
Cough and fever
TECHNIQUE:
PA and lateral chest radiographs were obtained.
FINDINGS:
The lungs are well expanded and clear. No focal consolidation,
pleural effusion, or pneumothorax is identified. The cardiac
silhouette is normal in size and contour. The mediastinal
contours are within normal limits. No acute bony abnormalities.
IMPRESSION:
Normal chest radiograph. No evidence of acute cardiopulmonary disease.
Electronically signed by:
Dr. Sarah Johnson, MD
Radiologist
"""
# Write sample files
samples = {
"sample_discharge_summary.txt": discharge_summary,
"sample_lab_report.txt": lab_report,
"sample_radiology_report.txt": radiology_report
}
for filename, content in samples.items():
file_path = self.download_dir / filename
if not file_path.exists():
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"β
Created sample file: {filename}")
sample_files.append(str(file_path))
else:
print(f"βοΈ Sample file already exists: {filename}")
sample_files.append(str(file_path))
return sample_files
def download_all_files(self, limit: int = 10) -> List[str]:
"""Download medical files for testing"""
downloaded_files = []
print("π₯ Medical File Downloader")
print("=" * 40)
# Create sample text files first (these always work)
print("\nπ Creating sample medical text files...")
sample_files = self.create_sample_medical_files()
downloaded_files.extend(sample_files)
# Try to download DICOM files (may fail if URLs don't exist)
print(f"\nπ₯ Attempting to download DICOM files...")
dicom_downloaded = 0
for i, url in enumerate(self.file_sources["dicom_samples"][:limit]):
if dicom_downloaded >= 5: # Limit DICOM downloads
break
filename = f"sample_dicom_{i+1}.dcm"
# Since these URLs may not exist, we'll create mock DICOM files instead
print(f"β οΈ Real DICOM download not available, creating mock file: {filename}")
mock_file_path = self.download_dir / filename
if not mock_file_path.exists():
# Create a small mock file (real DICOM would be much larger)
with open(mock_file_path, 'wb') as f:
f.write(b"DICM" + b"MOCK_DICOM_FOR_TESTING" * 100)
print(f"β
Created mock DICOM file: {filename}")
downloaded_files.append(str(mock_file_path))
dicom_downloaded += 1
else:
downloaded_files.append(str(mock_file_path))
dicom_downloaded += 1
time.sleep(0.1) # Be nice to servers
print(f"\nπ Download Summary:")
print(f" Total files available: {len(downloaded_files)}")
print(f" Text files: {len(sample_files)}")
print(f" DICOM files: {dicom_downloaded}")
print(f" Download directory: {self.download_dir}")
return downloaded_files
def list_downloaded_files(self) -> List[str]:
"""List all downloaded medical files"""
all_files = []
for file_path in self.download_dir.iterdir():
if file_path.is_file():
all_files.append(str(file_path))
return sorted(all_files)
def main():
"""Main download function"""
downloader = MedicalFileDownloader()
print("π Starting medical file download...")
files = downloader.download_all_files(limit=10)
print(f"\nβ
Download complete! {len(files)} files ready for testing.")
print("\nDownloaded files:")
for file_path in files:
file_size = os.path.getsize(file_path)
print(f" π {os.path.basename(file_path)} ({file_size} bytes)")
return files
if __name__ == "__main__":
main() |