Skip to content

Commit 8b95cb4

Browse files
author
Shoshana Berleant
committed
more efficient mean calculation
1 parent 0e90204 commit 8b95cb4

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

nipype/algorithms/confounds.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -509,29 +509,31 @@ def _list_outputs(self):
509509

510510
def regress_poly(degree, data, remove_mean=True, axis=-1):
511511
''' returns data with degree polynomial regressed out.
512-
Be default it is calculated along the last axis (usu. time)
512+
Be default it is calculated along the last axis (usu. time).
513+
If remove_mean is True (default), the data is demeaned (i.e. degree 0).
514+
If remove_mean is false, the data is not.
513515
'''
514516
datashape = data.shape
515517
timepoints = datashape[axis]
516518

517-
if remove_mean: # i.e. regress_poly degree 0, which the following does not do
518-
data = signal.detrend(data, axis=axis, type='constant')
519-
520519
# Rearrange all voxel-wise time-series in rows
521520
data = data.reshape((-1, timepoints))
522521

523522
# Generate design matrix
524-
X = np.ones((timepoints, 1))
523+
X = np.ones((timepoints, 1)) # quick way to calc degree 0
525524
for i in range(degree):
526-
polynomial_func = Legendre.basis(i+1)
525+
polynomial_func = Legendre.basis(i + 1)
527526
value_array = np.linspace(-1, 1, timepoints)
528527
X = np.hstack((X, polynomial_func(value_array)[:, np.newaxis]))
529528

530529
# Calculate coefficients
531530
betas = np.linalg.pinv(X).dot(data.T)
532531

533532
# Estimation
534-
datahat = X[:, 1:].dot(betas[1:, ...]).T
533+
if remove_mean:
534+
datahat = X.dot(betas).T
535+
else: # disregard the first layer of X, which is degree 0
536+
datahat = X[:, 1:].dot(betas[1:, ...]).T
535537
regressed_data = data - datahat
536538

537539
# Back to original shape

0 commit comments

Comments
 (0)