phylox.base

Functions

find_unused_node(network[, exclude])

Find an unused node in the network.

remove_unlabeled_leaves(network[, inplace])

Iteratively removes unlabeled leaves until none are left, then suppresses all degree-2 nodes.

suppress_node(network, node)

Suppresses a degree-2 node node and returns true if successful.

phylox.base.find_unused_node(network, exclude=[])

Find an unused node in the network.

Parameters:
  • network (networkx.DiGraph) – The network to find an unused node in.

  • exclude (list) – A list of additional nodes to exclude from the search.

Returns:

The unused node.

Return type:

int

Examples

>>> import networkx as nx
>>> import phylox
>>> network = nx.DiGraph()
>>> network.add_edges_from([(0, 1), (1, 2), (2, 3)])
>>> phylox.find_unused_node(network)
-1
>>> phylox.find_unused_node(network, exclude=[-1])
-2
phylox.base.suppress_node(network, node)

Suppresses a degree-2 node node and returns true if successful. The new arc has length length(p,node)+length(node,c). Returns false if node is not a degree-2 node.

Parameters:
  • network (phylox.DiNetwork) – The network to suppress a node in.

  • node (str or int) – The node to suppress.

Returns:

True if the node was suppressed, False otherwise.

Return type:

bool

Examples

>>> from phylox import DiNetwork
>>> network = DiNetwork()
>>> network.add_edges_from([(0, 1, {'length': 1}), (1, 2, {'length': 1})])
>>> suppress_node(network, 1)
True
>>> network.edges(data=True)
OutEdgeDataView([(0, 2, {'length': 2})])
phylox.base.remove_unlabeled_leaves(network, inplace=True)

Iteratively removes unlabeled leaves until none are left, then suppresses all degree-2 nodes.

Parameters:
  • network (phylox.DiNetwork) – The network to remove unlabeled leaves from.

  • inplace (bool) – Whether to modify the network in place or return a copy.

Returns:

The network with unlabeled leaves removed.

Return type:

phylox.DiNetwork

Examples

>>> from phylox import DiNetwork
>>> from phylox.constants import LABEL_ATTR
>>> network = DiNetwork(
...     edges=[(0, 1), (1, 2), (1, 3), (3, 4), (3, 5)],
...     labels=[(2, 'a'), (4, 'b')],
... )
>>> network = remove_unlabeled_leaves(network)
>>> network.edges()
OutEdgeView([(0, 1), (1, 2), (1, 4)])
>>> network.nodes(data=True)
NodeDataView({0: {}, 1: {}, 2: {'label': 'a'}, 4: {'label': 'b'}})