def fold_dict(data, nb_level=10**5):
    """Fold dict `data`.

    Turns dictionary keys, e.g. 'level1/level2/level3', into sub-dicts, e.g.
    data['level1']['level2']['level3'].

    Parameters
    ----------
    data: dict
        dict to be folded.
    nb_level: int
        Maximum recursion depth.

    Returns
    -------
    dict
        Folded dict.
    """
    if nb_level <= 0:
        return data

    groups = dict()
    levels = set()
    for key, value in data.items():
        idx = key.find('/')
        if idx > 0:
            level = key[:idx]
            group_dict = groups.setdefault(level, dict())
            group_dict[key[(idx + 1):]] = value
            levels.add(level)
        else:
            groups[key] = value
    for level in levels:
        groups[level] = fold_dict(groups[level], nb_level - 1)
    return groups