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