← Back to Pull requests

Optimize colorspace conversions and improve achromatic colormap handling #238

Closed
unknown closed this pull request without merging on Sep 18, 2026 14:19 UTC
View on GitHub
unknown unknown commented on Sep 17, 2026 15:40 UTC
Author

I had Claude inspect #143 and current main and decide what the best optimization approach would be. It found some really good fixes and performance has been improved (see Claude's summary below). The biggest change though is Claude's addition of "_split_achromatic_control_points" to produce better interpolation. Basically if a colormap when converted to Lch space has a really low chroma (black/white/gray colors) it has an undefined hue so when you interpolate the hue between that color and the colors next to it you get essentially garbage. Claude's new function borrows the hue from neighboring colors. I think I had tried to do that back when I added this new Cython version of the colorspace conversion but it seemed like too much work. Here are the changes:

Diverging colormaps before/after/diff

<img width="1000" height="1044" alt="diverging_before_after" src="https://github.com/user-attachments/assets/21797bd2-d776-4a91-a480-529dd73ec4d5" />

Diverging colormaps center zoom

<img width="900" height="594" alt="diverging_center_zoom" src="https://github.com/user-attachments/assets/6fbea703-f002-40ec-8e33-3d9d1461c362" />

<details>

The zoom is on the middle of each colorbar — columns 175–325 of the 500-pixel-wide bar, i.e. the 35%–65% range of the colormap's values, centered on the white/near-white control point at 50%. That's where all the changes are (pixels 30–44 of 75 in the test arrays, columns ~200–300 of 500 in the bars); everything outside that window is bit-identical between main and this branch.

Three colormaps are shown, chosen as the largest-diff cases with different neighbor hues: prgn (purple → white → green), puor (orange → white → purple), rdgy (red → white → gray). Each pair is main on top, this branch below, scaled 6× horizontally and 3× vertically so the bands are visible.

</details>

Claudes performance summary:

Colorspace kernel, 4M pixels (2000×2000), ms

| Conversion | dtype | main | PR #143 | this branch | speedup vs main |
| --- | --- | ---: | ---: | ---: | ---: |
| lch2rgb | float64 | 319 | 374 | 213 | 1.5× |
| lch2rgb | float32 | 301 | 343 | 146 | 2.1× |
| rgb2lch | float64 | 342 | 410 | 365 | 0.9× |
| rgb2lch | float32 | 332 | 385 | 276 | 1.2× |

rgb2lch only runs on the colormap control points in colorize, so it does not affect the
end-to-end numbers. The PR #143 column is the in-place kernel from that branch built from its
own commits with the same harness.

Per-step cost, 4M pixels, ms (where the kernel time goes)

| Step | float64 main → new | float32 main → new | What changed |
| --- | ---: | ---: | --- |
| lab→xyz | 149 → 19 | 107 → 13 | pow(t, 3) → ttt |
| lab→lch | 116 → 78 | 126 → 98 | pow(x, 0.5) → sqrt |
| lch→lab | 72 → 71 | 73 → 38 | sinf/cosf for float32 (GCC already fuses sincos) |
| rgb→xyz | 159 → 146 | 183 → 89 | powf for float32; pow(x, 2.4) is the floor |
| xyz→rgb | 144 → 138 | 163 → 90 | powf for float32; pow(x, 1/2.4) is the floor |
| xyz→lab | 130 → 131 | 81 → 131 | glibc cbrt is slower than pow(x, 1/3), so pow was kept |

End-to-end Colormap.colorize, 2000×2000 array, ms

| Colormap | dtype | main | this branch | speedup |
| --- | --- | ---: | ---: | ---: |
| 4-color test colormap | float64 | 548 | 330 | 1.7× |
| 4-color test colormap | float32 | 551 | 285 | 1.9× |
| brbg | float64 | 494 | 296 | 1.7× |

Beyond the kernel, the end-to-end gain comes from dropping the per-pixel _ununwrap pass
(~44 ms), writing the RGB planes directly into a channel-first output instead of strided
stores plus a final np.stack (~50 ms), and keeping float32 data in float32 end to end.

Things measured and rejected

| Experiment | Result |
| --- | --- |
| In-place conversion on one (N, 3) array (PR #143) | 5–15% slower than main |
| -fno-math-errno | no measurable change |
| -march=native | no measurable change (libm calls dominate) |
| glibc cbrt instead of pow(x, 1/3) | same (f64) or slower (f32) |
| Generic-stride [:, :] views instead of [:, ::1] | no measurable change (118 vs 114 ms), enables planar output |
| sRGB companding LUT (4096 entries) | lch2rgb 235 → 112 ms f64, 162 → 85 ms f32; max error 1.7e-5; deferred, see spec |

One note on the xyz→lab float32 row: main's number is lower because it called double pow on a promoted value, while the new float32 path calls powf on the equivalent expression; the row is measured on the isolated step, and inside the fused rgb2lch loop the float32 total still comes out ahead (332 → 276 ms).

- [ ] Closes #xxxx (remove if there is no corresponding issue, which should only be the case for minor changes)
- [ ] Tests added (for all bug fixes or enhancements)
- [ ] Tests passed (for all non-documentation changes)
- [ ] Passes `pre-commit run --all-files` (remove if you did not edit any Python files)
- [ ] Fully documented (remove if this change should not be visible to users, e.g., if it is an internal clean-up, or if this is part of a larger project that will be documented later)

Diff: 645 lines (+483 / -162) across 8 files Reviewer: pnuu Review duration: 1358m
All Vouch review confidence checks passed
Residual risk 0.23 = Change Risk (0.57) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.57 · 645 lines diff across 8 files
0.57
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.387
Shallow Review Depth shallow_review_depth
0.289
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #238"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 57.0% (MEDIUM) · Higher = larger blast radius
57.0%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 22.5% · Safe to merge
22.5%