Skip to content

Commit 270487d

Browse files
committed
add vis utils
1 parent f0e9d77 commit 270487d

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

BraTS/utils.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import matplotlib.pyplot as plt
3+
import nibabel as nib
4+
5+
DATA_FOLDER = "data"
6+
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)
34+
seg_np = nib.load(segmentation_file).get_fdata().transpose(2, 1, 0)
35+
_, ax = plt.subplots(1, 2, figsize=(8, 4))
36+
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")
40+
ax[1].imshow(seg_np[slice_index, :, :], cmap="plasma", alpha=0.3)
41+
for ax in ax:
42+
ax.axis("off")
43+
plt.tight_layout()

0 commit comments

Comments
 (0)