Repository navigation
viewer: auto-scale initial gain to recording amplitude - #203
Conversation
Compute a robust amplitude estimate (median across channels of each channel's 95th percentile |signal|, DC-removed) from the first loaded window and use it to set the initial gain so a typical trace fills ~70% of its channel slot. Manual Scale +/- still overrides, and group switches re-arm the estimate without stomping a manual change. Falls back to the modality DEFAULT_SCALINGS (gain=1) when there is no usable estimate (silent/all-bad channels), so a divide-by-zero or Infinity gain is never produced.
Deploying nemar-website with
|
| Latest commit: |
f794855
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://169ff9fd.nemar-website.pages.dev |
| Branch Preview URL: | https://feature-issue-109-viewer-aut.nemar-website.pages.dev |
|
Test gap fixed, pushed. You were right that the existing case proved nothing: Split it into two tests:
Re-ran the same mutation against the new test: The comment records the threshold and why the denormal input is load-bearing, so nobody "simplifies" it back to a friendlier-looking number later. On your other two observations
Thanks for mutation-testing this rather than reading it — that is what turned up the one real gap. |
Closes #109.
Estimator
Auto-scale computes a robust per-recording amplitude from the first
loaded window (the same data the first paint renders):
estimateSignalAmplitude(src/lib/eeg-viewer/dsp.ts) takes, perchannel, the 95th percentile of
|sample|(non-finite samplesskipped), then takes the median across channels of those
per-channel values. Median (not mean or max) is what makes this
robust to a single dead/railed channel: it becomes one entry among
many rather than a value that drags a mean or dominates a max.
autoscaleGainconverts that amplitude into a gain that puts it at70% of a channel slot's half-height (the issue's "60-80%" target,
using the midpoint), falling back to
1(unchanged/default) forany zero, negative, or non-finite input so a silent/all-bad
recording can never produce a divide-by-zero or Infinity gain.
In
viewer.ts, the estimate is built fromwin.channelsof thefirst successful
readFrameresult (line/band channels, DC-removedthe same way the render path removes DC), so no extra network read is
added beyond what first paint already does.
Edge cases covered (unit tests in dsp.test.ts)
expected percentile/median)
no throw)
toward 0)
autoscaleGainfallback to1for zero/negative/NaN amplitude andnon-positive
physPerDivSI, and never returns Infinity/NaN for avanishingly small amplitude
Manual override + group switch
Scale +/- (buttons and
+/-keys) setgainManuallySet = true.Auto-scale only fires once per mount, and re-arms on a channel-group
switch to estimate the new group's own amplitude/modality -- but the
gainManuallySetguard means it never overwrites a gain the useralready touched.
Before/after scale-bar values (measured in the browser)
n/a, ~tens-of-units amplitude)on003810's store unit is
n/aso the absolute label ("V" instead of"uV") is a pre-existing, out-of-scope quirk (per the issue: "do not go
hunting for a conversion bug") -- the estimator works on the same
numeric scale as
physPerDiv, so the fit is correct regardless of thelabel.
What I verified in the browser
Ran
bun run dev, opened both datasets' detail pages, expanded theBIDS tree to a signal file (
sub-001/eeg/..._eeg.setfor on003800,sub-02/eeg/..._run-1_eeg.edffor on003810), and confirmed:recordings.
CanvasRenderingContext2D.fillTextto avoid any OCR/screenshotambiguity) reads
4.7 µV(on003800) and25 V(on003810) at firstpaint.
on003800 4.7 -> 3.1 uV (Scale+) -> 7.0 uV (Scale- x2); on003810 25 ->
38 V (Scale-).
back toward 1 (~34 Scale+ presses) -- traces clip into solid blocks,
confirming this is the exact problem viewer: auto-scale initial gain to each recording's amplitude #109 describes.
clean, legible traces at first paint.
Two independent browser sessions were used for this (a shared
Puppeteer tab kept getting hijacked mid-session by another agent's
concurrent navigation in this same workspace -- visible as the tab
jumping to an unrelated dataset page), so the on003800 result was
reproduced twice from a clean state before trusting it.
Scope note
Re-autoscale-on-group-switch is implemented (not skipped) per the
issue's optional suggestion, guarded by the same
gainManuallySetcheck. No "fit" hotkey/button was added -- out of scope for this issue
(auto-scale is about the initial value, not a manual re-fit control).
Coordination
Issue #199 (full-screen modal for the viewer) is in flight in
parallel and touches
src/pages/dataset/[id].astroand the mountpath. This PR only touches
src/lib/eeg-viewer/dsp.ts,src/lib/eeg-viewer/dsp.test.ts, andsrc/lib/eeg-viewer/viewer.ts.