Example #1
0
def groupby_pattern(
        crumb: hansel.Crumb, arg_name: str,
        groups: Dict[str,
                     List[hansel.Crumb]]) -> Dict[str, List[hansel.Crumb]]:
    """Return a dictionary with the matches of `groups` values in the
    crumb argument `arg_name` in `crumb`.

    Parameters
    ----------
    crumb: Crumb
        Crumb to the folder tree.

    arg_name: str
        Name of the crumb argument in `crumb` that must be matched with the
        values of the `groups` dict.

    groups: dict[str]->str
        A dict where the keys are group names and the values are regular
        expressions (fnmatch xor re).

    Returns
    -------
    grouped: dict[str] -> list[Crumb]
        Map of paths from groups to the corresponding path matches.
    """
    if arg_name not in crumb:
        raise KeyError('Crumb {} has no argument {}.'.format(crumb, arg_name))

    paths_matched = set()
    mods = defaultdict(list)
    for mod_name, pattern in groups.items():
        crumb.set_pattern(arg_name, pattern)
        paths = crumb.ls(arg_name)
        if paths:
            mods[mod_name] = paths
            paths_matched = paths_matched.union(paths)

        crumb.clear_pattern(arg_name)

    return mods
Example #2
0
def test_regex_replace2(tmp_crumb):
    assert not op.exists(tmp_crumb.path)

    values_dict = {'session_id': ['session_{:02}'.format(i) for i in range(  2)],
                   'subject_id': ['subj_{:03}'.format(i)    for i in range(100)],
                   'modality':   ['anat'],
                   'image':      ['mprage1.nii'],
                   }

    _ = mktree(tmp_crumb, list(ParameterGrid(values_dict)))

    # a crumb with the pattern
    crumb = Crumb(tmp_crumb.path.replace('{subject_id}', '{subject_id:subj_02*}'),
                  regex='fnmatch')  # fnmatch

    # a crumb without the pattern, the pattern is added later
    crumb2 = Crumb(tmp_crumb.path, regex='fnmatch')

    crumb2.set_pattern('subject_id', 'subj_02*')
    assert crumb['subject_id'] == crumb2['subject_id']

    crumb2.clear_pattern('subject_id')
    assert tmp_crumb['subject_id'] == crumb2['subject_id']
Example #3
0
def test_regex_replace2(tmp_crumb):
    assert not os.path.exists(tmp_crumb.path)

    values_dict = {
        'session_id': ['session_{:02}'.format(i) for i in range(2)],
        'subject_id': ['subj_{:03}'.format(i) for i in range(100)],
        'modality': ['anat'],
        'image': ['mprage1.nii'],
    }

    mktree(tmp_crumb, list(ParameterGrid(values_dict)))

    # a crumb with the pattern
    crumb = Crumb(tmp_crumb.path.replace('{subject_id}', '{subject_id:subj_02*}'),
                  regex='fnmatch')  # fnmatch

    # a crumb without the pattern, the pattern is added later
    crumb2 = Crumb(tmp_crumb.path, regex='fnmatch')

    crumb2.set_pattern('subject_id', 'subj_02*')
    assert crumb['subject_id'] == crumb2['subject_id']

    crumb2.clear_pattern('subject_id')
    assert tmp_crumb['subject_id'] == crumb2['subject_id']