Skip to content

Window the spectrum and the trace independently; fix post_filter for SHG - #29

Open
chrisbrahms wants to merge 14 commits into
LupoLab:mainfrom
chrisbrahms:fix/independent-spectral-windows
Open

chrisbrahms wants to merge 14 commits into
LupoLab:mainfrom
chrisbrahms:fix/independent-spectral-windows

Conversation

@chrisbrahms

@chrisbrahms chrisbrahms commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What changed

The spectrum gets its own window (#23). load_and_clean(..., lams_lims=None) windows the independent fundamental spectrum by its own wavelength range, stored as TraceData.lams_lims. Before, the spectrum was cut by the trace window λm mapped through the interaction's carrier scaling, so an SHG spectrum was cut at twice the trace window. None means the spectrum's full extent. Sessions gain lams_min_nm and lams_max_nm (default unset), and the Prepare stage gains a λs range control, seeded from the loaded spectrum.

post_filter no longer removes an SHG result (#22). The session pipeline and the retrieve stage now window the retrieved field with TraceData.fundamental_window: for SHG, λs (or the full grid when it is unset); for PG and SD, λm as before. Before, SHG was given the trace-domain window, which lies at the second harmonic. A regression test covers SHG; PG and SD keep their behaviour.

The marginal check has a trust region for each marginal (#23). auto_third_order_exponent takes trace_window and spectrum_window in place of window, and the marginal-check stage has two range controls. A measured spectrum's region in the GUI still defaults to 190–1000 nm, or to its full range if it lies wholly outside that band.

Behaviour changes users will notice

  • changed: an SHG spectrum is no longer windowed by 2λm, so its outer wings now reach the spectral regularisation target and the initial guess unless λs is narrowed. This applies to PG and SD too: a saved session that narrowed λm reads back with λs unset (the full spectrum).
  • breaking: auto_third_order_exponent(window=...) becomes trace_window= and spectrum_window=. To reproduce the old behaviour, pass the same window to both.

Verification

  • uv run poe check on the final commit: ruff, pyright (0 errors), codespell, changelog check, and 1931 tests passed (2 skipped) with 100 % statement and branch coverage.
  • uv run poe docs builds without warnings. Updated pages: howto/preprocessing, howto/gui, howto/grids_and_filtering, explanation/marginals, howto/marginal_checks.
  • Tests cover:
    • SHG load_and_clean with independent windows in both directions, and a PG case;
    • an old session file without the new keys;
    • the trust-window functions and their error paths;
    • the new GUI controls, driven through qtbot.
  • Two rounds of independent review, with the findings fixed.

Related

Closes #22. Closes #23. Touches #17 (the marginal-check numerics this changes still live partly in the GUI stage). Dispersive SHG (#24) builds on the λs window.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WM2AxMiUXc12ViRwSf2cyL

chrisbrahms and others added 9 commits November 12, 2026 14:52
The fundamental spectrum was windowed by the trace's λm mapped through
omega0_scale, so for SHG a trace window cut the spectrum at twice its
wavelength and vice versa. load_and_clean now takes lams_lims for the
spectrum (default: its full extent) and TraceData offers
fundamental_window for windowing the retrieved field.

Refs LupoLab#23

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxMiUXc12ViRwSf2cyL
apply_post_filter and the retrieve stage passed the trace window lamm_lims
to post_filter, which measures it from the fundamental carrier. For SHG the
window then lay above the grid and removed the whole spectrum. Both now use
TraceData.fundamental_window.

Refs LupoLab#22

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxMiUXc12ViRwSf2cyL
PreprocParams gains lams_min_nm/lams_max_nm (None: the spectrum's full
extent, which is also how older options files read back), seeded from the
loaded spectrum and dropped with it. The Prepare stage gets a λs control
next to λm.

Refs LupoLab#23

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxMiUXc12ViRwSf2cyL
auto_third_order_exponent takes trace_window and spectrum_window in place
of the shared window, and the marginal-check stage has one control for
each. For SHG the spectrum lies an octave below the marginal, so a shared
region could not hold both.

Refs LupoLab#23

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxMiUXc12ViRwSf2cyL
- Reject a spectrum window that is reversed, empty or misses the spectrum,
  and fall back to the full extent when a replaced spectrum would clamp
  the kept window to zero width.
- Raise on a half-set window in a session; take the PG/SD fallback from
  the interaction rather than comparing carriers.
- Native-grid simulated loads window the field to the signal band.
- The retrieve stage's spectral post-filter is off, and says why, when
  there is no window at the fundamental (SHG without a spectrum).
- Regression tests through the session pipeline and the retrieve stage
  that fail if the trace window is passed again; drop a test that could
  not fail.
- Name which trust window an error refers to; say that PG/SD sessions
  that narrowed λm lose their spectrum taper on reload.

Refs LupoLab#22, LupoLab#23

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxMiUXc12ViRwSf2cyL
…0-1000 nm

For PG and SD the trace window is the signal's measured support at the
fundamental, so the post-filter keeps using it; the spectrum window serves
SHG only. A measured spectrum's trust region defaults to its range within
190-1000 nm, as the shared region did, or its full range outside that band.

Refs LupoLab#22, LupoLab#23

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxMiUXc12ViRwSf2cyL
- The spectrum window check uses filter_spectrum's open band and rejects a
  window without positive intensity.
- The retrieve stage refreshes the spectral post-filter's availability on
  re-entry, so a re-prepared trace cannot leave it stale.
- Text that called λs the post-filter window now says so for SHG only.
- A native-grid simulated spectrum's window is its full extent.
- Tests for the spectrum trust-window message and the simulated entry.

Refs LupoLab#22, LupoLab#23

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxMiUXc12ViRwSf2cyL
@chrisbrahms chrisbrahms added bug Something isn't working enhancement New feature or request area: preprocessing Loading, preprocessing, delay origin and marginal checks labels Nov 13, 2026
@read-the-docs-community

read-the-docs-community Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@jtravs jtravs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. The physics is right, the post-filter fix for #22 is correct, and the regression tests would genuinely fail if the trace window were passed again. A few points, mostly about default windows, before merging.

What's right

  • fundamental_window. λm for PG/SD (the trace sits at the fundamental and its support bounds the field's) and λs for SHG; the 650 + 1000 nm → 394 nm example is exactly why a doubled λm is not a bound. Returning None for SHG without a spectrum, and disabling the retrieve stage's spectral post-filter with a tooltip, is the honest choice.
  • The split. The omega0_scale mapping is gone, the trace taper is unchanged, and _check_spectrum_window fails loudly on a reversed, empty or dark window. With λs at the spectrum's full extent, filter_spectrum's taper lies outside the data, so "unset" really means "no window".
  • Tests. Both directions for SHG, an old options file, a half-set window, and regressions through apply_post_filter and the retrieve stage.

Points to address

  1. The marginal check's default spectrum trust region clips spectra that cross 1 µm. A measured spectrum's region is clipped to 190–1000 nm, falling back to the full range only when the spectrum lies wholly outside that band. A 1030 nm Yb spectrum measured on a 900–1100 nm spectrometer gets 900–1000 nm, which cuts off the peak; the predicted marginal and the Auto exponent's target then come from a sliver of one wing. The shared region did the same before, so this is not new, but 190–1000 nm was a heuristic for the trace detector, and the split is the natural moment to drop it for the spectrum. Suggest defaulting the spectrum region to its full range, like Prepare's λs.

  2. The two spectrum windows default differently, and one item of #23 is not done. Prepare's λs defaults to the full extent while the marginal check's spectrum region defaults to the clipped band, so noisy wings excluded from the check still reach the spectral regularisation target. #23's to-do says "seed the Prepare windows from them" (from the trust regions); that isn't done and the PR doesn't mention it. Either do it here (after fixing point 1) or say so in the description and open a follow-up issue.

  3. PG/SD behaviour changes, which #23 lists under "Must not change".

    • With default windows the PG/SD spectrum used to be tapered at the trace's extent; now it is not windowed at all. Usually harmless, since the retrieval grid bounds it.
    • A saved session that narrowed λm silently reloads with a different spectrum taper, and sessions carry no version to detect this. It is documented in the changelog and docs, but nothing warns at load time. A cheap alternative: when a file has no lams_* keys, set λs from λm × omega0_scale on read, which reproduces old sessions exactly (SHG included). Maintainer's call whether to migrate or accept the documented change.
  4. Small things.

    • When the spectrum trust region misses the spectrum, the status reads "No independent spectrum: showing the trace marginal only." There is a spectrum, so the message is misleading (a test asserts that text).
    • Some edited paragraphs in docs/explanation/marginals.md, docs/howto/gui.md and docs/howto/marginal_checks.md were not re-wrapped and have very long lines.
    • With the default λs (the spectrometer's full range) the SHG spectral post-filter does essentially nothing. Correct, but the help text could say so.

The auto_third_order_exponent rename and its new spectrum_window default (whole spectrum rather than 210–1000 nm) are correctly described in the breaking fragment.


Drafted with Claude Code.

chrisbrahms and others added 5 commits November 17, 2026 10:45
A measured spectrum's trust region was clipped to the trace detector's
190-1000 nm band, which cut a spectrum crossing 1 um down to one wing.
It now defaults to the spectrum's full range, as Prepare's lams does.
The status also says which input is missing when there is no summary:
no spectrum, no usable spectrum in its trust region, or too little trace
marginal in the trace's trust region.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
Bump options_version to 3. A file older than that, with lamm but no
lams keys, reads back with lams = lamm x omega0_scale of its loader's
interaction, which reproduces the old spectrum taper. A current file
omits an unset lams too, so the version tells the two apart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
A migrated session has a spectrum window but no bounds, so Prepare
showed lams disabled while the window still applied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WM2AxPM9w8Mg7mfKikaeCX
@chrisbrahms

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Drafted by Claude (AI) on behalf of the author; the changes are in 70c21b6..c242656.

  1. Spectrum trust region. Done (70c21b6). A spectrum's trust region now defaults to its full range whether it was measured or simulated, the same as Prepare's λs. The trace keeps its 190–1000 nm default. There is a new test for a 900–1100 nm Yb spectrum. The breaking-change fragment and the docs now describe the new default.

  2. Seeding Prepare from the trust regions. Not done here. It is Seed the Prepare stage's windows from the marginal-check trust regions #30, opened earlier as a follow-up. With point 1 fixed, both spectrum windows default to the full range, so for windows the user has not changed the two no longer disagree.

  3. Old sessions. Migrated (c3996f2, d35a904). Old and new files can't be told apart by their keys: _tomlify drops None, so a new file whose λs is the full extent also has no lams_* keys. So OPTIONS_VERSION goes from 2 to 3. SessionOptions.from_dict sets λs = λm × omega0_scale only when all of these hold:

    • the file is older than version 3;
    • it is an experimental or simulated session;
    • it has λm and no λs.

    This reproduces the old spectrum taper exactly, for PG, SD and SHG. The λs slider bounds are data extents, so they are not migrated. On reopen the GUI fills them from the reloaded spectrum and clamps the window to it. A migrated window that misses the spectrum falls back to the full extent in the GUI. Headless replay raises the clear _check_spectrum_window error, where the old code failed on an empty array. One consequence to note: for a migrated SHG session the post-filter now uses λs = 2λm, the window that actually tapered the spectrum before.

  4. Small things.

    • The status line now tells three cases apart: no spectrum; a spectrum with nothing usable inside its trust region; and a blank trace marginal (that last case was also mislabelled before).
    • The edited paragraphs in the three docs pages are re-wrapped.
    • The help text and docs now say the SHG spectral post-filter does essentially nothing at the default λs; narrow λs in Prepare to make it act (c242656).

uv run poe check passes with 100% coverage, and uv run poe docs builds without warnings.

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 bug Something isn't working enhancement New feature or request

Projects

None yet

2 participants