bacalhau.topic_tree.TopicTree

class bacalhau.topic_tree.TopicTree(data=None, **attr)[source]

Bases: networkx.classes.digraph.DiGraph

Represents a TopicTree. Extends networkx.DiGraph.

Creates a new TopicTree.

Parameters:
  • data (list, TopicTree or any networkx graph object.) – data to initialize the tree with. If no data is supplied an empty tree is created.
  • attr (key/value pairs.) – keyword arguments to add to the tree.
_eliminate_child_with_parent_name(node)[source]

Eliminate a child node whose name appears within the parent’s name.

Parameters:node (str.) – name of node to process.
_eliminate_parents(node, min_children)[source]

Recursively eliminates a parent of the current node that has fewer than min_children children, unless the parent is the root.

Parameters:
  • node (str.) – name of node to process.
  • min_children (int.) – minimum number of children that a parent should have
compress(min_children=2)[source]

Compresses the tree based on the castanet algorithm: 1. starting from the leaves, recursively eliminate a parent that has fewer than min_children, unless the parent is the root; 2. eliminate a child whose name appears within the parent’s name.

Parameters:min_children (int.) – minimum number of children that a parent should have, defaults to 2.
prune(nodes)[source]

Removes the given nodes from the tree.

Parameters:nodes (list of str.) – names of the nodes to be remove from the tree.
render(filepath, format='svg', prog='dot', attributes={})[source]

Renders the tree into the file at filepath.

filepath may also be a File-like object.

to_json(filepath)[source]

Serializes the TopicTree to JSON Graph format and writes it to a file.

filepath is a file path or File-like object.