Trees

All emdfile data classes inherit from the Node class, which adds core tree-building and metadata storage functionality. EMD trees must begin at an instance of the Root class. To define new classes which make use of the emdfile read and save methods, see the Node docstring. The Custom class enables composition of emdfile classes.

Node

class emdfile.classes.Node(name: str | None = 'node')

Base class for all EMD data object classes.

Nodes contain the machinery to create and modify filetree-like relations between themselves and other Node instances; to store arbitrary metadata; and to read/write themselves from/to HDF5 files.

add_to_tree(node)

Add node to the current tree as a child of this node. Note that if node has a root, this will error out - in this case, use either .graft or .force_add_to_tree instead.

cut(root_metadata=True)

Removes from the tree the branch beginning at this node, and returns it as a new tree. A new root is created at the base of the tree named ‘{old_root_name}_cut_{this_node_name}’.

Parameters:

root_metadata (True, False, or 'copy') – Specifies root metadata handling. If True, adds metadata from the original tree root to the new root. If False, adds no metadata. If ‘copy’, copies metadata from the old root to the new root.

Return type:

(Root) the new root node

force_add_to_tree(node)

Add node to the current tree as a child of this node, whether or not node has a root. If it has no root, performs a simple add. If has a root, performs a graft, excluding the root metadata from node. Note that this means the branch downstream of node will also be moved to the current tree.

classmethod from_h5(group)

Takes an h5py.Group which is open in read mode. Confirms that a Node of this name exists in this group, and loads and returns it with it’s metadata.

Parameters:

group (h5py Group)

Return type:

(Node)

get_from_tree(name)

Finds and returns the node from the current tree matching name, which must be a string with ‘/’ delimiters between ‘parent/child’ nodes. Search from the current node, or from the root node if name begins with ‘/’. So

>>> self.get_from_tree('x/y')

fetches node ‘y’ which is under node ‘x’ which is under the current node, and

>>> self.get_from_tree('/a/b')

fetches node ‘b’ which is under ‘a’ which is under the root node.

graft(node, merge_metadata=True)

Grafts a branch from one EMD tree onto another. The branch beginning at node is moved onto this tree underneath this node.

For the reverse - i.e. grafting from this tree to another tree - either use that tree’s .graft method, or use this tree’s ._graft.

Parameters:
  • node (Node)

  • merge_metadata (True, False, 'copy', or 'overwrite') – Specifies how root metadata should be treated. If True, adds the scion (incoming) root metadata to the stock (receiving) root, skipping entries that exist in both. If False, adds no metadata. If “overwrite”, entries existing in both scion and stock root metadata are overwritten. If “copy”, scion root metadata are copied to the stock root, skipping conflicts. If “copyover”, conflicting scion/stock roots are also copied and overwritten.

Return type:

(Root) this tree’s root node

static newnode(method)

Decorated methods must return a new node. After decoration the new node will be added to the parent node’s tree with a Metadata instance storing information about how the node was created, namely, the method’s name, the parent’s class and name, and all arguments passed to the method.

show_tree(root=False)

Display the object tree. If root is False, displays the branch of the tree downstream from this node, and if True, displays the full tree from the root node.

to_h5(group)

Creates a subgroup in group and writes this node into that group, including the group tags (emd_group_type, python_class), and the node’s metadata. No data beyond metadata and tags is written.

Parameters:

group (h5py Group)

Return type:

(h5py Group) the new node’s Group

tree(arg=None, **kwargs)

Usages -

>>> node.tree()                # show the tree downstream of this node
>>> node.tree(show=True)       # show the full tree from the root node
>>> node.tree(show=False)      # show from current node
>>> node.tree('path/to/node')  # return the node at the chosen location
>>> node.tree('/path/to/node') # specifiy the location starting from root
>>> node.tree(node)            # add a child node; must be a Node instance
>>> node.tree(cut=True)        # remove & return a branch; include root metadata
>>> node.tree(cut=False)       # discard root metadata
>>> node.tree(cut='copy')      # copy root metadata
>>> node.tree(graft=node)      # remove & graft a branch; add new root metadata
>>> node.tree(graft=(node,False))   # discard root metadata
>>> node.tree(graft=(node,'copy'))  # copy new root metadata
>>> node.tree(graft=(node,'overwrite'))  # add root metadata, overwrite conflicts
>>> node.tree(graft=(node,'copyover'))  # copy root metadata, overwrite conflicts

See the node.show, node.cut, node.graft, and node.add_to_tree docstrings for more info.

Root

class emdfile.classes.Root(name='root')

A Root is a Node with its .root property pointing to itself. EMD trees must begin with a Root, and tree building operations will raise an Exception if no root is present. Once a node is added to a tree, it can’t be removed or added to another tree unless a graft, cut, or force_add operation is used.

A Root instance’s metadata is considered associated with all nodes in its tree, and any write operation emanating from this tree will include its root metadata. Cut and merge node.tree include several handling options for root metadata - see those docstrings for details.

Custom

class emdfile.classes.Custom(name='custom')

The purpose of Custom nodes is to support creation of new classes by composition of emdfile classes. To do so, create a class inheriting from Custom, then add attributes which are emdfile classes, for instance

>>> class X(Custom):
>>>     def __init__(self, name, asize, bsize):
>>>         Custom.__init__(self, name=name)
>>>         self.a = Array(np.ones((asize)),'a')
>>>         self.b = Array(np.ones((bsize)),'b')

For some X instance x, on write x.a and x.b will be saved, along with any other attributes pointing to Node instances.

to_h5(group)

Calls Node.to_h5 to greate the group’s node and write its metadata. Then writes every attribute yielding a Node instance using that node’s .to_h5 method.

Parameters:

group (h5py Group)

Return type:

(h5py Group) the new node’s Group