prov.graph¶
prov.graph converts between a ProvDocument and a
NetworkX MultiDiGraph, with one node per element (entity,
activity, agent) and one edge per relation, so NetworkX’s graph algorithms can run over a
provenance graph. It needs the graph extra. Convert to and from a NetworkX graph has 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
flattened(), so the records of every bundle are included, and thenunified(), so records that share an identifier, in the document or across its bundles, 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 skipped with aProvWarning.- Parameters:
prov_document – The
ProvDocumentinstance to convert.- Returns:
A NetworkX
MultiDiGraphwith PROV elements as nodes and PROV relations as edges.- Raises:
ProvUnificationError – Propagated, unhandled, from
unified(), when records that share an identifier, in the document or across its bundles, disagree.- Warns:
ProvWarning – For each relation skipped because one of its first two formal attributes is unset, or because an undeclared endpoint’s element type cannot be inferred (its attribute has no
INFERRED_ELEMENT_CLASSentry).
- 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. The new document declares what the first record’s document declares, and a prefix for every other default namespace a record’s bundle or document reads, asflattened()does, so every name keeps a spelling.- Parameters:
g – The graph instance to convert.
- Returns:
A new
ProvDocumentcontaining the elements and relations recovered fromg.
- prov.graph.INFERRED_ELEMENT_CLASS¶
Maps a relation’s first/second formal attribute (e.g.
PROV_ATTR_ENTITY) to theProvElementsubclass used to create a bare node for an element referenced by a relation but not otherwise declared.