Skip to content

Add RectangularSplineAdapt{Density,Image} meshes for gradient-based samplers#289

Merged
Jammy2211 merged 1 commit intomainfrom
feature/rectangular-spline-mesh
Apr 22, 2026
Merged

Add RectangularSplineAdapt{Density,Image} meshes for gradient-based samplers#289
Jammy2211 merged 1 commit intomainfrom
feature/rectangular-spline-mesh

Conversation

@Jammy2211
Copy link
Copy Markdown
Collaborator

Summary

  • New RectangularSplineAdaptDensity and RectangularSplineAdaptImage meshes using a polynomial + Hermite-spline CDF transform (deg=11 default, tunable) instead of the empirical-CDF linear interp used by the existing adapt meshes.
  • Subclass relationship so existing isinstance(InterpolatorRectangular) dispatch in plot/inversion.py routes the spline source-plane reconstruction through _plot_rectangular automatically.
  • MeshGeometryRectangular carries a spline_deg and dispatches areas_transformed / edges_transformed through the spline helpers — required for AdaptiveBrightness regularization consistency.

Why

User is implementing gradient-based samplers. The empirical-CDF linear interp gives piecewise-constant gradients with 1/Δknot spikes where traced points crowd together — noisy under HMC symplectic integrators. The spline CDF is C¹ end-to-end and passes through jax.grad cleanly. Ported from z_staging/rect_adap_spline_invert_jax (1).ipynb (RSE prototype) in xp-aware form.

API changes

  • aa.mesh.RectangularSplineAdaptDensity(shape, spline_deg=11)
  • aa.mesh.RectangularSplineAdaptImage(shape, weight_power=1.0, weight_floor=0.0, spline_deg=11)
  • MeshGeometryRectangular.__init__ now accepts an optional spline_deg kwarg (default None → existing linear-CDF behaviour).

Scripts Changed

None in this repo. See autolens_workspace_developer PR (linked after that one is opened) for two new benchmark scripts.

Test plan

  • pytest test_autoarray/inversion/pixelization/mesh/test_rectangular_spline.py — 12/12 green (pure numpy, per no-JAX-in-unit-tests rule).
  • pytest test_autoarray/inversion/pixelization/ — 60/60 green (no regression in existing rectangular / delaunay / border tests).
  • Eager end-to-end fit on HST dataset at simulator truth lens parameters: both meshes reconstruct a concentrated central source; spline fom +26,239.6 vs linear +26,232.1 (delta +7.5, ≲3e-4 relative).

Known limitations

Under JIT with use_positive_only_solver=False the spline log_L shows small oscillations (~1e5 in std of first-difference) across a ±5% einstein_radius sweep where the linear path is monotone. Tracked in autolens_workspace_developer follow-up prompt (linked in the workspace PR) — shipping now as opt-in while the gradient investigation continues.

🤖 Generated with Claude Code

New spline-CDF variants of the adaptive rectangular meshes intended for
gradient-based samplers. Replaces the C⁰ empirical-CDF linear-interp
transform with a degree-11 polynomial fit to the inverse CDF plus a
cubic-Hermite spline inverter — C¹ continuous and JAX-autograd-compatible.
xp-aware so numpy and jax.numpy share one algorithm.

- `interpolator/rectangular_spline.py`: `InvertPolySpline`, transforms
  factory, adaptive mappings/areas helpers, `InterpolatorRectangularSpline`
  (subclass of `InterpolatorRectangular` so plot-dispatch isinstance
  checks work without further changes).
- `mesh/rectangular_spline_adapt_{density,image}.py`: two new mesh
  classes subclassing the existing density / image adapt meshes.
- `mesh_geometry/rectangular.py` + `abstract.py`: `MeshGeometryRectangular`
  takes `spline_deg` and dispatches `areas_transformed` /
  `edges_transformed` to the spline helpers when set. Linear path
  unchanged.
- `mesh/__init__.py`: export the two new classes.
- 12 new unit tests; 60/60 existing pixelization tests still pass.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@Jammy2211
Copy link
Copy Markdown
Collaborator Author

@Jammy2211 Jammy2211 merged commit 4ea58e1 into main Apr 22, 2026
4 checks passed
@Jammy2211 Jammy2211 deleted the feature/rectangular-spline-mesh branch April 22, 2026 13:47
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.

1 participant