This example takes a PROV-O activity graph and uses the PROV Python library, which is an implementation of the Provenance Data Model by the World Wide Web Consortium, to create a graphical representations like PNG, SVG, PDF.
We will use the Example 1 available on https://www.w3.org/TR/prov-o/ e.g. https://www.w3.org/TR/prov-o/#narrative-example-simple-1
To create a provenance document (a package of provenance statements or assertions), import ProvDocument
class from prov.model
:
In [2]:
from prov.model import ProvDocument
import prov.model as pm
Create some setup variables filename and basename which will be used for the encoding of the outputs
In [52]:
filename = "rdf/prov-ex1.ttl"
basename = "prov-ex1"
Use the prov library to deserialize the example document
In [53]:
# Create a new provenance document
d1 = pm.ProvDocument.deserialize(filename, format="rdf")
In addition to the PROV-N output (as above), the document can be exported into a graphical representation with the help of the GraphViz. It is provided as a software package in popular Linux distributions, or can be downloaded for Windows and Mac.
Once you have GraphViz installed and the dot
command available in your operating system's paths, you can save the document we have so far into a PNG file as follows.
In [54]:
# visualize the graph
from prov.dot import prov_to_dot
dot = prov_to_dot(d1)
dot.write_png(basename + '.png')
Out[54]:
The above saves the PNG file as article-prov.png
in your current folder. If you're runing this tutorial in Jupyter Notebook, you can see it here as well.
In [55]:
from IPython.display import Image
Image(basename + '.png')
Out[55]:
In [56]:
# Or save to a PDF
dot.write_pdf(basename + '.pdf')
Out[56]:
Similarly, the above saves the document into a PDF file in your current working folder. Graphviz supports a wide ranges of raster and vector outputs, to which you can export your provenance documents created by the library. To find out what formats are available from your version, run dot -T?
at the command line.
PROV-JSON is a JSON representation for PROV that was designed for the ease of accessing various PROV elements in a PROV document and to work well with web applications. The format is natively supported by the library and is its default serialisation format.
In [57]:
print(d1.serialize(indent=2))
You can also serialize the document directly to a file by providing a filename (below) or a Python File object.
In [58]:
d1.serialize(basename + '.json')