Skip to content

Feat/hyperspy parity 0.3.0#33

Merged
CSSFrancis merged 4 commits into
mainfrom
feat/hyperspy-parity-0.3.0
Jul 14, 2026
Merged

Feat/hyperspy parity 0.3.0#33
CSSFrancis merged 4 commits into
mainfrom
feat/hyperspy-parity-0.3.0

Conversation

@CSSFrancis

Copy link
Copy Markdown
Owner

Follow up to reach parity with matplotlib for use in hyperspy. Mostly adding in things like cycling colors and adding the ability to twin an axis.

…e size

A host (SpyDE's 3-D IPF window) mounts a scatter3d/voxels figure HIDDEN
(display:none) and reveals it later. The first draw3d then ran with the panel
at zero drawable size (pw/ph == 0 from a zero-size initial layout), so
_gpuInitPanel configured the WebGPU swapchain against a zero-size canvas and
the draw pass called getCurrentTexture() on it. On a zero-drawable-size surface
that is undefined behaviour: some drivers throw (the mid-draw catch latches
p._gpu = 'unavailable', which is terminal because the `_gpu === undefined` init
guard never re-runs) and others configure a dead 0x0 context that silently
draws nothing. Either way the panel stayed on the Canvas2D fallback forever,
even after being revealed at a real size.

Fix:
- draw3d gates the undefined -> pending transition on pw > 0 && ph > 0, so a
  zero-size panel keeps _gpu === undefined and re-attempts init on a later draw
  at the real size (the canvas path renders meanwhile; a hidden panel isn't
  visible anyway). Applies equally to points and voxels (same init).
- A ResizeObserver reveal branch fires redrawAll() when the observed container
  transitions from zero-size to a real size, so a reveal by a pure display
  toggle (no layout_json / fig_width model change) still re-attempts init.

Verified on a real WebGPU adapter (headless Chromium): a hidden zero-size
scatter3d/voxels figure activates WebGPU after reveal, while a normally-visible
figure still activates on first draw. A fake strict-driver test proves the
terminal 'unavailable' latch no longer fires at zero size even without an
adapter. New: tests/test_plot3d/test_gpu_hidden_reveal.py.
Add add_text(x, y, s, ...) to the shared _MarkerMixin, wrapping add_texts
for the common single-label case (navigation index, scale-bar label).
Returns a _TextHandle exposing set_text / set_color / remove so callers
can mutate the label after creation.

Backs the HyperSpy anyplotlib backend, whose add_text calls
plot.add_text(x, y, s) and relies on those handle methods; previously
only add_texts (plural) existed, so the backend fell back to a
non-rendering sentinel and nav-index / scale-bar labels never appeared.

Adds 12 tests covering both Plot1D and Plot2D, the data/axes/display
transforms, set_text / set_color / remove, and transform validation.

Claude-Session: https://claude.ai/code/session_018MLraeaoQBWFDScg72W41h
Add an independent right-hand y-axis to Plot1D so overlay curves on a
different scale (e.g. a temperature trace over a spectrum) render with
their own axis instead of being squashed onto the left scale.

Python (plot1d/_plot1d.py):
- add_right_axis(color) / remove_right_axis() / set_right_ylabel() /
  set_right_ylim() / get_right_ylim()
- add_line(..., axis="left"|"right"); right lines set right_axis=True and
  feed a separate auto-range (right_data_min/max), excluded from the left
  data range so the left scale is unaffected
- new state: right_axis, right_y_range, right_data_min/max, right_y_units,
  right_axis_color

Renderer (figure_esm.js):
- draw1d computes a second linear scale (right_y_range or auto) and a
  _toRightY mapper; _drawLine takes an optional y-map so axis=="right"
  overlays use it
- draws the right spine, coloured tick column, and rotated units label in
  a widened right gutter (only when right_axis is on, so existing 1D plots
  are unchanged)

Tests: 13 unit tests for the state API + a plot1d_twinx visual baseline
(headless Chromium) verifying the two scales render independently.

Claude-Session: https://claude.ai/code/session_018MLraeaoQBWFDScg72W41h
…ollections

Follow-up to the 0.3.0 parity work, addressing code-review findings:

- Line1D gains x/data read-back and color/linewidth/linestyle/alpha
  get+set accessors (backed by the extra_lines entry).  Previously an
  overlay/right-axis line handle exposed none of these, so a consumer
  reading handle.x crashed and setting handle.color silently no-oped.
- Line1D.set_data now recomputes the RIGHT range when the line is a
  right-axis overlay (it was only ever refreshing the left range, which
  excludes right lines), so a navigating twinx curve no longer renders
  against a frozen right scale.
- remove_right_axis also resets right_axis_color, and documents that it
  is a full teardown (a pinned range/label does not survive re-add).
- figure_esm.js: the right-axis tick loop iterates the ordered range so
  an inverted set_right_ylim(hi,lo) still draws tick labels (the flip is
  still honoured by the line mapping); right gutter widened 46→58px so
  the rotated units label clears the tick numbers.
- markers.py: an empty `lines` collection (segments=[]) is now valid and
  renders nothing instead of raising "segments must be shape (N,2,2)".

Tests: Line1D accessor + right-range recompute + empty-lines cases;
twinx visual baseline refreshed for the wider gutter.

Claude-Session: https://claude.ai/code/session_018MLraeaoQBWFDScg72W41h
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.36364% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.18%. Comparing base (c3f4d9b) to head (9b0878f).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
anyplotlib/plot1d/_plot1d.py 95.78% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #33      +/-   ##
==========================================
+ Coverage   90.00%   90.18%   +0.18%     
==========================================
  Files          39       39              
  Lines        3801     3902     +101     
==========================================
+ Hits         3421     3519      +98     
- Misses        380      383       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@CSSFrancis CSSFrancis merged commit 60d03d3 into main Jul 14, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants