Skip to content

Commit e14dbaa

Browse files
committed
Merge branch 'multichannel-segmentation' into main-master
* multichannel-segmentation: (35 commits) MISC: update generated .c files from .pyx files fixed type error related to numpy.exp fixed 64-bit bug in histogram cython function add optional user-specified initialization for brain seg class fixed 64-bit bug + add related tests minor clean up update reference parameters for segmentation initialization fix numerical issue slight refactoring of the brain segmentation class and further tests writting in current as opposed to temporary directory in example segmentation script add short comment enable ngb_size input argument in BrainSegmentation class adding a histogram test add segmentation tests + some code cleaning remove nibabel dependency in tissue classification example generalize brain segmentation model fixed imports fixed tests adding a fast histogram routine in algorithms.statistics fix memory leak ...
2 parents b6ce98b + 192a085 commit e14dbaa

15 files changed

+8303
-2000
lines changed

examples/tissue_classification.py

Lines changed: 31 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,25 @@
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
44
""" Script example of tissue classification
55
"""
6-
76
import numpy as np
87

9-
from nipy.externals.argparse import ArgumentParser
108
from nipy import load_image, save_image
11-
from nipy.algorithms.segmentation import brain_segmentation
12-
13-
NITERS = 10
14-
BETA = 0.2
15-
SCHEME = 'mf'
16-
NOISE = 'gauss'
17-
FREEZE_PROP = True
18-
LABELS = ('CSF', 'GM', 'WM')
19-
K_CSF = 1
20-
K_GM = 1
21-
K_WM = 1
9+
from nipy.core.image.image_spaces import (make_xyz_image,
10+
xyz_affine)
11+
from nipy.externals.argparse import ArgumentParser
12+
from nipy.algorithms.segmentation import BrainT1Segmentation
2213

2314

24-
def fuzzy_dice(gpm, ppm, mask):
15+
def fuzzy_dice(gold_ppm, ppm, mask):
2516
"""
2617
Fuzzy dice index.
2718
"""
2819
dices = np.zeros(3)
29-
if gpm == None:
20+
if gold_ppm == None:
3021
return dices
3122
for k in range(3):
32-
pk = gpm[k].get_data()[mask]
33-
qk = ppm.get_data()[mask][:, k]
23+
pk = gold_ppm[mask][:, k]
24+
qk = ppm[mask][:, k]
3425
PQ = np.sum(np.sqrt(np.maximum(pk * qk, 0)))
3526
P = np.sum(pk)
3627
Q = np.sum(qk)
@@ -46,22 +37,12 @@ def fuzzy_dice(gpm, ppm, mask):
4637
parser = ArgumentParser(description=description)
4738
parser.add_argument('img', metavar='img', nargs='+', help='input image')
4839
parser.add_argument('--mask', dest='mask', help='mask image')
49-
parser.add_argument('--k_csf', dest='k_csf',
50-
help='number of CSF classes (default=%d)' % K_CSF)
51-
parser.add_argument('--k_gm', dest='k_gm',
52-
help='number of GM classes (default=%d)' % K_GM)
53-
parser.add_argument('--k_wm', dest='k_wm',
54-
help='number of WM classes (default=%d)' % K_WM)
5540
parser.add_argument('--niters', dest='niters',
56-
help='number of iterations (default=%d)' % NITERS)
41+
help='number of iterations (default=%d)' % 25)
5742
parser.add_argument('--beta', dest='beta',
58-
help='Markov random field beta parameter (default=%f)' % BETA)
59-
parser.add_argument('--scheme', dest='scheme',
60-
help='message passing scheme (mf, icm or bp, default=%s)' % SCHEME)
61-
parser.add_argument('--noise', dest='noise',
62-
help='noise model (gauss or laplace, default=%s)' % NOISE)
63-
parser.add_argument('--freeze_prop', dest='freeze_prop',
64-
help='freeze tissue proportions (default=1)')
43+
help='Markov random field beta parameter (default=%f)' % 0.5)
44+
parser.add_argument('--ngb_size', dest='ngb_size',
45+
help='Markov random field neighborhood system (default=%d)' % 6)
6546
parser.add_argument('--probc', dest='probc', help='csf probability map')
6647
parser.add_argument('--probg', dest='probg',
6748
help='gray matter probability map')
@@ -81,52 +62,37 @@ def get_argument(dest, default):
8162
img = load_image(args.img[0])
8263

