PM4Py Examples — Process Modeling & Discovery Process Trees

Process trees can be learned with the Inductive Miner, converted using BPMN support, and explored through extensive playout.

PM4Py offers support for process trees, including visualization, conversion to Petri nets, log generation, importing/exporting, and tree generation functionality. This section outlines these features.

Importing/Exporting Process Trees

PM4Py supports importing and exporting process trees in the PTML format. Use the following code to import a process tree from a PTML file:

Python
import pm4py
from pm4py.objects.process_tree.obj import ProcessTree

if __name__ == "__main__":
    tree: ProcessTree = pm4py.read_ptml("tests/input_data/running-example.ptml")
Visualization of the object referenced in the example.

Use the following code to export a process tree to a PTML file:

Python
import pm4py

if __name__ == "__main__":
    pm4py.write_ptml(tree, "running-example.ptml")

Generation of Process Trees

The 'PTAndLogGenerator' approach, described in the paper 'PTAndLogGenerator: A Generator for Artificial Event Data', has been implemented in the PM4Py library. The following code snippet can be used to generate a process tree. Review the parameters below:

Python
import pm4py
from pm4py.objects.process_tree.obj import ProcessTree

if __name__ == "__main__":
    tree: ProcessTree = pm4py.generate_process_tree()
Visualization of the object referenced in the example.

Below is a table explaining the parameters used in the process tree generation:

ParameterMeaning
MODEMost frequent number of visible activities (default 20)
MINMinimum number of visible activities (default 10)
MAXMaximum number of visible activities (default 30)
SEQUENCEProbability of adding a sequence operator to the tree (default 0.25)
CHOICEProbability of adding a choice operator to the tree (default 0.25)
PARALLELProbability of adding a parallel operator to the tree (default 0.25)
LOOPProbability of adding a loop operator to the tree (default 0.25)
ORProbability of adding an OR operator to the tree (default 0)
SILENTProbability of adding silent activities to choice or loop operators (default 0.25)
DUPLICATEProbability of duplicating an activity label (default 0)
LT_DEPENDENCYProbability of adding random dependencies to the tree (default 0)
INFREQUENTProbability of making a choice have infrequent paths (default 0.25)
NO_MODELSNumber of trees to generate from the model population (default 10)
UNFOLD Whether to unfold loops to include choices in dependencies (0 = False, 1 = True).
If LT_DEPENDENCY ≤ 0, this should always be 0 (False).
If LT_DEPENDENCY > 0, this can be either 0 or 1 (True or False).
(default 10)
MAX_REPEATMaximum number of loop repetitions (used only when unfolding is True) (default 10)

Generation of a Log from a Process Tree

Playout returns a legacy EventLog, whose length counts traces. Use pm4py.convert_to_dataframe to analyze the generated events with Pandas.

Use the following code snippet to generate a log with 100 cases from a process tree:

Python
import pm4py
from pm4py.objects.log.obj import EventLog
if __name__ == "__main__":
	log: EventLog = pm4py.play_out(tree)
	print(len(log))
Example Output Number of simulated traces from the imported process tree (one random run).
1000

Conversion to Petri Net

Use the following code to convert a process tree into a Petri net:

Python
import pm4py
from pm4py.objects.petri_net.obj import Marking, PetriNet

if __name__ == "__main__":
    net: PetriNet
    im: Marking
    fm: Marking
    net, im, fm = pm4py.convert_to_petri_net(tree)
Visualization of the object referenced in the example.

Visualizing a Process Tree

A process tree can be printed as shown below:

Python
if __name__ == "__main__":
	print(tree)
Example Output Text representation of the imported process tree.
->( 'register request', *( ->( +( 'check ticket', X( 'examine thoroughly', 'examine casually' ) ), 'decide' ), 'reinitiate request' ), X( 'r …
… (excerpt)

A process tree can also be visualized as follows:

Python
import pm4py

if __name__ == "__main__":
    pm4py.view_process_tree(tree, format='png')
Visualization of the object referenced in the example.

Converting a Petri Net to a Process Tree

We present an approach to convert a block-structured accepting Petri net into a process tree. The method is based on the work by van Zelst, Sebastiaan J., "Translating Workflow Nets to Process Trees: An Algorithmic Approach" (arXiv preprint arXiv:2004.08213, 2020).

The approach returns a process tree for block-structured Petri nets and raises an exception if the Petri net is not block-structured. Below is an example of this approach:

First, we load an XES log and discover an accepting Petri net using the Alpha Miner algorithm:

Python
import pm4py
import os
import pandas
from pm4py.objects.petri_net.obj import Marking, PetriNet

if __name__ == "__main__":
	log: pandas.DataFrame = pm4py.read_xes(os.path.join("tests", "input_data", "running-example.xes"))
	net: PetriNet
	im: Marking
	fm: Marking
	net, im, fm = pm4py.discover_petri_net_alpha(log)
Visualization of the object referenced in the example.

Next, we convert this Petri net into a process tree:

Python
import pm4py
from pm4py.objects.process_tree.obj import ProcessTree

if __name__ == "__main__":
	tree: ProcessTree = pm4py.convert_to_process_tree(net, im, fm)
	print(tree)
Example Output Process tree converted from the Alpha Miner model.
->( 'register request', *( ->( +( 'check ticket', X( 'examine thoroughly', 'examine casually' ) ), 'decide' ), 'reinitiate request' ), X( 'r …
… (excerpt)

The method succeeds since the accepting Petri net is block-structured and generates a process tree (which, in this case, coincidentally matches the process tree discovered by the inductive miner).

Frequency Annotation of a Process Tree

By default, a process tree does not include frequency or performance annotations. To optimally match a log against a process tree, you can use the alignments algorithm. The results of this algorithm provide a list of visited leaves/operators during replay, which can then be used to infer the frequency of each node at the case/event level in the process tree.

The following code can be used to add frequency annotations to the nodes of a process tree:

Python
import pm4py
from pm4py.algo.conformance.alignments.process_tree.util import search_graph_pt_frequency_annotation
from typing import Any
if __name__ == "__main__":
	aligned_traces: list[dict[str, Any]] = pm4py.conformance_diagnostics_alignments(log, tree)
	tree = search_graph_pt_frequency_annotation.apply(tree, aligned_traces)

A frequency-based visualization of the process tree is also available:

Python
from pm4py.visualization.process_tree import visualizer as pt_visualizer
from graphviz import Graph
if __name__ == "__main__":
	gviz: Graph = pt_visualizer.apply(tree, parameters={"format": "svg"}, variant=pt_visualizer.Variants.FREQUENCY_ANNOTATION)
	pt_visualizer.view(gviz)
Example Output Process tree annotated with replay frequencies on the Running Example.
Process tree annotated with replay frequencies on the Running Example.