|
142 | 142 | # Because the BOLD signal is inherently slow due to the dynamics of |
143 | 143 | # neuro-vascular coupling, this model is unlikely to perform well. |
144 | 144 |
|
145 | | -pipeline_nodelay = make_pipeline( |
| 145 | +pipeline_no_delay = make_pipeline( |
146 | 146 | StandardScaler(with_mean=True, with_std=False), |
147 | 147 | KernelRidgeCV( |
148 | 148 | alphas=alphas, cv=cv, |
149 | 149 | solver_params=dict(n_targets_batch=500, n_alphas_batch=5, |
150 | 150 | n_targets_batch_refit=100)), |
151 | 151 | ) |
152 | | -pipeline_nodelay |
| 152 | +pipeline_no_delay |
153 | 153 |
|
154 | 154 | ############################################################################### |
155 | 155 | # We fit and score the model as the previous one. |
156 | | -pipeline_nodelay.fit(X_train, Y_train) |
157 | | -scores_nodelay = pipeline_nodelay.score(X_test, Y_test) |
| 156 | +pipeline_no_delay.fit(X_train, Y_train) |
| 157 | +scores_nodelay = pipeline_no_delay.score(X_test, Y_test) |
158 | 158 | scores_nodelay = backend.to_numpy(scores_nodelay) |
159 | 159 | print("(n_voxels,) =", scores_nodelay.shape) |
160 | 160 |
|
|
207 | 207 | voxel_selection = np.argsort(scores)[-10:] |
208 | 208 |
|
209 | 209 | # define a pipeline with more delays |
210 | | -pipeline_many_delays = make_pipeline( |
| 210 | +pipeline_more_delays = make_pipeline( |
211 | 211 | StandardScaler(with_mean=True, with_std=False), |
212 | 212 | Delayer(delays=[0, 1, 2, 3, 4, 5, 6]), |
213 | 213 | KernelRidgeCV( |
|
216 | 216 | n_targets_batch_refit=100)), |
217 | 217 | ) |
218 | 218 |
|
219 | | -pipeline_many_delays.fit(X_train, Y_train[:, voxel_selection]) |
| 219 | +pipeline_more_delays.fit(X_train, Y_train[:, voxel_selection]) |
220 | 220 |
|
221 | 221 | # get the (primal) ridge regression coefficients |
222 | | -primal_coef = pipeline_many_delays[-1].get_primal_coef() |
| 222 | +primal_coef = pipeline_more_delays[-1].get_primal_coef() |
223 | 223 | primal_coef = backend.to_numpy(primal_coef) |
224 | 224 |
|
225 | | -# get the delays |
226 | | -delays = pipeline_many_delays.named_steps['delayer'].delays |
227 | 225 | # split the ridge coefficients per delays |
228 | | -primal_coef_per_delay = np.stack(np.split(primal_coef, len(delays), axis=0)) |
| 226 | +delayer = pipeline_more_delays.named_steps['delayer'] |
| 227 | +primal_coef_per_delay = delayer.reshape_by_delays(primal_coef, axis=0) |
| 228 | +print("(n_delays, n_features, n_voxels) =", primal_coef_per_delay.shape) |
229 | 229 |
|
230 | 230 | # select the feature with the largest coefficients for each voxel |
231 | 231 | feature_selection = np.argmax(np.sum(np.abs(primal_coef_per_delay), axis=0), |
232 | 232 | axis=0) |
233 | 233 | primal_coef_selection = primal_coef_per_delay[:, feature_selection, |
234 | 234 | np.arange(len(voxel_selection))] |
235 | 235 |
|
236 | | -plt.plot(delays, primal_coef_selection) |
| 236 | +plt.plot(delayer.delays, primal_coef_selection) |
237 | 237 | plt.xlabel('Delays') |
238 | | -plt.xticks(delays) |
| 238 | +plt.xticks(delayer.delays) |
239 | 239 | plt.ylabel('Ridge coefficients') |
240 | 240 | plt.title(f'Largest feature for the {len(voxel_selection)} best voxels') |
241 | 241 | plt.axhline(0, color='k', linewidth=0.5) |
|
0 commit comments