コード例 #1
0
def test_first_level_models_from_bids():
    with InTemporaryDirectory():
        bids_path = _create_fake_bids_dataset(n_sub=10, n_ses=2,
                                             tasks=['localizer', 'main'],
                                             n_runs=[1, 3])
        # test arguments are provided correctly
        assert_raises(TypeError, first_level_models_from_bids, 2, 'main', 'MNI')
        assert_raises(ValueError, first_level_models_from_bids, 'lolo', 'main', 'MNI')
        assert_raises(TypeError, first_level_models_from_bids, bids_path, 2, 'MNI')
        assert_raises(TypeError, first_level_models_from_bids,
                      bids_path, 'main', 'MNI', model_init=[])
        # test output is as expected
        models, m_imgs, m_events, m_confounds = first_level_models_from_bids(
            bids_path, 'main', 'MNI', [('variant', 'some')])
        assert_true(len(models) == len(m_imgs))
        assert_true(len(models) == len(m_events))
        assert_true(len(models) == len(m_confounds))
        # test repeated run tag error when run tag is in filenames
        # can arise when variant or space is present and not specified
        assert_raises(ValueError, first_level_models_from_bids,
                      bids_path, 'main', 'T1w')  # variant not specified
        # test more than one ses file error when run tag is not in filenames
        # can arise when variant or space is present and not specified
        assert_raises(ValueError, first_level_models_from_bids,
                      bids_path, 'localizer', 'T1w')  # variant not specified
        # test issues with confound files. There should be only one confound
        # file per img. An one per image or None. Case when one is missing
        confound_files = get_bids_files(os.path.join(bids_path, 'derivatives'),
                                        file_tag='confounds')
        os.remove(confound_files[-1])
        assert_raises(ValueError, first_level_models_from_bids,
                      bids_path, 'main', 'MNI')
        # test issues with event files
        events_files = get_bids_files(bids_path, file_tag='events')
        os.remove(events_files[0])
        # one file missing
        assert_raises(ValueError, first_level_models_from_bids,
                      bids_path, 'main', 'MNI')
        for f in events_files[1:]:
            os.remove(f)
        # all files missing
        assert_raises(ValueError, first_level_models_from_bids,
                      bids_path, 'main', 'MNI')

        # In case different variant and spaces exist and are not selected we
        # fail and ask for more specific information
        shutil.rmtree(os.path.join(bids_path, 'derivatives'))
        # issue if no derivatives folder is present
        assert_raises(ValueError, first_level_models_from_bids,
                      bids_path, 'main', 'MNI')

        # check runs are not repeated when ses field is not used
        shutil.rmtree(bids_path)
        bids_path = _create_fake_bids_dataset(n_sub=10, n_ses=1,
                                             tasks=['localizer', 'main'],
                                             n_runs=[1, 3], no_session=True)
        # test repeated run tag error when run tag is in filenames and not ses
        # can arise when variant or space is present and not specified
        assert_raises(ValueError, first_level_models_from_bids,
                      bids_path, 'main', 'T1w')  # variant not specified
コード例 #2
0
def test_param_mask_deprecation_first_level_models_from_bids():
    deprecation_msg = (
        'The parameter "mask" will be removed in next release of Nistats. '
        'Please use the parameter "mask_img" instead.')
    mask_filepath = '~/masks/mask_01.nii.gz'

    with InTemporaryDirectory():
        bids_path = _create_fake_bids_dataset(n_sub=10,
                                              n_ses=2,
                                              tasks=['localizer', 'main'],
                                              n_runs=[1, 3])
        with warnings.catch_warnings(record=True) as raised_warnings:
            first_level_models_from_bids(bids_path,
                                         'main',
                                         'MNI', [('desc', 'preproc')],
                                         mask=mask_filepath)
            first_level_models_from_bids(bids_path,
                                         'main',
                                         'MNI', [('desc', 'preproc')],
                                         mask_img=mask_filepath)

    raised_param_deprecation_warnings = [
        raised_warning_ for raised_warning_ in raised_warnings
        if str(raised_warning_.message).startswith('The parameter')
    ]

    assert len(raised_param_deprecation_warnings) == 1
    for param_warning_ in raised_param_deprecation_warnings:
        assert str(param_warning_.message) == deprecation_msg
        assert param_warning_.category is DeprecationWarning
コード例 #3
0
ファイル: test_utils.py プロジェクト: jeromedockes/nistats
def test_get_bids_files():
    with InTemporaryDirectory():
        bids_path = _create_fake_bids_dataset(n_sub=10,
                                              n_ses=2,
                                              tasks=['localizer', 'main'],
                                              n_runs=[1, 3])
        # For each possible possible option of file selection we check
        # that we recover the appropriate amount of files, as included
        # in the fake bids dataset.

        # 250 files in total related to subject images. Top level files like
        # README not included
        selection = get_bids_files(bids_path)
        assert_true(len(selection) == 250)
        # 160 bold files expected. .nii and .json files
        selection = get_bids_files(bids_path, file_tag='bold')
        assert_true(len(selection) == 160)
        # Only 90 files are nii.gz. Bold and T1w files.
        selection = get_bids_files(bids_path, file_type='nii.gz')
        assert_true(len(selection) == 90)
        # Only 25 files correspond to subject 01
        selection = get_bids_files(bids_path, sub_label='01')
        assert_true(len(selection) == 25)
        # There are only 10 files in anat folders. One T1w per subject.
        selection = get_bids_files(bids_path, modality_folder='anat')
        assert_true(len(selection) == 10)
        # 20 files corresponding to run 1 of session 2 of main task.
        # 10 bold.nii.gz and 10 bold.json files. (10 subjects)
        filters = [('task', 'main'), ('run', '01'), ('ses', '02')]
        selection = get_bids_files(bids_path, file_tag='bold', filters=filters)
        assert_true(len(selection) == 20)
        # Get Top level folder files. Only 1 in this case, the README file.
        selection = get_bids_files(bids_path, sub_folder=False)
        assert_true(len(selection) == 1)
