Beispiel #1
0
 def test_named_tuple(self, file):
     file.tags = {'attrA': Tag(None, 'apple'), 'attrB': Tag(None, 'banana')}
     tup = file.as_named_tuple()
     assert(tup.filename == file.path)
     assert isinstance(tup, tuple)
     assert not hasattr(tup, 'task')
     assert tup.attrA == 'apple'
Beispiel #2
0
    def test_build_path(self, writable_file):
        writable_file.tags = {
            'task': Tag(None, 'rest'),
            'run': Tag(None, '2'),
            'subject': Tag(None, '3')
        }

        # Single simple pattern
        with pytest.raises(TypeError):
            build_path(writable_file.entities)
        pat = join(writable_file.dirname,
                   '{task}/sub-{subject}/run-{run}.nii.gz')
        target = join(writable_file.dirname, 'rest/sub-3/run-2.nii.gz')
        assert build_path(writable_file.entities, pat) == target

        # Multiple simple patterns
        pats = [
            '{session}/{task}/r-{run}.nii.gz',
            't-{task}/{subject}-{run}.nii.gz', '{subject}/{task}.nii.gz'
        ]
        pats = [join(writable_file.dirname, p) for p in pats]
        target = join(writable_file.dirname, 't-rest/3-2.nii.gz')
        assert build_path(writable_file.entities, pats) == target

        # Pattern with optional entity
        pats = [
            '[{session}/]{task}/r-{run}.nii.gz',
            't-{task}/{subject}-{run}.nii.gz'
        ]
        pats = [join(writable_file.dirname, p) for p in pats]
        target = join(writable_file.dirname, 'rest/r-2.nii.gz')
        assert build_path(writable_file.entities, pats) == target

        # Pattern with conditional values
        pats = [
            '{task<func|acq>}/r-{run}.nii.gz',
            't-{task}/{subject}-{run}.nii.gz'
        ]
        pats = [join(writable_file.dirname, p) for p in pats]
        target = join(writable_file.dirname, 't-rest/3-2.nii.gz')
        assert build_path(writable_file.entities, pats) == target

        # Pattern with valid conditional values
        pats = [
            '{task<func|rest>}/r-{run}.nii.gz',
            't-{task}/{subject}-{run}.nii.gz'
        ]
        pats = [join(writable_file.dirname, p) for p in pats]
        target = join(writable_file.dirname, 'rest/r-2.nii.gz')
        assert build_path(writable_file.entities, pats) == target

        # Pattern with optional entity with conditional values
        pats = [
            '[{task<func|acq>}/]r-{run}.nii.gz',
            't-{task}/{subject}-{run}.nii.gz'
        ]
        pats = [join(writable_file.dirname, p) for p in pats]
        target = join(writable_file.dirname, 'r-2.nii.gz')
        assert build_path(writable_file.entities, pats) == target
Beispiel #3
0
    def test_build_file(self, writable_file, tmpdir, caplog):
        writable_file.tags = {
            'task': Tag(None, 'rest'),
            'run': Tag(None, '2'),
            'subject': Tag(None, '3')
        }

        # Simple write out
        new_dir = join(writable_file.dirname, 'rest')
        pat = join(writable_file.dirname,
                   '{task}/sub-{subject}/run-{run}.nii.gz')
        target = join(writable_file.dirname, 'rest/sub-3/run-2.nii.gz')
        writable_file.copy(pat)
        assert exists(target)

        # Conflict handling
        with pytest.raises(ValueError):
            writable_file.copy(pat)
        with pytest.raises(ValueError):
            writable_file.copy(pat, conflicts='fail')
        writable_file.copy(pat, conflicts='skip')
        log_message = caplog.records[0].message
        assert log_message == 'A file at path {} already exists, ' \
                              'skipping writing file.'.format(target)
        writable_file.copy(pat, conflicts='append')
        append_target = join(writable_file.dirname,
                             'rest/sub-3/run-2_1.nii.gz')
        assert exists(append_target)
        writable_file.copy(pat, conflicts='overwrite')
        assert exists(target)
        shutil.rmtree(new_dir)

        # Symbolic linking
        writable_file.copy(pat, symbolic_link=True)
        assert islink(target)
        shutil.rmtree(new_dir)

        # Using different root
        root = str(tmpdir.mkdir('tmp2'))
        pat = join(root, '{task}/sub-{subject}/run-{run}.nii.gz')
        target = join(root, 'rest/sub-3/run-2.nii.gz')
        writable_file.copy(pat, root=root)
        assert exists(target)

        # Copy into directory functionality
        pat = join(writable_file.dirname, '{task}/')
        writable_file.copy(pat)
        target = join(writable_file.dirname, 'rest', writable_file.filename)
        assert exists(target)
        shutil.rmtree(new_dir)
Beispiel #4
0
 def test_matches(self, file):
     assert file._matches()
     assert file._matches(extensions='nii.gz')
     assert not file._matches(extensions=['.txt', '.rtf'])
     file.tags = {'task': Tag(None, 'rest'), 'run': Tag(None, '2')}
     assert file._matches(entities={'task': 'rest', 'run': 2})
     assert not file._matches(entities={'task': 'rest', 'run': 4})
     assert not file._matches(entities={'task': 'st'})
     assert file._matches(entities={'task': 'st'}, regex_search=True)
     # With list of matches
     assert not file._matches(entities={'task': ['A', 'B', 'C']})
     assert file._matches(entities={'task': ['A', 'B', 'rest']})
     assert file._matches(entities={'task': ['A', 'B', 'st']},
                          regex_search=True)
Beispiel #5
0
 def test_named_tuple_with_reserved_name(self, file):
     file = copy(file)
     file.tags['class'] = Tag(None, 'invalid')
     with pytest.warns(UserWarning) as w:
         res = file.as_named_tuple()
         assert w[0].message.args[0].startswith('Entity names cannot')
         assert hasattr(res, 'class_')
         assert not hasattr(res, 'class')