Exemplo n.º 1
0
def test_convolve(collection):
    rt = collection.variables['RT']
    transform.Convolve(collection, 'RT', output='reaction_time')
    rt_conv = collection.variables['reaction_time']

    assert rt_conv.values.shape[0] == \
        rt.get_duration() * collection.sampling_rate

    transform.ToDense(collection, 'RT', output='rt_dense')
    transform.Convolve(collection, 'rt_dense', output='dense_convolved')

    dense_conv = collection.variables['reaction_time']

    assert dense_conv.values.shape[0] == \
        rt.get_duration() * collection.sampling_rate

    # Test adapative oversampling computation
    with mock.patch('bids.analysis.transformations.compute.hrf') as mocked:
        transform.Convolve(collection, 'RT', output='rt_mock')
        mocked.compute_regressor.assert_called_with(
            mock.ANY, 'spm', mock.ANY, fir_delays=None, min_onset=0,
            oversampling=1.0)

    with mock.patch('bids.analysis.transformations.compute.hrf') as mocked:
        transform.Convolve(collection, 'rt_dense', output='rt_mock')
        mocked.compute_regressor.assert_called_with(
            mock.ANY, 'spm', mock.ANY, fir_delays=None, min_onset=0,
            oversampling=3.0)

    with mock.patch('bids.analysis.transformations.compute.hrf') as mocked:
        collection.sampling_rate = 0.5
        transform.Convolve(collection, 'RT', output='rt_mock')
        mocked.compute_regressor.assert_called_with(
            mock.ANY, 'spm', mock.ANY, fir_delays=None, min_onset=0,
            oversampling=2.0)
Exemplo n.º 2
0
def test_convolve(collection):
    rt = collection.variables['RT']
    transform.Convolve(collection, 'RT', output='reaction_time')
    rt_conv = collection.variables['reaction_time']

    assert rt_conv.values.shape[0] == \
        rt.get_duration() * collection.sampling_rate

    transform.ToDense(collection, 'RT', output='rt_dense')
    transform.Convolve(collection, 'rt_dense', output='dense_convolved')

    dense_conv = collection.variables['reaction_time']

    assert dense_conv.values.shape[0] == \
        rt.get_duration() * collection.sampling_rate

    # Test adapative oversampling computation
    # Events are 3s duration events every 4s, so resolution demanded by the data is 1Hz
    # To resolve 1Hz frequencies, we must sample at >=2Hz
    args = (mock.ANY, 'spm', mock.ANY)
    kwargs = dict(fir_delays=None, min_onset=0)
    with mock.patch('bids.analysis.transformations.compute.hrf') as mocked:
        # Sampling rate is 10Hz, no oversampling needed
        transform.Convolve(collection, 'RT', output='rt_mock')
        mocked.compute_regressor.assert_called_with(*args,
                                                    oversampling=1.0,
                                                    **kwargs)

    with mock.patch('bids.analysis.transformations.compute.hrf') as mocked:
        # Sampling rate is 10Hz, no oversampling needed
        transform.Convolve(collection, 'rt_dense', output='rt_mock')
        mocked.compute_regressor.assert_called_with(*args,
                                                    oversampling=1.0,
                                                    **kwargs)

    with mock.patch('bids.analysis.transformations.compute.hrf') as mocked:
        # Slow sampling rate, oversample (4x) to 2Hz
        collection.sampling_rate = 0.5
        transform.Convolve(collection, 'RT', output='rt_mock')
        mocked.compute_regressor.assert_called_with(*args,
                                                    oversampling=4.0,
                                                    **kwargs)

    with mock.patch('bids.analysis.transformations.compute.hrf') as mocked:
        # Dense variable is already sampled at 10Hz, no oversampling needed
        collection.sampling_rate = 0.5
        transform.Convolve(collection, 'rt_dense', output='rt_mock')
        mocked.compute_regressor.assert_called_with(*args,
                                                    oversampling=1.0,
                                                    **kwargs)

    with mock.patch('bids.analysis.transformations.compute.hrf') as mocked:
        # Onset requires 10Hz resolution, oversample (2x) to 20Hz
        collection.sampling_rate = 10
        collection['RT'].onset[0] += 0.1
        transform.Convolve(collection, 'RT', output='rt_mock')
        mocked.compute_regressor.assert_called_with(*args,
                                                    oversampling=2.0,
                                                    **kwargs)
Exemplo n.º 3
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')
def test_resample(collection):
    coll = collection.clone()

    transform.ToDense(coll, 'parametric gain', output='pg_dense')
    pg = coll.variables['pg_dense']
    old_shape = pg.values.shape
    old_auc = np.trapz(np.abs(pg.values.values.squeeze()), dx=0.1)
    transform.Resample(coll, 'pg_dense', 1)
    pg = coll.variables['pg_dense']
    new_shape = pg.values.shape
    # Spacing (dx) is 10* larger when downsampled fro 10hz to 1hz
    new_auc = np.trapz(np.abs(pg.values.values.squeeze()), dx=1)

    # Shape from 10hz to 1hz
    assert new_shape[0] == old_shape[0] / 10

    # Assert that the auc is more or less the same (not exact, rounding error)
    # Values are around 0.25
    assert np.allclose(old_auc, new_auc, rtol=0.05)