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][source]¶
Convert a
ProvDocumentto 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 usingINFERRED_ELEMENT_CLASS. Relations whose first two formal attributes are not both populated, or whose attribute type is not inINFERRED_ELEMENT_CLASS, are silently skipped.- Parameters:
prov_document – The
ProvDocumentinstance to convert.- Returns:
A NetworkX
MultiDiGraphwith PROV elements as nodes and PROV relations as edges.
- prov.graph.graph_to_prov(g: nx.MultiDiGraph[Any]) ProvDocument[source]¶
Convert a MultiDiGraph that was previously produced by
prov_to_graph()back to aProvDocument.Every node that is a
ProvRecordalready attached to a bundle is added to the new document; nodes without a bundle (e.g. bare nodes inferred byprov_to_graph()) are skipped. Every edge whose"relation"data is aProvRecordis likewise added; edges without relation data are skipped.- Parameters:
g – The graph instance to convert.
- Returns:
A new
ProvDocumentcontaining the elements and relations recovered fromg.