|
| 1 | +from pathlib import Path |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import nibabel as nib |
| 4 | + |
| 5 | +DATA_FOLDER = "data" |
| 6 | + |
| 7 | + |
| 8 | +def visualize_data( |
| 9 | + data_folder: str = DATA_FOLDER, |
| 10 | + subject_id: str = "BraTS-GLI-00001-000", |
| 11 | + slice_index: int = 75, |
| 12 | +): |
| 13 | + """Visualize the MRI modalities for a given slice index |
| 14 | +
|
| 15 | + Args: |
| 16 | + data_folder (str, optional): Path to the folder containing the t1, t1c, t2 & flair file. Defaults to DATA_FOLDER. |
| 17 | + slice_index (int, optional): Slice to be visualized (first index in data of shape (155, 240, 240)). Defaults to 75. |
| 18 | + """ |
| 19 | + _, axes = plt.subplots(1, 4, figsize=(12, 10)) |
| 20 | + |
| 21 | + subject_path = Path(data_folder) / subject_id |
| 22 | + modalities = ["t1n", "t1c", "t2f", "t2w"] |
| 23 | + for i, mod in enumerate(modalities): |
| 24 | + modality_file = subject_path / f"{subject_id}-{mod}.nii.gz" |
| 25 | + modality_np = nib.load(modality_file).get_fdata().transpose(2, 1, 0) |
| 26 | + axes[i].set_title(mod) |
| 27 | + axes[i].imshow(modality_np[slice_index, :, :], cmap="gray") |
| 28 | + axes[i].axis("off") |
| 29 | + |
| 30 | + |
| 31 | +def visualize_segmentation(modality_file: str, segmentation_file: str): |
| 32 | + """Visualize the MRI modality and the segmentation |
| 33 | +
|
| 34 | + Args: |
| 35 | + modality_file (str): Path to the desired modality file |
| 36 | + segmentation_file (str): Path to the segmentation file |
| 37 | + """ |
| 38 | + modality_np = nib.load(modality_file).get_fdata().transpose(2, 1, 0) |
| 39 | + seg_np = nib.load(segmentation_file).get_fdata().transpose(2, 1, 0) |
| 40 | + _, ax = plt.subplots(1, 2, figsize=(8, 4)) |
| 41 | + |
| 42 | + slice_index = modality_np.shape[0] // 2 # You can choose any slice here |
| 43 | + ax[0].imshow(modality_np[slice_index, :, :], cmap="gray") |
| 44 | + ax[1].imshow(modality_np[slice_index, :, :], cmap="gray") |
| 45 | + ax[1].imshow(seg_np[slice_index, :, :], cmap="plasma", alpha=0.3) |
| 46 | + for ax in ax: |
| 47 | + ax.axis("off") |
| 48 | + plt.tight_layout() |
0 commit comments