Run L2 penalized regression and keep 10 strongest locations. Parameters ---------- M : array, shape (n_channels, n_times) The whitened data. G : array, shape (n_channels, n_dipoles) The gain matrix a.k.a. the forward operator. The number of locations is n_dip
(M, G, n_orient)
| 143 | |
| 144 | |
| 145 | def solver(M, G, n_orient): |
| 146 | """Run L2 penalized regression and keep 10 strongest locations. |
| 147 | |
| 148 | Parameters |
| 149 | ---------- |
| 150 | M : array, shape (n_channels, n_times) |
| 151 | The whitened data. |
| 152 | G : array, shape (n_channels, n_dipoles) |
| 153 | The gain matrix a.k.a. the forward operator. The number of locations |
| 154 | is n_dipoles / n_orient. n_orient will be 1 for a fixed orientation |
| 155 | constraint or 3 when using a free orientation model. |
| 156 | n_orient : int |
| 157 | Can be 1 or 3 depending if one works with fixed or free orientations. |
| 158 | If n_orient is 3, then ``G[:, 2::3]`` corresponds to the dipoles that |
| 159 | are normal to the cortex. |
| 160 | |
| 161 | Returns |
| 162 | ------- |
| 163 | X : array, (n_active_dipoles, n_times) |
| 164 | The time series of the dipoles in the active set. |
| 165 | active_set : array (n_dipoles) |
| 166 | Array of bool. Entry j is True if dipole j is in the active set. |
| 167 | We have ``X_full[active_set] == X`` where X_full is the full X matrix |
| 168 | such that ``M = G X_full``. |
| 169 | """ |
| 170 | inner = np.dot(G, G.T) |
| 171 | trace = np.trace(inner) |
| 172 | K = linalg.solve(inner + 4e-6 * trace * np.eye(G.shape[0]), G).T |
| 173 | K /= np.linalg.norm(K, axis=1)[:, None] |
| 174 | X = np.dot(K, M) |
| 175 | |
| 176 | indices = np.argsort(np.sum(X**2, axis=1))[-10:] |
| 177 | active_set = np.zeros(G.shape[1], dtype=bool) |
| 178 | for idx in indices: |
| 179 | idx -= idx % n_orient |
| 180 | active_set[idx : idx + n_orient] = True |
| 181 | X = X[active_set] |
| 182 | return X, active_set |
| 183 | |
| 184 | |
| 185 | # %% |
no test coverage detected