# Learning low-order wavefront correction

A simulated phase-diversity control pilot developed with Codex assistance.

## Scope

Three SAC policies were each trained for 50,000 environment steps. Each policy controls six sampled Zernike pupil-phase modes from a noisy pair of focus/diversity images plus command, horizon, and photon calibration. The comparison is an image-driven nominal-model phase-diversity least-squares controller. Final tests use 100 paired phase screens in each of five fixed conditions.

## Held-out results

| Condition | No correction, on-axis | Untrained SAC | Trained SAC, on-axis | Phase-diversity fit | Trained SAC, median OPD [nm] | Trained SAC, leakage reduction |
| --- | --- | --- | --- | --- | --- | --- |
| Nominal | 0.8519 | 0.8363 | 0.9936 | 1.0000 | 5.10 | 29.0× |
| 1,000 photons | 0.8519 | 0.8363 | 0.9873 | 0.9939 | 8.62 | 13.1× |
| Larger aberrations | 0.5259 | 0.5179 | 0.9931 | 0.9999 | 6.09 | 105.7× |
| Uncontrolled modes | 0.8194 | 0.8046 | 0.9525 | 0.9586 | 17.40 | 1.9× |
| 15% gain error | 0.8519 | 0.8314 | 0.9914 | 0.9999 | 5.80 | 22.6× |

On-axis ratios are noise-free physical diagnostics. The leakage reduction is the ratio of geometric-mean annular starlight before and after correction, using a fixed ideal PSF reference. The nominal-model fit receives the same measurement type and exposure budget and follows the same command constraints.

![Development learning curves](learning.png)

![Held-out controller comparison](heldout.png)

![One predetermined optical case](optical-comparison.png)

## Training and sensing costs

The runs used 150,000 training commands and approximately 350,004 simulated sensing exposures, excluding 12,096 development exposures and validation. Measured wall time across the three training seeds was 5.8 minutes on CPU. Each held-out episode used 14 sensing exposures for every controller. The least-squares controller also uses its nominal forward model internally, with no additional physical exposures for those computations.

The trajectory CSV field controller_cpu_ms records elapsed wall-clock controller latency from time.perf_counter, measured while running on CPU. It is separate from image acquisition and simulator diagnostics.

## Interpretation

Under nominal conditions, the mean on-axis ratio increased from 0.8519 without correction to 0.9936 across the three trained SAC policies. The phase-diversity fit reached 0.99995.

The learned correction reduced nominal geometric-mean annular starlight leakage by 29.0 times, with mean off-axis aperture throughput 0.9925. The conventional fit achieved a higher mean final on-axis ratio in all five conditions. These observations support learned correction within this simulator and identify a remaining gap to its strong nominal-model baseline.

Development curves fluctuate and differ between training seeds. Every seed is retained, and the final checkpoint is fixed by the training budget. The 15% mismatch test changes actuator gain specifically. The extra-mode condition introduces pupil distortions outside the control basis.

## Assumptions and limits

The pupil is unobstructed and monochromatic. Aberrations are static within a six-command episode. The actuator is an ideal six-mode pupil-phase corrector, and the control objective is measured non-coronagraphic on-axis intensity. An ideal coherent-mode projection supplies downstream coronagraphic diagnostics. The off-axis source at 3 lambda/D is a unit-flux throughput probe. These diagnostics establish simulated image quality, leakage, and throughput behavior within the model.

The experiment adapts the physical principle and first stage of Gutierrez et al. (2024). Training size, algorithm, optics, and dimensionality differ from the published numerical experiment. Hardware behavior, broad-band spectra, time-varying atmospheric turbulence, and planet-detection completeness require further evaluation. Gain mismatch refers specifically to actuator gain.

## Uncertainty and reproducibility

Summary intervals resample paired held-out phase-screen cases 2,000 times and condition on the three trained policies. Individual training-seed outcomes are retained in summary.json. Across conditions the same base seeds support matched stress tests. The source bundle includes final and untrained model weights, code, dependency lockfile, run metadata, protocol, numerical validation, learning curves, and complete trajectory measurements.

For a fresh reproduction, start from the source files in a new working directory, install with uv sync, then run validate.py, train.py, evaluate.py, and summarize.py in that order. Set OPENBLAS_NUM_THREADS=1 and OMP_NUM_THREADS=1 for the documented CPU configuration. The Python scripts set PyTorch intra- and inter-op threads to one where training or evaluation uses the network. Existing complete runs are validated and resumed rather than overwritten.

## Primary sources

- [Gutierrez et al., 2024](https://arxiv.org/abs/2407.18733)
- [HCIPy](https://hcipy.org/)
- [Stable-Baselines3 SAC](https://stable-baselines3.readthedocs.io/en/master/modules/sac.html)
- [Methods notes](methods-sources.md)
