Zijia Wang · Christofer Toumazou
Centre for Bio-Inspired Technology, Department of Electrical and Electronic Engineering
Imperial College London, London, United Kingdom
One time series. One total-variation drift budget. Exact count envelopes.
Paper · Project page · Try your data · Run the code · Method · Results · BibTeX
96 public BIG001 glucose readings, one per well. As recorded, 84 lie in the TITR band [70,140] mg/dL; over every integer offset path in −30…30 with total variation at most 4, the count is at least 63 and at most 92. The film subtracts offsets (gi − xi), as the browser tool does; on this symmetric domain that is the paper's gi + δi model with xi = −δi. Skin, sensor and plate are schematic; the trace, offset paths and counts are computed from the data (details, MP4).
Threshold-count and range-proportion queries, such as time in range (TIR) and time in tight range (TITR) for continuous glucose monitoring (CGM), are usually evaluated on the readings as recorded. Calibration bias and drift, however, perturb a sensor smoothly over time. LocalWindows is an exact query operator that returns the minimum and maximum attainable threshold counts when every reading may be shifted by an integer offset and consecutive offsets share a total-variation (TV) budget, for a whole bank of budgets at once. This repository contains a Python implementation, a native C++ driver with the paper's exact baselines, an in-browser query tool and the paper.
The browser tool lets you:
- Paste readings or upload a CSV, with optional 0/1 row weights.
- Set the band, integer offset domain and shared movement (total-variation) budgets.
- Compute exact minimum and maximum counts, then download the result as JSON.
It supports exact decimal predicates, up to 2,000 rows, 401 integer states, and budgets 0–64. Inputs stay in your browser, with no external upload. Input format and worked walkthrough · Video/GIF guide
Check the browser solver independently with node tests/test_browser_solver.cjs (320 exhaustive small cases plus fixed public and decimal-boundary checks; no dependencies).
These screen recordings show the browser application in use: entering data, changing a query, inspecting exact counts and downloading the query with its results. Each session demonstrates a complete task.
| Task | What the session shows | Recording |
|---|---|---|
| Paste data, compute and export | Paste two readings, compute budgets 0 and 1, edit a reading, recompute and download JSON. | MP4 · GIF |
| Check a public glucose report | Load 96 public BIG001 readings, inspect five budgets, narrow the allowed offsets and export the revised report. | MP4 · GIF |
| Report selected readings | Import a five-row CSV with a 0/1 count mask, keep temporal order, add a budget and export. | MP4 · GIF |
Session steps, example files and data attribution.
Take readings 70 and 141 mg/dL. Count how many corrected readings fall in the closed band [70,140]. In the browser tool and the Python API, corrected reading = glucose − offset; here each offset is 0 or 1.
- Budget 0: either constant offset gives exactly one hit → [1,1].
- Budget 1: offsets (0,1) give two hits; (1,0) give zero → [0,2].
The budget limits total movement across the sequence. It is shared by all rows. Zero movement permits any constant offset in the domain. The paper writes the same model as a perturbed reading
The paper's running example is public record BIG001: 96 samples, offsets
These are exact extrema, not confidence intervals. Separate metrics may attain their extrema on different paths. The offset radius and drift budget are user-specified query parameters; the operator does not fit them to sensor data. Explore the example interactively →
Results in brief: across 6,804 query cells per request mode on 211 public CGM series, LocalWindows agrees bit for bit with three exact DP baselines (SparseGlobal, GapComponents, RunTailFullG) and lowers prepared-operator runtime at
Let
For every budget
This repository's implementations solve the same problem in a slightly more general form: any consecutive integer domain
Python 3.10+; the Python package uses the standard library.
python -m pip install -e .
python -m lsw_range examples/synthetic.jsonfrom lsw_range import solve_intervals
# The four weighted runs in the mechanism figure: (lower, upper, weight).
answer = solve_intervals(
[(2, 6, 2), (7, 13, 1), (4, 19, 3), (10, 17, 2)],
domain=(0, 23),
budgets=[4],
)
print(answer.bounds) # ((0, 8),)For measured observations, solve_glucose compiles a glucose band to exact hit-offset intervals before solving.
The native C++17 path accepts unit-count rows. This synthetic example expands the mechanism figure's integer weights into repeated unit rows:
mkdir -p build
c++ -std=c++17 -O3 native/lsw_range.cpp -o build/lsw-native
build/lsw-native < examples/synthetic_native.txtThe JSON CLI demo and native demo return bounds [[0,6], [0,6], [0,8]] for budgets [0,1,4]. API & examples · Native input protocol · Implementation provenance
In Python, use method="global" or method="gap" to compare exact global-grid and gap-split plans, and method="reference" as a small-case literal-state correctness oracle. The native driver also exposes the paper's three baselines (GlobalBudgetBank = SparseGlobal, GapComponents, RunTailFullGThenGather = RunTailFullG) and the LocalWindowsGeneric ablation. The local plan is intended for small and moderate maximum budgets; this release does not provide an automatic plan selector.
LocalWindows has three stages; lemma and theorem numbers refer to the paper.
-
Extrema-preserving run reduction. Always-hit (
$I_i=D$ ) and always-miss ($I_i=\emptyset$ ) rows contract into scalar constants$c_1,c_0$ (Lemma 1, fixed-row contraction), and adjacent identical offset intervals merge into$r$ positive-weight runs (Theorem 1, duplicate-run merging). Neither step changes any budget-constrained minimum or maximum. A single$O(r+m)$ difference-array sweep then gives the static depth$d(z)$ and the exact$V=0$ envelope. -
Overlapping
$2V$ -state window cover. A trajectory with$\operatorname{TV}\le V$ has range diameter at most$V$ , so overlapping windows$W_j=B_j\cup B_{j+1}$ built from adjacent blocks of the offset grid contain every feasible trajectory (Lemma 2). Relative to a window, each run is a disjoint miss, a covered hit, or an active boundary run. Each run is active in at most four windows, so there are at most$4r$ run–window incidences in total (Lemma 3), and covered runs fold into an$O(1)$ static-depth baseline read from$d(z)$ . -
Linear-time local sweeps. Each active window runs a two-pass
$O(|W_j|)$ state-line transition kernel up to a local saturation cap shared by the whole budget bank (Proposition 1). For unit drift, an$O(r+m)$ boundary-prefix fast path evaluates only single steps at run boundaries and needs no DP table (Proposition 2).
Exactness and complexity (Theorem 2). Run compression and the static
inner-loop state updates:
The repository's Python and browser solvers use blocks of
Mechanism figure and two more teaching examples
A mathematical example computed with this repository's solver:
Both teaching examples below use the closed band [70,140] mg/dL, corrected value glucose − offset, and offsets 0 or 1.
| Example | Budget | Exact count bounds | Why |
|---|---|---|---|
| One reading: 141 | 0 | [0,1] | Offset 0 leaves 141 outside; offset 1 gives 140 inside. |
| Mask the middle: 70,100,141, weights 1,0,1 | 0 → 1 | [1,1] → [0,2] | The middle row adds no count; the retained rows still share one budget. |
These and the opening two-reading example are fully specified synthetic teaching cases. The BIG001 demo uses public observations.
211 public CGM series · 6,804 query cells per request mode · two independent replications (R1, R2). The series come from the PhysioNet BIG IDEAs cohort (16 participants) and the Shanghai diabetes CGM cohort (195 patient series). Each cell is an
| Budget |
LocalWindows / SparseGlobal | LocalWindows / GapComponents | LocalWindows / RunTailFullG | Regime |
|---|---|---|---|---|
| 1 | 0.838–0.846 (T 0.562–0.565) | 0.867–0.882 (T 0.679–0.686) | 0.488–0.499 (T 0.054–0.062) | Unit drift: faster than all baselines, |
| 4 | 0.856–0.861 (T 0.456–0.471) | 0.880–0.889 (T 0.543–0.557) | 0.614–0.620 (T 0.092–0.104) | Moderate drift: faster than all baselines, |
| 16 | 1.069–1.086 (T 0.830–0.880) | 1.040–1.058 (T 0.883–0.981) | 0.890–0.898 (T 0.298–0.309) | Transition: medians favor SparseGlobal and GapComponents; T and the RunTailFullG comparison favor LocalWindows |
| 64 | 1.291–1.293 (T 2.194–2.256) | 1.276–1.283 (T 2.169–2.203) | 1.411–1.416 (T 1.387–1.574) | Dense: baselines faster |
| Bank: 0, 1, 4, 16, 64 | 1.305–1.319 (T 2.095–2.185) | 1.269–1.275 (T 2.102–2.118) | 1.381–1.424 (T 1.298–1.604) | Dense: baselines faster |
Entries are the participant-level median geometric-mean runtime ratio (LocalWindows/baseline), shown as the range over replications R1 and R2, followed by the summed-time ratio
Why it is faster. On the 3,798 non-static cells (
Longer horizons. Summed-time ratio LocalWindows/SparseGlobal (R1 / R2), Table IV of the paper:
| Budget |
|
|
|
|
|---|---|---|---|---|
| 0.310 / 0.317 | 0.172 / 0.175 | 0.130 / 0.131 | 0.101 / 0.096 | |
| 0.332 / 0.340 | 0.203 / 0.224 | 0.181 / 0.180 | 0.163 / 0.177 | |
| 0.827 / 0.804 | 0.626 / 0.622 | 0.586 / 0.583 | 0.546 / 0.574 |
At
Scope. These are the paper's prepared-operator timings of C++20 implementations (g++ -O3) that share parsing, interval compilation and 64-bit integer arithmetic. In a cold, uncached source-to-report pipeline, file I/O and parsing (30–75 ms per 18-cell report) dominate the prepared operator (0.05–2.5 ms), and most 95% intervals on end-to-end report time span 1.0. The numbers above are not timings of this repository's Python package, native driver or browser tool. All tables, estimators and protocol →
BIG001 figure and data attribution
For public record BIG001,
The plotted data are from BIG IDEAs Lab Glycemic Variability and Wearable Device Data, version 1.1.3; attribution and figure details accompany the artwork. The optional BIG001 verifier takes a user-supplied official CSV; see the verification command.
@misc{wang2026rangecount,
title = {Exact Range-Count Envelopes Over Ordered Time Series Under Total-Variation Drift Budgets},
author = {Wang, Zijia and Toumazou, Christofer},
year = {2026},
url = {https://github.com/OliverDOU776/Exact-Temporal-Range-Queries-through-Local-State-Windows}
}The paper is included as docs/assets/paper.pdf. CITATION.cff carries the same reference.
Software and manuscript: Zijia Wang and Christofer Toumazou, Centre for Bio-Inspired Technology, Department of Electrical and Electronic Engineering, Imperial College London. The project page starts with worked examples and includes the browser query tool, the BIG001 example, the paper's results and runnable code.
The paper's evaluation uses BIG IDEAs Lab data v1.1.3 and Shanghai diabetes datasets v5. The clinical thresholds follow the international consensus on time in range (Battelino et al., Diabetes Care 42(8), 2019). Retain the third-party attribution and applicable licenses when reusing data or derived figures. See figure provenance and the third-party notices.