コード例 #4
0
def test_param_mask_deprecation_first_level_models_from_bids():
    deprecation_msg = (
        'The parameter "mask" will be removed in next release of Nistats. '
        'Please use the parameter "mask_img" instead.'
    )
    mask_filepath = '~/masks/mask_01.nii.gz'

    with InTemporaryDirectory():
        bids_path = _create_fake_bids_dataset(n_sub=10, n_ses=2,
                                              tasks=['localizer', 'main'],
                                              n_runs=[1, 3])
        with warnings.catch_warnings(record=True) as raised_warnings:
            first_level_models_from_bids(
                    bids_path, 'main', 'MNI', [('variant', 'some')],
                    mask=mask_filepath)
            first_level_models_from_bids(
                    bids_path, 'main', 'MNI', [('variant', 'some')],
                    mask_img=mask_filepath)

    raised_param_deprecation_warnings = [
        raised_warning_ for raised_warning_
        in raised_warnings if
        str(raised_warning_.message).startswith('The parameter')
        ]

    assert len(raised_param_deprecation_warnings) == 1
    for param_warning_ in raised_param_deprecation_warnings:
        assert str(param_warning_.message) == deprecation_msg
        assert param_warning_.category is DeprecationWarning
コード例 #5
0
def test_first_level_models_from_bids():
    with InTemporaryDirectory():
        bids_path = _create_fake_bids_dataset(n_sub=10,
                                              n_ses=2,
                                              tasks=['localizer', 'main'],
                                              n_runs=[1, 3])
        # test arguments are provided correctly
        with pytest.raises(TypeError):
            first_level_models_from_bids(2, 'main', 'MNI')
        with pytest.raises(ValueError):
            first_level_models_from_bids('lolo', 'main', 'MNI')
        with pytest.raises(TypeError):
            first_level_models_from_bids(bids_path, 2, 'MNI')
        with pytest.raises(TypeError):
            first_level_models_from_bids(bids_path,
                                         'main',
                                         'MNI',
                                         model_init=[])
        # test output is as expected
        models, m_imgs, m_events, m_confounds = first_level_models_from_bids(
            bids_path, 'main', 'MNI', [('desc', 'preproc')])
        assert len(models) == len(m_imgs)
        assert len(models) == len(m_events)
        assert len(models) == len(m_confounds)
        # test repeated run tag error when run tag is in filenames
        # can arise when desc or space is present and not specified
        with pytest.raises(ValueError):
            first_level_models_from_bids(bids_path, 'main',
                                         'T1w')  # desc not specified
        # test more than one ses file error when run tag is not in filenames
        # can arise when desc or space is present and not specified
        with pytest.raises(ValueError):
            first_level_models_from_bids(bids_path, 'localizer',
                                         'T1w')  # desc not specified
        # test issues with confound files. There should be only one confound
        # file per img. An one per image or None. Case when one is missing
        confound_files = get_bids_files(os.path.join(bids_path, 'derivatives'),
                                        file_tag='desc-confounds_regressors')
        os.remove(confound_files[-1])
        with pytest.raises(ValueError):
            first_level_models_from_bids(bids_path, 'main', 'MNI')
        # test issues with event files
        events_files = get_bids_files(bids_path, file_tag='events')
        os.remove(events_files[0])
        # one file missing
        with pytest.raises(ValueError):
            first_level_models_from_bids(bids_path, 'main', 'MNI')
        for f in events_files[1:]:
            os.remove(f)
        # all files missing
        with pytest.raises(ValueError):
            first_level_models_from_bids(bids_path, 'main', 'MNI')

        # In case different desc and spaces exist and are not selected we
        # fail and ask for more specific information
        shutil.rmtree(os.path.join(bids_path, 'derivatives'))
        # issue if no derivatives folder is present
        with pytest.raises(ValueError):
            first_level_models_from_bids(bids_path, 'main', 'MNI')

        # check runs are not repeated when ses field is not used
        shutil.rmtree(bids_path)
        bids_path = _create_fake_bids_dataset(n_sub=10,
                                              n_ses=1,
                                              tasks=['localizer', 'main'],
                                              n_runs=[1, 3],
                                              no_session=True)
        # test repeated run tag error when run tag is in filenames and not ses
        # can arise when desc or space is present and not specified
        with pytest.raises(ValueError):
            first_level_models_from_bids(bids_path, 'main',
                                         'T1w')  # desc not specified