def test_maxwell_filter_additional(): """Test processing of Maxwell filtered data""" # TODO: Future tests integrate with mne/io/tests/test_proc_history # Load testing data (raw, SSS std origin, SSS non-standard origin) data_path = op.join(testing.data_path(download=False)) file_name = 'test_move_anon' raw_fname = op.join(data_path, 'SSS', file_name + '_raw.fif') with warnings.catch_warnings(record=True): # maxshield # Use 2.0 seconds of data to get stable cov. estimate raw = Raw(raw_fname, preload=False, proj=False, allow_maxshield=True).crop(0., 2., False) # Get MEG channels, compute Maxwell filtered data raw.preload_data() raw.pick_types(meg=True, eeg=False) int_order, ext_order = 8, 3 raw_sss = maxwell.maxwell_filter(raw, int_order=int_order, ext_order=ext_order) # Test io on processed data tempdir = _TempDir() test_outname = op.join(tempdir, 'test_raw_sss.fif') raw_sss.save(test_outname) raw_sss_loaded = Raw(test_outname, preload=True, proj=False, allow_maxshield=True) # Some numerical imprecision since save uses 'single' fmt assert_allclose(raw_sss_loaded._data[:, :], raw_sss._data[:, :], rtol=1e-6, atol=1e-20) # Test rank of covariance matrices for raw and SSS processed data cov_raw = compute_raw_data_covariance(raw) cov_sss = compute_raw_data_covariance(raw_sss) scalings = None cov_raw_rank = _estimate_rank_meeg_cov(cov_raw['data'], raw.info, scalings) cov_sss_rank = _estimate_rank_meeg_cov(cov_sss['data'], raw_sss.info, scalings) assert_equal(cov_raw_rank, raw.info['nchan']) assert_equal(cov_sss_rank, maxwell.get_num_moments(int_order, 0))
def test_maxwell_filter_additional(): """Test processing of Maxwell filtered data""" # TODO: Future tests integrate with mne/io/tests/test_proc_history # Load testing data (raw, SSS std origin, SSS non-standard origin) data_path = op.join(testing.data_path(download=False)) file_name = 'test_move_anon' raw_fname = op.join(data_path, 'SSS', file_name + '_raw.fif') with warnings.catch_warnings(record=True): # maxshield # Use 2.0 seconds of data to get stable cov. estimate raw = Raw(raw_fname, preload=False, proj=False, allow_maxshield=True).crop(0., 2., False) # Get MEG channels, compute Maxwell filtered data raw.load_data() raw.pick_types(meg=True, eeg=False) int_order, ext_order = 8, 3 raw_sss = maxwell.maxwell_filter(raw, int_order=int_order, ext_order=ext_order) # Test io on processed data tempdir = _TempDir() test_outname = op.join(tempdir, 'test_raw_sss.fif') raw_sss.save(test_outname) raw_sss_loaded = Raw(test_outname, preload=True, proj=False, allow_maxshield=True) # Some numerical imprecision since save uses 'single' fmt assert_allclose(raw_sss_loaded._data[:, :], raw_sss._data[:, :], rtol=1e-6, atol=1e-20) # Test rank of covariance matrices for raw and SSS processed data cov_raw = compute_raw_covariance(raw) cov_sss = compute_raw_covariance(raw_sss) scalings = None cov_raw_rank = _estimate_rank_meeg_cov(cov_raw['data'], raw.info, scalings) cov_sss_rank = _estimate_rank_meeg_cov(cov_sss['data'], raw_sss.info, scalings) assert_equal(cov_raw_rank, raw.info['nchan']) assert_equal(cov_sss_rank, maxwell.get_num_moments(int_order, 0))
def test_maxwell_filter_additional(): """Test processing of Maxwell filtered data.""" # TODO: Future tests integrate with mne/io/tests/test_proc_history # Load testing data (raw, SSS std origin, SSS non-standard origin) data_path = op.join(testing.data_path(download=False)) file_name = 'test_move_anon' raw_fname = op.join(data_path, 'SSS', file_name + '_raw.fif') # Use 2.0 seconds of data to get stable cov. estimate raw = read_crop(raw_fname, (0., 2.)) # Get MEG channels, compute Maxwell filtered data raw.load_data() raw.pick_types(meg=True, eeg=False) int_order = 8 raw_sss = maxwell_filter(raw, origin=mf_head_origin, regularize=None, bad_condition='ignore') # Test io on processed data tempdir = _TempDir() test_outname = op.join(tempdir, 'test_raw_sss.fif') raw_sss.save(test_outname) raw_sss_loaded = read_crop(test_outname).load_data() # Some numerical imprecision since save uses 'single' fmt assert_allclose(raw_sss_loaded[:][0], raw_sss[:][0], rtol=1e-6, atol=1e-20) # Test rank of covariance matrices for raw and SSS processed data cov_raw = compute_raw_covariance(raw) cov_sss = compute_raw_covariance(raw_sss) scalings = None cov_raw_rank = _estimate_rank_meeg_cov(cov_raw['data'], raw.info, scalings) cov_sss_rank = _estimate_rank_meeg_cov(cov_sss['data'], raw_sss.info, scalings) assert_equal(cov_raw_rank, raw.info['nchan']) assert_equal(cov_sss_rank, _get_n_moments(int_order))
def test_maxwell_filter_additional(): """Test processing of Maxwell filtered data""" # TODO: Future tests integrate with mne/io/tests/test_proc_history # Load testing data (raw, SSS std origin, SSS non-standard origin) data_path = op.join(testing.data_path(download=False)) file_name = "test_move_anon" raw_fname = op.join(data_path, "SSS", file_name + "_raw.fif") with warnings.catch_warnings(record=True): # maxshield # Use 2.0 seconds of data to get stable cov. estimate raw = Raw(raw_fname, allow_maxshield=True).crop(0.0, 2.0, False) # Get MEG channels, compute Maxwell filtered data raw.load_data() raw.pick_types(meg=True, eeg=False) int_order = 8 raw_sss = maxwell_filter(raw, origin=mf_head_origin, regularize=None, bad_condition="ignore") # Test io on processed data tempdir = _TempDir() test_outname = op.join(tempdir, "test_raw_sss.fif") raw_sss.save(test_outname) raw_sss_loaded = Raw(test_outname, preload=True) # Some numerical imprecision since save uses 'single' fmt assert_allclose(raw_sss_loaded[:][0], raw_sss[:][0], rtol=1e-6, atol=1e-20) # Test rank of covariance matrices for raw and SSS processed data cov_raw = compute_raw_covariance(raw) cov_sss = compute_raw_covariance(raw_sss) scalings = None cov_raw_rank = _estimate_rank_meeg_cov(cov_raw["data"], raw.info, scalings) cov_sss_rank = _estimate_rank_meeg_cov(cov_sss["data"], raw_sss.info, scalings) assert_equal(cov_raw_rank, raw.info["nchan"]) assert_equal(cov_sss_rank, _get_n_moments(int_order))
def test_rank(): """Test cov rank estimation.""" # Test that our rank estimation works properly on a simple case evoked = read_evokeds(ave_fname, condition=0, baseline=(None, 0), proj=False) cov = read_cov(cov_fname) ch_names = [ ch for ch in evoked.info['ch_names'] if '053' not in ch and ch.startswith('EEG') ] cov = prepare_noise_cov(cov, evoked.info, ch_names, None) assert_equal(cov['eig'][0], 0.) # avg projector should set this to zero assert_true((cov['eig'][1:] > 0).all()) # all else should be > 0 # Now do some more comprehensive tests raw_sample = read_raw_fif(raw_fname) raw_sss = read_raw_fif(hp_fif_fname) raw_sss.add_proj(compute_proj_raw(raw_sss)) cov_sample = compute_raw_covariance(raw_sample) cov_sample_proj = compute_raw_covariance(raw_sample.copy().apply_proj()) cov_sss = compute_raw_covariance(raw_sss) cov_sss_proj = compute_raw_covariance(raw_sss.copy().apply_proj()) picks_all_sample = pick_types(raw_sample.info, meg=True, eeg=True) picks_all_sss = pick_types(raw_sss.info, meg=True, eeg=True) info_sample = pick_info(raw_sample.info, picks_all_sample) picks_stack_sample = [('eeg', pick_types(info_sample, meg=False, eeg=True))] picks_stack_sample += [('meg', pick_types(info_sample, meg=True))] picks_stack_sample += [('all', pick_types(info_sample, meg=True, eeg=True))] info_sss = pick_info(raw_sss.info, picks_all_sss) picks_stack_somato = [('eeg', pick_types(info_sss, meg=False, eeg=True))] picks_stack_somato += [('meg', pick_types(info_sss, meg=True))] picks_stack_somato += [('all', pick_types(info_sss, meg=True, eeg=True))] iter_tests = list( itt.product( [(cov_sample, picks_stack_sample, info_sample), (cov_sample_proj, picks_stack_sample, info_sample), (cov_sss, picks_stack_somato, info_sss), (cov_sss_proj, picks_stack_somato, info_sss)], # sss [dict(mag=1e15, grad=1e13, eeg=1e6)])) for (cov, picks_list, this_info), scalings in iter_tests: for ch_type, picks in picks_list: this_very_info = pick_info(this_info, picks) # compute subset of projs this_projs = [ c['active'] and len( set(c['data']['col_names']).intersection( set(this_very_info['ch_names']))) > 0 for c in cov['projs'] ] n_projs = sum(this_projs) # count channel types ch_types = [ channel_type(this_very_info, idx) for idx in range(len(picks)) ] n_eeg, n_mag, n_grad = [ ch_types.count(k) for k in ['eeg', 'mag', 'grad'] ] n_meg = n_mag + n_grad if ch_type in ('all', 'eeg'): n_projs_eeg = 1 else: n_projs_eeg = 0 # check sss if len(this_very_info['proc_history']) > 0: mf = this_very_info['proc_history'][0]['max_info'] n_free = _get_sss_rank(mf) if 'mag' not in ch_types and 'grad' not in ch_types: n_free = 0 # - n_projs XXX clarify expected_rank = n_free + n_eeg if n_projs > 0 and ch_type in ('all', 'eeg'): expected_rank -= n_projs_eeg else: expected_rank = n_meg + n_eeg - n_projs C = cov['data'][np.ix_(picks, picks)] est_rank = _estimate_rank_meeg_cov(C, this_very_info, scalings=scalings) assert_equal(expected_rank, est_rank)
def test_rank(): """Test cov rank estimation.""" # Test that our rank estimation works properly on a simple case evoked = read_evokeds(ave_fname, condition=0, baseline=(None, 0), proj=False) cov = read_cov(cov_fname) ch_names = [ch for ch in evoked.info['ch_names'] if '053' not in ch and ch.startswith('EEG')] cov = prepare_noise_cov(cov, evoked.info, ch_names, None) assert_equal(cov['eig'][0], 0.) # avg projector should set this to zero assert_true((cov['eig'][1:] > 0).all()) # all else should be > 0 # Now do some more comprehensive tests raw_sample = read_raw_fif(raw_fname) raw_sss = read_raw_fif(hp_fif_fname) raw_sss.add_proj(compute_proj_raw(raw_sss)) cov_sample = compute_raw_covariance(raw_sample) cov_sample_proj = compute_raw_covariance( raw_sample.copy().apply_proj()) cov_sss = compute_raw_covariance(raw_sss) cov_sss_proj = compute_raw_covariance( raw_sss.copy().apply_proj()) picks_all_sample = pick_types(raw_sample.info, meg=True, eeg=True) picks_all_sss = pick_types(raw_sss.info, meg=True, eeg=True) info_sample = pick_info(raw_sample.info, picks_all_sample) picks_stack_sample = [('eeg', pick_types(info_sample, meg=False, eeg=True))] picks_stack_sample += [('meg', pick_types(info_sample, meg=True))] picks_stack_sample += [('all', pick_types(info_sample, meg=True, eeg=True))] info_sss = pick_info(raw_sss.info, picks_all_sss) picks_stack_somato = [('eeg', pick_types(info_sss, meg=False, eeg=True))] picks_stack_somato += [('meg', pick_types(info_sss, meg=True))] picks_stack_somato += [('all', pick_types(info_sss, meg=True, eeg=True))] iter_tests = list(itt.product( [(cov_sample, picks_stack_sample, info_sample), (cov_sample_proj, picks_stack_sample, info_sample), (cov_sss, picks_stack_somato, info_sss), (cov_sss_proj, picks_stack_somato, info_sss)], # sss [dict(mag=1e15, grad=1e13, eeg=1e6)] )) for (cov, picks_list, this_info), scalings in iter_tests: for ch_type, picks in picks_list: this_very_info = pick_info(this_info, picks) # compute subset of projs this_projs = [c['active'] and len(set(c['data']['col_names']) .intersection(set(this_very_info['ch_names']))) > 0 for c in cov['projs']] n_projs = sum(this_projs) # count channel types ch_types = [channel_type(this_very_info, idx) for idx in range(len(picks))] n_eeg, n_mag, n_grad = [ch_types.count(k) for k in ['eeg', 'mag', 'grad']] n_meg = n_mag + n_grad if ch_type in ('all', 'eeg'): n_projs_eeg = 1 else: n_projs_eeg = 0 # check sss if len(this_very_info['proc_history']) > 0: mf = this_very_info['proc_history'][0]['max_info'] n_free = _get_sss_rank(mf) if 'mag' not in ch_types and 'grad' not in ch_types: n_free = 0 # - n_projs XXX clarify expected_rank = n_free + n_eeg if n_projs > 0 and ch_type in ('all', 'eeg'): expected_rank -= n_projs_eeg else: expected_rank = n_meg + n_eeg - n_projs C = cov['data'][np.ix_(picks, picks)] est_rank = _estimate_rank_meeg_cov(C, this_very_info, scalings=scalings) assert_equal(expected_rank, est_rank)
def test_rank(): """Test cov rank estimation""" raw_sample = Raw(raw_fname) raw_sss = Raw(hp_fif_fname) raw_sss.add_proj(compute_proj_raw(raw_sss)) cov_sample = compute_raw_data_covariance(raw_sample) cov_sample_proj = compute_raw_data_covariance( raw_sample.copy().apply_proj()) cov_sss = compute_raw_data_covariance(raw_sss) cov_sss_proj = compute_raw_data_covariance(raw_sss.copy().apply_proj()) picks_all_sample = pick_types(raw_sample.info, meg=True, eeg=True) picks_all_sss = pick_types(raw_sss.info, meg=True, eeg=True) info_sample = pick_info(raw_sample.info, picks_all_sample) picks_stack_sample = [('eeg', pick_types(info_sample, meg=False, eeg=True))] picks_stack_sample += [('meg', pick_types(info_sample, meg=True))] picks_stack_sample += [('all', pick_types(info_sample, meg=True, eeg=True))] info_sss = pick_info(raw_sss.info, picks_all_sss) picks_stack_somato = [('eeg', pick_types(info_sss, meg=False, eeg=True))] picks_stack_somato += [('meg', pick_types(info_sss, meg=True))] picks_stack_somato += [('all', pick_types(info_sss, meg=True, eeg=True))] iter_tests = list( itt.product( [(cov_sample, picks_stack_sample, info_sample), (cov_sample_proj, picks_stack_sample, info_sample), (cov_sss, picks_stack_somato, info_sss), (cov_sss_proj, picks_stack_somato, info_sss)], # sss [dict(mag=1e15, grad=1e13, eeg=1e6)])) for (cov, picks_list, this_info), scalings in iter_tests: for ch_type, picks in picks_list: this_very_info = pick_info(this_info, picks) # compute subset of projs this_projs = [ c['active'] and len( set(c['data']['col_names']).intersection( set(this_very_info['ch_names']))) > 0 for c in cov['projs'] ] n_projs = sum(this_projs) # count channel types ch_types = [ channel_type(this_very_info, idx) for idx in range(len(picks)) ] n_eeg, n_mag, n_grad = [ ch_types.count(k) for k in ['eeg', 'mag', 'grad'] ] n_meg = n_mag + n_grad if ch_type in ('all', 'eeg'): n_projs_eeg = 1 else: n_projs_eeg = 0 # check sss if 'proc_history' in this_very_info: mf = this_very_info['proc_history'][0]['max_info'] n_free = _get_sss_rank(mf) if 'mag' not in ch_types and 'grad' not in ch_types: n_free = 0 # - n_projs XXX clarify expected_rank = n_free + n_eeg if n_projs > 0 and ch_type in ('all', 'eeg'): expected_rank -= n_projs_eeg else: expected_rank = n_meg + n_eeg - n_projs C = cov['data'][np.ix_(picks, picks)] est_rank = _estimate_rank_meeg_cov(C, this_very_info, scalings=scalings) assert_equal(expected_rank, est_rank)
def test_rank(): """Test cov rank estimation""" raw_sample = Raw(raw_fname) raw_sss = Raw(hp_fif_fname) raw_sss.add_proj(compute_proj_raw(raw_sss)) cov_sample = compute_raw_data_covariance(raw_sample) cov_sample_proj = compute_raw_data_covariance( raw_sample.copy().apply_proj()) cov_sss = compute_raw_data_covariance(raw_sss) cov_sss_proj = compute_raw_data_covariance( raw_sss.copy().apply_proj()) picks_all_sample = pick_types(raw_sample.info, meg=True, eeg=True) picks_all_sss = pick_types(raw_sss.info, meg=True, eeg=True) info_sample = pick_info(raw_sample.info, picks_all_sample) picks_stack_sample = [('eeg', pick_types(info_sample, meg=False, eeg=True))] picks_stack_sample += [('meg', pick_types(info_sample, meg=True))] picks_stack_sample += [('all', pick_types(info_sample, meg=True, eeg=True))] info_sss = pick_info(raw_sss.info, picks_all_sss) picks_stack_somato = [('eeg', pick_types(info_sss, meg=False, eeg=True))] picks_stack_somato += [('meg', pick_types(info_sss, meg=True))] picks_stack_somato += [('all', pick_types(info_sss, meg=True, eeg=True))] iter_tests = list(itt.product( [(cov_sample, picks_stack_sample, info_sample), (cov_sample_proj, picks_stack_sample, info_sample), (cov_sss, picks_stack_somato, info_sss), (cov_sss_proj, picks_stack_somato, info_sss)], # sss [dict(mag=1e15, grad=1e13, eeg=1e6)] )) for (cov, picks_list, this_info), scalings in iter_tests: for ch_type, picks in picks_list: this_very_info = pick_info(this_info, picks) # compute subset of projs this_projs = [c['active'] and len(set(c['data']['col_names']) .intersection(set(this_very_info['ch_names']))) > 0 for c in cov['projs']] n_projs = sum(this_projs) # count channel types ch_types = [channel_type(this_very_info, idx) for idx in range(len(picks))] n_eeg, n_mag, n_grad = [ch_types.count(k) for k in ['eeg', 'mag', 'grad']] n_meg = n_mag + n_grad if ch_type in ('all', 'eeg'): n_projs_eeg = 1 else: n_projs_eeg = 0 # check sss if 'proc_history' in this_very_info: mf = this_very_info['proc_history'][0]['max_info'] n_free = _get_sss_rank(mf) if 'mag' not in ch_types and 'grad' not in ch_types: n_free = 0 # - n_projs XXX clarify expected_rank = n_free + n_eeg if n_projs > 0 and ch_type in ('all', 'eeg'): expected_rank -= n_projs_eeg else: expected_rank = n_meg + n_eeg - n_projs C = cov['data'][np.ix_(picks, picks)] est_rank = _estimate_rank_meeg_cov(C, this_very_info, scalings=scalings) assert_equal(expected_rank, est_rank)