Skip to content

Commit 140b159

Browse files
committed
add regression tests
1 parent 63519b8 commit 140b159

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

nipype/algorithms/metrics.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,18 +446,19 @@ def _run_interface(self, runtime):
446446
'Size of "in_tst" %s must match that of "in_ref" %s.' %
447447
(tstdata.shape, refdata.shape))
448448

449+
ncomp = refdata.shape[-1]
450+
449451
# Load mask
450-
mask = np.ones_like(refdata[..., 0], dtype=bool)
452+
mask = np.ones_like(refdata, dtype=bool)
451453
if isdefined(self.inputs.in_mask):
452454
mask = nb.load(self.inputs.in_mask).get_data()
453455
mask = mask > 0
454-
assert mask.shape == refdata.shape[:-1]
455-
456-
ncomp = refdata.shape[-1]
456+
mask = np.repeat(mask[..., np.newaxis], ncomp, -1)
457+
assert mask.shape == refdata.shape
457458

458459
# Drop data outside mask
459-
refdata = refdata[mask[..., np.newaxis]]
460-
tstdata = tstdata[mask[..., np.newaxis]]
460+
refdata = refdata[mask]
461+
tstdata = tstdata[mask]
461462

462463
if np.any(refdata < 0.0):
463464
iflogger.warning('Negative values encountered in "in_ref" input, '
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
4+
import numpy as np
5+
import nibabel as nb
6+
from nipype.testing import example_data
7+
from ..metrics import FuzzyOverlap
8+
9+
10+
def test_fuzzy_overlap(tmpdir):
11+
tmpdir.chdir()
12+
13+
# Tests with tissue probability maps
14+
in_mask = example_data('tpms_msk.nii.gz')
15+
tpms = [example_data('tpm_%02d.nii.gz' % i) for i in range(3)]
16+
out = FuzzyOverlap(in_ref=tpms[0], in_tst=tpms[0]).run().outputs
17+
assert out.dice == 1
18+
19+
out = FuzzyOverlap(
20+
in_mask=in_mask, in_ref=tpms[0], in_tst=tpms[0]).run().outputs
21+
assert out.dice == 1
22+
23+
out = FuzzyOverlap(
24+
in_mask=in_mask, in_ref=tpms[0], in_tst=tpms[1]).run().outputs
25+
assert 0 < out.dice < 1
26+
27+
out = FuzzyOverlap(in_ref=tpms, in_tst=tpms).run().outputs
28+
assert out.dice == 1.0
29+
30+
out = FuzzyOverlap(
31+
in_mask=in_mask, in_ref=tpms, in_tst=tpms).run().outputs
32+
assert out.dice == 1.0
33+
34+
# Tests with synthetic 3x3x3 images
35+
data = np.zeros((3, 3, 3), dtype=float)
36+
data[0, 0, 0] = 0.5
37+
data[2, 2, 2] = 0.25
38+
data[1, 1, 1] = 0.3
39+
nb.Nifti1Image(data, np.eye(4)).to_filename('test1.nii.gz')
40+
41+
data = np.zeros((3, 3, 3), dtype=float)
42+
data[0, 0, 0] = 0.6
43+
data[1, 1, 1] = 0.3
44+
nb.Nifti1Image(data, np.eye(4)).to_filename('test2.nii.gz')
45+
46+
out = FuzzyOverlap(in_ref='test1.nii.gz', in_tst='test2.nii.gz').run().outputs
47+
assert np.allclose(out.dice, 0.82051)
48+
49+
# Just considering the mask, the central pixel
50+
# that raised the index now is left aside.
51+
data = np.zeros((3, 3, 3), dtype=int)
52+
data[0, 0, 0] = 1
53+
data[2, 2, 2] = 1
54+
nb.Nifti1Image(data, np.eye(4)).to_filename('mask.nii.gz')
55+
56+
out = FuzzyOverlap(in_ref='test1.nii.gz', in_tst='test2.nii.gz',
57+
in_mask='mask.nii.gz').run().outputs
58+
assert np.allclose(out.dice, 0.74074)

0 commit comments

Comments
 (0)