def test_cov_scaling(): """Test rescaling covs.""" evoked = read_evokeds(ave_fname, condition=0, baseline=(None, 0), proj=True) cov = read_cov(cov_fname)['data'] cov2 = read_cov(cov_fname)['data'] assert_array_equal(cov, cov2) evoked.pick_channels([ evoked.ch_names[k] for k in pick_types(evoked.info, meg=True, eeg=True) ]) picks_list = _picks_by_type(evoked.info) scalings = dict(mag=1e15, grad=1e13, eeg=1e6) _apply_scaling_cov(cov2, picks_list, scalings=scalings) _apply_scaling_cov(cov, picks_list, scalings=scalings) assert_array_equal(cov, cov2) assert cov.max() > 1 _undo_scaling_cov(cov2, picks_list, scalings=scalings) _undo_scaling_cov(cov, picks_list, scalings=scalings) assert_array_equal(cov, cov2) assert cov.max() < 1 data = evoked.data.copy() _apply_scaling_array(data, picks_list, scalings=scalings) _undo_scaling_array(data, picks_list, scalings=scalings) assert_allclose(data, evoked.data, atol=1e-20)
def test_cov_scaling(): """Test rescaling covs.""" evoked = read_evokeds(ave_fname, condition=0, baseline=(None, 0), proj=True) cov = read_cov(cov_fname)['data'] cov2 = read_cov(cov_fname)['data'] assert_array_equal(cov, cov2) evoked.pick_channels([evoked.ch_names[k] for k in pick_types( evoked.info, meg=True, eeg=True )]) picks_list = _picks_by_type(evoked.info) scalings = dict(mag=1e15, grad=1e13, eeg=1e6) _apply_scaling_cov(cov2, picks_list, scalings=scalings) _apply_scaling_cov(cov, picks_list, scalings=scalings) assert_array_equal(cov, cov2) assert cov.max() > 1 _undo_scaling_cov(cov2, picks_list, scalings=scalings) _undo_scaling_cov(cov, picks_list, scalings=scalings) assert_array_equal(cov, cov2) assert cov.max() < 1 data = evoked.data.copy() _apply_scaling_array(data, picks_list, scalings=scalings) _undo_scaling_array(data, picks_list, scalings=scalings) assert_allclose(data, evoked.data, atol=1e-20)
def _estimate_rank_meeg_cov(data, info, scalings, tol='auto', return_singular=False): """Estimate rank of M/EEG covariance data, given the covariance. Parameters ---------- data : np.ndarray of float, shape (n_channels, n_channels) The M/EEG covariance. info : Info The measurement info. scalings : dict | 'norm' | np.ndarray | None The rescaling method to be applied. If dict, it will override the following default dict: dict(mag=1e12, grad=1e11, eeg=1e5) If 'norm' data will be scaled by channel-wise norms. If array, pre-specified norms will be used. If None, no scaling will be applied. tol : float | str Tolerance. See ``estimate_rank``. return_singular : bool If True, also return the singular values that were used to determine the rank. Returns ------- rank : int Estimated rank of the data. s : array If return_singular is True, the singular values that were thresholded to determine the rank are also returned. """ picks_list = _picks_by_type(info) scalings = _handle_default('scalings_cov_rank', scalings) _apply_scaling_cov(data, picks_list, scalings) if data.shape[1] < data.shape[0]: ValueError("You've got fewer samples than channels, your " "rank estimate might be inaccurate.") out = estimate_rank(data, tol=tol, norm=False, return_singular=return_singular) rank = out[0] if isinstance(out, tuple) else out ch_type = ' + '.join(list(zip(*picks_list))[0]) logger.info('estimated rank (%s): %d' % (ch_type, rank)) _undo_scaling_cov(data, picks_list, scalings) return out