@@ -69,19 +69,8 @@ def _run_interface(self, runtime):
69
69
data = data .astype (np .float32 )
70
70
71
71
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 )
85
74
86
75
meanimg = np .mean (data , axis = 3 )
87
76
stddevimg = np .std (data , axis = 3 )
@@ -103,3 +92,19 @@ def _list_outputs(self):
103
92
if isdefined (self .inputs .regress_poly ):
104
93
outputs ['detrended_file' ] = op .abspath (self .inputs .detrended_file )
105
94
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