finesse.utilities.graph module

Functions to aid manipulation of networkx graphs as well as some graph related utilities.

finesse.utilities.graph.class_graph(cls, G=None)[source]

Creates a directed graph from a class and all of its base classes.

object is ignored because every class derives from object and so it just clutters up the graph.

finesse.utilities.graph.class_graph_from_module_graph(module_graph, G=None)[source]

Creates a directed graph for all the classes and base classes in the module graph.

finesse.utilities.graph.copy_graph(G)[source]

A trick I often see in networkx’s codebase to copy a graph.

type(G) returns the class of G (e.g. nx.DiGraph) which can accept a graph to make a copy of. Useful for writing ‘pure’ functions on graphs.

finesse.utilities.graph.default_key(d, key, default=None)[source]
finesse.utilities.graph.flip_dict(dd)[source]

Swap kay-value in a dictionary.

finesse.utilities.graph.get_orphan_nodes(G)[source]

An orphan node is a node with no edges.

finesse.utilities.graph.get_sink_nodes(G)[source]

A sink node is a node with no outgoing edges.

finesse.utilities.graph.get_source_nodes(G)[source]

A source node is a node with no incoming edges.

finesse.utilities.graph.module_graph(module, G=None, external_modules=False, include_root_module=False)[source]

Creates a directed graph from a module and all of its submodules. External submodules can be included in graph but they will not be traversed since that could cause the graph to walk an absurd number of packages.

The root module is typically not included since it tends to clutter the graph.

finesse.utilities.graph.remove_orphans(G, inplace=False)[source]

Removes nodes with in and out degree 0 from graph G.

This should not need to be recursive.

finesse.utilities.graph.remove_sinks(G, recursive=True, inplace=False)[source]

Removes nodes with out degree 0 from graph G.

Sometimes removing a out degree 0 node creates a new out degree 0 node. So it is necessary to remove out degree 0 nodes recursively.

finesse.utilities.graph.remove_sources(G, recursive=True, inplace=False)[source]

Removes nodes with in degree 0 from graph G.

Sometimes removing a in degree 0 node creates a new in degree 0 node. So it is necessary to remove in degree 0 nodes recursively.