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
Example #2
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
Example #3
0
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)