Skip to content

The flow axis

The flow axis follows a signal along the input and reads its trend: how steeply it is rising or falling, which way it is heading, how long it has kept heading that way, and where it turns. The signal is each token’s magnitude by default, its stress depth or its length on request, and any value a Rust caller computes per token. With --analytic the axis also reads how widely the signal swings and how fast.

Source: src/flow.rs.

What it reads that nothing else does

3 9 27 81 243 729 243 81 27 9

Each value is three times the one before until the turn, half an order of magnitude a step, so magnitude finds no jump and no outlier: no step is far from its neighbor and no value far from the rest. Flow reads the run: a slope of +0.48 a token, a rising run six tokens long, and the reversal at the second 27, where the trend has turned to falling. The same reading finds a queue filling, a latency creeping up, or a count that peaked and is draining.

Most examples on this page read that line.

The readings

ReadingMeaning
slopethe average change a step over the trailing window, 4 units by default
directionrising, steady or falling: a slope within the steady band of zero, 0.05 by default, is steady
momentumhow many units in a row have held the same rising or falling direction
reversala unit where the direction flips between rising and falling; a steady stretch between them does not count as a flip
Signal--signalSource
Magnitudemagnitudethe magnitude of each token
StressDepthstressthe stress depth of each token
Lengthlengtheach token’s byte length

The unit is a token by default; --grain super reads one value a supertoken instead.

Reading the axis

The summary names the signal, counts the units and the reversals, and gives the longest run:

$ trex flow --text '3 9 27 81 243 729 243 81 27 9'
trex flow (signal magnitude): 29 bytes, 10 tokens, 1 reversal(s)
  peak momentum: 6 (rising)

Every unit’s reading

--field lists each unit’s slope, direction and momentum, and --limit N stops this list and the analytic one after N units and counts the rest. -Detail in PowerShell and detail=True in Python add the same frames:

$ trex flow --field --text '3 9 27 81 243 729 243 81 27 9'
trex flow (signal magnitude): 29 bytes, 10 tokens, 1 reversal(s)
  peak momentum: 6 (rising)
  per-token (token -> slope | direction | momentum):
    3              slope=  +0.00 --   mom=0
    9              slope=  +0.48 up   mom=1
    27             slope=  +0.48 up   mom=2
    81             slope=  +0.48 up   mom=3
    243            slope=  +0.48 up   mom=4
    729            slope=  +0.48 up   mom=5
    243            slope=  +0.24 up   mom=6
    81             slope=  +0.00 --   mom=0
    27             slope=  -0.24 down mom=1
    9              slope=  -0.48 down mom=2

The slope averages over the window, so it lags the turn: at the second 243 the values are already falling, but the window still reaches back to the rise and reads +0.24. The trend crosses zero at the second 81 and reads falling from the second 27.

Reversals

A reversal is the unit where the direction flips between rising and falling. The steady 81 between the two runs is passed over, so the reversal is at the first falling unit:

$ trex flow --reversals --text '3 9 27 81 243 729 243 81 27 9'
trex flow (signal magnitude): 29 bytes, 10 tokens, 1 reversal(s)
  peak momentum: 6 (rising)
  reversals (1):
    @    25  27 9

Another signal

--signal stress follows the bracket depth and --signal length the token length. Over nested calls the depth rises into the innermost bracket and turns at the first close:

$ trex flow --signal stress --reversals --text 'f(g(h(x))) k(y)'
trex flow (signal stress): 15 bytes, 14 tokens, 1 reversal(s)
  peak momentum: 6 (rising)
  reversals (1):
    @     8  )) k(y)

The supertoken grain

--grain super reads one value a supertoken, the magnitude or length summed over its span, so the trend is of statements rather than of tokens. The summary counts supertokens:

$ trex flow --grain super --text 'x = 1 ; f(a, b) ; y = 22 ; g(c) ; z = 333'
trex flow (signal magnitude): 41 bytes, 7 supertokens, 0 reversal(s)
  peak momentum: 1 (rising)

