Example #1
0
def test_parse_file_entities_from_layout(layout_synthetic):
    layout = layout_synthetic
    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',
        'extension': 'nii.gz'
    }
    assert target == layout.parse_file_entities(filename, config='bids')
    config = Config.load('bids')
    assert target == layout.parse_file_entities(filename, config=[config])
    assert target == layout.parse_file_entities(filename, scope='raw')

    # Test with default scope--i.e., everything
    target = {
        'subject': '03',
        'session': '07',
        'run': 4,
        'suffix': 'sekret',
        'desc': 'bleargh',
        'extension': 'nii.gz'
    }
    assert target == layout.parse_file_entities(filename)
    # Test with only the fmriprep pipeline (which includes both configs)
    assert target == layout.parse_file_entities(filename, scope='fmriprep')
    assert target == layout.parse_file_entities(filename, scope='derivatives')

    # Test with only the derivative config
    target = {'desc': 'bleargh'}
    assert target == layout.parse_file_entities(filename, config='derivatives')
Example #2
0
def test_load_existing_config():
    session = create_session()
    first = Config('dummy')
    session.add(first)
    session.commit()

    second = Config.load({"name": "dummy"}, session=session)
    assert first == second
    session.add(second)
    session.commit()

    from sqlalchemy.orm.exc import FlushError
    with pytest.raises(FlushError):
        second = Config.load({"name": "dummy"})
        session.add(second)
        session.commit()
Example #3
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',
              'extension': 'nii.gz'}
    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', 'extension': 'nii.gz'}
    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)
Example #4
0
def test_add_config_paths():
    bids_dir = dirname(bids.__file__)
    bids_json = os.path.join(bids_dir, 'layout', 'config', 'bids.json')
    with pytest.raises(ValueError) as exc:
        add_config_paths(test_config1='nonexistentpath.json')
    assert str(exc.value).startswith('Configuration file')
    with pytest.raises(ValueError) as exc:
        add_config_paths(bids=bids_json)
    assert str(exc.value).startswith("Configuration 'bids' already")
    add_config_paths(dummy=bids_json)
    config = Config.load('dummy')
    assert 'subject' in config.entities
Example #5
0
def test_config_init_with_args():
    session = create_session()
    ents = [{
        "name": "task",
        "pattern": "[_/\\\\]task-([a-zA-Z0-9]+)"
    }, {
        "name": "acquisition",
        "pattern": "[_/\\\\]acq-([a-zA-Z0-9]+)"
    }]
    patterns = ['this_will_never_match_anything', 'and_neither_will_this']
    config = Config('custom', entities=ents, default_path_patterns=patterns)
    assert config.name == 'custom'
    target = {'task', 'acquisition'}
    assert set(ent.name for ent in config.entities.values()) == target
    assert config.default_path_patterns == patterns
Example #6
0
def loadBidsEntities() -> dict:
    """
    Loads all accepted BIDS entities from PyBids into a dictionary.

    Returns:
        A dictionary mapping the entity names to the PyBids Entity object
            containing information about that entity.
    """
    # PyBids uses its own, internal bids.json to configure what entities it
    # accepts and what form they take. A custom config could be specified with a
    # full path name, but using just 'bids' will direct the PyBids Config class
    # to get the bids.json from its own internal package.
    BIDS_DEFAULT_CONFIG_NAME = 'bids'
    BIDS_DERIVATIES_CONFIG_NAME = 'derivatives'

    entities = {}
    for configName in [BIDS_DEFAULT_CONFIG_NAME, BIDS_DERIVATIES_CONFIG_NAME]:
        entities.update(BidsConfig.load(configName).entities)

    return entities
Example #7
0
def test_parse_file_entities_from_layout_cached_db_replay(
        layout_synthetic_cached_db_replay):
    """
    We need to ensure that after caching the database requesting cached values
    provides all the original values.
    :param layout_synthetic_cached_db_replay:
    """
    layout = layout_synthetic_cached_db_replay
    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',
        'extension': 'nii.gz'
    }
    assert target == layout.parse_file_entities(filename, config='bids')
    config = Config.load('bids')
    assert target == layout.parse_file_entities(filename, config=[config])
    assert target == layout.parse_file_entities(filename, scope='raw')

    # Test with default scope--i.e., everything
    target = {
        'subject': '03',
        'session': '07',
        'run': 4,
        'suffix': 'sekret',
        'desc': 'bleargh',
        'extension': 'nii.gz'
    }
    assert target == layout.parse_file_entities(filename)
    # Test with only the fmriprep pipeline (which includes both configs)
    assert target == layout.parse_file_entities(filename, scope='fmriprep')
    assert target == layout.parse_file_entities(filename, scope='derivatives')

    # Test with only the derivative config
    target = {'desc': 'bleargh'}
    assert target == layout.parse_file_entities(filename, config='derivatives')