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 create_hrf(self, hrf_params=[1.0, 1.0, 0.0]): """ construct single or multiple HRFs Parameters ---------- hrf_params : TYPE, optional DESCRIPTION. The default is [1.0, 1.0, 0.0]. Returns ------- TYPE DESCRIPTION. """ hrf = np.array([ np.ones_like(hrf_params[1]) * hrf_params[0] * spm_hrf(tr=self.stimulus.TR, oversampling=1, time_length=40)[..., np.newaxis], hrf_params[1] * spm_time_derivative( tr=self.stimulus.TR, oversampling=1, time_length=40)[..., np.newaxis], hrf_params[2] * spm_dispersion_derivative( tr=self.stimulus.TR, oversampling=1, time_length=40)[..., np.newaxis] ]).sum(axis=0) return hrf.T
def test_spm_hrf_derivative(): """ test that the spm_hrf is correctly normalized and has correct length """ h = spm_time_derivative(2.0) assert_almost_equal(h.sum(), 0) assert len(h) == 800 h = spm_dispersion_derivative(2.0) assert_almost_equal(h.sum(), 0) assert len(h) == 800
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)