Skip to main content
Rust4PM

alignments

process_mining::conformance::case_centric::alignments

Related Examples(1)

Functions in this Module(3)

align_trace

conformance::case_centric::alignmentsSource

Compute alignment for a single trace (given as activity sequence).

Stops once a Control is cancelled, and reports no progress or partial results.

Arguments
Import from .json, .pnml, .apnml

Options for computing alignment

AlignmentResult

Alignment Result

import r4pm

# Also: .pnml, .apnml
net = r4pm.import_item('AcceptingPetriNet', 'net.json')
res = r4pm.bindings.align_trace(net, ['example'])

align_variants

conformance::case_centric::alignmentsSource

Compute alignments for all variants from a pre-computed activity projection.

With AlignmentOptions::retry_out_of_memory, every variant that ran out of its memory share is aligned again afterwards, one at a time on the calling thread, with the whole AlignmentOptions::max_memory_mib. Such a search may take much longer than its first attempt, and may still run out.

Reports progress with stage "Aligning variants", one unit per variant. One partial result is a finished variant, {"variant": i, "result": r}, where i is its index in projection.traces and r its VariantAlignmentResult. A retry reports stage "Retrying variants with more memory", one unit per retried variant, and emits each retried variant again: a later partial result with the same index replaces the earlier one.

Arguments
Import from .json, .pnml, .apnml
Import from .json, .xes, .xes.gz, or pass EventLog (converted automatically)

Options for computing alignment

import r4pm

log = r4pm.import_item("EventLog", "data/road-traffic-fines.xes.gz")
# Any accepting Petri net works, e.g., r4pm.petri_net.import_pnml("model.pnml").
net = r4pm.bindings.discover_inductive_net(log)

# One optimal alignment per variant
alignments = r4pm.bindings.align_variants(net, log)
print(len(alignments), "variants")

deviating = [a for a in alignments if "Ok" in a["result"] and a["result"]["Ok"]["cost"] > 0]
deviating.sort(key=lambda a: -a["frequency"])
for a in deviating[:2]:
    moves = a["result"]["Ok"]["moves"]
    log_moves = [a["activities"][m["LogMove"]["trace_event_index"]] for m in moves if "LogMove" in m]
    print(f"{a['frequency']} traces, cost {a['result']['Ok']['cost']}, log moves: {log_moves}")

# Model moves twice as expensive, and at most 100,000 states per variant
options = {
    "cost_fn": {"log_move_cost": 1, "model_move_cost": 2, "sync_move_cost": 0, "silent_move_cost": 0},
    "max_states": 100_000,
    "max_memory_mib": 6144,
    "retry_out_of_memory": False,
}
alignments = r4pm.bindings.align_variants(net, log, options)

# r4pm.conformance.alignments returns the alignments shaped like pm4py's.

compute_fitness

conformance::case_centric::alignmentsSource

Compute fitness stats from alignment results

Also constructs the empty-trace alignment (shortest path through model).

Without any traces, all fitness values are 1.0.

Arguments
Import from .json, .pnml, .apnml

Options for computing alignment

FitnessResult

Alignment Fitness Result

import r4pm

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

# With custom alignment options, pass the same ones to both calls.
alignments = r4pm.bindings.align_variants(net, log)
fitness = r4pm.bindings.compute_fitness(net, alignments)

print(f"log fitness {fitness['log_fitness']:.3f}")
print(f"average trace fitness {fitness['average_fitness']:.3f}")
print(f"{fitness['perfectly_fitting_frac']:.1%} of traces fit perfectly")