@@ -509,29 +509,31 @@ def _list_outputs(self):
509
509
510
510
def regress_poly (degree , data , remove_mean = True , axis = - 1 ):
511
511
''' 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.
513
515
'''
514
516
datashape = data .shape
515
517
timepoints = datashape [axis ]
516
518
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
-
520
519
# Rearrange all voxel-wise time-series in rows
521
520
data = data .reshape ((- 1 , timepoints ))
522
521
523
522
# Generate design matrix
524
- X = np .ones ((timepoints , 1 ))
523
+ X = np .ones ((timepoints , 1 )) # quick way to calc degree 0
525
524
for i in range (degree ):
526
- polynomial_func = Legendre .basis (i + 1 )
525
+ polynomial_func = Legendre .basis (i + 1 )
527
526
value_array = np .linspace (- 1 , 1 , timepoints )
528
527
X = np .hstack ((X , polynomial_func (value_array )[:, np .newaxis ]))
529
528
530
529
# Calculate coefficients
531
530
betas = np .linalg .pinv (X ).dot (data .T )
532
531
533
532
# 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
535
537
regressed_data = data - datahat
536
538
537
539
# Back to original shape
0 commit comments