@@ -88,7 +88,7 @@ def set_direction(self, direction):
88
88
self .direction = int (direction )
89
89
90
90
91
- def fitting (self , data ):
91
+ def fitting (self , data , mask = [], threshold = 100 ):
92
92
'''
93
93
Parameters
94
94
----------
@@ -107,11 +107,21 @@ def fitting(self, data):
107
107
F = np .exp (self .direction * 2j * np .pi * self .f_stim * (self .time - self .delay ))
108
108
X = zscore (np .array ([np .real (F ), np .imag (F )]).transpose ())
109
109
110
- data = zscore ( np .reshape (
110
+ data = np .reshape (
111
111
data .astype (float ),
112
112
(self .n_samples ,
113
- self .n_total )),
114
- axis = 0 )
113
+ self .n_total ))
114
+
115
+ mean_signal = np .mean (data , axis = 0 )
116
+
117
+ if np .size (mask )== 0 :
118
+ mask = mean_signal >= threshold
119
+
120
+ mask = np .reshape (mask ,self .n_total )
121
+ voxel_index = np .where (mask )[0 ]
122
+ data = zscore (data [:, mask ], axis = 0 )
123
+ n_voxels = voxel_index .size
124
+
115
125
beta = regress (data , X )
116
126
Y_ = np .matmul (X , beta )
117
127
residuals = data - Y_
@@ -122,34 +132,35 @@ def fitting(self, data):
122
132
'p_value' : np .zeros (self .n_total )}
123
133
df1 = 1.0
124
134
df2 = self .n_samples - 2.0
125
- for v in range (self .n_total ):
126
- if (std_signal [v ]> 0 ):
135
+ print ('\n performing analysis' )
136
+ for m in range (n_voxels ):
137
+ v = voxel_index [m ]
127
138
128
139
129
- # estimate and correct for autocorrelation
130
- T = np .array ([np .append (0 ,residuals [0 :- 1 , v ]),
131
- np .append (np .zeros (2 ), residuals [0 :- 2 , v ])]).transpose ()
132
- W = np .append (1 , - regress (residuals [:, v ], T ))
140
+ # estimate and correct for autocorrelation
141
+ T = np .array ([np .append (0 ,residuals [0 :- 1 , m ]),
142
+ np .append (np .zeros (2 ), residuals [0 :- 2 , m ])]).transpose ()
143
+ W = np .append (1 , - regress (residuals [:, m ], T ))
133
144
134
- X_corrected = correct_autocorr (X , W )
135
- D_corrected = correct_autocorr (data [:,v ], W )
136
- #---------------------------------------------------------------------
145
+ X_corrected = correct_autocorr (X , W )
146
+ D_corrected = correct_autocorr (data [:,m ], W )
147
+ #---------------------------------------------------------------------
137
148
138
- beta_corrected = regress (D_corrected , X_corrected )
139
- Y_ = np .matmul (X_corrected , beta_corrected )
140
- mu = np .mean (D_corrected , axis = 0 );
141
- MSM = np .dot ((Y_ - mu ).transpose (), Y_ - mu ) / df1
142
- MSE = np .dot ((Y_ - D_corrected ).transpose (), Y_ - D_corrected ) / df2
143
- beta_complex = beta_corrected [0 ] + beta_corrected [1 ] * 1j
144
- results ['phase' ][v ] = np .angle (beta_complex )
145
- results ['amplitude' ][v ] = np .abs (beta_complex )
146
- results ['f_stat' ][v ] = MSM / MSE ;
147
- results ['p_value' ][v ] = max (1 - f .cdf (MSM / MSE , df1 , df2 ), 1e-20 )
149
+ beta_corrected = regress (D_corrected , X_corrected )
150
+ Y_ = np .matmul (X_corrected , beta_corrected )
151
+ mu = np .mean (D_corrected , axis = 0 );
152
+ MSM = np .dot ((Y_ - mu ).transpose (), Y_ - mu ) / df1
153
+ MSE = np .dot ((Y_ - D_corrected ).transpose (), Y_ - D_corrected ) / df2
154
+ beta_complex = beta_corrected [0 ] + beta_corrected [1 ] * 1j
155
+ results ['phase' ][v ] = np .angle (beta_complex )
156
+ results ['amplitude' ][v ] = np .abs (beta_complex )
157
+ results ['f_stat' ][v ] = MSM / MSE ;
158
+ results ['p_value' ][v ] = max (1 - f .cdf (MSM / MSE , df1 , df2 ), 1e-20 )
148
159
149
160
150
- i = int (v / self . n_total * 21 )
151
- sys .stdout .write ('\r ' )
152
- sys .stdout .write ("performing analysis [%-20s] %d%%"
161
+ i = int (m / n_voxels * 21 )
162
+ sys .stdout .write ('\r ' )
163
+ sys .stdout .write ("[%-20s] %d%%"
153
164
% ('=' * i , 5 * i ))
154
165
155
166
for key in results :
@@ -158,8 +169,8 @@ def fitting(self, data):
158
169
(self .n_rows ,
159
170
self .n_cols ,
160
171
self .n_slices )))
161
-
172
+
162
173
return results
163
174
164
175
165
-
176
+
0 commit comments