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 ProvDocument to a MultiDiGraph instance of the NetworkX library.

The document is flattened(), so the records of every bundle are included, and then unified(), 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 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 skipped with a ProvWarning.

Parameters:

prov_document – The ProvDocument instance to convert.

Returns:

A NetworkX MultiDiGraph with 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_CLASS entry).

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

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. 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, as flattened() does, so every name keeps a spelling.

Parameters:

g – The graph instance to convert.

Returns:

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

prov.graph.INFERRED_ELEMENT_CLASS

Maps a relation’s first/second formal attribute (e.g. PROV_ATTR_ENTITY) to the ProvElement subclass used to create a bare node for an element referenced by a relation but not otherwise declared.