def _compute_rank(p, subj, run_indices): """Compute rank of the data.""" epochs_fnames, _ = get_epochs_evokeds_fnames(p, subj, p.analyses) _, fif_file = epochs_fnames epochs = read_epochs(fif_file) # .crop(p.bmin, p.bmax) maybe someday...? meg, eeg = 'meg' in epochs, 'eeg' in epochs rank = dict() epochs.apply_proj() if p.cov_rank_method == 'estimate_rank': # old way if meg: eps = epochs.copy().pick_types(meg=meg, eeg=False) eps = eps.get_data().transpose([1, 0, 2]) eps = eps.reshape(len(eps), -1) if 'grad' in epochs and 'mag' in epochs: # Neuromag key = 'meg' else: key = 'grad' if 'grad' in epochs else 'mag' rank[key] = estimate_rank(eps, tol=p.cov_rank_tol) if eeg: eps = epochs.copy().pick_types(meg=False, eeg=eeg) eps = eps.get_data().transpose([1, 0, 2]) eps = eps.reshape(len(eps), -1) rank['eeg'] = estimate_rank(eps, tol=p.cov_rank_tol) else: assert p.cov_rank_method == 'compute_rank' # new way rank = compute_rank(epochs, tol=p.cov_rank_tol, tol_kind='relative') for k, v in rank.items(): print(' : %s rank %2d' % (k.upper(), v), end='') return rank
def test_estimate_rank(): """Test rank estimation.""" data = np.eye(10) assert_array_equal( estimate_rank(data, return_singular=True)[1], np.ones(10)) data[0, 0] = 0 assert estimate_rank(data) == 9 pytest.raises(ValueError, estimate_rank, data, 'foo')
def test_estimate_rank(): """Test rank estimation.""" data = np.eye(10) assert_array_equal(estimate_rank(data, return_singular=True)[1], np.ones(10)) data[0, 0] = 0 assert estimate_rank(data) == 9 pytest.raises(ValueError, estimate_rank, data, 'foo')
def check_rank_cov_matrix(cov_mat, epochs): """Check the covariance matrix and report rank and channel number.""" ch_num = len(cov_mat.ch_names) rank_cov = estimate_rank(cov_mat['data'], tol='auto') cov_ok = ch_num - rank_cov cond_num = cond(cov_mat['data']) if cov_ok > 0 and cond_num > 10e15: # cond num: amazing is < 10e4, really shitty is > 10e12, horrible 10e19 print_ok = 'WARNING! Bad cov matrix.' else: print_ok = 'ok.' print('%i trials, %i channels, cov matrix has rank %i and condition ' 'number %s: %s' % (len(epochs.events), ch_num, rank_cov, '{:.2E}'.format(Decimal(cond_num)), print_ok))
def _make_diagonal_noise_matrix(csd, reg): """Make a diagonal matrix suitable for using as a noise CSD. Starts with an identity matrix and scales it to the smallest singular value of the CSD matrix that is comfortably larger than 0. Parameters ---------- csd : ndarray, shape (n_series, n_series) The data cross-spectral density (CSD) matrix. reg : float The regulariation parameter used when inverting the CSD matrix. Returns ------- noise : ndarray, shape (n_series, n_series) A suitable noise cross-spectral density (CSD) matrix. """ rank, s = estimate_rank(csd, tol='auto', norm=False, return_singular=True) last_singular_value = abs(s[rank - 1]) ratio = max(abs(reg), last_singular_value) return np.eye(len(csd)) * ratio