""" 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" } }