|
|
|
|
|
"""Test handling of IM_0001 and other files without extensions""" |
|
|
|
|
|
import sys |
|
|
import os |
|
|
import tempfile |
|
|
import shutil |
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend')) |
|
|
|
|
|
from gradio_medical_image_analyzer import MedicalImageAnalyzer |
|
|
|
|
|
def test_im_files(): |
|
|
"""Test files without extensions like IM_0001""" |
|
|
|
|
|
print("π₯ Testing Medical Image Analyzer with IM_0001 Files") |
|
|
print("=" * 50) |
|
|
|
|
|
analyzer = MedicalImageAnalyzer() |
|
|
|
|
|
|
|
|
temp_dir = tempfile.mkdtemp() |
|
|
|
|
|
try: |
|
|
|
|
|
test_dicom = None |
|
|
possible_paths = [ |
|
|
"../vetdicomviewer/tools/data/CTImage.dcm", |
|
|
"tools/data/CTImage.dcm", |
|
|
"../tests/data/CTImage.dcm" |
|
|
] |
|
|
|
|
|
for path in possible_paths: |
|
|
if os.path.exists(path): |
|
|
test_dicom = path |
|
|
break |
|
|
|
|
|
if test_dicom: |
|
|
|
|
|
im_file = os.path.join(temp_dir, "IM_0001") |
|
|
shutil.copy(test_dicom, im_file) |
|
|
|
|
|
print(f"π Created test file: IM_0001 (from {os.path.basename(test_dicom)})") |
|
|
print(f"π Location: {im_file}") |
|
|
print(f"π Size: {os.path.getsize(im_file)} bytes") |
|
|
print() |
|
|
|
|
|
|
|
|
print("π¬ Processing IM_0001 file...") |
|
|
try: |
|
|
pixel_array, display_array, metadata = analyzer.process_file(im_file) |
|
|
|
|
|
print("β
Successfully processed IM_0001!") |
|
|
print(f" - Shape: {pixel_array.shape}") |
|
|
print(f" - Modality: {metadata.get('modality', 'Unknown')}") |
|
|
print(f" - File type: {metadata.get('file_type', 'Unknown')}") |
|
|
|
|
|
if metadata.get('file_type') == 'DICOM': |
|
|
print(f" - Patient: {metadata.get('patient_name', 'Anonymous')}") |
|
|
print(f" - Study date: {metadata.get('study_date', 'N/A')}") |
|
|
|
|
|
if 'window_center' in metadata: |
|
|
print(f" - Window Center: {metadata['window_center']:.0f}") |
|
|
print(f" - Window Width: {metadata['window_width']:.0f}") |
|
|
|
|
|
|
|
|
print("\nπ― Performing point analysis...") |
|
|
result = analyzer.analyze_image( |
|
|
image=pixel_array, |
|
|
modality=metadata.get('modality', 'CT'), |
|
|
task="analyze_point", |
|
|
roi={"x": pixel_array.shape[1]//2, "y": pixel_array.shape[0]//2, "radius": 10} |
|
|
) |
|
|
|
|
|
if 'point_analysis' in result: |
|
|
pa = result['point_analysis'] |
|
|
print(f" - HU Value: {pa.get('hu_value', 'N/A')}") |
|
|
tissue = pa.get('tissue_type', {}) |
|
|
print(f" - Tissue: {tissue.get('icon', '')} {tissue.get('type', 'Unknown')}") |
|
|
print(f" - Confidence: {pa.get('confidence', 'N/A')}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"β Error processing IM_0001: {str(e)}") |
|
|
import traceback |
|
|
traceback.print_exc() |
|
|
else: |
|
|
print("β οΈ No test DICOM file found to create IM_0001") |
|
|
print(" You can manually test by renaming any DICOM file to IM_0001") |
|
|
|
|
|
print("\n" + "=" * 50) |
|
|
print("π Implementation Details:") |
|
|
print("1. Backend uses: pydicom.dcmread(file_path, force=True)") |
|
|
print("2. This allows reading files without extensions") |
|
|
print("3. The force=True parameter tells pydicom to try reading as DICOM") |
|
|
print("4. If DICOM reading fails, it falls back to regular image processing") |
|
|
print("5. Frontend accepts all file types (no restrictions)") |
|
|
|
|
|
finally: |
|
|
|
|
|
shutil.rmtree(temp_dir) |
|
|
|
|
|
print("\nβ
The medical_image_analyzer fully supports IM_0001 files!") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
test_im_files() |