Skip to content

Commit b66db5f

Browse files
author
Shoshana Berleant
committed
make compcor tests more sensitive to changes, simplify regress_poly()
1 parent 1511bea commit b66db5f

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

nipype/algorithms/compcor.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from nipype.pipeline import engine as pe
55
from nipype.interfaces.utility import IdentityInterface
6+
from nipype.algorithms.tsnr import regress_poly
67

78
import nibabel as nb
89
import numpy as np
@@ -14,12 +15,15 @@ class CompCorInputSpec(BaseInterfaceInputSpec):
1415
desc='already realigned brain image (4D)')
1516
mask_file = File(exists=True, mandatory=False,
1617
desc='mask file that determines ROI (3D)')
17-
components_file = File('components_file.txt', exists=False, mandatory=False, usedefault=True,
18+
components_file = File('components_file.txt', exists=False, mandatory=False,
19+
usedefault=True,
1820
desc='filename to store physiological components in')
1921
num_components = traits.Int(6, usedefault=True) # 6 for BOLD, 4 for ASL
22+
regress_poly = traits.Range(low=1, default=1, usedefault=True)
2023

2124
class CompCorOutputSpec(TraitedSpec):
22-
components_file = File(desc='text file containing the noise components', exists=True)
25+
components_file = File(exists=True,
26+
desc='text file containing the noise components')
2327

2428
class CompCor(BaseInterface):
2529
'''

nipype/algorithms/tests/test_compcor.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import nibabel as nb
1111
import numpy as np
1212
import os
13+
from hashlib import sha1
1314

1415
class TestCompCor(unittest.TestCase):
1516
''' Note: Tests currently do a poor job of testing functionality '''
@@ -30,21 +31,25 @@ def test_compcor(self):
3031
mask[0,0,1] = 0
3132
mask_file = utils.save_toy_nii(mask, self.filenames['masknii'])
3233

34+
expected_sha1 = 'b0dd7f9ab7ba8f516712eb0204dacc9e397fc6aa'
35+
3336
ccresult = self.run_cc(CompCor(realigned_file=self.realigned_file,
34-
mask_file=mask_file))
37+
mask_file=mask_file),
38+
expected_sha1)
3539

3640
accresult = self.run_cc(ACompCor(realigned_file=self.realigned_file,
37-
mask_file=mask_file,
38-
components_file='acc_components_file'))
41+
mask_file=mask_file,
42+
components_file='acc_components_file'),
43+
expected_sha1)
3944

4045
assert_equal(os.path.getsize(ccresult.outputs.components_file),
4146
os.path.getsize(accresult.outputs.components_file))
4247

4348
def test_tcompcor(self):
4449
ccinterface = TCompCor(realigned_file=self.realigned_file)
45-
self.run_cc(ccinterface)
50+
self.run_cc(ccinterface, '12e54c07281a28ac0da3b934dce5c9d27626848a')
4651

47-
def run_cc(self, ccinterface):
52+
def run_cc(self, ccinterface, expected_components_data_sha1):
4853
# run
4954
ccresult = ccinterface.run()
5055

@@ -55,6 +60,13 @@ def run_cc(self, ccinterface):
5560
assert_true(os.path.getsize(expected_file) > 0)
5661
assert_equal(ccinterface.inputs.num_components, 6)
5762

63+
with open(ccresult.outputs.components_file, 'r') as components_file:
64+
components_data = [line for line in components_file]
65+
num_got_components = len(components_data)
66+
assert_true(num_got_components == ccinterface.inputs.num_components
67+
or num_got_components == self.fake_data.shape[3])
68+
print(str(components_data), "comdata")
69+
assert_equal(sha1(str(components_data)).hexdigest(), expected_components_data_sha1)
5870
return ccresult
5971

6072
def tearDown(self):

nipype/algorithms/tsnr.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ def _run_interface(self, runtime):
6969
data = data.astype(np.float32)
7070

7171
if isdefined(self.inputs.regress_poly):
72-
img, data = regress_poly(self.inputs.regress_poly, header, img, data,
73-
self.inputs.detrended_file)
72+
data = regress_poly(self.inputs.regress_poly, data)
73+
img = nb.Nifti1Image(data, img.get_affine(), header)
74+
nb.save(img, op.abspath(self.inputs.detrended_file))
7475

7576
meanimg = np.mean(data, axis=3)
7677
stddevimg = np.std(data, axis=3)
@@ -93,8 +94,8 @@ def _list_outputs(self):
9394
outputs['detrended_file'] = op.abspath(self.inputs.detrended_file)
9495
return outputs
9596

96-
def regress_poly(degree, header, img, data, filename):
97-
timepoints = img.shape[-1]
97+
def regress_poly(degree, data):
98+
timepoints = data.shape[-1]
9899
X = np.ones((timepoints, 1))
99100
for i in range(degree):
100101
X = np.hstack((X, legendre(
@@ -105,6 +106,4 @@ def regress_poly(degree, header, img, data, filename):
105106
betas[1:, :, :, :], 0, 3)),
106107
0, 4)
107108
regressed_data = data - datahat
108-
regressed_img = nb.Nifti1Image(regressed_data, img.get_affine(), header)
109-
nb.save(regressed_img, op.abspath(filename))
110-
return regressed_img, regressed_data
109+
return regressed_data

0 commit comments

Comments
 (0)