Skip to content
Back to top
View this page

# prov.graph

`prov.graph` converts between a `ProvDocument` and a NetworkX `MultiDiGraph`, one node per element (entity, activity, agent) and one edge per relation, so graph algorithms NetworkX provides (and `prov` does not implement) can run directly over a provenance graph. See Convert to/from a NetworkX graph for worked examples.

prov.graph.prov_to_graph(prov_document: ProvDocument) → nx.MultiDiGraph[Any]

  Convert a `ProvDocument` to a MultiDiGraph instance of the NetworkX library.

  The document is first `unified()` so that records referring to the same real-world thing are merged before the graph is built. Every PROV element becomes a graph node. Each relation becomes an edge between the elements named in its first two formal attributes, with the relation record stored under the edge’s `"relation"` attribute; if either of those two elements was not already added as a node, a bare node is created for it using `INFERRED_ELEMENT_CLASS`. Relations whose first two formal attributes are not both populated, or whose attribute type is not in `INFERRED_ELEMENT_CLASS`, are silently skipped.

  Parameters:

    prov_document – The `ProvDocument` instance to convert.

  Returns:

    A NetworkX `MultiDiGraph` with PROV elements as nodes and PROV relations as edges.

prov.graph.graph_to_prov(g: nx.MultiDiGraph[Any]) → ProvDocument

  Convert a MultiDiGraph that was previously produced by `prov_to_graph()` back to a `ProvDocument`.

  Every node that is a `ProvRecord` already attached to a bundle is added to the new document; nodes without a bundle (e.g. bare nodes inferred by `prov_to_graph()`) are skipped. Every edge whose `"relation"` data is a `ProvRecord` is likewise added; edges without relation data are skipped.

  Parameters:

    g – The graph instance to convert.

  Returns:

    A new `ProvDocument` containing the elements and relations recovered from `g`.