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(): """Test multipolar moment and Maxwell filter""" # TODO: Future tests integrate with mne/io/tests/test_proc_history # Load testing data (raw, SSS std origin, SSS non-standard origin) with warnings.catch_warnings(record=True): # maxshield raw = Raw(raw_fname, preload=False, proj=False, allow_maxshield=True).crop(0., 1., False) raw.preload_data() with warnings.catch_warnings(record=True): # maxshield, naming sss_std = Raw(sss_std_fname, preload=True, proj=False, allow_maxshield=True) sss_nonStd = Raw(sss_nonstd_fname, preload=True, proj=False, allow_maxshield=True) raw_err = Raw(raw_fname, preload=False, proj=True, allow_maxshield=True).crop(0., 0.1, False) assert_raises(RuntimeError, maxwell.maxwell_filter, raw_err) # Create coils all_coils, _, _, meg_info = _prep_meg_channels(raw.info, ignore_ref=True, elekta_defs=True) picks = [ raw.info['ch_names'].index(ch) for ch in [coil['chname'] for coil in all_coils] ] coils = [all_coils[ci] for ci in picks] ncoils = len(coils) int_order, ext_order = 8, 3 n_int_bases = int_order**2 + 2 * int_order n_ext_bases = ext_order**2 + 2 * ext_order nbases = n_int_bases + n_ext_bases # Check number of bases computed correctly assert_equal(maxwell.get_num_moments(int_order, ext_order), nbases) # Check multipolar moment basis set S_in, S_out = maxwell._sss_basis(origin=np.array([0, 0, 40]), coils=coils, int_order=int_order, ext_order=ext_order) assert_equal(S_in.shape, (ncoils, n_int_bases), 'S_in has incorrect shape') assert_equal(S_out.shape, (ncoils, n_ext_bases), 'S_out has incorrect shape') # Test sss computation at the standard head origin raw_sss = maxwell.maxwell_filter(raw, origin=[0., 0., 40.], int_order=int_order, ext_order=ext_order) assert_array_almost_equal(raw_sss._data[picks, :], sss_std._data[picks, :], decimal=11, err_msg='Maxwell filtered data at ' 'standard origin incorrect.') # Confirm SNR is above 100 bench_rms = np.sqrt(np.mean(sss_std._data[picks, :]**2, axis=1)) error = raw_sss._data[picks, :] - sss_std._data[picks, :] error_rms = np.sqrt(np.mean(error**2, axis=1)) assert_true(np.mean(bench_rms / error_rms) > 1000, 'SNR < 1000') # Test sss computation at non-standard head origin raw_sss = maxwell.maxwell_filter(raw, origin=[0., 20., 20.], int_order=int_order, ext_order=ext_order) assert_array_almost_equal(raw_sss._data[picks, :], sss_nonStd._data[picks, :], decimal=11, err_msg='Maxwell filtered data at non-std ' 'origin incorrect.') # Confirm SNR is above 100 bench_rms = np.sqrt(np.mean(sss_nonStd._data[picks, :]**2, axis=1)) error = raw_sss._data[picks, :] - sss_nonStd._data[picks, :] error_rms = np.sqrt(np.mean(error**2, axis=1)) assert_true(np.mean(bench_rms / error_rms) > 1000, 'SNR < 1000') # Check against SSS functions from proc_history sss_info = raw_sss.info['proc_history'][0]['max_info'] assert_equal(maxwell.get_num_moments(int_order, 0), proc_history._get_sss_rank(sss_info))
def test_maxwell_filter(): """Test multipolar moment and Maxwell filter""" # TODO: Future tests integrate with mne/io/tests/test_proc_history # Load testing data (raw, SSS std origin, SSS non-standard origin) with warnings.catch_warnings(record=True): # maxshield raw = Raw(raw_fname, preload=False, proj=False, allow_maxshield=True).crop(0., 1., False) raw.load_data() with warnings.catch_warnings(record=True): # maxshield, naming sss_std = Raw(sss_std_fname, preload=True, proj=False, allow_maxshield=True) sss_nonStd = Raw(sss_nonstd_fname, preload=True, proj=False, allow_maxshield=True) raw_err = Raw(raw_fname, preload=False, proj=True, allow_maxshield=True).crop(0., 0.1, False) assert_raises(RuntimeError, maxwell.maxwell_filter, raw_err) # Create coils all_coils, _, _, meg_info = _prep_meg_channels(raw.info, ignore_ref=True, elekta_defs=True) picks = [raw.info['ch_names'].index(ch) for ch in [coil['chname'] for coil in all_coils]] coils = [all_coils[ci] for ci in picks] ncoils = len(coils) int_order, ext_order = 8, 3 n_int_bases = int_order ** 2 + 2 * int_order n_ext_bases = ext_order ** 2 + 2 * ext_order nbases = n_int_bases + n_ext_bases # Check number of bases computed correctly assert_equal(maxwell.get_num_moments(int_order, ext_order), nbases) # Check multipolar moment basis set S_in, S_out = maxwell._sss_basis(origin=np.array([0, 0, 40]), coils=coils, int_order=int_order, ext_order=ext_order) assert_equal(S_in.shape, (ncoils, n_int_bases), 'S_in has incorrect shape') assert_equal(S_out.shape, (ncoils, n_ext_bases), 'S_out has incorrect shape') # Test sss computation at the standard head origin raw_sss = maxwell.maxwell_filter(raw, origin=[0., 0., 40.], int_order=int_order, ext_order=ext_order) assert_array_almost_equal(raw_sss._data[picks, :], sss_std._data[picks, :], decimal=11, err_msg='Maxwell filtered data at ' 'standard origin incorrect.') # Confirm SNR is above 100 bench_rms = np.sqrt(np.mean(sss_std._data[picks, :] ** 2, axis=1)) error = raw_sss._data[picks, :] - sss_std._data[picks, :] error_rms = np.sqrt(np.mean(error ** 2, axis=1)) assert_true(np.mean(bench_rms / error_rms) > 1000, 'SNR < 1000') # Test sss computation at non-standard head origin raw_sss = maxwell.maxwell_filter(raw, origin=[0., 20., 20.], int_order=int_order, ext_order=ext_order) assert_array_almost_equal(raw_sss._data[picks, :], sss_nonStd._data[picks, :], decimal=11, err_msg='Maxwell filtered data at non-std ' 'origin incorrect.') # Confirm SNR is above 100 bench_rms = np.sqrt(np.mean(sss_nonStd._data[picks, :] ** 2, axis=1)) error = raw_sss._data[picks, :] - sss_nonStd._data[picks, :] error_rms = np.sqrt(np.mean(error ** 2, axis=1)) assert_true(np.mean(bench_rms / error_rms) > 1000, 'SNR < 1000') # Check against SSS functions from proc_history sss_info = raw_sss.info['proc_history'][0]['max_info'] assert_equal(maxwell.get_num_moments(int_order, 0), proc_history._get_sss_rank(sss_info))