Ejemplo n.º 1
0
def generate_DEV(name='test', sr=20, duration=480):
    n = duration * sr
    values = np.random.normal(size=n)
    ent_names = ['task', 'run', 'session', 'subject']
    entities = {e: uuid.uuid4().hex for e in ent_names}
    image = uuid.uuid4().hex + '.nii.gz'
    run_info = RunInfo(entities, duration, 2, image)
    return DenseRunVariable('test', values, run_info, 'dummy', sr)
Ejemplo n.º 2
0
def sparse_run_variable_with_missing_values():
    data = pd.DataFrame({
        'onset': [2, 5, 11, 17],
        'duration': [1.2, 1.6, 0.8, 2],
        'amplitude': [1, 1, np.nan, 1]
    })
    run_info = [RunInfo({'subject': '01'}, 20, 2, 'dummy.nii.gz')]
    var = SparseRunVariable('var', data, run_info, 'events')
    return BIDSRunVariableCollection([var])
Ejemplo n.º 3
0
def test_Lag():
    var = DenseRunVariable(
        name="rot_x",
        values=np.arange(5., 20.),
        run_info=RunInfo({}, 15, 1, "none", 15),
        source='regressors',
        sampling_rate=1
    )
    coll = BIDSRunVariableCollection([var], sampling_rate=1)

    # Forward shift
    transform.Lag(coll, "rot_x", output="d_rot_x")
    d_rot_x = coll["d_rot_x"].values.values
    assert np.isclose(d_rot_x[0, 0], 5.)
    assert np.allclose(d_rot_x[1:, 0], np.arange(5., 19.))

    # Backward shift
    transform.Lag(coll, "rot_x", output="d_rot_x", shift=-1)
    d_rot_x = coll["d_rot_x"].values.values
    assert np.isclose(d_rot_x[-1, 0], 19.)
    assert np.allclose(d_rot_x[:-1, 0], np.arange(6., 20.))

    # Half shift; don't know why you'd want to do it, but you can
    transform.Lag(coll, "rot_x", output="half_shift", shift=0.5, order=1)
    half_shift = coll["half_shift"].values.values
    assert np.isclose(half_shift[0, 0], 5.)
    assert np.allclose(half_shift[1:, 0], np.arange(5.5, 19.5))

    # Constant mode
    transform.Lag(coll, "rot_x", output="d_rot_x", mode="constant")
    d_rot_x = coll["d_rot_x"].values.values
    assert np.isclose(d_rot_x[0, 0], 0.)
    assert np.allclose(d_rot_x[1:, 0], np.arange(5., 19.))

    # Reflect mode
    transform.Lag(coll, "rot_x", output="d_rot_x", mode="reflect")
    d_rot_x = coll["d_rot_x"].values.values
    assert np.isclose(d_rot_x[0, 0], 5.)
    assert np.allclose(d_rot_x[1:, 0], np.arange(5., 19.))

    # Forward shift -> Backward difference
    transform.Lag(coll, "rot_x", output="d_rot_x", difference=True)
    d_rot_x = coll["d_rot_x"].values.values
    assert np.isclose(d_rot_x[0, 0], 0.)
    assert np.allclose(d_rot_x[1:, 0], 1.)

    # Backward shift -> Forward difference
    transform.Lag(coll, "rot_x", output="d_rot_x", shift=-1, difference=True)
    d_rot_x = coll["d_rot_x"].values.values
    assert np.isclose(d_rot_x[-1, 0], 0.)
    assert np.allclose(d_rot_x[:-1, 0], 1.)
Ejemplo n.º 4
0
def test_convolve_impulse():
    # Smoke test impulse convolution
    data = pd.DataFrame({
        'onset': [10, 20],
        'duration': [0, 0],
        'amplitude': [1, 1]
    })
    run_info = [RunInfo({'subject': '01'}, 20, 2, 'dummy.nii.gz', 10)]
    var = SparseRunVariable(
        name='var', data=data, run_info=run_info, source='events')
    coll = BIDSRunVariableCollection([var])
    transform.ToDense(coll, 'var', output='var_dense')
    transform.Convolve(coll, 'var', output='var_hrf')
    transform.Convolve(coll, 'var_dense', output='var_dense_hrf')
Ejemplo n.º 5
0
def test_run_variable_collection_get_sampling_rate(run_coll):
    coll = run_coll.clone()
    assert coll._get_sampling_rate(None) == 10
    coll.variables['RT'].run_info[0] = RunInfo({}, 200, 10, None, 20)
    with pytest.raises(ValueError) as exc:
        coll._get_sampling_rate('TR')
    assert str(exc.value).startswith('Non-unique')
    assert coll._get_sampling_rate('highest') == 10
    coll.variables['RT1'] = coll.variables['RT'].to_dense(5.)
    coll.variables['RT2'] = coll.variables['RT'].to_dense(12.)
    assert coll._get_sampling_rate('highest') == 12.
    assert coll._get_sampling_rate(20) == 20
    with pytest.raises(ValueError) as exc:
        coll._get_sampling_rate('BLARGH')
    assert str(exc.value).startswith('Invalid')