Skip to content

Handle extreme outliers in 1D quantization by rebuilding histogram over trimmed range#190

Merged
slimbuck merged 4 commits intoplaycanvas:mainfrom
slimbuck:sog-dev
Mar 23, 2026
Merged

Handle extreme outliers in 1D quantization by rebuilding histogram over trimmed range#190
slimbuck merged 4 commits intoplaycanvas:mainfrom
slimbuck:sog-dev

Conversation

@slimbuck
Copy link
Copy Markdown
Member

Summary

  • When extreme outliers stretch the histogram range so wide that most bins are empty (non-empty bins < k), the quantizer now rebuilds the histogram over a percentile-trimmed range (0.1% from each tail) to restore bin resolution
  • Outlier values are clamped into edge bins rather than discarded, so no data is lost — they just no longer dominate the range and starve the codebook of useful centroids

Technical Details

The quantize1d function builds a 1024-bin histogram between dataMin and dataMax, then uses DP to find optimal centroids. When a handful of extreme outliers inflate the range, nearly all values cluster into a tiny fraction of the bins, leaving most bins empty. If nonEmpty < k, the DP cannot produce k distinct centroids, resulting in a degraded codebook.

The fix detects this condition after the initial histogram build and, when triggered:

  1. Walks inward from both tails to find the bins containing the 0.1th percentile boundaries
  2. Recomputes newMin/newMax from those bins
  3. Rebuilds counts, sums, centers, weights, and all prefix-sum arrays over the tighter range
  4. Recalculates nonEmpty before proceeding to the DP solver

Values outside the trimmed range are clamped to bin 0 or bin H-1, preserving their contribution without wasting resolution on near-empty stretches of the number line.

Test Plan

  • Verify quantization quality on scenes with known extreme outlier splats (e.g., scale values spanning many orders of magnitude)
  • Confirm no regression on clean scenes where outliers are absent (the rebuild path is skipped when nonEmpty >= k)
  • Check that codebook centroids are well-distributed across the value range after the fix

@slimbuck slimbuck requested review from a team and Copilot March 23, 2026 12:03
@slimbuck slimbuck self-assigned this Mar 23, 2026
@slimbuck slimbuck added the bug Something isn't working label Mar 23, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the 1D quantizer’s histogram pre-processing to better handle datasets with extreme outliers that otherwise make the histogram too sparse to support k distinct centroids.

Changes:

  • Detects when the initial histogram has fewer non-empty bins than k, and rebuilds the histogram over a percentile-trimmed range.
  • Clamps values outside the trimmed range into the edge bins so outliers still contribute without dominating histogram range.
  • Recomputes bin centers, weights, and prefix sums after rebuilding before running the DP solver.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lib/spatial/quantize-1d.ts Outdated
Comment thread src/lib/spatial/quantize-1d.ts Outdated
Comment thread src/lib/spatial/quantize-1d.ts Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lib/spatial/quantize-1d.ts Outdated
Comment thread src/lib/spatial/quantize-1d.ts
Comment thread src/lib/spatial/quantize-1d.ts
Comment thread src/lib/spatial/quantize-1d.ts Outdated
@slimbuck slimbuck marked this pull request as ready for review March 23, 2026 13:46
@slimbuck slimbuck merged commit 15124f7 into playcanvas:main Mar 23, 2026
3 checks passed
@slimbuck slimbuck deleted the sog-dev branch March 23, 2026 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants