Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/post-filter-shg.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The post-retrieval spectral window no longer removes the whole retrieved field for SHG: the session pipeline and the retrieve stage now window with `TraceData.fundamental_window` (for SHG the spectrum window λs, for PG/SD the trace window λm as before) rather than the trace window, which for SHG lies at the second harmonic (#22).
1 change: 1 addition & 0 deletions changelog.d/spectrum-window.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The independent fundamental spectrum is windowed by its own wavelength range, `lams_lims` in `load_and_clean` (λs in the Prepare stage; `lams_min_nm`/`lams_max_nm` in a session), instead of the trace window λm mapped through the interaction's carrier scaling. It defaults to the spectrum's full extent, so an SHG spectrum is no longer cut by twice the trace window, and the spectrum's 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, and so also tapered its spectrum, reads back with λs unset (the spectrum's full extent); set λs to taper the spectrum again (#23).
1 change: 1 addition & 0 deletions changelog.d/trust-windows.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`auto_third_order_exponent` takes `trace_window` (the trace marginal's wavelengths) and `spectrum_window` (the independent spectrum's; default its full range, where `window` used to apply 210-1000 nm to both) in place of the single `window`, and the marginal-check stage has a trust region for each. A measured spectrum's region in the GUI still defaults to 190-1000 nm (its full range if it lies wholly outside that band, as an SHG fundamental beyond 1 µm can). Replace `window=w` with `trace_window=w`, and pass `spectrum_window=w` as well to reproduce the old behaviour exactly (#23).
25 changes: 25 additions & 0 deletions croak/gui/stage_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"tau_bound_max_fs",
"lamm_bound_min_nm",
"lamm_bound_max_nm",
"lams_bound_min_nm",
"lams_bound_max_nm",
"lam_bound_min_nm",
"lam_bound_max_nm",
)
Expand Down Expand Up @@ -109,6 +111,16 @@ def compute_preproc_defaults(data) -> PreprocParams:
pp.lamm_min_nm, pp.lamm_max_nm = lmin, lmax
pp.lamm_bound_min_nm, pp.lamm_bound_max_nm = lmin, lmax

# independent-spectrum window (fundamental λ): its own extents, if present
if data["lam_spec"] is not None:
# round off the m→nm conversion error first, so a spectrum ending at
# exactly 1000 nm does not ceil to 1001 nm
spec_nm = np.round(np.asarray(data["lam_spec"]) / 1e-9, 6)
smin = float(np.floor(spec_nm.min()))
smax = float(np.ceil(spec_nm.max()))
pp.lams_min_nm, pp.lams_max_nm = smin, smax
pp.lams_bound_min_nm, pp.lams_bound_max_nm = smin, smax

# retrieval grid λ: default to the trace extents × ω0 scaling (= fundamental
# range). Bounds stay generous so the user can widen the grid if needed.
scale = get_interaction(data["interaction"]).omega0_scale
Expand Down Expand Up @@ -146,6 +158,19 @@ def _clamp_preproc_to_bounds(pp: PreprocParams) -> None:
pp.lamm_max_nm = min(
max(pp.lamm_max_nm, pp.lamm_bound_min_nm), pp.lamm_bound_max_nm
)
if pp.lams_bound_min_nm is None or pp.lams_bound_max_nm is None:
# no spectrum (any more): its window goes with it
pp.lams_min_nm = pp.lams_max_nm = None
elif pp.lams_min_nm is not None and pp.lams_max_nm is not None:
lo, hi = pp.lams_bound_min_nm, pp.lams_bound_max_nm
low = min(max(pp.lams_min_nm, lo), hi)
high = min(max(pp.lams_max_nm, lo), hi)
# A window that misses a replaced spectrum would clamp to zero width at
# its edge; fall back to the full extent rather than keep a useless one.
if low < high:
pp.lams_min_nm, pp.lams_max_nm = low, high
else:
pp.lams_min_nm = pp.lams_max_nm = None
for attr in ("tau_min_fs", "tau_max_fs", "taum_min_fs", "taum_max_fs"):
v = getattr(pp, attr)
setattr(pp, attr, min(max(v, pp.tau_bound_min_fs), pp.tau_bound_max_fs))
Expand Down
123 changes: 97 additions & 26 deletions croak/gui/stage_marginal_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
efficiency correction — with an ``Auto`` that matches the marginal centroid onto
the prediction.

