Skip to main content
Rust4PM

Visualize models

Draw Petri nets, process trees, DFGs and object-centric models as SVG images, in a notebook or as files.

Rust4PM draws its process models as SVG images. The layout is computed in Rust with graphlay and needs no Graphviz installation.

import r4pm

log = r4pm.import_item("EventLog", "data/road-traffic-fines.xes.gz")
net = r4pm.bindings.discover_inductive_net(log)

# An Svg is a str holding the SVG markup. Notebooks display it as an image.
svg = r4pm.bindings.petri_net_svg(net)
svg.save("model.svg")

# Options steer the layout, the style sets colors and sizes. Fields left out keep their default.
vertical = r4pm.bindings.petri_net_svg(
    net, {"direction": "topbottom"}, {"base": {"background": "#f5f5f4"}}
)
vertical.save("model-vertical.svg")

dfg = r4pm.bindings.discover_dfg(log)
r4pm.bindings.dfg_svg(dfg).save("dfg.svg")

The snippet writes three files: the Petri net that IMf discovers for Road Traffic Fines (model.svg), the same net from top to bottom on a gray background (model-vertical.svg), and the DFG of the log (dfg.svg).

The Svg class

In Python, every *_svg binding returns an Svg, a str subclass that holds the SVG markup. .save(path) writes it to a file. Jupyter and VS Code notebooks display an Svg as an image when it is the value of a cell, e.g., a cell that ends with r4pm.bindings.petri_net_svg(net). As a string, it can also be written into an HTML page.

Models

ModelPythonRust
Petri netpetri_net_svg(net)net.export_svg(path)
Process treeprocess_tree_svg(tree)tree.export_svg(path)
Directly-follows graphdfg_svg(dfg)dfg.export_svg(path)
Partial order tracepartial_order_svg(trace)trace.export_svg(classifier, path)
Object-centric DFGocdfg_svg(ocdfg)svg(&ocdfg, ..)
Object-centric Petri netocpn_svg(ocpn)svg(&ocpn.to_json_form(), ..)
OC-DECLARE constraintsoc_declare_svg(arcs)svg(arcs.as_slice(), ..)
Object and event types of an OCELoc_type_graph_svg(oc_type_graph_from_ocel(ocel))svg(&OcTypeGraph::from_linked_ocel(&ocel), ..)
Object-centric Petri net annotated by Reflowreflow_annotated_ocpn_svg(model)model.svg(&style, None)

In Rust, process_mining::visualization::svg(&model, &options, &style) returns the SVG as a String for every model, and export_svg writes it with the default options and style. For example, the object-centric models of the Order Management log:

python
ocel = r4pm.import_item("SlimLinkedOCEL", "data/order-management.xml.gz")
r4pm.bindings.ocdfg_svg(r4pm.bindings.discover_ocdfg(ocel)).save("ocdfg.svg")
r4pm.bindings.ocpn_svg(r4pm.bindings.discover_ocpn(ocel)).save("ocpn.svg")
r4pm.bindings.oc_type_graph_svg(r4pm.bindings.oc_type_graph_from_ocel(ocel)).save("types.svg")

Analyze object-centric data discovers these models.

Options, style and palette

The *_svg bindings of the models in the table take three optional arguments, except reflow_annotated_ocpn_svg, which takes only style and palette. In the dicts for options and style, fields left out keep their default.

  • options change what is drawn and how it is laid out. direction is "leftright" (the default for Petri nets) or "topbottom" (the default for DFGs). layout_patch changes fields of the layout engine's settings, e.g., {"routing": {"style": "diagonal"}}. For DFGs, OC-DFGs, Petri nets and object-centric Petri nets, annotations replace the labels, layout weights and line widths of nodes and edges, e.g., to show durations instead of counts. For DFGs, OC-DFGs and object-centric Petri nets, badges show object types as small discs in the node of an activity. A DFG also takes terminals, which draws the start and end nodes (on by default).
  • style sets colors, line widths, font sizes and padding. Every style has the shared fields under base (under petri.base for object-centric Petri nets), e.g., {"base": {"background": "#f5f5f4"}}, and most have more fields, e.g., the color of silent transitions in a Petri net.
  • palette gives six fallback colors (node_bg, node_border, node_text, arc_color, arc_label_bg and export_bg) for elements whose style names no color. A palette needs all six. The default styles name every color, and a palette does not change the default drawing of any model in the table. It applies to scenes with elements that name no color, e.g., a scene that you edited yourself.

Custom layouts

Besides *_svg, every model has four more bindings for interactive editors:

  • *_layout returns the positions of nodes and the routes of edges.
  • *_svg_from_layout draws the model over a given layout, e.g., one with moved nodes, with optional overrides of the style of single nodes and edges.
  • *_scene_from_layout returns the drawing as a scene of shapes, texts and lines, which render_scene_svg(scene, palette) turns into an SVG.
  • *_layout_request returns the input of the layout engine. layout_graph lays it out (layout_tree for process trees), and reroute_graph routes the edges again after nodes were moved.
python
layout = r4pm.bindings.petri_net_layout(net)
layout["nodes"][0]["center"]["y"] += 100
r4pm.bindings.petri_net_svg_from_layout(net, layout).save("moved.svg")

In Rust, layout, layout_request, scene_from_layout and svg in process_mining::visualization take any model, and visualization::engine has layout_graph, layout_tree, reroute_graph and render_scene_svg. Reference: visualization bindings.