[7f9fb8]: / mne / _fiff / tree.py

Download this file

109 lines (90 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
from ..utils import logger, verbose
from .constants import FIFF
from .tag import read_tag
def dir_tree_find(tree, kind):
"""Find nodes of the given kind from a directory tree structure.
Parameters
----------
tree : dict
Directory tree.
kind : int
Kind to find.
Returns
-------
nodes : list
List of matching nodes.
"""
nodes = []
if isinstance(tree, list):
for t in tree:
nodes += dir_tree_find(t, kind)
else:
# Am I desirable myself?
if tree["block"] == kind:
nodes.append(tree)
# Search the subtrees
for child in tree["children"]:
nodes += dir_tree_find(child, kind)
return nodes
@verbose
def make_dir_tree(fid, directory, start=0, indent=0, verbose=None):
"""Create the directory tree structure."""
if directory[start].kind == FIFF.FIFF_BLOCK_START:
tag = read_tag(fid, directory[start].pos)
block = tag.data.item()
else:
block = 0
start_separate = False
this = start
tree = dict()
tree["block"] = block
tree["id"] = None
tree["parent_id"] = None
tree["nent"] = 0
tree["nchild"] = 0
tree["directory"] = directory[this]
tree["children"] = []
while this < len(directory):
if directory[this].kind == FIFF.FIFF_BLOCK_START:
if this != start:
if not start_separate:
start_separate = True
logger.debug(" " * indent + f"start {{ {block}")
child, this = make_dir_tree(fid, directory, this, indent + 1)
tree["nchild"] += 1
tree["children"].append(child)
elif directory[this].kind == FIFF.FIFF_BLOCK_END:
tag = read_tag(fid, directory[start].pos)
if tag.data == block:
break
else:
tree["nent"] += 1
if tree["nent"] == 1:
tree["directory"] = list()
tree["directory"].append(directory[this])
# Add the id information if available
if block == 0:
if directory[this].kind == FIFF.FIFF_FILE_ID:
tag = read_tag(fid, directory[this].pos)
tree["id"] = tag.data
else:
if directory[this].kind == FIFF.FIFF_BLOCK_ID:
tag = read_tag(fid, directory[this].pos)
tree["id"] = tag.data
elif directory[this].kind == FIFF.FIFF_PARENT_BLOCK_ID:
tag = read_tag(fid, directory[this].pos)
tree["parent_id"] = tag.data
this += 1
# Eliminate the empty directory
if tree["nent"] == 0:
tree["directory"] = None
content = f"block = {tree['block']} nent = {tree['nent']} nchild = {tree['nchild']}"
if start_separate:
logger.debug(" " * indent + f"end }} {content}")
else:
logger.debug(" " * indent + content)
last = this
return tree, last