Skip to content

Commit 1511bea

Browse files
author
Shoshana Berleant
committed
naive refactor
1 parent 33d2f30 commit 1511bea

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

nipype/algorithms/tsnr.py

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

7171
if isdefined(self.inputs.regress_poly):
72-
timepoints = img.shape[-1]
73-
X = np.ones((timepoints, 1))
74-
for i in range(self.inputs.regress_poly):
75-
X = np.hstack((X, legendre(
76-
i + 1)(np.linspace(-1, 1, timepoints))[:, None]))
77-
betas = np.dot(np.linalg.pinv(X), np.rollaxis(data, 3, 2))
78-
datahat = np.rollaxis(np.dot(X[:, 1:],
79-
np.rollaxis(
80-
betas[1:, :, :, :], 0, 3)),
81-
0, 4)
82-
data = data - datahat
83-
img = nb.Nifti1Image(data, img.get_affine(), header)
84-
nb.save(img, op.abspath(self.inputs.detrended_file))
72+
img, data = regress_poly(self.inputs.regress_poly, header, img, data,
73+
self.inputs.detrended_file)
8574

8675
meanimg = np.mean(data, axis=3)
8776
stddevimg = np.std(data, axis=3)
@@ -103,3 +92,19 @@ def _list_outputs(self):
10392
if isdefined(self.inputs.regress_poly):
10493
outputs['detrended_file'] = op.abspath(self.inputs.detrended_file)
10594
return outputs
95+
96+
def regress_poly(degree, header, img, data, filename):
97+
timepoints = img.shape[-1]
98+
X = np.ones((timepoints, 1))
99+
for i in range(degree):
100+
X = np.hstack((X, legendre(
101+
i + 1)(np.linspace(-1, 1, timepoints))[:, None]))
102+
betas = np.dot(np.linalg.pinv(X), np.rollaxis(data, 3, 2))
103+
datahat = np.rollaxis(np.dot(X[:, 1:],
104+
np.rollaxis(
105+
betas[1:, :, :, :], 0, 3)),
106+
0, 4)
107+
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

0 commit comments

Comments
 (0)