8364
# Input mask image
84-
mask = get_argument('mask', None)
85-
if mask == None:
65+
mask_img = get_argument('mask', None)
66+
if mask_img == None:
8667
mask_img = img
8768
else:
88-
mask_img = load_image(mask)
69+
mask_img = load_image(mask_img)
8970

9071
# Other optional arguments
91-
k_csf = int(get_argument('k_csf', K_CSF))
92-
k_gm = int(get_argument('k_gm', K_GM))
93-
k_wm = int(get_argument('k_wm', K_WM))
94-
niters = int(get_argument('niters', NITERS))
95-
beta = float(get_argument('beta', BETA))
96-
scheme = get_argument('scheme', SCHEME)
97-
noise = get_argument('noise', NOISE)
98-
freeze_prop = get_argument('freeze_prop', FREEZE_PROP)
99-
100-
101-
# Labels
102-
labels = []
103-
labels += ['CSF' for k in range(k_csf)]
104-
labels += ['GM' for k in range(k_gm)]
105-
labels += ['WM' for k in range(k_wm)]
106-
mixmat = []
107-
mixmat += [[1, 0, 0] for k in range(k_csf)]
108-
mixmat += [[0, 1, 0] for k in range(k_gm)]
109-
mixmat += [[0, 0, 1] for k in range(k_wm)]
110-
mixmat = np.array(mixmat)
72+
niters = int(get_argument('niters', 25))
73+
beta = float(get_argument('beta', 0.5))
74+
ngb_size = int(get_argument('ngb_size', 6))
11175

11276
# Perform tissue classification
113-
ppm_img, label_img = brain_segmentation(img,
114-
mask_img=mask_img,
115-
beta=beta, niters=niters,
116-
labels=labels, mixmat=mixmat,
117-
noise=noise, freeze_prop=freeze_prop,
118-
scheme=SCHEME)
77+
mask = mask_img.get_data() > 0
78+
S = BrainT1Segmentation(img.get_data(), mask=mask, model='5k',
79+
niters=niters, beta=beta, ngb_size=ngb_size)
80+
81+
# Save label image
11982
outfile = 'hard_classif.nii'
120-
save_image(label_img, outfile)
83+
save_image(make_xyz_image(S.label, xyz_affine(img), 'scanner'),
84+
outfile)
12185
print('Label image saved in: %s' % outfile)
12286

12387
# Compute fuzzy Dice indices if a 3-class fuzzy model is provided
12488
if not args.probc == None and \
12589
not args.probg == None and \
12690
not args.probw == None:
12791
print('Computing Dice index')
128-
gpm = [load_image(args.probc),
129-
load_image(args.probg),
130-
load_image(args.probw)]
131-
d = fuzzy_dice(gpm, ppm_img, np.where(mask_img.get_data() > 0))
92+
gold_ppm = np.zeros(S.ppm.shape)
93+
gold_ppm_img = (args.probc, args.probg, args.probw)
94+
for k in range(3):
95+
img = load_image(gold_ppm_img[k])
96+
gold_ppm[..., k] = img.get_data()
97+
d = fuzzy_dice(gold_ppm, S.ppm, np.where(mask_img.get_data() > 0))
13298
print('Fuzzy Dice indices: %s' % d)
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from .brain_segmentation import (initialize_parameters, brain_segmentation)
2-
from .vem import (gauss_dist, laplace_dist, vm_step_gauss, weighted_median,
3-
vm_step_laplace, VEM)
1+
from .brain_segmentation import BrainT1Segmentation
2+
from .segmentation import Segmentation, moment_matching
43

54
from nipy.testing import Tester
65
test = Tester().test
76
bench = Tester().bench
8-

0 commit comments

Comments
 (0)