Ejemplo n.º 1
0
def test_parse_file_entities():
    filename = '/sub-03_ses-07_run-4_desc-bleargh_sekret.nii.gz'

    # Test with entities taken from bids config
    target = {'subject': '03', 'session': '07', 'run': 4, 'suffix': 'sekret'}
    assert target == parse_file_entities(filename, config='bids')
    config = Config.load('bids')
    assert target == parse_file_entities(filename, config=[config])

    # Test with entities taken from bids and derivatives config
    target = {
        'subject': '03',
        'session': '07',
        'run': 4,
        'suffix': 'sekret',
        'desc': 'bleargh'
    }
    assert target == parse_file_entities(filename)
    assert target == parse_file_entities(filename,
                                         config=['bids', 'derivatives'])

    # Test with list of Entities
    entities = [
        Entity('subject', "[/\\\\]sub-([a-zA-Z0-9]+)"),
        Entity('run', "[_/\\\\]run-0*(\\d+)", dtype=int),
        Entity('suffix', "[._]*([a-zA-Z0-9]*?)\\.[^/\\\\]+$"),
        Entity('desc', "desc-([a-zA-Z0-9]+)"),
    ]
    # Leave out session to distinguish from previous test target
    target = {'subject': '03', 'run': 4, 'suffix': 'sekret', 'desc': 'bleargh'}
    assert target == parse_file_entities(filename, entities=entities)
Ejemplo n.º 2
0
def test_entity_matches(tmpdir):
    filename = "aardvark-4-reporting-for-duty.txt"
    tmpdir.mkdir("tmp").join(filename).write("###")
    f = BIDSFile(os.path.join(str(tmpdir), filename))
    e = Entity('avaricious', r'aardvark-(\d+)')
    result = e.match_file(f)
    assert result == '4'
Ejemplo n.º 3
0
def test_entity_matches(tmpdir):
    filename = "aardvark-4-reporting-for-duty.txt"
    tmpdir.mkdir("tmp").join(filename).write("###")
    f = BIDSFile(os.path.join(str(tmpdir), filename))
    e = Entity('avaricious', r'aardvark-(\d+)')
    result = e.match_file(f)
    assert result == '4'
Ejemplo n.º 4
0
def test_entity_unique_and_count():
    e = Entity('prop', r'-(\d+)')
    e.files = {
        'test1-10.txt': '10',
        'test2-7.txt': '7',
        'test3-7.txt': '7'
    }
    assert sorted(e.unique()) == ['10', '7']
    assert e.count() == 2
    assert e.count(files=True) == 3
Ejemplo n.º 5
0
def test_entity_init_minimal():
    e = Entity('avaricious', r'aardvark-(\d+)')
    assert e.name == 'avaricious'
    assert e.pattern == r'aardvark-(\d+)'
    assert not e.mandatory
    assert e.directory is None
    assert e.files == {}
Ejemplo n.º 6
0
def subject_entity():
    return Entity('subject',
                  "[/\\\\]sub-([a-zA-Z0-9]+)",
                  False,
                  "{{root}}{subject}",
                  None,
                  bleargh=True)
Ejemplo n.º 7
0
def test_entity_add_file():
    e = Entity('prop', r'-(\d+)')
    e.add_file('a', '1')
    assert e.files['a'] == '1'
Ejemplo n.º 8
0
def test_entity_matches_with_map_func(sample_bidsfile):
    bf = sample_bidsfile
    e = Entity('test', map_func=lambda x: x.filename.split('-')[1])
    assert e.match_file(bf) == '03_ses'
Ejemplo n.º 9
0
def test_entity_init_with_bad_dtype():
    with pytest.raises(ValueError) as exc:
        ent = Entity('test', dtype='superfloat')
        msg = exc.value.message
        assert msg.startswith("Invalid dtype")
Ejemplo n.º 10
0
def test_entity_add_file():
    e = Entity('prop', r'-(\d+)')
    e.add_file('a', '1')
    assert e.files['a'] == '1'
Ejemplo n.º 11
0
def test_entity_unique_and_count():
    e = Entity('prop', r'-(\d+)')
    e.files = {'test1-10.txt': '10', 'test2-7.txt': '7', 'test3-7.txt': '7'}
    assert sorted(e.unique()) == ['10', '7']
    assert e.count() == 2
    assert e.count(files=True) == 3
Ejemplo n.º 12
0
def test_entity_matches_with_map_func(sample_bidsfile):
    bf = sample_bidsfile
    e = Entity('test', map_func=lambda x: x.filename.split('-')[1])
    assert e.match_file(bf) == '03_ses'