33import nibabel as nib
44from pathlib import Path
55
6-
7- def visualize_data2 (t1c , t1 , t2 , flair , height_p = 0.5 ): #
6+
7+ def visualize_data2 (t1c , t1 , t2 , flair , height_p = 0.5 ): #
88 """Visualize the MRI modalities. This function is different from "visualize_data" in the following ways:
9- a) it expects the four file paths directly (no naming convention required)
10- b) it does not take a slice index but a percentage height (e.g. 0.5 = 50% = middle slice) as imputs may have different dimensions
9+ a) it expects the four file paths directly (no naming convention required)
10+ b) it does not take a slice index but a percentage height (e.g. 0.5 = 50% = middle slice) as imputs may have different dimensions
1111
1212 Args:
13- height_p (float, optional): the percentage of slicing height. 0.5 means 50% means exactly in the middle of the cuboid
13+ height_p (float, optional): the percentage of slicing height. 0.5 means 50% means exactly in the middle of the cuboid
1414 slice_index (int, optional): Slice to be visualized (first index in data of shape (155, 240, 240)). Defaults to 75.
1515 """
1616 _ , axes = plt .subplots (1 , 4 , figsize = (12 , 10 ))
1717
18- for i , (mod , modality_file ) in enumerate ([("t1" ,t1 ), ("t1c" ,t1c ), ("t2" ,t2 ), ("flair" ,flair )]):
19-
18+ for i , (mod , modality_file ) in enumerate (
19+ [("t1" , t1 ), ("t1c" , t1c ), ("t2" , t2 ), ("flair" , flair )]
20+ ):
21+
2022 data = nib .load (modality_file ).get_fdata ()
21- # Get the middle slice along the specified axis
22- slice_index = int (data .shape [2 ] * height_p ) # show slice that is exactly in the middle
23+ # Get the middle slice along the specified axis
24+ slice_index = int (
25+ data .shape [2 ] * height_p
26+ ) # show slice that is exactly in the middle
2327 slice_data = data [:, ::- 1 , slice_index ].T
24-
28+
2529 axes [i ].set_title (mod )
2630 axes [i ].imshow (slice_data , cmap = "gray" )
2731 axes [i ].axis ("off" )
2832
33+
2934DATA_FOLDER = "data"
3035
36+
3137def visualize_data (data_folder : str = DATA_FOLDER , slice_index : int = 75 ):
3238 """Visualize the MRI modalities for a given slice index
3339
@@ -45,7 +51,8 @@ def visualize_data(data_folder: str = DATA_FOLDER, slice_index: int = 75):
4551 axes [i ].imshow (modality_np [slice_index , :, :], cmap = "gray" )
4652 axes [i ].axis ("off" )
4753
48- def visualize_segmentation (modality_file : str , segmentation_file : str , slice_p = 0.5 ):
54+
55+ def visualize_segmentation (modality_file : str , segmentation_file : str , slice_p = 0.5 ):
4956 """Visualize the MRI modality and the segmentation
5057
5158 Args:
@@ -63,5 +70,3 @@ def visualize_segmentation(modality_file: str, segmentation_file: str, slice_p =
6370 for ax in ax :
6471 ax .axis ("off" )
6572 plt .tight_layout ()
66-
67-
0 commit comments