Aluode/PerceptionLabPortable
0
1# Authors: The MNE-Python contributors.2# License: BSD-3-Clause3# Copyright the MNE-Python contributors.4 5 6from ..utils import logger, verbose7from .constants import FIFF8from .tag import read_tag9 10 11def dir_tree_find(tree, kind):12 """Find nodes of the given kind from a directory tree structure.13 14 Parameters15 ----------16 tree : dict17 Directory tree.18 kind : int19 Kind to find.20 21 Returns22 -------23 nodes : list24 List of matching nodes.25 """26 nodes = []27 28 if isinstance(tree, list):29 for t in tree:30 nodes += dir_tree_find(t, kind)31 else:32 # Am I desirable myself?33 if tree["block"] == kind:34 nodes.append(tree)35 36 # Search the subtrees37 for child in tree["children"]:38 nodes += dir_tree_find(child, kind)39 return nodes40 41 42@verbose43def make_dir_tree(fid, directory, start=0, indent=0, verbose=None):44 """Create the directory tree structure."""45 if directory[start].kind == FIFF.FIFF_BLOCK_START:46 tag = read_tag(fid, directory[start].pos)47 block = tag.data.item()48 else:49 block = 050 51 start_separate = False52 53 this = start54 55 tree = dict()56 tree["block"] = block57 tree["id"] = None58 tree["parent_id"] = None59 tree["nent"] = 060 tree["nchild"] = 061 tree["directory"] = directory[this]62 tree["children"] = []63 64 while this < len(directory):65 if directory[this].kind == FIFF.FIFF_BLOCK_START:66 if this != start:67 if not start_separate:68 start_separate = True69 logger.debug(" " * indent + f"start {{ {block}")70 child, this = make_dir_tree(fid, directory, this, indent + 1)71 tree["nchild"] += 172 tree["children"].append(child)73 elif directory[this].kind == FIFF.FIFF_BLOCK_END:74 tag = read_tag(fid, directory[start].pos)75 if tag.data == block:76 break77 else:78 tree["nent"] += 179 if tree["nent"] == 1:80 tree["directory"] = list()81 tree["directory"].append(directory[this])82 83 # Add the id information if available84 if block == 0:85 if directory[this].kind == FIFF.FIFF_FILE_ID:86 tag = read_tag(fid, directory[this].pos)87 tree["id"] = tag.data88 else:89 if directory[this].kind == FIFF.FIFF_BLOCK_ID:90 tag = read_tag(fid, directory[this].pos)91 tree["id"] = tag.data92 elif directory[this].kind == FIFF.FIFF_PARENT_BLOCK_ID:93 tag = read_tag(fid, directory[this].pos)94 tree["parent_id"] = tag.data95 96 this += 197 98 # Eliminate the empty directory99 if tree["nent"] == 0:100 tree["directory"] = None101 102 content = f"block = {tree['block']} nent = {tree['nent']} nchild = {tree['nchild']}"103 if start_separate:104 logger.debug(" " * indent + f"end }} {content}")105 else:106 logger.debug(" " * indent + content)107 last = this108 return tree, last109 