Visualization

InnerEye.ML.plotting.add_legend(series_count: int) None[source]

Adds a legend to the present plot, with the column layout depending on the number of series.

Parameters:

series_count

Returns:

InnerEye.ML.plotting.get_val_dice_names(metric_names: Iterable[str]) List[str][source]

Returns a list of those metric names from the argument that fulfill the is_val_dice predicate.

Parameters:

metric_names

Returns:

InnerEye.ML.plotting.is_val_dice(name: str) bool[source]

Returns true if the given metric name is a Dice score on the validation set, for a class that is not the background class.

Parameters:

name

Returns:

InnerEye.ML.plotting.plot_before_after_statistics(image_before: ndarray, image_after: ndarray, mask: Optional[ndarray], z_slice: int, normalizer_status_message: Optional[str], plot_file_name: Path) Path[source]

Creates a plot in a PNG file that describes the pixel value distribution of two 3D images in Z x Y x X order, that were obtained before and after a transformation of pixel values. The plot contains histograms, box plots, and visualizations of a single XY slice at z_slice. If a mask argument is provided, only the image pixel values inside of the mask will be plotted.

Parameters:
  • image_before – The first image for which to plot statistics.

  • image_after – The second image for which to plot statistics.

  • mask – Indicators with 1 for foreground, 0 for background. If None, plot statistics for all image pixels.

  • z_slice – The z position for visualizing a single example slice.

  • normalizer_status_message – A string with diagnostic information that will be set as the plot title.

  • plot_file_name – The file name under which to save the plot. A PNG extension will be added.

Returns:

The path of the created PNG file.

InnerEye.ML.plotting.plot_contours_for_all_classes(sample: Sample, segmentation: ndarray, foreground_class_names: List[str], result_folder: Path, result_prefix: str = '', image_range: Optional[Tuple[float, float]] = None, channel_index: int = 0) List[Path][source]

Creates a plot with the image, the ground truth, and the predicted segmentation overlaid. One plot is created for each class, each plotting the Z slice where the ground truth has most pixels.

Parameters:
  • sample – The image sample, with the photonormalized image and the ground truth labels.

  • segmentation – The predicted segmentation: multi-value, size Z x Y x X.

  • foreground_class_names – The names of all classes, excluding the background class.

  • result_folder – The folder into which the resulting plot PNG files should be written.

  • result_prefix – A string prefix that will be used for all plots.

  • image_range – The minimum and maximum image values that will be mapped to the color map ranges. If None, use the actual min and max values.

  • channel_index – The index of the image channel that should be plotted.

Returns:

The paths to all generated PNG files.

InnerEye.ML.plotting.plot_image_and_label_contour(image: ndarray, labels: Union[ndarray, List[ndarray]], plot_file_name: Path, contour_arguments: Optional[Union[Dict, List[Dict]]] = None, image_range: Optional[Tuple[float, float]] = None) Path[source]

Creates a plot that shows the given 2D image in greyscale, and overlays a contour that shows where the ‘labels’ array has value 1.

Parameters:
  • image – A 2D image

  • labels – A binary 2D image, or a list of binary 2D images. A contour will be plotted for each of those binary images.

  • contour_arguments – A dictionary of keyword arguments that will be passed directly into matplotlib’s contour function. Can also be a list of dictionaries, with one dict per entry in the ‘labels’ argument.

  • image_range – If provided, the image will be plotted using the given range for the color limits. If None, the minimum and maximum image values will be mapped to the endpoints of the color map.

  • plot_file_name – The file name that should be used to save the plot.

InnerEye.ML.plotting.plot_loss_per_epoch(metrics: Dict[str, Any], metric_name: str, label: Optional[str] = None) int[source]

Adds a plot of loss (y-axis) versus epoch (x-axis) to the current plot, if the metric is present in the metrics dictionary.

Parameters:
  • metrics – A dictionary of metrics.

  • metric_name – The name of the single metric to plot.

  • label – The label for the series that will be plotted.

Returns:

1 if the metric is present in the dictionary and was plotted, 0 otherwise.

InnerEye.ML.plotting.plot_normalization_result(loaded_images: Sample, normalizer: PhotometricNormalization, result_folder: Path, result_prefix: str = '', image_range: Optional[Tuple[float, float]] = None, channel_index: int = 0, class_index: int = 1, contour_file_suffix: str = '') List[Path][source]

Creates two PNG plots that summarize the result of photometric normalization of the first channel in the sample image. The first plot contains pixel value histograms before and after photometric normalization. The second plot contains the normalized image, overlayed with contours for the foreground pixels, at the slice where the foreground has most pixels.

Parameters:
  • loaded_images – An instance of Sample with the image and the labels. The first channel of the image will be plotted.

  • image_range – The image value range that will be mapped to the color map. If None, the full image range will be mapped to the colormap.

  • normalizer – The photometric normalization that should be applied.

  • result_folder – The folder into which the resulting PNG files should be written.

  • result_prefix – The prefix for all output filenames.

  • channel_index – Compute normalization results for this channel.

  • class_index – When plotting image/contour overlays, use this class.

  • contour_file_suffix – Use this suffix for the file name that contains the image/contour overlay.

Returns:

The paths of the two PNG files that the function writes.

InnerEye.ML.plotting.plot_val_dice_per_epoch(metrics: Dict[str, Any]) int[source]

Creates a plot of all validation Dice scores per epoch, for all classes apart from background.

Parameters:

metrics

Returns:

The number of series that were plotted in the graph. Can return 0 if the metrics dictionary does not contain any validation Dice score.

InnerEye.ML.plotting.resize_and_save(width_inch: int, height_inch: int, filename: Union[Path, str], dpi: int = 150) None[source]