The trust region (190–1000 nm for a measured trace; the full loaded range for
the noise-free simulated/synthetic traces) bounds the peak finding, the
Two trust regions bound what enters the checks, one per instrument. The
**trace** trust region (190–1000 nm for a measured trace; the full loaded range
for the noise-free simulated/synthetic traces) bounds the peak finding, the
centroid/width moments, the marginal normalisation and the trace display
scaling, so out-of-band detector noise — strongly amplified by the tilt — cannot
distort either the checks or the colour scaling (the cause of the load preview
"saturating" as the exponent was raised).
"saturating" as the exponent was raised). The **spectrum** trust region (a
measured spectrum's range within 190–1000 nm, else its full range) windows the
independent spectrum before the marginal is predicted from it. They are
separate because the two are measured by different instruments and, for SHG,
lie an octave apart.

The marginals are plotted against frequency by default: a marginal is a
frequency density and the convolution/centroid/width identities live in
Expand Down Expand Up @@ -84,18 +89,29 @@ class StageMarginalCheck(Stage):
)
HELP = [
(
"Spectral trust region",
"Spectral trust regions",
[
(
"λ range (nm)",
"Lower/upper wavelength bounds (nm) of the trust region. It "
"windows the centroid/width checks and the marginal "
"normalisation, scales the trace display, and Planck-tapers the "
"independent spectrum before the marginal is predicted — so "
"noisy measured wings (amplified by the (λ/µm)^exp tilt) cannot "
"contaminate the prediction. Defaults to 190–1000 nm for a "
"measured trace; the noise-free simulated/synthetic traces "
"default to their full loaded wavelength range.",
"trace λ (nm)",
"Lower/upper wavelength bounds (nm) of the trace's trust "
"region, in the trace's own wavelengths (for SHG, around half "
"the fundamental). It windows the centroid/width checks and the "
"marginal normalisation and scales the trace display, so noisy "
"wings (amplified by the (λ/µm)^exp tilt) cannot distort them. "
"Defaults to 190–1000 nm for a measured trace; the noise-free "
"simulated/synthetic traces default to their full loaded "
"wavelength range.",
),
(
"spectrum λ (nm)",
"Lower/upper wavelength bounds (nm) of the independent "
"spectrum's trust region, in fundamental wavelengths. The "
"spectrum is Planck-tapered to it before the marginal is "
"predicted, so its noisy wings cannot contaminate the "
"prediction. Independent of the trace's region. A measured "
"spectrum defaults to its range within 190–1000 nm (its full "
"range if it lies outside that band), a simulated or synthetic "
"one to its full range; off without a spectrum.",
),
],
),
Expand Down Expand Up @@ -150,12 +166,22 @@ def __init__(self, state):
# noise-free simulated/synthetic traces, 190–1000 nm for measured).
self._trust_nm = (190.0, 1000.0)
self._trust_key: tuple | None = None # the trace a default was seeded for
# The independent spectrum's own region (fundamental λ); re-seeded to the
# spectrum's range whenever the spectrum changes.
self._spec_trust_nm = (100.0, 3000.0)
self._spec_trust_key: tuple | None = None

# -- trust region --
box, form = group("Spectral trust region")
# -- trust regions --
box, form = group("Spectral trust regions")
self.trust = RangeControl(100.0, 3000.0, *self._trust_nm, decimals=0, step=10.0)
self.trust.changed.connect(self._on_trust)
add_wide_row(form, "λ range (nm)", self.trust)
add_wide_row(form, "trace λ (nm)", self.trust)
self.spec_trust = RangeControl(
*self._spec_trust_nm, *self._spec_trust_nm, decimals=0, step=10.0
)
self.spec_trust.changed.connect(self._on_spec_trust)
self.spec_trust.setEnabled(False)
add_wide_row(form, "spectrum λ (nm)", self.spec_trust)
self.controls.addWidget(box)

# -- display --
Expand Down Expand Up @@ -264,12 +290,47 @@ def _seed_trust(self, data) -> None:
self._trust_key = key
self.trust.set_values(*trust) # signal-safe

def _seed_spec_trust(self, data) -> None:
"""Default the spectrum's trust region for a freshly loaded spectrum.

A measured spectrum defaults to its range clipped to 190–1000 nm, the
UV–NIR band that excludes a spectrometer's noisy out-of-band wings (as the
single shared region did before the two were split); where the spectrum
lies wholly outside that band (a fundamental beyond 1 µm), to its full
range. Simulated and synthetic spectra carry no detector noise and
default to their full range. Keyed on the entry and the spectrum, so a
manual adjustment survives navigation while a different spectrum
re-defaults. Disabled without a spectrum.
"""
lam_spec = data.get("lam_spec")
has_spectrum = lam_spec is not None and data.get("Ilam_spec") is not None
self.spec_trust.setEnabled(has_spectrum)
if not has_spectrum:
self._spec_trust_key = None
return
# round off the m→nm conversion error before flooring/ceiling
spec_nm = np.round(np.asarray(lam_spec) / 1e-9, 6)
extent = (float(np.floor(spec_nm.min())), float(np.ceil(spec_nm.max())))
key = (self.state.entry, *extent)
if key == self._spec_trust_key:
return
trust = extent
if self.state.entry == "experimental":
lo, hi = max(extent[0], 190.0), min(extent[1], 1000.0)
if lo < hi:
trust = (lo, hi)
self._spec_trust_nm = trust
self._spec_trust_key = key
self.spec_trust.set_bounds(*extent)
self.spec_trust.set_values(*trust) # signal-safe

def on_enter(self) -> None:
data = self.state.load_data
if data is None:
self.set_status("Load a trace first.")
return
self._seed_trust(data)
self._seed_spec_trust(data)
params = self._loader_params()
enabled = params is not None
self.third_order_check.setEnabled(enabled)
Expand Down Expand Up @@ -315,6 +376,10 @@ def _on_trust(self, lo: float, hi: float) -> None:
self._trust_nm = (lo, hi)
self._draw() # trust only affects display/QC, no re-assembly

def _on_spec_trust(self, lo: float, hi: float) -> None:
self._spec_trust_nm = (lo, hi)
self._draw()

def _on_scale(self, _value: str) -> None:
self._draw()

Expand Down Expand Up @@ -354,7 +419,11 @@ def _auto_exponent(self) -> None:
# along delay), so the fit returns the absolute exponent to apply.
if params.third_order:
marg = marg / third_order_scale(lam, params.third_order_exp)
window = (self._trust_nm[0] * 1e-9, self._trust_nm[1] * 1e-9)
trace_window = (self._trust_nm[0] * 1e-9, self._trust_nm[1] * 1e-9)
spectrum_window = (
self._spec_trust_nm[0] * 1e-9,
self._spec_trust_nm[1] * 1e-9,
)
# Match the *displayed* predicted centroid (same spectrum windowing as the
# overlay) so the two dashed centroid lines coincide after the fit.
pred = self._predict(data["lam_spec"], data["Ilam_spec"], data["interaction"])
Expand All @@ -367,7 +436,8 @@ def _auto_exponent(self) -> None:
data["lam_spec"],
data["Ilam_spec"],
data["interaction"],
window=window,
trace_window=trace_window,
spectrum_window=spectrum_window,
target_centroid=target,
),
2,
Expand Down Expand Up @@ -470,7 +540,9 @@ def _marginal_view(self, lam, lam_nm, trace, in_band, data) -> LineView:
if data.get("Ilam_spec") is not None:
lam_spec = np.asarray(data["lam_spec"])
Ilam_spec = np.asarray(data["Ilam_spec"])
sl_band = (lam_spec / 1e-9 >= lo) & (lam_spec / 1e-9 <= hi)
# the spectrum is normalised within its own trust region
s_lo, s_hi = self._spec_trust_nm
sl_band = (lam_spec / 1e-9 >= s_lo) & (lam_spec / 1e-9 <= s_hi)
if freq:
x_sp, y_sp = wlfreq(lam_spec) / _TWO_PI_PHZ, Ilam_spec * lam_spec**2
else:
Expand Down Expand Up @@ -534,22 +606,22 @@ def _f_hi(self) -> float:

