Created
August 24, 2018 00:01
-
-
Save larsoner/9ea52b1cb96f345b0a34ee5c736e8478 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def compute_whitener(noise_cov, info, rank=None): | |
| """Compute whitening matrix.""" | |
| from mne.cov import _get_whitener | |
| from mne.defaults import _handle_default | |
| from mne.io.pick import pick_channels_cov, _pick_data_channels | |
| picks = _pick_data_channels(info, with_ref_meg=False, exclude='bads') | |
| ch_names = [info['ch_names'][k] for k in picks] | |
| noise_cov = pick_channels_cov(noise_cov, include=ch_names, exclude=[]) | |
| if len(noise_cov['data']) != len(ch_names): | |
| missing = list(set(ch_names) - set(noise_cov['names'])) | |
| raise RuntimeError('Not all channels present in noise covariance:\n%s' | |
| % missing) | |
| scalings = _handle_default('scalings_cov_rank', None) | |
| W, noise_cov, n_nzero = _get_whitener(noise_cov, info, ch_names, rank, | |
| pca=True, scalings=scalings) | |
| # Do the back projection | |
| assert W.shape[0] == n_nzero | |
| return W | |
| import mne # noqa, analysis:ignore | |
| data_path = mne.datasets.sample.data_path() + '/MEG/sample/' | |
| fname_cov = data_path + 'sample_audvis-cov.fif' | |
| fname_info = data_path + 'sample_audvis_raw.fif' | |
| cov = mne.read_cov(fname_cov) | |
| info = mne.io.read_info(fname_info) | |
| whitener = compute_whitener(cov, info) | |
| assert whitener.shape[1] == 364 # good MEG+EEG chs | |
| assert whitener.shape[0] < 364 # rank-reduced | |
| print(whitener.shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment