File size: 3,597 Bytes
1ef3bf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Utility functions for neuroimaging preprocessing and analysis
"""
import numpy as np
from PIL import Image

def normalize_medical_image(image_array: np.ndarray) -> np.ndarray:
    """
    Normalize medical image intensities to 0-255 range
    Handles various bit depths common in medical imaging
    """
    img = image_array.astype(np.float32)
    
    # Handle different intensity ranges
    if img.max() > 255:
        # Likely 12-bit or 16-bit image
        p1, p99 = np.percentile(img, [1, 99])
        img = np.clip(img, p1, p99)
    
    # Normalize to 0-255
    img_min, img_max = img.min(), img.max()
    if img_max > img_min:
        img = (img - img_min) / (img_max - img_min) * 255
    
    return img.astype(np.uint8)

def apply_window_level(image_array: np.ndarray, window: float, level: float) -> np.ndarray:
    """
    Apply window/level (contrast/brightness) adjustment
    Common in CT viewing
    
    Args:
        image_array: Input image
        window: Window width (contrast)
        level: Window center (brightness)
    """
    img = image_array.astype(np.float32)
    
    min_val = level - window / 2
    max_val = level + window / 2
    
    img = np.clip(img, min_val, max_val)
    img = (img - min_val) / (max_val - min_val) * 255
    
    return img.astype(np.uint8)

def enhance_brain_contrast(image: Image.Image) -> Image.Image:
    """
    Enhance contrast specifically for brain MRI visualization
    """
    img_array = np.array(image)
    
    # Convert to grayscale if needed
    if len(img_array.shape) == 3:
        gray = np.mean(img_array, axis=2)
    else:
        gray = img_array
    
    # Apply histogram equalization
    from PIL import ImageOps
    enhanced = ImageOps.equalize(Image.fromarray(gray.astype(np.uint8)))
    
    # Convert back to RGB
    enhanced_array = np.array(enhanced)
    rgb_array = np.stack([enhanced_array] * 3, axis=-1)
    
    return Image.fromarray(rgb_array)

# Common neuroimaging structure mappings
STRUCTURE_ALIASES = {
    "hippocampus": ["hippocampal formation", "hippocampal", "medial temporal"],
    "ventricle": ["ventricular system", "lateral ventricle", "CSF space"],
    "white matter": ["WM", "cerebral white matter", "deep white matter"],
    "gray matter": ["GM", "cortical gray matter", "cortex"],
    "tumor": ["mass", "lesion", "neoplasm", "growth"],
    "thalamus": ["thalamic", "diencephalon"],
    "basal ganglia": ["striatum", "caudate", "putamen", "globus pallidus"],
}

def get_structure_aliases(structure: str) -> list:
    """Get alternative names for a neuroanatomical structure"""
    structure_lower = structure.lower()
    
    for key, aliases in STRUCTURE_ALIASES.items():
        if structure_lower == key or structure_lower in aliases:
            return [key] + aliases
    
    return [structure]

# Hugging Face datasets for neuroimaging
HF_NEUROIMAGING_DATASETS = {
    "brain-tumor-classification": {
        "repo": "sartajbhuvaji/brain-tumor-classification",
        "description": "Brain MRI scans classified by tumor type (glioma, meningioma, pituitary, no tumor)",
        "image_key": "image",
        "label_key": "label"
    },
    "brain-tumor-detection": {
        "repo": "keremberke/brain-tumor-object-detection",
        "description": "Brain MRI with bounding box annotations for tumors",
        "image_key": "image",
        "label_key": "objects"
    },
    "chest-xray": {
        "repo": "alkzar90/NIH-Chest-X-ray-dataset",
        "description": "Chest X-ray images with disease labels",
        "image_key": "image",
        "label_key": "labels"
    }
}