def test_hkernel(): """ test the hrf computation """ tr = 2.0 h = _hrf_kernel('spm', tr) assert_almost_equal(h[0], spm_hrf(tr)) assert len(h) == 1 h = _hrf_kernel('spm + derivative', tr) assert_almost_equal(h[1], spm_time_derivative(tr)) assert len(h) == 2 h = _hrf_kernel('spm + derivative + dispersion', tr) assert_almost_equal(h[2], spm_dispersion_derivative(tr)) assert len(h) == 3 h = _hrf_kernel('glover', tr) assert_almost_equal(h[0], glover_hrf(tr)) assert len(h) == 1 h = _hrf_kernel('glover + derivative', tr) assert_almost_equal(h[1], glover_time_derivative(tr)) assert_almost_equal(h[0], glover_hrf(tr)) assert len(h) == 2 h = _hrf_kernel('fir', tr, fir_delays=np.arange(4)) assert len(h) == 4 for dh in h: assert_almost_equal(dh.sum(), 1.) h = _hrf_kernel(None, tr) assert len(h) == 1 assert_almost_equal(h[0], np.hstack((1, np.zeros(49))))
def test_hkernel(): """ test the hrf computation """ tr = 2.0 h = _hrf_kernel('spm', tr) assert_almost_equal(h[0], spm_hrf(tr)) assert len(h) == 1 h = _hrf_kernel('spm + derivative', tr) assert_almost_equal(h[1], spm_time_derivative(tr)) assert len(h) == 2 h = _hrf_kernel('spm + derivative + dispersion', tr) assert_almost_equal(h[2], spm_dispersion_derivative(tr)) assert len(h) == 3 h = _hrf_kernel('glover', tr) assert_almost_equal(h[0], glover_hrf(tr)) assert len(h) == 1 h = _hrf_kernel('glover + derivative', tr) assert_almost_equal(h[1], glover_time_derivative(tr)) assert_almost_equal(h[0], glover_hrf(tr)) assert len(h) == 2 h = _hrf_kernel('glover + derivative + dispersion', tr) assert len(h) == 3 assert_almost_equal(h[2], glover_dispersion_derivative(tr)) assert_almost_equal(h[1], glover_time_derivative(tr)) assert_almost_equal(h[0], glover_hrf(tr)) h = _hrf_kernel('fir', tr, fir_delays=np.arange(4)) assert len(h) == 4 for dh in h: assert_almost_equal(dh.sum(), 1.) h = _hrf_kernel(None, tr) assert len(h) == 1 assert_almost_equal(h[0], np.hstack((1, np.zeros(49)))) with pytest.raises(ValueError, match="Could not process custom HRF model provided."): _hrf_kernel(lambda x: np.ones(int(x)), tr) _hrf_kernel([lambda x, y, z: x + y + z], tr) _hrf_kernel([lambda x: np.ones(int(x))] * 2, tr) h = _hrf_kernel(lambda tr, ov: np.ones(int(tr * ov)), tr) assert len(h) == 1 assert_almost_equal(h[0], np.ones(100)) h = _hrf_kernel([lambda tr, ov: np.ones(int(tr * ov))], tr) assert len(h) == 1 assert_almost_equal(h[0], np.ones(100)) with pytest.raises(ValueError, match="is not a known hrf model."): _hrf_kernel("foo", tr)
def test_glover_hrf(): """ test that the spm_hrf is correctly normalized and has correct length """ h = glover_hrf(2.0) assert_almost_equal(h.sum(), 1) assert len(h) == 800 h = glover_dispersion_derivative(2.0) assert_almost_equal(h.sum(), 0) assert len(h) == 800
def simulate_signal(onsets, conditions, TR=2, duration=None, icept=0, params_canon=None, params_tderiv=None, params_deriv1=None, phi=None, std_noise=1, osf=100, rnd_seed=None, plot=True): """ Simulates a somewhat realistic voxel signal with an associated design matrix. """ if rnd_seed is not None: np.random.seed(rnd_seed) conds = sorted(np.unique(conditions)) P = len(conds) if duration is None: duration = np.max(onsets) + 30 if params_canon is None: params_canon = np.zeros(P) # TMP FIX params_tderiv = params_deriv1 if params_tderiv is None: params_tderiv = np.zeros(P) X = np.zeros((duration * osf, P)) for ons, con in zip(onsets, conditions): X[int(ons * osf), conds.index(con)] = 1 hrf = glover_hrf(tr=1, oversampling=osf) hrf_d = glover_time_derivative(tr=1, oversampling=osf) t_orig = np.arange(0, duration, 1 / osf) t_new = np.arange(0, duration, TR) Xconv = np.zeros((t_orig.size, P * 2)) idx = 0 for i, this_hrf in enumerate([hrf, hrf_d]): this_hrf /= this_hrf.max() for ii in range(P): Xconv[:, idx] = np.convolve(X[:, ii], this_hrf)[:t_orig.size] idx += 1 Xconv = np.c_[np.ones(Xconv.shape[0]), Xconv] params = np.r_[icept, params_canon, params_tderiv] y = Xconv @ params resampler = interp1d(t_orig, y) y = resampler(t_new) if phi is None: noise_cov = std_noise ** 2 * np.eye(y.size) else: noise_cov = std_noise ** 2 * phi ** toeplitz(np.arange(y.size)) y = y + np.random.multivariate_normal(np.zeros(y.size), noise_cov) Xconv_ds = np.zeros((t_new.size, Xconv.shape[1])) for i in range(Xconv.shape[1]): resampler = interp1d(t_orig, Xconv[:, i]) Xconv_ds[:, i] = resampler(t_new) est_betas = np.linalg.lstsq(Xconv_ds, y, rcond=None)[0] if plot: plt.figure(figsize=(15, 5)) plt.plot(y) plt.plot(Xconv_ds @ est_betas) plt.xlim(0, y.size) plt.legend(['y', 'y-hat']) plt.show() return y, Xconv_ds