node¶
Node and Link classes for use in markov graphs
Besides initializing Node ‘s, you will rarely need
to directly interact with these objects, as Graph provides
much easier and more powerful interactions.
-
class
blur.markov.node.Link(target, weight)¶ A one-way link pointing to a
Nodewith a weight.For use in conjunction with the
NodeandGraphclasses. You will rarely need to deal withLink‘s directly. The best way to create aLinkfrom oneNodeto another is by callingsome_node.add_link(another_node, 5)instead.Parameters: - target (Node) – The
NodethisLinkwill point to - weight (float or int) – The numerical weight for this
Link
- target (Node) – The
-
class
blur.markov.node.Node(value=None, self_destruct=False)¶ A node to be used in a Markov graph.
Parameters: - value (Any) – Value of the node
- self_destruct (bool) – whether this note deletes itself after being picked by a Graph
-
merge_links_from(other_node, merge_same_value_targets=False)¶ Merge links from another node with
self.link_list.Copy links from another node, merging when copied links point to a node which this already links to.
Parameters: - other_node (Node) – The node to merge links from
- merge_same_value_targets (bool) – Whether or not to merge links
whose targets have the same value (but are not necessarily
the same
Node). If False, links will only be merged whenlink_in_other.target == link_in_self.target. If True, links will be merged whenlink_in_other.target.value == link_in_self.target.value
Returns: None
Example
>>> node_1 = Node('One') >>> node_2 = Node('Two') >>> node_1.add_link(node_1, 1) >>> node_1.add_link(node_2, 3) >>> node_2.add_link(node_1, 4) >>> node_1.merge_links_from(node_2) >>> print(node_1) node.Node instance with value One with 2 links: 0: 5 --> One 1: 3 --> Two
-
find_link(target_node)¶ Find the link that points to
target_nodeif it exists.If no link in
selfpoints totarget_node, return NoneParameters: target_node (Node) – The node to look for in self.link_listReturns: Link – An existing link pointing to target_nodeif foundNone: If no such link exists
Example
>>> node_1 = Node('One') >>> node_2 = Node('Two') >>> node_1.add_link(node_2, 1) >>> link_1 = node_1.link_list[0] >>> found_link = node_1.find_link(node_2) >>> found_link == link_1 True
-
add_link(targets, weight)¶ Add link(s) pointing to
targets.If a link already exists pointing to a target, just add
weightto that link’s weightParameters: - targets (Node or list[Node]) – node or nodes to link to
- weight (int or float) – weight for the new link(s)
Returns: None
Example
>>> node_1 = Node('One') >>> node_2 = Node('Two') >>> node_1.add_link(node_2, 1) >>> new_link = node_1.link_list[0] >>> print(new_link) node.Link instance pointing to node with value "Two" with weight 1
-
add_link_to_self(source, weight)¶ Create and add a
Linkfrom a source node toself.Parameters: - source (Node) – The node that will own the new
Linkpointing toself - weight (int or float) – The weight of the newly created
Link
Returns: None
Example
>>> node_1 = Node('One') >>> node_2 = Node('Two') >>> node_1.add_link_to_self(node_2, 5) >>> new_link = node_2.link_list[0] >>> print('{} {}'.format(new_link.target.value, new_link.weight)) One 5 >>> print(new_link) node.Link instance pointing to node with value "One" with weight 5
- source (Node) – The node that will own the new
-
add_reciprocal_link(target, weight)¶ Add links pointing in either direction between
selfandtarget.This creates a
Linkfromselftotargetand aLinkfromtargettoselfof equal weight. Iftargetis a list ofNode‘s, repeat this for each one.Parameters: - target (Node or list[Node]) –
- weight (int or float) –
Returns: None
Example
>>> node_1 = Node('One') >>> node_2 = Node('Two') >>> node_1.add_reciprocal_link(node_2, 5) >>> new_link_1 = node_1.link_list[0] >>> new_link_2 = node_2.link_list[0] >>> print(new_link_1) node.Link instance pointing to node with value "Two" with weight 5 >>> print(new_link_2) node.Link instance pointing to node with value "One" with weight 5
-
remove_links_to_self()¶ Remove any link in
self.link_listwhosetargetisself.Returns: None
Example
>>> node_1 = Node('One') >>> node_1.add_link(node_1, 5) >>> node_1.remove_links_to_self() >>> len(node_1.link_list) 0
-
get_value()¶ Get the value of this
Node.For this class, this simply returns
self.value, but for subclasses with more complex behavior, this could be more powerful. For example, aNodemight have a value which is aSoftColor, in which case this method could return aSoftColor.get()value.Returns: Any
Example
>>> node_1 = Node('One') >>> node_1.get_value() 'One'