fix(violin): prevent FP drift in KDE sampling loop (avoid underrun/overrun on tiny spans) #7582
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.






Fixes #7581
Supersedes plotly/plotly.js#6490
Summary
Replace cumulative
t += steploop insrc/traces/violin/calc.jswith an index-based loop to eliminate floating-point drift when spans are extremely small (≈1e-13).With near-equal, high-precision values, the KDE sampling loop accumulates FP error via
t += step. Depending on drift direction this can:cdi.densityunfilled →Cannot read properties of undefined (reading 'v')(often seen via Kaleido).TimeoutErrorduring export.This PR computes
tfrom the integer index on each iteration sodensity.length === ndeterministically and avoids cumulative error.Change (logic only)
Why it works:
We already compute
n = Math.ceil(dist / (bandwidth/3))andstep = dist / n. Index-based sampling guarantees exactlynpoints with no cumulative drift, fixes both failure modes, and preserves existing spacing and scaling.Tests
Added a jasmine test in
test/jasmine/tests/violin_test.js:verified locally with:
Demo
https://codepen.io/Koookadooo/pen/PwZKrrv?editors=1111
calc_test.js
run with:
debug_html.py
to run, build my branch locally with:
or
and then run:
Impact
Cannot read properties of undefined (reading 'v')andTimeoutErrorin KDE sampling.