save_volumes
save_volumes(prediction, directory, *, reference, cases=None, suffix='')A predicted volume as NIfTI, written onto the geometry of the scan it came from.
This is what save_class_masks’s own refusal tells a reader to do, and until now there was nowhere in this package to do it: a PNG carries neither voxel spacing nor orientation, so a volume written as pictures cannot be laid back over its scan. A mask array on its own has the same problem. The affine is the point - every viewer, every registration tool and every volume in millilitres reads position and spacing out of it, and a file carrying the wrong one is either silently misplaced or rejected.
Written as integer labels, int16, because that is what a volume format holds and what the next tool expects to read back. reference is required rather than optional for the same reason: there is no sensible affine to invent.
Refuses an overlapping prediction instead of taking the argmax, exactly as onehot_to_colours does and for the same reason one level along: a label map holds one integer per voxel, so a voxel in both the tumour core and the whole tumour would be written as whichever channel read higher. Those are regions, and save_class_volumes is where they go. Detected from the array rather than from a flag, so a caller holding only a prediction can answer it, and a softmax never trips it.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| prediction | np.ndarray | Sequence[np.ndarray] | Channels-last probabilities. A list of (d, h, w, c) arrays, which is what predict(space="source") returns, or one such array, or a stacked (n, d, h, w, c). |
required |
| directory | str | Path | Where to write. Created, with parents, if it does not exist. | required |
| reference | Sequence[str | Path] | The scan each mask was predicted from, in the same order. Its canonical affine is what the mask is written with. | required |
| cases | Sequence[str] | None | One name per volume, without extension - usually split’s keys. Defaults to mask_0001 and so on. Names that repeat are refused rather than overwritten. |
None |
| suffix | str | Added before the extension, so two models can write into one directory. | '' |
Returns
| Name | Type | Description |
|---|---|---|
| list[Path] | The paths written, in the order the predictions were given. |