# -- physics plumbing ---------------------------------------------------
def _trust_window(self, lam: np.ndarray) -> np.ndarray:
"""Near-hard window over the trust region (1 inside, 0 outside).
"""Near-hard window over the spectrum's trust region (1 inside, 0 outside).

Zero outside ``[lo, hi]`` with only a few-nm Planck roll-off just inside
the bounds — enough to stop the sharp cut ringing through the FFTs, but
narrow enough that it does not reshape the in-band spectrum (a wide taper
would clip a broadband spectrum's wing and bias the predicted centroid).
"""
lo_nm, hi_nm = self._trust_nm
lo_nm, hi_nm = self._spec_trust_nm
lo, hi = lo_nm * 1e-9, hi_nm * 1e-9
edge = min(0.02 * max(hi - lo, 1e-12), 5e-9)
return planck_taper(lam, lo, lo + edge, hi - edge, hi)

def _predict(self, lam_spec, Ilam_spec, interaction) -> dict | None:
"""Predicted marginal on the trace's wavelength axis, plus its centroid (nm).

Planck-tapers the spectrum to the **trust region** first — the noisy
Planck-tapers the spectrum to **its own trust region** first — the noisy
measured wings outside it would otherwise contaminate the transform-limited
field and the autoconvolution — then resamples onto a uniform
angular-frequency grid (applying the λ→ω Jacobian), predicts the marginal,
Expand All @@ -563,10 +635,9 @@ def _predict(self, lam_spec, Ilam_spec, interaction) -> dict | None:
Il = np.clip(Ilam_spec[order], 0.0, None) * self._trust_window(lam_s)
if not np.any(Il > 0):
return None
# Predict on a grid spanning the trust ∩ spectrum band, so the spectrum
# width σ_S is measured over the same window as the trace marginal — the
# width bracket and the measured width are then directly comparable.
lo_nm, hi_nm = self._trust_nm
# Predict on a grid spanning the trusted part of the spectrum, so the
# spectrum width σ_S is measured over the band that feeds the prediction.
lo_nm, hi_nm = self._spec_trust_nm
lam_lo = max(lo_nm * 1e-9, float(lam_s[0]))
lam_hi = min(hi_nm * 1e-9, float(lam_s[-1]))
if lam_hi <= lam_lo:
Expand Down
53 changes: 48 additions & 5 deletions croak/gui/stage_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
default 2× the scan delay range); ``τ range`` (the delay range to **use** —
a hard crop of the delay axis); delay subsampling; time resampling; and the
resampling pre-filter.
* **Windowing** — measurement spectral window (λm) and delay filtering window
(τm, a smooth Planck taper); both always on, defaulting to the full
wavelength/delay extents and narrowed to taste.
* **Windowing** — the trace's spectral window (λm, in the trace's wavelengths),
the independent spectrum's window (λs, in fundamental wavelengths, independent
of λm; off without a spectrum) and the delay filtering window (τm, a smooth
Planck taper); all default to the full extents and are narrowed to taste.
* **Filtering** — signal-aware de-fringing (Takeda FTSI) and arPLS baseline
removal, the legacy fringe-notch / DC / low-pass delay filters, and
thresholding (the SHG marginal correction lives on the marginal-check stage).
Expand Down Expand Up @@ -127,8 +128,19 @@ class StagePreprocess(Stage):
[
(
"λm (nm)",
"Measurement spectral window: wavelengths outside this range "
"are tapered out of the trace. Defaults to the full extent.",
"Trace spectral window, in the trace's own wavelengths (for "
"SHG, around half the fundamental): wavelengths outside this "
"range are tapered out of the trace. It does not touch the "
"independent spectrum. Defaults to the full extent.",
),
(
"λs (nm)",
"Independent-spectrum window, in fundamental wavelengths: the "
"spectrum is tapered to zero outside it. For SHG it is also "
"the post-filter's spectral window (for PG/SD that is λm). "
"Independent of λm; defaults "
"to the spectrum's full extent, and is off without a "
"spectrum.",
),
(
"τm (fs)",
Expand Down Expand Up @@ -280,6 +292,18 @@ def __init__(self, state):
)
self.lamm_range.changed.connect(self._on_lamm_range)
add_wide_row(form, "λm (nm)", self.lamm_range)
# Placeholder bounds until a spectrum is loaded; _seed_ranges sets the
# real ones and enables the control.
self.lams_range = RangeControl(
p.lamm_bound_min_nm,
p.lamm_bound_max_nm,
p.lamm_bound_min_nm,
p.lamm_bound_max_nm,
**_RANGE_KW,
)
self.lams_range.changed.connect(self._on_lams_range)
self.lams_range.setEnabled(False)
add_wide_row(form, "λs (nm)", self.lams_range)
self.taum_range = RangeControl(
p.tau_bound_min_fs,
p.tau_bound_max_fs,
Expand Down Expand Up @@ -542,6 +566,10 @@ def _on_lamm_range(self, lo, hi):
self.state.preproc.lamm_min_nm, self.state.preproc.lamm_max_nm = lo, hi
self._schedule()

def _on_lams_range(self, lo, hi):
self.state.preproc.lams_min_nm, self.state.preproc.lams_max_nm = lo, hi
self._schedule()

def _on_taum_range(self, lo, hi):
self.state.preproc.taum_min_fs, self.state.preproc.taum_max_fs = lo, hi
self._schedule()
Expand All @@ -556,13 +584,28 @@ def _seed_ranges(self):
self.tau_range.set_values(p.tau_min_fs, p.tau_max_fs)
self.lamm_range.set_bounds(p.lamm_bound_min_nm, p.lamm_bound_max_nm)
self.lamm_range.set_values(p.lamm_min_nm, p.lamm_max_nm)
self._seed_lams_range()
self.taum_range.set_bounds(p.tau_bound_min_fs, p.tau_bound_max_fs)
self.taum_range.set_values(p.taum_min_fs, p.taum_max_fs)
self.trange_spin.blockSignals(True)
self.trange_spin.setValue(p.trange_fs)
self.trange_spin.blockSignals(False)
self._seeding = False

def _seed_lams_range(self):
"""Seed the spectrum window, enabled only when there is a spectrum."""
p = self.state.preproc
lo, hi = p.lams_bound_min_nm, p.lams_bound_max_nm
has_spectrum = lo is not None and hi is not None
self.lams_range.setEnabled(has_spectrum)
if lo is None or hi is None:
return
self.lams_range.set_bounds(lo, hi)
# unset values mean the full extent
low = lo if p.lams_min_nm is None else p.lams_min_nm
high = hi if p.lams_max_nm is None else p.lams_max_nm
self.lams_range.set_values(low, high)

def _seed_all(self):
"""Push *every* preprocess parameter into its widget (signal-safe).

Expand Down
Loading
Loading