Context
At every token, the other axes’ readings folded over what the token is read against: the window
before it, its column, the tokens since the last regime change, the heads of the brackets
enclosing it, its earlier occurrences, and the values bound to its key. A relative predicate in a
pattern (\N{>+1}, \N{>+1:k}) compares a token’s reading with one of these.
Source: src/context.rs.
What it reads that nothing else does
latency = 100 ; latency = 120 ; size = 5000000 ; latency = 90 ; latency = 12000 ;Against the whole line the value out of scale is 5000000: magnitude reads it 6.70 orders up
and names it the stream’s one outlier, and a fixed bound, \M{>3}, takes 5000000 and 12000
alike. Context reads each value against the values its own key held before. size has none, so
5000000 has nothing to be read against, while 12000 is two orders above latency’s 100, 120 and 90,
and "latency":k "=" \N{>+1:k} matches latency = 12000 alone. The same folds read a value
against its column, the window before it, or the tokens since the input last changed texture.
Reading the axis
trex context reads every fold of an input. Its summary counts the tokens and the supertokens,
gives the record period with every live period, and the share of supertokens at which each lower
grain’s nearest boundary keeps its role’s usual offset:
$ trex context --text 'latency = 100 ; latency = 120 ; size = 5000000 ; latency = 90 ; latency = 12000 ;'
trex context (window fold): 81 bytes, 20 tokens, 5 supertokens
record period: 4 tokens (live periods 4, 8)
alignment: regime 0.00, shape 0.60, seam 0.20 of supertokens at their role's usual offset
Every token’s context
--field lists every token with how many tokens its fold holds and each axis’s reading over
them, and --fold F names the fold: window, the default, unit, units, enclosing,
echo, regime, phase or key, as \N{>+1:F} names it. -Fold and fold= name it in
PowerShell and Python, and -Detail and detail=True add the frames. Under the key fold each
latency reads the values latency was bound to before it, three at the last, whose magnitude
averages 2.01 against the 4.08 of 12000:
$ trex context --fold key --field --limit 5 --text 'latency = 100 ; latency = 120 ; size = 5000000 ; latency = 90 ; latency = 12000 ;'
trex context (key fold): 81 bytes, 20 tokens, 5 supertokens
record period: 4 tokens (live periods 4, 8)
alignment: regime 0.00, shape 0.60, seam 0.20 of supertokens at their role's usual offset
per-token context, the values each one's key was bound to before:
@ 0 latency over 0
@ 8 = over 0
@ 10 100 over 0
@ 14 ; over 0
@ 16 latency over 1
magnitude: mean 2.00, spread 0.00, max 2.00
stress: mean 0.00, max 0
spectral: entropy 0.56, period 0, strength 0.00
echo: novel 1, echoed 0
observation: causal 0.64, anticausal 0.83, centered 0.75, disagreement 0.18, disagreement max 0.18, contested 0
seam: forward 0.92, backward 0.00, strength 1.37, strength max 1.37, cuts 1
flow: slope -0.20, rising 0, falling 1, steady 0, momentum 4, reversals 0
... (+15 more tokens; raise --limit)
A fold that holds no token reads over 0, and a relative predicate that reads it there matches
nothing.
The record period
--period lists each token’s column of the record period, and the report’s frames carry it as
phase. Rows of six tokens repeat at 6, and at 12 as well:
$ trex context --period --limit 8 --text 'a , 10 , x ; b , 12 , y ; c , 9000 , z ; d , 11 , w ;'
trex context (window fold): 53 bytes, 24 tokens, 4 supertokens
record period: 6 tokens (live periods 6, 12)
alignment: regime 0.00, shape 0.25, seam 1.00 of supertokens at their role's usual offset
per-token column of the record period:
@ 0 a 0
@ 2 , 1
@ 4 10 2
@ 7 , 3
@ 9 x 4
@ 11 ; 5
@ 13 b 0
@ 15 , 1
... (+16 more tokens; raise --limit)
Choosing a threshold
A relative predicate’s delta is in orders of magnitude, or in standard deviations of the
context with an s. Read the context before choosing: against the window before it 5000000
and 12000 are both more than an order above, and only 5000000 more than two of the
window’s standard deviations; in the rows 9000 reads 3.95 against its column’s mean of 1.04,
2.91 orders above, so a delta of 3 takes nothing:
$ trex scan '\N{>+1}' --text 'latency = 100 ; latency = 120 ; size = 5000000 ; latency = 90 ; latency = 12000 ;'
[39..46] "5000000"
[74..79] "12000"
$ trex scan '\N{>+2s}' --text 'latency = 100 ; latency = 120 ; size = 5000000 ; latency = 90 ; latency = 12000 ;'
[39..46] "5000000"
$ trex scan '\N{>+3:phase}' --text 'a , 10 , x ; b , 12 , y ; c , 9000 , z ; d , 11 , w ;'
no match
--token-window N sets how many significant tokens the window folds, 32 by default, and
--unit-window N how many supertokens the units fold, 8; -TokenWindow and -UnitWindow, and
token_window= and unit_window=, set them in PowerShell and Python. A shorter window reads a
value against its nearer neighbors only.
Declarations
A declared shape or kind changes the tokens the folds hold and the values a key is bound to.
trex context takes the declarations a scan takes, --lib, --shape, --shape-after,
--kind, --let and --declare, as -Library and lib= do in PowerShell and Python
(pattern files).
The rolling window
A reading has an identity and an associative combine, so a span’s reading is the fold of its tokens’ readings. The window slides one token at a time and keeps two stacks, suffix folds of its older half and a running fold of its newer half, so each step is one combine whatever the window’s length. It folds at the token rung and, one rung up, over the supertokens, each unit’s reading being the fold of its tokens. The fold carries the magnitude, stress, spectral, echo, observation, seam and flow readings.
| Reading | Folds |
|---|---|
ContextField::at_token[i] | the window of significant tokens ending at token i |
ContextField::of_unit[u] | unit u’s own tokens |
ContextField::at_unit[u] | the window of units ending at unit u |
The window is 32 significant tokens, the observation reader’s window, and 8
units (ContextConfig).
The related context
Five more folds admit tokens by relation rather than by distance. Each is a running fold, exact and unbounded in range, one combine when a token joins it.
| Fold | Over |
|---|---|
enclosing | the heads of the brackets enclosing the token, outermost first |
echoing | every earlier occurrence of the token’s key |
regime | the significant tokens since the byte grain’s last spectral change-point |
phase | every earlier significant token at the token’s phase of the record period |
value_history | the values bound by = or : to earlier occurrences of the token’s key |
Each leaves the token itself out, so it is the context before the token.
The record period is the smallest lag, up to 32 tokens, at which the silhouettes of the
significant tokens best repeat: the share of positions whose silhouette equals the one a lag
later, at or above the floor a byte period must clear. It reads silhouettes rather than kinds; in
kinds the record word , number , word ; alternates content and punctuation and repeats at 2.
let text = b"a , 1 , x ; b , 2 , y ; c , 3 , z ;";
let toks = trex::lexer::lex(text);
assert_eq!(trex::context::record_period(&toks, text), Some(6));
let field = trex::context::analyze(text);
assert_eq!(field.at_token.len(), toks.len());
let related = trex::context::relate_bytes(text);
assert_eq!(related.period, Some(6));| Item | Signature |
|---|---|
analyze / analyze_with | fn(bytes: &[u8]) -> ContextField, the second with a &ContextConfig |
fold_windows / fold_windows_parallel | fn(toks: &[Token], ctx: &AxisCtx, supers: &SuperContext, cfg: &ContextConfig) -> ContextField |
relate / relate_bytes | fn relate(toks, ctx, regime_cuts: &[usize], echo: &EchoField, period: Option<u16>) -> RelationContext |
record_period | fn record_period(toks: &[Token], bytes: &[u8]) -> Option<u16> |
agreement | fn agreement(units, toks, regime_cuts, shape_cuts, seam_cuts) -> Vec<Agreement> |
Window<P> | new(capacity), push(value), fold() over any AxisProfile |
In a pattern
| Form | Baseline |
|---|---|
\N{>+1} \N{>+2s} \N{<-1} | the window before the token |
\N{>+1:phase} | the token’s column |
\N{>+1:regime} | since the last regime change |
\N{>+1:echo} | the token’s earlier occurrences |
\N{>+1:enclosing} | the enclosing heads |
"key":k "=" \N{>+1:k} | the values bound to earlier occurrences of the key k holds |
@phase:k | column k of the record period |
@phase:k/p @phase:k#n | column k of the live p-token period, or of the n-th strongest live period |
The delta is in orders of magnitude, or in standard deviations of the context with an s
suffix. An empty context is no baseline and matches nothing; a sigma form also needs a context
of more than one value with a spread.
$ trex scan '"latency":k "=" \N{>+1:k}' --text 'latency = 100 ; latency = 120 ; size = 5000000 ; latency = 90 ; latency = 12000 ;'
[64..79] "latency = 12000" captures: k="latency"
$ trex scan '\N{>+2:phase}' --text 'a , 10 , x ; b , 12 , y ; c , 9000 , z ; d , 11 , w ;'
[30..34] "9000"
Where the grains agree
The byte grain places spectral change-points, the token grain shape change-points and seam cuts,
the unit grain the unit starts. ContextField::agreement records, per unit, where each lower
grain’s nearest boundary is relative to the unit’s start, in significant tokens, and
alignment() how consistently that offset repeats for units of one role. On the code corpus
benches/context_window.rs
reads, the token-grain seam cuts one token before a call, two before a key, one after a list or a
binding: 0.875 of units at their role’s offset, against 0.372 on the same tokens shuffled.
--agreement lists every supertoken with its role and the three offsets, a dash where a grain
places no boundary near it, and agreement and Agreement hold the same in Python and
PowerShell. In the rows every seam cut is at a supertoken’s start, so the seam’s share is
1.00:
$ trex context --agreement --text 'a , 10 , x ; b , 12 , y ; c , 9000 , z ; d , 11 , w ;'
trex context (window fold): 53 bytes, 24 tokens, 4 supertokens
record period: 6 tokens (live periods 6, 12)
alignment: regime 0.00, shape 0.25, seam 1.00 of supertokens at their role's usual offset
per-supertoken (role, then the nearest spectral change point, shape change point and seam cut, in tokens from its start):
@ 0 a , 10 , x list - 0 0
@ 13 b , 12 , y list - -2 0
@ 26 c , 9000 , z list - -8 0
@ 41 d , 11 , w list - -14 0