Optimize colorspace conversions and improve achromatic colormap handling #238
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
pnuu
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>
</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× |rgb2lchonly runs on the colormap control points incolorize, so it does not affect theend-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/cosffor float32 (GCC already fusessincos) ||
rgb→xyz| 159 → 146 | 183 → 89 |powffor float32;pow(x, 2.4)is the floor ||
xyz→rgb| 144 → 138 | 163 → 90 |powffor float32;pow(x, 1/2.4)is the floor ||
xyz→lab| 130 → 131 | 81 → 131 | glibccbrtis slower thanpow(x, 1/3), sopowwas 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
_ununwrappass(~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
cbrtinstead ofpow(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) |
lch2rgb235 → 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)