Resizes the present figure to the given (width, height) in inches, and saves it to the given filename.

Parameters:
  • width_inch – The width of the figure in inches.

  • height_inch – The height of the figure in inches.

  • filename – The filename to save to.

  • dpi – Image resolution in dots per inch

InnerEye.ML.plotting.scan_with_transparent_overlay(scan: ndarray, overlay: ndarray, dimension: int, position: int, spacing: Tuple[float, float, float]) None[source]

Creates a plot with one slice of a (CT) scan, with a transparent overlay that contains a second piece of information in the range [0, 1]. High values of the overlay are shown as opaque red, low values as transparent red. Plots are created in the current axis.

Parameters:
  • scan – A 3-dimensional image in (Z, Y, X) ordering

  • overlay – A 3-dimensional image in (Z, Y, X) ordering, with values between 0 and 1.

  • dimension – The array dimension along with the plot should be created. dimension=0 will generate an axial slice.

  • position – The index in the chosen dimension where the plot should be created.

  • spacing – The tuple of voxel spacings, in (Z, Y, X) order.

InnerEye.ML.plotting.segmentation_and_groundtruth_plot(prediction: ndarray, ground_truth: ndarray, subject_id: int, structure_name: str, plane: Plane, output_img_dir: Path, annotator: Optional[str] = None, save_fig: bool = True) None[source]

Plot predicted and the ground truth segmentations. Always plots the middle slice (to match surface distance plots), which can sometimes lead to an empty plot.

Parameters:
  • prediction – 3D volume (X x Y x Z) of predicted segmentation

  • ground_truth – 3D volume (X x Y x Z) of ground truth segmentation

  • subject_id – ID of subject for annotating plot

  • structure_name – Name of structure for annotating plot

  • plane – The plane to view images in (axial, sagittal or coronal)

  • output_img_dir – The dir in which to store the plots

  • annotator – Optional annotator name for annotating plot

  • save_fig – If True, saves image. Otherwise displays it.

Returns:

InnerEye.ML.plotting.surface_distance_ground_truth_plot(ct: ndarray, ground_truth: ndarray, sds_full: ndarray, subject_id: int, structure: str, plane: Plane, output_img_dir: Path, dice: Optional[float] = None, save_fig: bool = True, annotator: Optional[str] = None) None[source]

Plot surface distances where prediction > 0, with ground truth contour

Parameters:
  • ct – CT scan

  • ground_truth – Ground truth segmentation

  • sds_full – Surface distances (full= where prediction > 0)

  • subject_id – ID of subject for annotating plot

  • structure – Name of structure for annotating plot

  • plane – The plane to view images in (axial, sagittal or coronal)

  • output_img_dir – The dir in which to store the plots

  • dice – Optional dice score for annotating plot

  • save_fig – If True, saves image. Otherwise displays it.

  • annotator – Optional annotator name for annotating plot

Returns:

class InnerEye.ML.normalize_and_visualize_dataset.NormalizeAndVisualizeConfig(should_validate: bool = True, throw_if_unknown_param: bool = False, **params: Any)[source]
gt_channel: Optional[str] = None
ignore_mask: bool = False
image_channel: Optional[str] = None
name = 'NormalizeAndVisualizeConfig'
only_first: int = 0
param = <param.parameterized.Parameters object>
result_folder: str = 'NormResults'
InnerEye.ML.normalize_and_visualize_dataset.create_parser(yaml_file_path: Path) ParserResult[source]

Create a parser for all runner arguments, even though we are only using a subset of the arguments. This way, we can get secrets handling in a consistent way. In particular, this will create arguments for

  • --local_dataset

  • --azure_dataset_id

InnerEye.ML.normalize_and_visualize_dataset.get_configs(default_model_config: SegmentationModelBase, yaml_file_path: Path) Tuple[SegmentationModelBase, AzureConfig, Dict][source]
InnerEye.ML.normalize_and_visualize_dataset.main(yaml_file_path: Path) None[source]

Invoke either by

  • specifying a model, --model Lung

  • or specifying dataset and normalization parameters separately: --azure_dataset_id=foo --norm_method=None

In addition, the arguments --image_channel and --gt_channel must be specified.

class InnerEye.ML.surface_distance_heatmaps.Segmentation(segmentation_path: Path, structure_name: str, subject_id: int, annotator: Optional[str] = None, dice_score: Optional[float] = None)[source]

Each individual structure segmentation (whether model prediction or human annotation) will have the properties structure_name (i.e. body part), subject_id and a unique path. Optionally, it may also have an associated annotator name and calculated Dice score, compared to ground truth.

annotator: Optional[str] = None
dice_score: Optional[float] = None
segmentation_path: Path
structure_name: str
subject_id: int
InnerEye.ML.surface_distance_heatmaps.load_predictions(run_type: SurfaceDistanceRunType, azure_config: AzureConfig, model_config: SegmentationModelBase, execution_mode: ModelExecutionMode, extended_annotators: List[str], outlier_range: float) List[Segmentation][source]

For each run type (IOV or outliers), instantiate a list of predicted Segmentations and return

Parameters:
  • run_type – either “iov” or “outliers:

  • azure_config – AzureConfig

  • model_config – GenericConfig

  • execution_mode – ModelExecutionMode: Either Test, Train or Val

  • extended_annotators – List of annotators plus model_name to load segmentations for

  • outlier_range – The standard deviation from the mean which the points have to be below to be considered an outlier.

Returns:

list of [(subject_id, structure name and dice_scores)]

InnerEye.ML.surface_distance_heatmaps.main() None[source]