def test_simulate_raw_chpi(): """Test simulation of raw data with cHPI""" with warnings.catch_warnings(record=True): # MaxShield raw = Raw(raw_chpi_fname, allow_maxshield=True) sphere = make_sphere_model('auto', 'auto', raw.info) # make sparse spherical source space sphere_vol = tuple(sphere['r0'] * 1000.) + (sphere.radius * 1000.,) src = setup_volume_source_space('sample', sphere=sphere_vol, pos=70.) stc = _make_stc(raw, src) # simulate data with cHPI on raw_sim = simulate_raw(raw, stc, None, src, sphere, cov=None, chpi=False) # need to trim extra samples off this one raw_chpi = simulate_raw(raw, stc, None, src, sphere, cov=None, chpi=True, head_pos=pos_fname) # test that the cHPI signals make some reasonable values psd_sim, freqs_sim = compute_raw_psd(raw_sim) psd_chpi, freqs_chpi = compute_raw_psd(raw_chpi) assert_array_equal(freqs_sim, freqs_chpi) hpi_freqs = _get_hpi_info(raw.info)[0] freq_idx = np.sort([np.argmin(np.abs(freqs_sim - f)) for f in hpi_freqs]) picks_meg = pick_types(raw.info, meg=True, eeg=False) picks_eeg = pick_types(raw.info, meg=False, eeg=True) assert_allclose(psd_sim[picks_eeg], psd_chpi[picks_eeg], atol=1e-20) assert_true((psd_chpi[picks_meg][:, freq_idx] > 100 * psd_sim[picks_meg][:, freq_idx]).all()) # test localization based on cHPI information trans_sim, rot_sim, t_sim = _calculate_chpi_positions(raw_chpi) trans, rot, t = get_chpi_positions(pos_fname) t -= raw.first_samp / raw.info['sfreq'] _compare_positions((trans, rot, t), (trans_sim, rot_sim, t_sim), max_dist=0.005)
def test_calculate_chpi_positions(): """Test calculation of cHPI positions """ trans, rot, t = get_chpi_positions(pos_fname) with warnings.catch_warnings(record=True): raw = Raw(raw_fif_fname, allow_maxshield=True, preload=True) t -= raw.first_samp / raw.info['sfreq'] trans_est, rot_est, t_est = _calculate_chpi_positions(raw, verbose='debug') _compare_positions((trans, rot, t), (trans_est, rot_est, t_est)) # degenerate conditions raw_no_chpi = Raw(test_fif_fname) assert_raises(RuntimeError, _calculate_chpi_positions, raw_no_chpi) raw_bad = raw.copy() for d in raw_bad.info['dig']: if d['kind'] == FIFF.FIFFV_POINT_HPI: d['coord_frame'] = 999 break assert_raises(RuntimeError, _calculate_chpi_positions, raw_bad) raw_bad = raw.copy() for d in raw_bad.info['dig']: if d['kind'] == FIFF.FIFFV_POINT_HPI: d['r'] = np.ones(3) raw_bad.crop(0, 1., copy=False) tempdir = _TempDir() log_file = op.join(tempdir, 'temp_log.txt') set_log_file(log_file, overwrite=True) try: _calculate_chpi_positions(raw_bad) finally: set_log_file() with open(log_file, 'r') as fid: for line in fid: assert_true('0/5 acceptable' in line)
def test_calculate_chpi_positions(): """Test calculation of cHPI positions """ trans, rot, t = get_chpi_positions(pos_fname) with warnings.catch_warnings(record=True): raw = Raw(raw_fif_fname, allow_maxshield=True, preload=True) t -= raw.first_samp / raw.info['sfreq'] trans_est, rot_est, t_est = _calculate_chpi_positions(raw, verbose='debug') _compare_positions((trans, rot, t), (trans_est, rot_est, t_est)) # degenerate conditions raw_no_chpi = Raw(test_fif_fname) assert_raises(RuntimeError, _calculate_chpi_positions, raw_no_chpi) raw_bad = raw.copy() for d in raw_bad.info['dig']: if d['kind'] == FIFF.FIFFV_POINT_HPI: d['coord_frame'] = 999 break assert_raises(RuntimeError, _calculate_chpi_positions, raw_bad) raw_bad = raw.copy() for d in raw_bad.info['dig']: if d['kind'] == FIFF.FIFFV_POINT_HPI: d['r'] = np.ones(3) raw_bad.crop(0, 1., copy=False) with catch_logging() as log_file: _calculate_chpi_positions(raw_bad) for line in log_file.getvalue().split('\n')[:-1]: assert_true('0/5 acceptable' in line)
def test_get_chpi(): """Test CHPI position computation """ trans0, rot0 = get_chpi_positions(hp_fname)[:2] trans0, rot0 = trans0[:-1], rot0[:-1] raw = Raw(hp_fif_fname) out = get_chpi_positions(raw) trans1, rot1, t1 = out trans1, rot1 = trans1[2:], rot1[2:] # these will not be exact because they don't use equiv. time points assert_allclose(trans0, trans1, atol=1e-5, rtol=1e-1) assert_allclose(rot0, rot1, atol=1e-6, rtol=1e-1) # run through input checking assert_raises(TypeError, get_chpi_positions, 1) assert_raises(ValueError, get_chpi_positions, hp_fname, [1]) raw_no_chpi = Raw(test_fif_fname) assert_raises(RuntimeError, get_chpi_positions, raw_no_chpi) assert_raises(ValueError, get_chpi_positions, raw, t_step='foo') assert_raises(IOError, get_chpi_positions, 'foo')
def test_get_chpi(): """Test CHPI position computation """ with warnings.catch_warnings(record=True): # deprecation trans0, rot0, _, quat0 = get_chpi_positions(hp_fname, return_quat=True) assert_allclose(rot0[0], quat_to_rot(quat0[0])) trans0, rot0 = trans0[:-1], rot0[:-1] raw = Raw(hp_fif_fname) with warnings.catch_warnings(record=True): # deprecation out = get_chpi_positions(raw) trans1, rot1, t1 = out trans1, rot1 = trans1[2:], rot1[2:] # these will not be exact because they don't use equiv. time points assert_allclose(trans0, trans1, atol=1e-5, rtol=1e-1) assert_allclose(rot0, rot1, atol=1e-6, rtol=1e-1) # run through input checking raw_no_chpi = Raw(test_fif_fname) with warnings.catch_warnings(record=True): # deprecation assert_raises(TypeError, get_chpi_positions, 1) assert_raises(ValueError, get_chpi_positions, hp_fname, [1]) assert_raises(RuntimeError, get_chpi_positions, raw_no_chpi) assert_raises(ValueError, get_chpi_positions, raw, t_step='foo') assert_raises(IOError, get_chpi_positions, 'foo')
def test_simulate_raw_chpi(): """Test simulation of raw data with cHPI""" with warnings.catch_warnings(record=True): # MaxShield raw = Raw(raw_chpi_fname, allow_maxshield=True) sphere = make_sphere_model('auto', 'auto', raw.info) # make sparse spherical source space sphere_vol = tuple(sphere['r0'] * 1000.) + (sphere.radius * 1000., ) src = setup_volume_source_space('sample', sphere=sphere_vol, pos=70.) stc = _make_stc(raw, src) # simulate data with cHPI on raw_sim = simulate_raw(raw, stc, None, src, sphere, cov=None, chpi=False) # need to trim extra samples off this one raw_chpi = simulate_raw(raw, stc, None, src, sphere, cov=None, chpi=True, head_pos=pos_fname) # test cHPI indication hpi_freqs, _, hpi_pick, hpi_on, _ = _get_hpi_info(raw.info) assert_allclose(raw_sim[hpi_pick][0], 0.) assert_allclose(raw_chpi[hpi_pick][0], hpi_on) # test that the cHPI signals make some reasonable values picks_meg = pick_types(raw.info, meg=True, eeg=False) picks_eeg = pick_types(raw.info, meg=False, eeg=True) for picks in [picks_meg, picks_eeg]: psd_sim, freqs_sim = psd_welch(raw_sim, picks=picks) psd_chpi, freqs_chpi = psd_welch(raw_chpi, picks=picks) assert_array_equal(freqs_sim, freqs_chpi) freq_idx = np.sort( [np.argmin(np.abs(freqs_sim - f)) for f in hpi_freqs]) if picks is picks_meg: assert_true( (psd_chpi[:, freq_idx] > 100 * psd_sim[:, freq_idx]).all()) else: assert_allclose(psd_sim, psd_chpi, atol=1e-20) # test localization based on cHPI information trans_sim, rot_sim, t_sim = _calculate_chpi_positions(raw_chpi) trans, rot, t = get_chpi_positions(pos_fname) t -= raw.first_samp / raw.info['sfreq'] _compare_positions((trans, rot, t), (trans_sim, rot_sim, t_sim), max_dist=0.005)