petri_net_nn.anomalies¶
anomalies ¶
Synthetic anomaly generators for XES traces.
§7.2 of the architecture spec promises anomaly detection grounded in the Petri-net substrate — a process instance that produces an unusual activation pattern, relative to the trained distribution, is anomalous. To quantify that promise we need controlled anomalies: traces that deliberately violate the structure in known ways. These generators produce them.
Each function takes a normal XESTrace and returns a new
XESTrace with the corruption applied; the input trace is not
mutated. The corruption types correspond to the kinds of process
deviations that §10 Step 4 calls out as evaluation targets:
- drop_event — a step is skipped entirely;
- insert_event — a spurious step is added;
- swap_event_labels — branch flipping (a step from the wrong BPMN branch is recorded);
- shuffle_events — the right steps fire but out of order.
A frequency-baseline detector lives here too, so anomaly evaluations can compare the structured Petri-net detection against a model that sees only marginal event frequencies — isolating the contribution of the structural prior plus attribute conditioning.
FrequencyBaseline ¶
Marginal-frequency anomaly detector.
Fits a unigram distribution over event labels in a training set of traces; the anomaly score of a new trace is the negative log probability of its events under that distribution, summed across events. Unseen labels get a small smoothing mass.
This baseline deliberately ignores trace-level attributes and the process structure. Its purpose in Phase 7 is to provide a contrast: branch-flip anomalies that the structured Petri-net detector can catch — because it conditions on the input marking derived from trace attributes — are invisible to the frequency baseline, which sees only that the events are familiar event labels.
Source code in petri_net_nn/anomalies.py
drop_event ¶
Return a copy of trace with one event removed (last by
default). Empty input traces are returned unchanged — there is
nothing to drop.
Source code in petri_net_nn/anomalies.py
insert_event ¶
Return a copy of trace with a new event named label
inserted at index (end of the trace by default).
Source code in petri_net_nn/anomalies.py
swap_event_labels ¶
Return a copy of trace with every occurrence of label_a
replaced by label_b and vice versa. Models BPMN branch
flipping: the wrong branch's task name appears in the trace.
Source code in petri_net_nn/anomalies.py
shuffle_events ¶
Return a copy of trace with its events reordered randomly.
Useful for processes where the order matters (sequential / AND-join
patterns); for single-event traces it is a no-op.