opto.trace.nodes.AbstractNode#
- class AbstractNode[source]#
AbstractNode represents an abstract data node in a directed graph.
Notes
The AbstractNode class is meant to be subclassed and extended to create specific types of nodes. The node can have multiple parents and children, forming a directed graph structure. The node has a name, which is used to identify it within the graph. The py_name attribute is the same as the name attribute, but with the “:” character removed.
The node can be initialized with a value, an optional name, and an optional trainable flag. If the value is an instance of the Node class, the node will be initialized as a reference to that node, otherwise, the value will be stored directly in the node. The default name is generated based on the type of the value and a version number which serves as the identifier, separated by “:”.
The AbstractNode class provides several properties to access its attributes. The data property allows access to the stored data. If the node is being traced within a context, the data property adds the node to the list of nodes used in that context. The parents property returns a list of parent nodes, and the children property returns a list of child nodes. The name property returns the name of the node, and the py_name property returns the name without the “:” character. The id property returns the version number/identifier extracted from the name. The level property returns the level of the node in the DAG. The is_root property returns True if the node has no parents, and the is_leaf property returns True if the node has no children.
The AbstractNode class also provides internal methods to add parents and children to the node. The _add_child method adds a child node to the node’s list of children. The _add_parent method adds a parent node to the node’s list of parents and updates the level of the node based on the parent’s level.
The AbstractNode class overrides the __str__ method to provide a string representation of the node. The representation includes the name, the type of the data, and the data itself. The AbstractNode class implements the __deepcopy__ method to create a deep copy of the node. This allows the node to be detached from the original graph. The AbstractNode class provides comparison methods lt and gt to compare the levels of two nodes in the DAG.
Initialize an instance of the AbstractNode class.
- Parameters:
value – The value to be assigned to the node.
name (str, optional) – The name of the node. Defaults to None.
trainable (bool, optional) – Whether the node is trainable or not. Defaults to False.
Notes
During initialization, this function generates a default name for the node based on the type of the value parameter. If the name parameter is provided, it is appended to the default name. The format of the name is “type:version”, where the version is set to 0 if no name is provided. If the value parameter is an instance of the Node class, the _data attribute of the current node is set to the _data attribute of the value parameter, and the _name attribute is set to the _name attribute of the value parameter if no name is provided. Otherwise, the _data attribute is set to the value parameter itself, and the _name attribute is set to the default name. Finally, the function calls the register function of the GRAPH object to register the current node in the graph.
Attributes
Get the children of a node.
Retrieve the internal data of a node.
Get the identifier part of the node's name.
Check if the node is a leaf node.
Check if the node is a root node.
Get the level of the node in the graph.
Get the name of the node.
Get the parents of a node.
Get the Python-friendly name of the node.
Methods
gt
(other)Compare if this node's level is greater than another node's level.
lt
(other)Compare if this node's level is less than another node's level.
- __init__(value, *, name=None, trainable=False) None [source]#
Initialize an instance of the AbstractNode class.
- Parameters:
value – The value to be assigned to the node.
name (str, optional) – The name of the node. Defaults to None.
trainable (bool, optional) – Whether the node is trainable or not. Defaults to False.
Notes
During initialization, this function generates a default name for the node based on the type of the value parameter. If the name parameter is provided, it is appended to the default name. The format of the name is “type:version”, where the version is set to 0 if no name is provided. If the value parameter is an instance of the Node class, the _data attribute of the current node is set to the _data attribute of the value parameter, and the _name attribute is set to the _name attribute of the value parameter if no name is provided. Otherwise, the _data attribute is set to the value parameter itself, and the _name attribute is set to the default name. Finally, the function calls the register function of the GRAPH object to register the current node in the graph.
- __new__(**kwargs)#