Skip to content

Predict and correct the SHG marginal through a crystal - #39

Open
chrisbrahms wants to merge 15 commits into
LupoLab:feature/shg-smearingfrom
chrisbrahms:feature/shg-marginal-correction
Open

chrisbrahms wants to merge 15 commits into
LupoLab:feature/shg-smearingfrom
chrisbrahms:feature/shg-marginal-correction

Conversation

@chrisbrahms

@chrisbrahms chrisbrahms commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Important

This PR is part of a stack. Each PR builds on the one before it. Review and merge them in this order:

  1. Model SHG in a Type I crystal in the forward models #31: SHG crystal core: croak.crystals and the forward models
  2. Thread the SHG crystal through the solvers, sessions and GUI, with BBO #37: threading through solvers, sessions and GUI; BBO
  3. Two-beam delay smearing for SHG FROG #38: two-beam SHG delay smearing
  4. Predict and correct the SHG marginal through a crystal #39: SHG marginal through a crystal ← this PR

This PR's base is #38's branch (feature/shg-smearing), so its diff shows only its own changes. Merge the PRs bottom-up. After each merge, retarget the next PR onto main.

What changed

The marginal checks and the SHG marginal correction can now predict the marginal through a Type I crystal.

  • croak.crystals.shg_marginal gives the crystal model's delay-summed marginal. predicted_marginal, predicted_centroid and apply_marginal_correction take a croak.crystals.PhaseMatching (the crystal and its thickness) together with the pulse carrier.
  • load_and_clean(marginal_correct=True, marginal_crystal=...) corrects a measured trace onto the crystal's marginal. This removes the instrument's spectral response but keeps the phase matching, which the crystal model needs. The crystal used is recorded as TraceData.marginal_crystal.
  • Retrieval. A retrieval refuses a corrected trace only when its medium differs from the one the correction used. Before, it refused every corrected trace when a crystal was set.
  • Sessions correct through the retrieval's crystal. The trace is prepared again whenever the correction or its crystal changes.
  • GUI. The Data stage shows the crystal and beam settings, and its Check step uses them.
  • Docs. The marginal-check how-to gains a section on SHG through a crystal. The marginals explanation derives the SHG marginal through a crystal. There is a changelog fragment.

Verification

  • uv run poe check passes on the final commit: the full suite at 100 % statement and branch coverage. uv run poe docs builds without warnings.
  • The tests check:
    • that shg_marginal equals the crystal forward model's trace summed over delay. This is checked collinear, crossing out of the principal plane, and crossing in it with the delayed beam on either side, at 0, 20 and 100 µm;
    • the zero-thickness limit, which is the autoconvolution times the prefactor;
    • that a correction removes a spectral response but keeps the crystal;
    • retrieval through the same crystal, and refusal of a different medium;
    • a thin correction with a thin model.
  • Each block had an independent review, and the findings were fixed.

Related

Closes #25. Part of #24. Stacked on #38. This PR's base is not main, so #25 has to be closed by hand when this reaches main. Related: #35, where the SHG marginal correction scales the regrid's tapered edge rows back up. This PR does not change that.

Drafted by Claude (AI).

🤖 Generated with Claude Code

https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX

chrisbrahms and others added 15 commits November 15, 2026 17:31
croak.crystals.shg_marginal gives the crystal model's delay-summed SHG
marginal (the autoconvolution filtered by the pairs' sinc^2 phase matching,
with the principal-plane drift, weighted by the generation prefactor).
predicted_marginal, predicted_centroid and apply_marginal_correction take
an optional PhaseMatching (crystal + thickness), and load_and_clean's
marginal_correct can target it (marginal_crystal), so the correction
removes the instrument's response and leaves the phase matching for the
crystal model. TraceData records the crystal; the retrieval refuses a
corrected trace retrieved through a different medium instead of refusing
every corrected trace with a crystal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
build_tracedata takes the crystal the trace was measured through and hands
it to the marginal correction; marginal_crystal derives it from the
retrieval settings, so the correction and the retrieval always describe
the same medium. The session engine and exported scripts pass it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
correction_crystal picks the crystal build_tracedata corrects through (only
when the correction is on) and pulse_carrier gives the carrier the retrieval
grid will have, so a prediction made before preprocessing matches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
- Refuse a generation envelope (depth_weight) on a crystal-corrected trace:
  it reweights the depth integral, so the marginal is no longer the one
  the correction targeted.
- Refuse the session's thickness uncertainty on a crystal-corrected trace
  (the draws would misfit a marginal fixed to the central thickness), and
  say so in thickness_bootstrap's docstring.
- Check that a crystal correction gets the trace's centred grid, and that
  shg_marginal's inputs are finite.
- Say in the guard's message that the same CrystalSetup is needed.
- Generated scripts pick the crystal with correction_crystal, as the engine.
- Tests: odd grids, an end-to-end correction on a grid whose carrier is
  not the simulation's, and the new refusals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
The marginals explanation derives the crystal marginal; the marginal-check
and SHG-crystal how-tos show the check and the correction through a
crystal and what the retrieval then requires. The pending crystal
integration fragment no longer says a crystal refuses a corrected trace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
The crystal and the beams describe the apparatus, and the Check stage
needs them before Retrieve. One CrystalPanel, bound to state.retrieve,
now shows on the measured and synthetic Data pages for SHG; Retrieve
keeps a read-only summary and the delay-smearing switch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
The Check stage predicts, labels and previews the correction through
the crystal when it is modelled, falls back to the thin medium with a
warning when the settings give no crystal, and says in the correction's
tooltip what it corrects onto.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
Prepare resolves the correction's crystal with its parameter snapshot
and hands it to build_tracedata, inline and in the worker; invalid
crystal settings are a status error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
… stage in the docs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
Toggling the marginal correction on the Check step, or editing the crystal
on the Data stage while the correction is on, left the prepared trace in
place, and Prepare only recomputes when there is none: the retrieval then
used a trace prepared with the old setting (or, through the crystal,
refused it with a medium mismatch). Both now drop the prepared trace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
From the model's signal: the pair's depth-dependent phase, the frame
dropping out to leave the true mismatch, the analytic depth integral and
Parseval over delay; then why the principal-plane drift enters at second
order, smearing and zero thickness, and the assumptions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
# Conflicts:
#	changelog.d/shg-crystal-integration.added.md
@read-the-docs-community

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: preprocessing Loading, preprocessing, delay origin and marginal checks enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant