Chapter 6
Flows and transitions
Flow views follow the cases from step to step. This chapter covers the Sankey diagram, the directly-follows graph and its matrix forms, MatrixWave and the chord diagram, all of which aggregate consecutive steps. It then introduces two views that connect flows with frequent sequences: the growth Sankey, which shows how support splits when frequent sequences grow, and the pattern-induced higher-order network, which keeps the history of a step only where frequent sequences show that the history matters.
6.1 Problem and idea
Trees keep the whole history of every prefix, and their size grows with it. Flow views make the opposite choice: they keep only what happens between two consecutive steps, at a given position (Sankey diagrams, MatrixWave) or anywhere in the sequences (directly-follows graphs, chord diagrams). The aggregation makes them compact and familiar — the directly-follows graph is the standard first view of process mining [17] — but it also makes them forget: a directly-follows graph cannot tell whether visitors who reach checkout came from the cart or from a saved basket, because both paths share the same edges. Frequent sequences carry exactly this missing information, and the two new views of the chapter use them to put the history back where it matters.
6.2 Theory
6.2.1 Sankey diagrams
A Sankey or alluvial diagram has one column per step . Column stacks the events found at position of the sequences, with heights proportional to their counts; a ribbon from event in column to event in column carries the sequences that have at position and at position , and a terminal box collects the sequences that end at step [45]. Frequence used the diagram for frequent event sequences of patients [46], and DecisionFlow made it scale to thousands of event types by merging [47]. To stay readable, each column keeps its most frequent events and merges the others into other. With an anchor pattern , the positions are counted from the end of the first occurrence of , so the diagram shows what happens after .
6.2.2 Directly-follows graphs and transition matrices
The directly-follows graph (DFG) has the events as nodes and an edge weighted by the number of times directly follows anywhere in the sequences; artificial start and end nodes mark the first and last events. Normalizing every row gives the transition probabilities of a first-order Markov chain, and the same numbers drawn as a heat map form the transition matrix. Node-link drawings of dense DFGs become unreadable; filtering the edges by frequency is the usual remedy, and matrices are the better choice when many events are connected [73]. MatrixWave [43] splits the transition matrix by position: one matrix for the transitions from step 1 to step 2, one from step 2 to step 3, and so on, arranged in a chain. A chord diagram arranges the events on a circle and draws a ribbon from the arc of to the arc of for every transition [44]; it shows loops and reciprocal flows well.
6.2.3 The growth Sankey
The extension tree of section 2.2.2 can be drawn as a flow as well. In the growth Sankey, column holds frequent sequences of length , a ribbon from to carries , and the rest of the support of a parent, over its drawn children, flows to a stop box. Each column keeps the most supported children of every drawn parent. The diagram reads like a funnel of frequent sequences: it shows at a glance which continuations keep most of the support and how quickly the support of a start event thins out with length. The flow is exact when the children exclude each other, as in a prefix tree. When gaps are allowed, a case can continue a pattern in several ways and support several children, and the ribbons leaving a node can then carry more than the node itself — a visible reminder that frequent sequences, unlike prefixes, overlap.
6.2.4 Pattern-induced higher-order networks
A first-order network assumes that the next step depends on the current step only. Rosvall and co-authors showed that this assumption distorts the flows and communities of many real networks, and that second-order models correct them [49]. Xu, Wickramarathne and Chawla went further and let each node keep as much history as the data justify [50]. Their BuildHON algorithm compares, for every context of order , the distribution of the next event with the distribution of the shorter context that drops the oldest event, and keeps as a separate node when the Kullback–Leibler divergence exceeds a threshold that falls with the support of the context,
| (6.1) |
A context is examined only if its support reaches a minimum, which makes the contexts contiguous frequent sequences; the kept contexts are those frequent sequences whose continuation cannot be told from their last event alone. They become memory nodes: work|commute is the event work reached after commute. Every edge of the network leads to the node of the longest kept context of its target, so a walk on the network remembers exactly as much history as the frequent sequences require. We call the result a pattern-induced higher-order network. It is the flow-view counterpart of the continuation-entropy tree of section 5.2.3: the entropy tree marks where the next step is uncertain, the higher-order network where the history changes it.
In a nutshell: Flows and transitions
- Sankey diagrams follow the cases step by step; an anchor shows what happens after a pattern.
- The directly-follows graph, the transition matrix, MatrixWave and the chord diagram aggregate consecutive pairs; they are compact but forget the history.
- The growth Sankey draws how the support of frequent sequences splits as they grow.
- The pattern-induced higher-order network keeps memory nodes exactly where frequent sequences change the next step significantly.
6.3 Using it in spmviz
plot_sankey(db, max_steps, top, anchor) draws the Sankey diagram, plot_transition_graph, plot_transition_matrix and plot_matrixwave the first-order views, plot_chord the chord diagram and plot_growth_sankey(ps) the growth Sankey. networks.transition_graph(db) returns the DFG as a NetworkX DiGraph with count, probability and n_sequences on the edges; networks.higher_order_network(db, max_order, min_support, threshold) returns the higher-order network with the attributes item, history, order, count and entropy on the nodes, and plot_hon draws it. interactive.sankey_figure gives an interactive Sankey diagram.
6.4 Examples
Sankey diagrams of a web shop. Figure 6.1 follows the thousand synthetic journeys of chapter 5 through five steps; fig. 6.2 anchors the diagram at the first visit to the cart and shows the four steps after it, and fig. 6.3 compares three and eight pages per column.
plot_sankey(db, max_steps=5, top=6)
Directly-follows graph, matrix and MatrixWave. Figure 6.4 is the DFG of the shop with its 24 strongest transitions; the edge from product to cart counts 273 transitions in 254 journeys, 29% of all transitions out of product pages. Figure 6.5 shows the full transition matrix, fig. 6.6 the MatrixWave of the first three steps, and fig. 6.7 the effect of the frequency filter.
plot_transition_graph(db, top_edges=24) G = sv.networks.transition_graph(db) # networkx.DiGraph with counts and probabilities print(G["product"]["cart"])
Open in ColabExample plot_02_transition_graph.py in the gallery
Chord diagrams of a day. The chord diagram of the daily routines (fig. 6.8) shows the loops of a day at a glance; one chord per persona (fig. 6.9) shows how differently the three kinds of people move between their activities.
db = sv.datasets.make_daily_routines(300, random_state=0) plot_chord(db, top=12)
Growth Sankey. Figure 6.10 draws how the support of the frequent sequences that start with wake up splits as they grow, three children per node; fig. 6.11 does the same for work and lunch.
plot_growth_sankey(ps, start_items=["wake up"], max_length=5, top=3)
Open in ColabExample plot_04_growth_sankey.py in the gallery
When history matters. In the daily routines the next activity after work depends on how work was reached. After the morning commute, 43% of the transitions go to lunch and 43% to a meeting; after lunch, 80% go to the commute home. The higher-order network of fig. 6.12 keeps 30 memory nodes, among them work|commute and work|lunch. The web-shop journeys are generated from first-order transition probabilities (one set for mobile and one for desktop visitors), and no memory node survives the test (fig. 6.13). The threshold of eq. (6.1) adapts to the support; fixed thresholds of 0.1, 0.5 and 1 bit keep 52, 30 and 18 memory nodes in the daily routines.
H = higher_order_network(db, max_order=3, min_support=10) memory = [v for v, d in H.nodes(data=True) if d["order"] > 1] print(len(memory), "memory nodes:", memory[:8]) for v in ("work|commute", "work|lunch"): if v in H: print(v, "->", {w: round(d["probability"], 2) for _, w, d in H.out_edges(v, data=True)}) plot_hon(H)
Open in ColabExample plot_05_higher_order_network.py in the gallery