Vision plot
validate_anatomical_plane
def validate_anatomical_plane(
anatomical_plane
):
Ensure anatomical_plane is either 0, 1, or 2.
show_med_img
def show_med_img(
im, ctx, channel:int, slice_index:int, anatomical_plane:int, voxel_size:(<class 'int'>, <class 'list'>),
ax:NoneType=None, figsize:NoneType=None, title:NoneType=None, cmap:NoneType=None, norm:NoneType=None,
aspect:NoneType=None, interpolation:NoneType=None, alpha:NoneType=None, vmin:NoneType=None, vmax:NoneType=None,
colorizer:NoneType=None, origin:NoneType=None, extent:NoneType=None, interpolation_stage:NoneType=None,
filternorm:bool=True, filterrad:float=4.0, resample:NoneType=None, url:NoneType=None, data:NoneType=None,
kwargs:VAR_KEYWORD
):
Show an image on ax. This is a modified code from the fastai function show_image.
Args: im: The input image. ctx: The context. channel: Channel of the image. slice_index: Index of the 2D slice. anatomical_plane: Anatomical plane of the image. voxel_size: Voxel size for the image. ax: Axis for the plot. figsize: Figure size for the plot. title: Title for the plot. kwargs: Additional parameters for plt.Axes.imshow method.
Returns: Axis with the plot.
find_max_slice
def find_max_slice(
mask_data, anatomical_plane
):
Find slice index based on mask
show_segmentation_comparison
def show_segmentation_comparison(
image, ground_truth, prediction, slice_index:int=None, anatomical_plane:int=2, metric_value:float=None,
metric_name:str='DSC', channel:int=0, figsize:tuple=(15, 5), cmap_img:str='gray', cmap_mask:str='gray',
voxel_size:NoneType=None
):
Display 3-panel comparison: Image | Ground Truth | Prediction.
Useful for validating segmentation results, especially after patch-based inference where results are not in standard fastai batch format.
Args: image: Input image (MedImage, MedMask, or tensor [C, H, W, D]) ground_truth: Ground truth mask (MedMask or tensor [C, H, W, D]) prediction: Predicted mask (tensor [C, H, W, D]) slice_index: Slice to display. If None, uses find_max_slice on ground_truth. anatomical_plane: 0=sagittal, 1=coronal, 2=axial (default) metric_value: Optional metric value to display in prediction title metric_name: Name of metric for title (default ‘DSC’) channel: Channel to display for multi-channel data (default 0) figsize: Figure size (default (15, 5)) cmap_img: Colormap for image (default ‘gray’) cmap_mask: Colormap for masks (default ‘gray’) voxel_size: Voxel spacing for aspect ratio. If None, aspect=1.
Example::
# After patch_inference()
show_segmentation_comparison(
image=val_img,
ground_truth=val_gt,
prediction=predictions[0],
metric_value=results_df.iloc[0]['DSC'],
anatomical_plane=2
)
show_mask_overlay
def show_mask_overlay(
image, mask, ctx:NoneType=None, channel:int=0, slice_index:int=None, anatomical_plane:int=2,
voxel_size:NoneType=None, alpha:float=0.5, cmap_img:str='gray', cmap_mask:str='autumn', ax:NoneType=None,
figsize:NoneType=None, title:NoneType=None, kwargs:VAR_KEYWORD
):
Overlay mask on image on a single axis, using the same slicing and orientation as .show().
Draws the image, then the mask on top with zero voxels transparent, so it lines up with MedImage.show() / MedMask.show(). Reuses the same _get_slice those methods use.
Args: image: Background image (MedImage or object with .data [C, H, W, D]). mask: Overlay mask (MedMask or object with .data [C, H, W, D]); zero voxels are transparent. ctx: Axis to draw on (fastai-style context); same role as ax. channel: Channel to display. slice_index: Slice to show; if None, uses find_max_slice(mask, anatomical_plane). anatomical_plane: 0=sagittal, 1=coronal, 2=axial (default). voxel_size: Voxel spacing for aspect ratio; if None, aspect defaults to 1. alpha: Overlay opacity. cmap_img: Colormap for the background image. cmap_mask: Colormap for the mask overlay. ax: Axis to draw on (overrides ctx). figsize: Figure size when a new axis is created. title: Optional axis title. kwargs: Extra args forwarded to the background image imshow.
Returns: The matplotlib axis with the overlay.