Save overlapping classes as one NIfTI per class

Description

The volume counterpart of save_class_masks(), and the one regions was motivated by: BraTS names three overlapping regions and stores them as volumes. A label-map NIfTI holds one integer per voxel, so save_volumes() refuses an overlapping prediction rather than reducing it, and this is what it refuses it in favour of - one binary volume per class, each carrying the geometry of the scan it was predicted from.

Usage

save_class_volumes(
  masks,
  dir,
  reference,
  classes,
  names = NULL,
  suffix = "",
  threshold = 0.5
)

Arguments

masks Channel probabilities - a list of depth x height x width x class arrays, which is what predict(type = “probability”, space = “source”) gives, or one stacked volume x depth x height x width x class array.
dir Directory to write into; created if it does not exist.
reference The scan each mask was predicted from, as paths. Every class of one scan is written with that scan’s geometry, which is the whole reason this is not save_class_masks() with a different extension.
classes One name per channel, in channel order, which is the order the regions were written in. inspect_masks() names them the same way.
names File name stems, one per volume. Defaults to the reference file names with their extensions removed - and names that collide are refused, because one directory per case usually means the same filename inside each.
suffix Added before the class, so predictions from different models can sit in one directory.
threshold A class is written where its channel reaches this. 0.5 is what every metric in the engine thresholds a channel at.

Details

Each file is <name><suffix>_<class>.nii.gz. The class name becomes part of a filename the same way save_class_masks() does, and two class names that become the same filename are refused rather than written.

Why one file per class rather than one file with several values: a voxel in the tumour core is also in the whole tumour, and no single integer says both. Any viewer can load three binary masks over one scan; none can unpack an overlap out of one label map.

Value

The paths written, invisibly, volume by volume and within a volume class by class.

See Also

save_class_masks() for 2D, save_volumes() for exclusive classes.

Examples

library("rplatypus")

channels <- predict(fit, "brats", split = "test", type = "probability", space = "source")
save_class_volumes(channels, "predictions",
                   reference = split_path(split, "test")$image,
                   classes = c("whole tumour", "tumour core", "enhancing"))