Spaces:
Sleeping
Sleeping
File size: 1,281 Bytes
f60b8aa | 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 | import numpy as np
import nibabel as nib
from pathlib import Path
def preprocess_input(file_path, target_shape=(128, 128, 128)):
"""Load and preprocess a NIfTI file"""
img = nib.load(str(file_path)).get_fdata().astype(np.float32)
if img.shape != target_shape:
from scipy.ndimage import zoom
factors = [t/s for t, s in zip(target_shape, img.shape)]
img = zoom(img, factors, order=1)
img = (img - img.min()) / (img.max() - img.min() + 1e-8)
return img
def load_multimodal_scan(t1_path, t1ce_path, t2_path, flair_path):
"""Load all 4 MRI modalities and stack them"""
channels = []
for path in [t1_path, t1ce_path, t2_path, flair_path]:
if path and Path(path).exists():
img = preprocess_input(path)
else:
img = np.zeros((128, 128, 128), dtype=np.float32)
channels.append(img)
return np.stack(channels, axis=0)
def calculate_tumor_metrics(pred_mask):
"""Calculate tumor volume metrics"""
return {
'total_voxels': np.sum(pred_mask > 0),
'necrotic_voxels': np.sum(pred_mask == 1),
'edema_voxels': np.sum(pred_mask == 2),
'enhancing_voxels': np.sum(pred_mask == 3),
'volume_ml': np.sum(pred_mask > 0) * 0.001
}
|