File size: 3,719 Bytes
25a57da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Port of the upstream reference implementation's default (non-pve)
post-segmentation chain for label-output models like mindmap (the reference
implementation's main inference routine, label-model branch): largest-
component cleanup and reslice back to the input's native grid, producing an
18-class label map -- mindmap's output *is* the label volume itself.
"""

from __future__ import annotations

import numpy as np
from scipy.ndimage import label

from .conform import reslice_to_grid

__all__ = ["mindmap_postprocess"]


def _largest_component_keep_labels(labels: np.ndarray) -> np.ndarray:
    """bwlabel.c `bwlabel_largest`, `binary_output=False` (mindmap is a
    labels model, BC_MINDMAP_OUTPUT_IS_MASK=0): a single 26-connected flood
    fill over *every* nonzero voxel regardless of its class value (i.e.
    connectivity does not distinguish between different label values), keep
    only the single largest such component, and zero every voxel outside it
    -- but voxels *inside* the retained component keep their original class
    label rather than being binarized.
    """
    structure = np.ones((3, 3, 3), dtype=np.int8)
    connected, num_components = label(labels > 0, structure=structure)
    if num_components == 0:
        return np.zeros_like(labels, dtype=np.float32)
    counts = np.bincount(connected.ravel())
    counts[0] = 0
    best_component = int(np.argmax(counts))
    out = labels.astype(np.float32).copy()
    out[connected != best_component] = 0.0
    return out


def mindmap_postprocess(
    class_logits: np.ndarray,
    conform_affine: np.ndarray,
    orig_shape: tuple[int, int, int],
    orig_affine: np.ndarray,
    save_conform: bool = False,
) -> np.ndarray:
    """
    Args:
        class_logits: network output, shape (18, 256, 256, 256) in conform
            space (channel 0 = background/"Unknown").
        conform_affine: the 4x4 affine returned by `mindmap_preprocess`.
        orig_shape: the original input volume's spatial shape (nx, ny, nz).
        orig_affine: the original input volume's 4x4 voxel-to-world affine.
        save_conform: if True, skip the reslice back to the input's native
            grid and return the label map in 256^3 conform space instead
            (the reference implementation's `--save-conform` option;
            mindmap's model_meta.json sets `save_conform: true` in its
            capabilities).

    Returns:
        float32 array of class labels (0-17): shaped like `orig_shape` by
        default, or (256, 256, 256) if `save_conform` is True.
    """
    # meshnet_cpu.c `mn_classify`: argmax over classes, ties keep the lower
    # (background) index -- numpy's argmax already returns the first max.
    pred = np.argmax(class_logits, axis=0).astype(np.float32)
    pred = _largest_component_keep_labels(pred)

    if save_conform:
        return pred

    # Reslicing back to native space uses nearest-neighbor (linear=0);
    # source min (0.0, background) is the out-of-FOV fill, matching
    # `do_reslice`'s prefill.
    return reslice_to_grid(pred, conform_affine, np.asarray(orig_affine, dtype=np.float64), orig_shape, linear=False)