Skip to content

Commit 6f76129

Browse files
author
Shoshana Berleant
committed
Merge branch 'fix-shoshber-compcor1594' of https://github.com/oesteban/nipype into oesteban-fix-shoshber-compcor1594
2 parents da1e59a + e78b0ae commit 6f76129

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ inplace:
5353
$(PYTHON) setup.py build_ext -i
5454

5555
test-code: in
56-
$(NOSETESTS) -s nipype --with-doctest
56+
# $(NOSETESTS) -s nipype/algorithms/compcor.py --with-doctest -v
57+
$(NOSETESTS) -s nipype/algorithms/tests/test_tsnr.py #--with-coverage
58+
$(NOSETESTS) -s nipype/algorithms/tests/test_compcor.py #--with-coverage
59+
# $(NOSETESTS) -s nipype/algorithms/misc.py --with-doctest -v
5760

5861
test-doc:
5962
$(NOSETESTS) -s --with-doctest --doctest-tests --doctest-extension=rst \

nipype/algorithms/compcor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def _run_interface(self, runtime):
123123
mask_file = 'mask.nii'
124124
nb.nifti1.save(nb.Nifti1Image(mask, np.eye(4)), mask_file)
125125
self.inputs.mask_file = 'mask.nii'
126+
126127
super(TCompCor, self)._run_interface(runtime)
127128
return runtime
128129

nipype/algorithms/tsnr.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ def _run_interface(self, runtime):
6060
img = nb.load(self.inputs.in_file[0])
6161
header = img.header.copy()
6262
vollist = [nb.load(filename) for filename in self.inputs.in_file]
63-
data = np.concatenate([vol.get_data().reshape(
64-
vol.get_shape()[:3] + (-1,)) for vol in vollist], axis=3)
63+
data = np.concatenate(
64+
[vol.get_data().reshape(
65+
vol.get_shape()[:3] + (-1,))
66+
for vol in vollist],
67+
axis=3)
6568
data = np.nan_to_num(data)
6669

6770
if data.dtype.kind == 'i':
@@ -98,17 +101,25 @@ def regress_poly(degree, data):
98101
''' returns data with degree polynomial regressed out.
99102
The last dimension (i.e. data.shape[-1]) should be time.
100103
'''
101-
timepoints = data.shape[-1]
104+
datashape = data.shape
105+
timepoints = datashape[-1]
106+
107+
# Rearrange all voxel-wise time-series in rows
108+
data = data.reshape((-1, timepoints))
109+
110+
# Generate design matrix
102111
X = np.ones((timepoints, 1))
103112
for i in range(degree):
104113
polynomial_func = legendre(i+1)
105114
value_array = np.linspace(-1, 1, timepoints)
106115
X = np.hstack((X, polynomial_func(value_array)[:, newaxis]))
107116

108-
betas = np.dot(np.linalg.pinv(X), np.rollaxis(data, 3, 2))
109-
datahat = np.rollaxis(np.dot(X[:, 1:],
110-
np.rollaxis(
111-
betas[1:, :, :, :], 0, 3)),
112-
0, 4)
117+
# Calculate coefficients
118+
betas = np.linalg.pinv(X).dot(data.T)
119+
120+
# Estimation
121+
datahat = X[:, 1:].dot(betas[1:, ...]).T
113122
regressed_data = data - datahat
114-
return regressed_data
123+
124+
# Back to original shape
125+
return regressed_data.reshape(datashape)

nipype/testing/data/mask.nii

416 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)