The analytic reading

A slope says which way a signal heads; it cannot say how widely the signal swings or how fast, and a signal that oscillates keeps reversing however steady its swing. --analytic reads the signal’s analytic form at every unit: its amplitude, the envelope of the swing; its phase, the unit’s position in the swing, from -pi to pi; and its frequency, how far the phase advances a unit, in radians.

The transform reads no further than the input goes: each unit uses the input up to 15 units past it, so the last 15 units have no full reading, and the command prints a dash for each. Over magnitudes that climb and fall every six tokens, 1 10 100 1000 100 10 repeated, the phase turns once every six tokens:

$ trex flow --analytic --text '1 10 100 1000 100 10 1 10 100 1000 100 10 1 10 100 1000 100 10 1 10 100 1000 100 10 1 10 100 1000 100 10 1 10 100 1000 100 10 1 10 100 1000 100 10 1 10 100 1000 100 10' | head -4
trex flow (signal magnitude): 167 bytes, 48 tokens, 14 reversal(s)
  peak momentum: 4 (rising)
  analytic (token -> amplitude | phase | frequency), latency 15 tokens:
    1              amp=  0.58 phase= -1.57 freq= +0.00

The report lists the units with a full reading, 33 of the 48 here. The frequency alternates around 2pi/6, about 1.05 radians a unit, the six-token period of the input.

Choosing a threshold

Both settings shape how readily the trend moves.

The window. The slope averages over the trailing window, so a short window follows the signal closely and turns as soon as it does, while a long one smooths a jagged signal and turns later. With a window of 2 the second 243 already reads steady and the fall begins one unit sooner:

$ trex flow --reversals --window 2 --text '3 9 27 81 243 729 243 81 27 9'
trex flow (signal magnitude): 29 bytes, 10 tokens, 1 reversal(s)
  peak momentum: 5 (rising)
  reversals (1):
    @    22  81 27 9

The steady band. A slope within the band reads steady, which neither extends a run nor ends one in a reversal. Set it from the slopes --field reports: just above the size of the jitter you want to ignore, and below the trend you want to see. Here every slope is at most 0.48, so a band of 0.6 reads the whole line as steady and finds no reversal at all. On the command line the setting is --steady-band, in PowerShell -SteadyBand and in Python steady_band=. The comparison is inclusive: a slope of exactly the band is steady.

Declarations

A declared shape or kind changes what a token is, and so changes the value the signal reads at it. Every axis that reads tokens takes the declarations a scan takes: --lib, --shape, --shape-after, --kind, --let and --declare on the command line, -Library in PowerShell and lib= in Python (pattern files); the magnitude page shows one at work.

Data model

FlowFrame, one unit’s reading:

FieldTypeMeaning
slopef32windowed rate of change
directioni8-1, 0 or +1
momentumu16consecutive same-direction units

FlowField, keyed by byte offset:

FieldTypeMeaning
n_tokensusizeunit count
spansVec<(usize, usize)>byte span per unit
framesVec<FlowFrame>one per unit
reversalsVec<usize>turning-point byte offsets

AnalyticField holds amplitude, phase and frequency, one each a unit, and latency. Query API: slope_at(byte), direction_at(byte); analyze_signal(tokens, signal, cfg) and analytic_signal(signal, cfg) read any per-token f32 a caller computes, and FlowConfig holds window (4) and steady_band (0.05).

Algorithm

Per unit: slope = (signal[i] - signal[i - window]) / window, the average trend over the trailing window; direction thresholds the slope against the steady band; momentum is the run length of the same non-steady direction; a reversal is recorded where the direction flips between +1 and -1. One pass, O(1) per unit; the signal costs one pass of the axis it comes from.

The analytic reading detrends the signal by a trailing mean over 16 units, then applies a 31-tap Hamming-windowed Hilbert kernel shifted to be causal, which delays it by 15 units: amplitude and phase are the modulus and angle of the result, and frequency the phase advance.