a b/ants/segmentation/kelly_kapowski.py
1
"""
2
Kelly Kapowski algorithm with computing cortical thickness
3
"""
4
5
__all__ = ['kelly_kapowski']
6
7
import ants
8
from ants.internal import get_lib_fn, get_pointer_string, process_arguments
9
10
11
def kelly_kapowski(s, g, w, its=45, r=0.025, m=1.5, gm_label=2, wm_label=3, **kwargs):
12
    """
13
    Compute cortical thickness using the DiReCT algorithm.
14
15
    Diffeomorphic registration-based cortical thickness based on probabilistic
16
    segmentation of an image.  This is an optimization algorithm.
17
18
19
    Arguments
20
    ---------
21
    s : ANTsimage
22
        segmentation image
23
24
    g : ANTsImage
25
        gray matter probability image
26
27
    w : ANTsImage
28
        white matter probability image
29
30
    its : integer
31
        convergence params - controls iterations
32
33
    r : scalar
34
        gradient descent update parameter
35
36
    m : scalar
37
        gradient field smoothing parameter
38
39
    gm_label : integer
40
        label for gray matter in the segmentation image
41
42
    wm_label : integer
43
        label for white matter in the segmentation image
44
45
    kwargs : keyword arguments
46
        anything else, see KellyKapowski help in ANTs
47
48
    Returns
49
    -------
50
    ANTsImage
51
52
    Example
53
    -------
54
    >>> import ants
55
    >>> img = ants.image_read( ants.get_ants_data('r16') ,2)
56
    >>> img = ants.resample_image(img, (64,64),1,0)
57
    >>> mask = ants.get_mask( img )
58
    >>> segs = ants.kmeans_segmentation( img, k=3, kmask = mask)
59
    >>> thick = ants.kelly_kapowski(s=segs['segmentation'], g=segs['probabilityimages'][1],
60
                                    w=segs['probabilityimages'][2], its=45,
61
                                    r=0.5, m=1)
62
    """
63
    if ants.is_image(s):
64
        s = s.clone('unsigned int')
65
66
    d = s.dimension
67
    outimg = g.clone() * 0.0
68
    kellargs = {'d': d,
69
                's': "[{},{},{}]".format(get_pointer_string(s),gm_label,wm_label),
70
                'g': g,
71
                'w': w,
72
                'c': "[{}]".format(its),
73
                'r': r,
74
                'm': m,
75
                'o': outimg}
76
    for k, v in kwargs.items():
77
        kellargs[k] = v
78
79
    processed_kellargs = process_arguments(kellargs)
80
81
    libfn = get_lib_fn('KellyKapowski')
82
    libfn(processed_kellargs)
83
84
    # Check thickness is not still all zeros
85
    if outimg.sum() == 0.0:
86
        raise RuntimeError("KellyKapowski failed to compute thickness")
87
88
    return outimg
89
90
91