diff --git a/changelog.d/post-filter-shg.fixed.md b/changelog.d/post-filter-shg.fixed.md new file mode 100644 index 0000000..852c353 --- /dev/null +++ b/changelog.d/post-filter-shg.fixed.md @@ -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). diff --git a/changelog.d/spectrum-window.changed.md b/changelog.d/spectrum-window.changed.md new file mode 100644 index 0000000..02c7f0e --- /dev/null +++ b/changelog.d/spectrum-window.changed.md @@ -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). diff --git a/changelog.d/trust-windows.breaking.md b/changelog.d/trust-windows.breaking.md new file mode 100644 index 0000000..c0152dd --- /dev/null +++ b/changelog.d/trust-windows.breaking.md @@ -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). diff --git a/croak/gui/stage_load.py b/croak/gui/stage_load.py index e8bb667..1d9a42f 100644 --- a/croak/gui/stage_load.py +++ b/croak/gui/stage_load.py @@ -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", ) @@ -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 @@ -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)) diff --git a/croak/gui/stage_marginal_check.py b/croak/gui/stage_marginal_check.py index 5924d85..5d29ac1 100644 --- a/croak/gui/stage_marginal_check.py +++ b/croak/gui/stage_marginal_check.py @@ -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 @@ -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.", ), ], ), @@ -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 -- @@ -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) @@ -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() @@ -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"]) @@ -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, @@ -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: @@ -534,14 +606,14 @@ 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) @@ -549,7 +621,7 @@ def _trust_window(self, lam: np.ndarray) -> np.ndarray: 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, @@ -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: diff --git a/croak/gui/stage_preprocess.py b/croak/gui/stage_preprocess.py index 351306b..6487508 100644 --- a/croak/gui/stage_preprocess.py +++ b/croak/gui/stage_preprocess.py @@ -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). @@ -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)", @@ -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, @@ -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() @@ -556,6 +584,7 @@ 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) @@ -563,6 +592,20 @@ def _seed_ranges(self): 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). diff --git a/croak/gui/stage_retrieve.py b/croak/gui/stage_retrieve.py index 90bbe20..90feaad 100644 --- a/croak/gui/stage_retrieve.py +++ b/croak/gui/stage_retrieve.py @@ -314,8 +314,10 @@ class StageRetrieve(RunControls, Stage): [ ( "Spectral (λ window)", - "Mask the retrieved result outside the measurement λ window — " - "applied to the stored result, no re-run.", + "Mask the retrieved result outside the spectral window at the " + "fundamental: the trace window λm for PG/SD, the spectrum " + "window λs for SHG (off for SHG without a spectrum). Applied " + "to the stored result, no re-run.", ), ( "Temporal (τ window)", @@ -1375,9 +1377,12 @@ def on_enter(self): if self.state.result is not None and self._base_result is None: self._base_result = self.state.result self.retarget_btn.setEnabled(True) - self.pf_lam.setEnabled(True) - self.pf_tau.setEnabled(True) + self._enable_post_filters() self._update_view() + elif self._base_result is not None: + # The trace may have been re-prepared (a spectrum added or removed) + # since the result was drawn; the spectral window may have changed. + self._enable_post_filters() # -- retrieval ---------------------------------------------------------- def _seeded_extras(self, seed) -> str: @@ -1542,10 +1547,27 @@ def _finalise(self, result, elapsed): self._elapsed_timer.stop() self.set_running(False) self.retarget_btn.setEnabled(True) - self.pf_lam.setEnabled(True) - self.pf_tau.setEnabled(True) + self._enable_post_filters() self._update_view() + def _enable_post_filters(self) -> None: + """Enable the post-filters a result can use. + + The spectral one needs a window at the fundamental + (:attr:`~croak.preprocess.TraceData.fundamental_window`); an SHG trace + without an independent spectrum has none, so it stays off and says why. + """ + self.pf_tau.setEnabled(True) + td = self.state.tracedata + has_window = td is not None and td.fundamental_window is not None + self.pf_lam.setEnabled(has_window) + self.pf_lam.setToolTip( + "" + if has_window + else "SHG without an independent spectrum has no spectral window at " + "the fundamental: the trace window lies at the second harmonic." + ) + def _on_post_filter(self, _=None): self.state.retrieve.post_filter_lam = self.pf_lam.isChecked() self.state.retrieve.post_filter_tau = self.pf_tau.isChecked() @@ -1596,8 +1618,9 @@ def _update_view(self): td = self.state.tracedata if td is None: return - # window to the measurement λm/τm ranges (not the full grid/trace extent) - lam_lims = td.lamm_lims if self.pf_lam.isChecked() else None + # window to the measurement ranges (not the full grid/trace extent); the + # spectral one at the fundamental, where the retrieved field lives + lam_lims = td.fundamental_window if self.pf_lam.isChecked() else None tau_lims = td.tau_lims if self.pf_tau.isChecked() else None if lam_lims is None and tau_lims is None: result = self._base_result diff --git a/croak/marginal_checks.py b/croak/marginal_checks.py index ae0ea49..2d52c0a 100644 --- a/croak/marginal_checks.py +++ b/croak/marginal_checks.py @@ -417,7 +417,8 @@ def auto_third_order_exponent( Ilam_spec: ArrayLike, interaction: str, *, - window: tuple[float, float] = (210e-9, 1000e-9), + trace_window: tuple[float, float] = (210e-9, 1000e-9), + spectrum_window: tuple[float, float] | None = None, bounds: tuple[float, float] = (0.0, 6.0), target_centroid: float | None = None, ) -> float: @@ -427,7 +428,8 @@ def auto_third_order_exponent( a power law :math:`(\lambda/\mu\mathrm{m})^n` (:func:`croak.preprocess.third_order_scale`). This chooses ``n`` so the **centroid** of the corrected marginal (computed as an angular-frequency - density, within a wavelength ``window``) equals the centroid predicted from + density, within the wavelength ``trace_window``) equals the centroid + predicted from the independent spectrum (:func:`predicted_centroid`: :math:`\bar\omega_S` at the fundamental for PG/TG/SD, :math:`2\bar\omega_S` for SHG). @@ -454,14 +456,20 @@ def auto_third_order_exponent( Independent fundamental spectrum and its wavelength grid (m). interaction : str ``"shg"``, ``"sd"``, ``"pg"`` or ``"tg"`` (case-insensitive). - window : tuple of float, optional - ``(lo, hi)`` trust window in metres (default ``(210e-9, 1000e-9)``); only + trace_window : tuple of float, optional + ``(lo, hi)`` trust window of the **trace marginal**, in the trace's own + wavelengths (m; default ``(210e-9, 1000e-9)``); only marginal wavelengths inside it enter the centroid. + spectrum_window : tuple of float, optional + ``(lo, hi)`` trust window of the independent **fundamental spectrum** + (m), applied when the target is formed from it. ``None`` (default) uses + the whole spectrum. It is separate from ``trace_window`` because for SHG + the spectrum lies an octave below the marginal. bounds : tuple of float, optional ``(lo, hi)`` clamp on the returned exponent (default ``(0.0, 6.0)``). target_centroid : float, optional Predicted centroid to match (rad/s). When ``None`` (default) it is - computed from ``lam_spec``/``Ilam_spec`` over the window. + computed from ``lam_spec``/``Ilam_spec`` over ``spectrum_window``. Returns ------- @@ -471,7 +479,7 @@ def auto_third_order_exponent( Raises ------ ValueError - If fewer than three wavelengths carry signal in the window, or the + If fewer than three wavelengths carry signal in either window, or the spectrum centroid cannot be formed. Notes @@ -491,17 +499,24 @@ def auto_third_order_exponent( if target_centroid is not None: target = float(target_centroid) else: - spec_in = (lam_spec >= window[0]) & (lam_spec <= window[1]) & (Ilam_spec > 0) + lo, hi = ( + (lam_spec.min(), lam_spec.max()) + if spectrum_window is None + else spectrum_window + ) + spec_in = (lam_spec >= lo) & (lam_spec <= hi) & (Ilam_spec > 0) if np.count_nonzero(spec_in) < 3: - raise ValueError("independent spectrum has no signal in the trust window") + raise ValueError( + "independent spectrum has no signal in the spectrum's trust window" + ) omega_spec = wlfreq(lam_spec[spec_in]) s_omega = Ilam_spec[spec_in] * lam_spec[spec_in] ** 2 target = predicted_centroid(omega_spec, s_omega, interaction) # Measured marginal centroid as a function of n, also as an omega-density. - in_window = (lam >= window[0]) & (lam <= window[1]) & (marginal > 0) + in_window = (lam >= trace_window[0]) & (lam <= trace_window[1]) & (marginal > 0) if np.count_nonzero(in_window) < 3: - raise ValueError("trace marginal has no signal in the trust window") + raise ValueError("trace marginal has no signal in the trace's trust window") lam_w = lam[in_window] omega_w = wlfreq(lam_w) # omega-density of the *base* marginal, and (lambda/um)^1 raised to n below diff --git a/croak/preprocess.py b/croak/preprocess.py index 14085a7..0f7ca15 100644 --- a/croak/preprocess.py +++ b/croak/preprocess.py @@ -1465,9 +1465,8 @@ class TraceData: lam_min, lam_max : float Retrieval wavelength range (m) — the output grid extent. lamm_lims : tuple or None - Measurement wavelength window λm (m): where the trace carries signal. - Use this (not ``lam_min/lam_max``) for any post-retrieval spectral - windowing — see :func:`~croak.processing.post_filter`. + Measurement wavelength window λm (m) of the **trace**: where the trace + carries signal. For SHG it lies near half the fundamental wavelength. tau_lims : tuple or None Measurement delay window τm (s), or ``None``. The companion to ``lamm_lims`` for post-retrieval temporal windowing. @@ -1477,6 +1476,12 @@ class TraceData: Independent spectrum (measured/filtered) or ``None``. scalecurve : numpy.ndarray Per-wavelength scaling applied during filtering. + lams_lims : tuple or None + Wavelength window (m) applied to the independent **fundamental + spectrum**, independent of ``lamm_lims``; ``None`` when there is no + spectrum to window. To window the retrieved field use + :attr:`fundamental_window`, which is this for SHG and ``lamm_lims`` for + PG/SD. taper_loss : float Fraction of the measured trace removed by :func:`regrid`'s edge taper (see :data:`TAPER_COLLAR_BINS`). A band chosen with room to spare leaves @@ -1507,12 +1512,58 @@ class TraceData: Ilam_spec_filt: NDArray[np.float64] | None scalecurve: NDArray[np.float64] taper_loss: float = 0.0 + lams_lims: tuple[float, float] | None = None @property def omega(self) -> NDArray[np.float64]: """Centred angular-frequency axis (rad/s).""" return self.grid.omega + @property + def fundamental_window(self) -> tuple[float, float] | None: + """Wavelength window (m) at the **fundamental**, for windowing the field. + + For PG and SD, whose trace sits at the fundamental, the trace window + ``lamm_lims``: the measured support of the signal. For SHG the trace + window lies at the second harmonic, and doubling it is not a bound on the + fundamental's support (a 650 nm and a 1000 nm component mix to 394 nm, + inside a 350-450 nm trace window), so it is the spectrum window + ``lams_lims``, or ``None`` without a spectrum. + """ + if get_interaction(self.interaction).omega0_scale == 1.0: + return self.lamm_lims + return self.lams_lims + + +def _check_spectrum_window( + lam_spec: NDArray[np.float64], + Ilam_spec: ArrayLike, + lams_lims: tuple[float, float], +) -> None: + """Raise unless ``lams_lims`` is an ordered window holding spectral signal. + + The band is open, as in :func:`filter_spectrum`, which normalises to the + peak strictly inside it. + """ + lo, hi = lams_lims + if not lo < hi: + raise ValueError( + f"the spectrum window lams_lims needs its low edge below its high edge, " + f"got ({lo * 1e9:.1f}, {hi * 1e9:.1f}) nm" + ) + band = (lam_spec > lo) & (lam_spec < hi) + if not np.any(band): + raise ValueError( + f"the spectrum window lams_lims ({lo * 1e9:.1f}, {hi * 1e9:.1f}) nm " + f"holds no sample of the spectrum strictly inside it; the spectrum " + f"spans {lam_spec.min() * 1e9:.1f}-{lam_spec.max() * 1e9:.1f} nm" + ) + if not np.max(np.asarray(Ilam_spec, dtype=float)[band]) > 0.0: + raise ValueError( + f"the spectrum window lams_lims ({lo * 1e9:.1f}, {hi * 1e9:.1f}) nm " + f"holds no positive spectral intensity" + ) + def load_and_clean( Ifrog: ArrayLike, @@ -1523,6 +1574,7 @@ def load_and_clean( lam_min: float, lam_max: float, lamm_lims: tuple[float, float] | None = None, + lams_lims: tuple[float, float] | None = None, input_unit: str = "position", trange: float | None = None, tau_crop: tuple[float, float] | None = None, @@ -1568,9 +1620,17 @@ def load_and_clean( lam_min, lam_max : float Fundamental-pulse retrieval wavelength range (m), also for SHG. lamm_lims : tuple, optional - Useful measurement wavelength window λm (m); defaults to the full - extent. Applied as a spectral window before regridding, and stored on the - returned :class:`TraceData` for optional post-retrieval windowing. + Useful measurement wavelength window λm (m) of the **trace** (for SHG, + near half the fundamental wavelength); defaults to the trace's full + extent. Applied to the trace before regridding. It does not touch the + independent spectrum, which has its own ``lams_lims``. + lams_lims : tuple, optional + Wavelength window (m) of the independent **fundamental spectrum**, + applied to it before regridding and stored on the returned + :class:`TraceData` (for SHG, also the window of the retrieved field; + see :attr:`TraceData.fundamental_window`). + Defaults to the spectrum's full extent. Requires ``lam_spec`` and + ``Ilam_spec``. input_unit : {"position", "delay"}, optional Whether ``scanaxis`` is a stage position (m) or a delay (s). Default ``"position"``, converted with ``τ = 2z/c``. @@ -1703,6 +1763,11 @@ def load_and_clean( inter = get_interaction(interaction) if lamm_lims is None: lamm_lims = (float(lam_frog.min()), float(lam_frog.max())) + has_spectrum = lam_spec is not None and Ilam_spec is not None + if lams_lims is not None and not has_spectrum: + raise ValueError( + "lams_lims needs an independent spectrum (lam_spec and Ilam_spec)" + ) if input_unit == "position": tau_meas = 2.0 * scanaxis / C @@ -1736,13 +1801,15 @@ def load_and_clean( ) if lam_spec is not None and Ilam_spec is not None: - # The trace λm window maps to the fundamental-spectrum window by the same - # factor as omega0_scale: for SHG λ_fund = 2·λ_trace (scale 2); for - # sd/pg the trace is already at the fundamental (scale 1). - scale = inter.omega0_scale - Ilam_spec_filt = filter_spectrum( - lam_spec, Ilam_spec, lamm_lims[0] * scale, lamm_lims[1] * scale - ) + # The spectrum has its own window, deliberately not derived from the + # trace's λm: the two are measured by different instruments, with noise + # and artefacts at unrelated wavelengths (and for SHG a ×2 map of λm is + # not even a support bound of the fundamental). See issue #23. + lam_spec = np.asarray(lam_spec, dtype=float) + if lams_lims is None: + lams_lims = (float(lam_spec.min()), float(lam_spec.max())) + _check_spectrum_window(lam_spec, Ilam_spec, lams_lims) + Ilam_spec_filt = filter_spectrum(lam_spec, Ilam_spec, *lams_lims) else: Ilam_spec_filt = None @@ -1799,4 +1866,5 @@ def load_and_clean( Ilam_spec_filt=Ilam_spec_filt, scalecurve=sc, taper_loss=taper_loss, + lams_lims=lams_lims, ) diff --git a/croak/processing.py b/croak/processing.py index 8f3e397..715b922 100644 --- a/croak/processing.py +++ b/croak/processing.py @@ -143,12 +143,15 @@ def post_filter( may be ``None``. Order matters: windowing in time redistributes spectral content, so the spectral taper must see the already-windowed field. - ``lam_lims``/``tau_lims`` should be the **measurement** windows λm/τm — the - ranges over which the trace actually carries signal (available as - :attr:`~croak.preprocess.TraceData.lamm_lims` / - :attr:`~croak.preprocess.TraceData.tau_lims`). Passing the full retrieval-grid - extent instead would taper only the outermost few bins and do essentially - nothing. + ``lam_lims`` is a wavelength window at the **fundamental** (the retrieved + field's own wavelengths, also for SHG), and ``tau_lims`` the measurement delay + window τm. Take them from + :attr:`~croak.preprocess.TraceData.fundamental_window` and + :attr:`~croak.preprocess.TraceData.tau_lims`. For SHG, do not pass the + trace's ``lamm_lims``: it lies at the second harmonic, and the window would + land above the grid and remove the whole spectrum. Passing the full + retrieval-grid extent instead would taper only the outermost few bins and + do essentially nothing. Returns a new :class:`~croak.result.RetrievalResult` with the filtered spectrum (the original is unchanged), so the effect can be toggled without diff --git a/croak/session/params.py b/croak/session/params.py index ab6b95f..df14cf2 100644 --- a/croak/session/params.py +++ b/croak/session/params.py @@ -250,6 +250,13 @@ class PreprocParams: lamm_max_nm: float = 1100.0 lamm_bound_min_nm: float = 300.0 lamm_bound_max_nm: float = 1100.0 + # independent fundamental-spectrum window (fundamental λ domain); None means + # the spectrum's full extent (and is what a file without a spectrum, or one + # written before this window existed, reads back as) + lams_min_nm: float | None = None + lams_max_nm: float | None = None + lams_bound_min_nm: float | None = None + lams_bound_max_nm: float | None = None # measurement delay window (τm; always on, crops the delay axis) taum_min_fs: float = -500.0 taum_max_fs: float = 500.0 diff --git a/croak/session/pipeline.py b/croak/session/pipeline.py index d7b2ddd..f113ba8 100644 --- a/croak/session/pipeline.py +++ b/croak/session/pipeline.py @@ -663,6 +663,10 @@ def assemble_simulated_tracedata(p: SimulatedLoadParams) -> TraceData: Ilam_spec_meas=Ilam_spec, Ilam_spec_filt=Ilam_spec, scalecurve=np.ones(lam_frog.shape), + # The native-grid spectrum is not windowed: its window is its full extent. + lams_lims=( + None if lam_spec is None else (float(lam_spec[0]), float(lam_spec[-1])) + ), ) @@ -692,6 +696,21 @@ def build_tracedata( """ ds = _FAST_BASELINE_DOWNSAMPLE if fast_baseline else 1 skip = _FAST_BASELINE_SKIP_BELOW if fast_baseline else 0.0 + if (p.lams_min_nm is None) != (p.lams_max_nm is None): + raise ValueError( + "the spectrum window needs both lams_min_nm and lams_max_nm, or neither " + f"(the spectrum's full extent); got {p.lams_min_nm!r} and " + f"{p.lams_max_nm!r}" + ) + # The spectrum window only means something with a spectrum; a window left + # over from one that has since been removed is dropped, not an error. + lams_lims = None + if ( + data["lam_spec"] is not None + and p.lams_min_nm is not None + and p.lams_max_nm is not None + ): + lams_lims = (p.lams_min_nm * 1e-9, p.lams_max_nm * 1e-9) return preprocess.load_and_clean( data["trace"], data["lam"], @@ -700,6 +719,7 @@ def build_tracedata( lam_min=p.lam_min_nm * 1e-9, lam_max=p.lam_max_nm * 1e-9, lamm_lims=(p.lamm_min_nm * 1e-9, p.lamm_max_nm * 1e-9), + lams_lims=lams_lims, input_unit=data["input_unit"], trange=p.trange_fs * 1e-15, tau_crop=(p.tau_min_fs * 1e-15, p.tau_max_fs * 1e-15), @@ -1107,10 +1127,13 @@ def apply_post_filter( """Window the result to the measurement λ/τ ranges if the toggles are set. Mirrors the retrieve stage's post-filter pane: a spectral and/or temporal - window to the measurement extents (``td.lamm_lims`` / ``td.tau_lims``), not - the full grid. Returns the result unchanged when neither toggle is on. + window to the measurement extents (``td.fundamental_window`` / + ``td.tau_lims``), not the full grid. The spectral window is the one at the + fundamental, where the retrieved field lives; see + :attr:`~croak.preprocess.TraceData.fundamental_window`. Returns the result + unchanged when neither toggle is on (or no window applies). """ - lam_lims = td.lamm_lims if p.post_filter_lam else None + lam_lims = td.fundamental_window if p.post_filter_lam else None tau_lims = td.tau_lims if p.post_filter_tau else None if lam_lims is None and tau_lims is None: return result diff --git a/docs/.jupyter_cache/global.db b/docs/.jupyter_cache/global.db index 019e531..507fae5 100644 Binary files a/docs/.jupyter_cache/global.db and b/docs/.jupyter_cache/global.db differ diff --git a/docs/explanation/marginals.md b/docs/explanation/marginals.md index 410b5dc..64cf2d4 100644 --- a/docs/explanation/marginals.md +++ b/docs/explanation/marginals.md @@ -216,9 +216,12 @@ How the three PG checks carry over: - Width and centroid estimates can be sensitive to noise and cropping; the full-shape overlay is the most sensitive to scatter and detector nonlinearity but also the most affected by the TL assumption for PG/SD. Restrict the moments and the - normalisation to a **spectral trust region** (in the GUI, 190–1000 nm for a - measured trace, or the full loaded range for the noise-free simulated/synthetic - traces) so out-of-band detector noise cannot dominate. + normalisation to **spectral trust regions**, one for the trace (in the GUI, + 190–1000 nm for a measured trace, or the full loaded range for the noise-free + simulated/synthetic traces) and one for the independent spectrum (in the GUI, + a measured spectrum's range within 190–1000 nm by default), so out-of-band detector noise cannot dominate. They are + separate because the two come from different instruments and, for SHG, lie an + octave apart. - For the rigorous SHG and PG-centroid checks, report the numeric discrepancy (measured vs predicted) as a QC metric alongside the retrieval error. @@ -231,6 +234,7 @@ marginal by a power law $(\lambda/\mu\mathrm{m})^n$ {func}`croak.marginal_checks.auto_third_order_exponent` picks the exponent $n$ whose corrected-marginal centroid lands on the predicted centroid. The tilt shifts the centroid monotonically in $n$, so the match is at most one root over a non-degenerate positive trace; it is -restricted to the trust region so amplified out-of-band noise cannot bias it. +restricted to the trace's trust region so amplified out-of-band noise cannot bias +it, and the target is formed from the spectrum within its own trust region. This is the physically grounded replacement for fitting the marginal *shape* to the spectrum. diff --git a/docs/howto/grids_and_filtering.md b/docs/howto/grids_and_filtering.md index 232cd84..79e3faf 100644 --- a/docs/howto/grids_and_filtering.md +++ b/docs/howto/grids_and_filtering.md @@ -145,14 +145,18 @@ the effect is reversible): ```python clean = croak.processing.post_filter(result, - tau_lims=td.tau_lims, # measurement τm - lam_lims=td.lamm_lims) # measurement λm + tau_lims=td.tau_lims, # τm + lam_lims=td.fundamental_window) # at the fundamental ``` -Pass the **measurement** windows λm/τm — the ranges where the trace actually -carries signal, stored on the `TraceData` as `td.lamm_lims` / `td.tau_lims`. (The -full retrieval-grid extent, `td.lam_min`/`td.lam_max`, would taper only the -outermost few bins and do nothing useful.) +Pass **measurement** windows: the delay window τm (`td.tau_lims`), and a +wavelength window **at the fundamental**, where the retrieved field lives +(`td.fundamental_window`: for PG/SD the trace window λm, for SHG the spectrum +window λs, and `None` for SHG without a spectrum). For SHG, never pass the trace +window `td.lamm_lims`: it lies +at the second harmonic, so the taper would land above the grid and remove the +whole field. (The full retrieval-grid extent, `td.lam_min`/`td.lam_max`, would +taper only the outermost few bins and do nothing useful.) The stored trace and error still describe the original unfiltered fit. This is distinct from the [spectral-match penalty](regularisation.md), which diff --git a/docs/howto/gui.md b/docs/howto/gui.md index 88aa6a6..704c8b9 100644 --- a/docs/howto/gui.md +++ b/docs/howto/gui.md @@ -318,9 +318,14 @@ independent spectrum ({mod}`croak.marginal_checks`): exact for SHG (the spectral autoconvolution) and transform-limited for PG/TG/SD, within the thin local model; the [marginal guide](marginal_checks.md) states its assumptions. The page overlays the predicted marginal, marks the predicted vs measured **centroid**, -reports the **width-bracket** check, and exposes the spectral **trust region** -(defaults to 190–1000 nm for a measured trace, or the full loaded range for the -noise-free simulated/synthetic traces), a **log/linear** marginal toggle (default +reports the **width-bracket** check, and exposes two spectral **trust regions**: +**trace λ** for the trace marginal (defaults to 190–1000 nm for a measured trace, +or the full loaded range for the noise-free simulated/synthetic traces) and +**spectrum λ** for the independent spectrum (a measured spectrum defaults to its +range within 190–1000 nm, or its full range if it lies outside that band; a +simulated or synthetic one to its full range; off without a spectrum). There are two because the trace and the spectrum come from +different instruments and, for SHG, lie an octave apart. The page also has a +**log/linear** marginal toggle (default linear, peak-normalised) and the **3rd-order** `(λ/µm)^exp` efficiency correction. Its **Auto** button picks the exponent whose corrected-marginal centroid matches the prediction @@ -343,7 +348,12 @@ frequency centroid matches. Fringe/DC/low-pass filtering, windowing and regridding ({func}`~croak.preprocess.load_and_clean`), recomputed automatically as you -change a setting. The plots show six maps in two rows of three — the measured, +change a setting. The *Windowing* group has three windows: **λm** for the trace, +in its own wavelengths (for SHG, around half the fundamental); **λs** for the +independent spectrum, in fundamental wavelengths (its full range by default, and +off without a spectrum; for SHG it is also the post-filter's spectral window, +which for PG/SD is λm); and +**τm** for the delay axis. λm and λs are independent of each other. The plots show six maps in two rows of three — the measured, filtered and regridded trace, in dB above and linear below — with linked axes: all six share the delay axis, and the measured and filtered maps share the wavelength axis, so a zoom on one follows on the others. Above the maps, five diff --git a/docs/howto/marginal_checks.md b/docs/howto/marginal_checks.md index 3cf4e95..fff8be2 100644 --- a/docs/howto/marginal_checks.md +++ b/docs/howto/marginal_checks.md @@ -74,8 +74,11 @@ Interpreting a mismatch: - **SD** — only the width lower bound is rigorous; the centroid holds at TL or for a temporally symmetric pulse. -Restrict the moments and the shape comparison to a spectral **trust region** so -out-of-band detector noise — amplified by any efficiency tilt — cannot dominate. +Restrict the moments and the shape comparison to spectral **trust regions** so +out-of-band detector noise — amplified by any efficiency tilt — cannot dominate: +one for the trace marginal, in the trace's wavelengths, and one for the +independent spectrum, in fundamental wavelengths. For SHG the two lie an octave +apart, so a single shared region cannot hold both. ## Fit the third-order efficiency exponent @@ -87,7 +90,13 @@ because the tilt shifts the centroid monotonically in $n$: ```python n = mc.auto_third_order_exponent( - lam, marg, lam_spec, Ilam_spec, "pg", window=(210e-9, 1000e-9) + lam, + marg, + lam_spec, + Ilam_spec, + "pg", + trace_window=(210e-9, 1000e-9), + spectrum_window=(700e-9, 900e-9), ) corrected = trace * croak.preprocess.third_order_scale(lam, n)[:, None] ``` @@ -101,7 +110,7 @@ All of the above is interactive on the GUI's **Check the marginal** step, the second step of the Data stage, which sits between the data loaders and Prepare for measured, simulated and synthetic sessions. It overlays the predicted marginal on the measured one, marks the predicted vs measured centroid, reports the width-bracket check, -and exposes the trust region, a log/linear toggle and the `(λ/µm)^exp` correction +and exposes the two trust regions (trace and spectrum), a log/linear toggle and the `(λ/µm)^exp` correction with an `Auto` (centroid-matching) button. The **SHG marginal correction** also lives here: it rescales the trace's frequency rows onto the predicted SHG autoconvolution ({func}`~croak.marginal_checks.apply_marginal_correction`), diff --git a/docs/howto/postprocessing.md b/docs/howto/postprocessing.md index 8a3f5b6..3f273b7 100644 --- a/docs/howto/postprocessing.md +++ b/docs/howto/postprocessing.md @@ -290,9 +290,10 @@ publication figure ({func}`~croak.plotting.export.views_figure`, To trim ringing or out-of-band energy from the *result* before plotting (without re-running the retrieval), apply {func}`croak.processing.post_filter` — a -reversible temporal/spectral Planck taper that returns a new result. Pass the -measurement windows `td.lamm_lims` / `td.tau_lims` (λm/τm) as `lam_lims` / -`tau_lims`. The stored trace and error still describe the unfiltered retrieval. +reversible temporal/spectral Planck taper that returns a new result. Pass +`td.fundamental_window` as `lam_lims` (a window at the fundamental, also for +SHG) and `td.tau_lims` (τm) as `tau_lims`. The stored trace and error still +describe the unfiltered retrieval. See [Grids and filtering](grids_and_filtering.md). diff --git a/docs/howto/preprocessing.md b/docs/howto/preprocessing.md index f000af4..f5fb1f2 100644 --- a/docs/howto/preprocessing.md +++ b/docs/howto/preprocessing.md @@ -97,12 +97,23 @@ A few `load_and_clean` keywords do most of the work: - **`tau_lims`** (soft Planck taper, the τm window) and **`tau_crop`** (hard crop, applied last on the regridded delay axis) — window the delay axis. `tau_lims` is stored on the `TraceData` for post-retrieval windowing. -- **`lamm_lims`** — the *useful* measurement wavelength window λm (applied before - regridding and stored on the `TraceData` for post-retrieval windowing). +- **`lamm_lims`** — the *useful* wavelength window λm of the **trace**, in the + trace's own wavelengths (for SHG, around half the fundamental). Applied to the + trace before regridding; it does not touch the independent spectrum. - **`lam_spec`, `Ilam_spec`** — an independently measured fundamental spectrum; when supplied it is resampled onto the grid as `td.Iomega` and can seed the guess, drive the [spectral-match penalty](regularisation.md), or enable SHG marginal correction (`marginal_correct=True`). +- **`lams_lims`** — the wavelength window λs of the independent **spectrum**, in + fundamental wavelengths, independent of λm (default: the spectrum's full + extent). The spectrum is tapered to zero outside it before it is resampled. + The two windows are separate because the trace and the spectrum come from + different instruments, with noise at unrelated wavelengths, and for SHG lie an + octave apart. `td.fundamental_window` gives the window to use on the retrieved + field: λm for PG/SD (whose trace sits at the fundamental), λs for SHG, and + `None` for SHG without a spectrum. In croak 0.4 and earlier, λm also tapered + the spectrum (×2 for SHG); a session saved then reads back with λs unset, so set + λs to taper the spectrum as before. ## De-fringing: notch vs. Takeda low-pass diff --git a/tests/gui/test_stage_marginal_check.py b/tests/gui/test_stage_marginal_check.py index 06174ef..992c45e 100644 --- a/tests/gui/test_stage_marginal_check.py +++ b/tests/gui/test_stage_marginal_check.py @@ -177,11 +177,11 @@ def test_stage_marginal_check_predict_returns_none_without_usable_spectrum( stage, monkeypatch ): spec = np.exp(-(((LAM - 800e-9) / 30e-9) ** 2)) - # all of the spectrum outside the trust region - stage._trust_nm = (1200.0, 1300.0) + # all of the spectrum outside its trust region + stage._spec_trust_nm = (1200.0, 1300.0) assert stage._predict(LAM, spec, "pg") is None # a single in-band sample spans no band to predict on - stage._trust_nm = (700.0, 900.0) + stage._spec_trust_nm = (700.0, 900.0) assert stage._predict(np.array([800e-9]), np.array([1.0]), "pg") is None def refuse(*args, **kwargs): @@ -320,3 +320,87 @@ def test_peak_norm_falls_back_outside_the_mask(): # all-zero data is returned unchanged np.testing.assert_allclose(_peak_norm(np.zeros(3), np.ones(3, bool)), 0.0) assert _peak_norm(np.zeros(0), np.zeros(0, bool)).size == 0 + + +# -- separate trust regions for the trace and the spectrum --------------------- +def _shg_data_an_octave_apart(): + """An SHG trace near 400 nm with its fundamental spectrum near 800 nm.""" + data = _data("shg") + data["lam"] = LAM / 2.0 + return data + + +def test_stage_marginal_check_spectrum_trust_defaults_to_the_spectrum(stage): + _enter(stage, _shg_data_an_octave_apart()) + assert stage._trust_nm == (190.0, 1000.0) + assert stage._spec_trust_nm == (700.0, 900.0) + assert stage.spec_trust.isEnabled() + # with the trust regions an octave apart the prediction still forms (one + # shared region cannot hold a 400 nm trace and an 800 nm spectrum) + stage._on_trust(340.0, 460.0) + spec = np.exp(-(((LAM - 800e-9) / 30e-9) ** 2)) + assert stage._predict(LAM, spec, "shg") is not None + + +def test_stage_marginal_check_spectrum_trust_is_kept_for_the_same_spectrum(stage): + _enter(stage, _data()) + stage._on_spec_trust(720.0, 880.0) + assert stage._spec_trust_nm == (720.0, 880.0) + stage.on_enter() # same spectrum: the manual region is kept + assert stage._spec_trust_nm == (720.0, 880.0) + data = _data() + data["lam_spec"] = np.linspace(600e-9, 1000e-9, 64) + _enter(stage, data) # a different spectrum re-defaults + assert stage._spec_trust_nm == (600.0, 1000.0) + + +def test_stage_marginal_check_spectrum_trust_is_off_without_a_spectrum(stage): + _enter(stage, _data(spectrum=False)) + assert not stage.spec_trust.isEnabled() + + +def test_stage_marginal_check_auto_passes_both_trust_regions(stage, monkeypatch): + seen = {} + + def fit(*args, **kwargs): + seen.update(kwargs) + return 1.0 + + monkeypatch.setattr(stage_marginal_check, "auto_third_order_exponent", fit) + _enter(stage, _shg_data_an_octave_apart()) + stage._on_trust(340.0, 460.0) + stage._on_spec_trust(720.0, 880.0) + stage._auto_exponent() + assert seen["trace_window"] == pytest.approx((340e-9, 460e-9)) + assert seen["spectrum_window"] == pytest.approx((720e-9, 880e-9)) + + +def test_stage_marginal_check_spectrum_trust_outside_the_spectrum_shows_no_prediction( + stage, +): + _enter(stage, _data()) + stage._on_spec_trust(1200.0, 1300.0) # no spectrum sample in the region + legend = _legend(stage.marg_view) + assert "spectrum" in legend and "predicted" not in legend + assert stage.status_label.text() == ( + "No independent spectrum: showing the trace marginal only." + ) + + +@pytest.mark.parametrize( + ("entry", "extent", "expected"), + [ + # measured: clipped to the 190-1000 nm detector band + ("experimental", (600e-9, 1200e-9), (600.0, 1000.0)), + # measured, wholly beyond 1 um (a 1.6 um fundamental): its full range + ("experimental", (1400e-9, 1800e-9), (1400.0, 1800.0)), + # noise-free: the full range + ("synthetic", (600e-9, 1200e-9), (600.0, 1200.0)), + ("simulated", (600e-9, 1200e-9), (600.0, 1200.0)), + ], +) +def test_stage_marginal_check_spectrum_trust_default(stage, entry, extent, expected): + data = _data() + data["lam_spec"] = np.linspace(*extent, 64) + _enter(stage, data, entry=entry) + assert stage._spec_trust_nm == expected diff --git a/tests/gui/test_stage_preprocess.py b/tests/gui/test_stage_preprocess.py index e72d153..f2d98a6 100644 --- a/tests/gui/test_stage_preprocess.py +++ b/tests/gui/test_stage_preprocess.py @@ -6,6 +6,7 @@ from __future__ import annotations +import numpy as np import pytest pytest.importorskip("PySide6") @@ -13,7 +14,7 @@ import shiboken6 from croak.gui import stage_preprocess -from croak.gui.stage_load import seed_preproc_defaults +from croak.gui.stage_load import compute_preproc_defaults, seed_preproc_defaults from croak.gui.stage_preprocess import StagePreprocess from croak.gui.state import WizardState from croak.session.pipeline import assemble_load_data, build_tracedata @@ -57,6 +58,7 @@ def test_stage_preprocess_range_handlers_store_values_and_schedule(stage): stage._on_lam_range: ("lam_min_nm", "lam_max_nm"), stage._on_tau_range: ("tau_min_fs", "tau_max_fs"), stage._on_lamm_range: ("lamm_min_nm", "lamm_max_nm"), + stage._on_lams_range: ("lams_min_nm", "lams_max_nm"), stage._on_taum_range: ("taum_min_fs", "taum_max_fs"), } p = stage.state.preproc @@ -249,3 +251,77 @@ def test_stage_preprocess_numbers_describe_the_grid(loaded): assert numbers["Taper loss"].property("warn") is True loaded._show_tracedata(replace(td, taper_loss=0.0)) assert numbers["Taper loss"].text() == "< 0.1%" + + +def _with_spectrum(stage, *, extent_nm=(600.0, 1000.0)): + """Give the loaded data an independent spectrum and re-seed the defaults.""" + data = dict(stage.state.load_data) + lam = np.linspace(extent_nm[0], extent_nm[1], 200) * 1e-9 + data["lam_spec"], data["Ilam_spec"] = lam, np.ones_like(lam) + stage.state.set_load_data(data) + return lam + + +def test_preproc_defaults_seed_the_spectrum_window_from_the_spectrum(loaded): + _with_spectrum(loaded) + defaults = compute_preproc_defaults(loaded.state.load_data) + assert (defaults.lams_min_nm, defaults.lams_max_nm) == (600.0, 1000.0) + assert (defaults.lams_bound_min_nm, defaults.lams_bound_max_nm) == (600.0, 1000.0) + + +def test_preproc_defaults_without_a_spectrum_leave_the_spectrum_window_unset(loaded): + defaults = compute_preproc_defaults(loaded.state.load_data) + assert defaults.lams_min_nm is None and defaults.lams_max_nm is None + assert defaults.lams_bound_min_nm is None and defaults.lams_bound_max_nm is None + + +def test_reseeding_clamps_the_spectrum_window_and_drops_it_with_the_spectrum(loaded): + _with_spectrum(loaded) + seed_preproc_defaults(loaded.state) + p = loaded.state.preproc + p.lams_min_nm, p.lams_max_nm = 650.0, 950.0 + # same trace, narrower spectrum: values are kept but clamped to the bounds + _with_spectrum(loaded, extent_nm=(700.0, 900.0)) + seed_preproc_defaults(loaded.state, reset_values=False) + assert (p.lams_min_nm, p.lams_max_nm) == (700.0, 900.0) + # spectrum removed: the window goes with it + data = dict(loaded.state.load_data) | dict(lam_spec=None, Ilam_spec=None) + loaded.state.set_load_data(data) + seed_preproc_defaults(loaded.state, reset_values=False) + assert p.lams_min_nm is None and p.lams_bound_min_nm is None + + +def test_spectrum_window_control_follows_the_spectrum(loaded): + loaded._seed_ranges() + assert not loaded.lams_range.isEnabled() + _with_spectrum(loaded) + seed_preproc_defaults(loaded.state) + loaded._seed_ranges() + assert loaded.lams_range.isEnabled() + assert (loaded.lams_range.lo_spin.value(), loaded.lams_range.hi_spin.value()) == ( + 600.0, + 1000.0, + ) + + +def test_reseeding_keeps_an_unset_spectrum_window_unset(loaded): + # An older session: a spectrum is loaded but the window was never set, so it + # stays "full extent" rather than being invented from the bounds. + _with_spectrum(loaded) + p = loaded.state.preproc + p.lams_min_nm = p.lams_max_nm = None + seed_preproc_defaults(loaded.state, reset_values=False) + assert p.lams_bound_min_nm == 600.0 + assert p.lams_min_nm is None and p.lams_max_nm is None + + +def test_reseeding_resets_a_spectrum_window_that_misses_the_new_spectrum(loaded): + # Same trace, a spectrum in a different band: the kept window would clamp to a + # zero-width band at the new edge, so it falls back to the full extent instead. + _with_spectrum(loaded) + seed_preproc_defaults(loaded.state) + p = loaded.state.preproc + p.lams_min_nm, p.lams_max_nm = 650.0, 950.0 + _with_spectrum(loaded, extent_nm=(1400.0, 1800.0)) + seed_preproc_defaults(loaded.state, reset_values=False) + assert p.lams_min_nm is None and p.lams_max_nm is None diff --git a/tests/gui/test_stage_retrieve.py b/tests/gui/test_stage_retrieve.py index 819ff7c..9715bbc 100644 --- a/tests/gui/test_stage_retrieve.py +++ b/tests/gui/test_stage_retrieve.py @@ -456,3 +456,56 @@ def test_retrieve_form_finds_a_setting_by_name_or_help(qtbot): assert not regularisation.body.isHidden() # unfolded to show the matches stage.form.find.setText("") assert regularisation.folded and regularisation.body.isHidden() + + +# -- post-filter windows (issue #22) -------------------------------------------- +def _as_shg(td, *, lams_lims): + """``td`` relabelled as an SHG trace of its 800 nm pulse: λm around 400 nm.""" + return replace( + td, + interaction="shg", + omega0_trace=2.0 * td.omega0_pulse, + lamm_lims=(350e-9, 450e-9), + lams_lims=lams_lims, + ) + + +def _energy(spectrum): + return float(np.sum(np.abs(spectrum) ** 2)) + + +def test_stage_retrieve_spectral_post_filter_keeps_an_shg_field(stage, run): + td = run[2] # a 6 fs pulse at 800 nm + stage.state.tracedata = _as_shg(td, lams_lims=(700e-9, 900e-9)) + stage.state.result = run[3] + stage.on_enter() + assert stage.pf_lam.isEnabled() + stage.pf_lam.setChecked(True) + # The fundamental window holds the pulse; the SHG trace window (half the + # wavelengths) would have removed it entirely. + assert _energy(stage.state.result.spectrum) > 0.5 * _energy(run[3].spectrum) + + +def test_stage_retrieve_spectral_post_filter_is_off_without_a_window(stage, run): + stage.state.tracedata = _as_shg(run[2], lams_lims=None) + stage.state.result = run[3] + stage.on_enter() + assert not stage.pf_lam.isEnabled() + assert "no spectral window" in stage.pf_lam.toolTip() + assert stage.pf_tau.isEnabled() + + +def test_stage_retrieve_spectral_post_filter_follows_a_re_prepared_trace(stage, run): + td = run[2] + stage.state.tracedata = _as_shg(td, lams_lims=(700e-9, 900e-9)) + stage.state.result = run[3] + stage.on_enter() + assert stage.pf_lam.isEnabled() + # the spectrum is removed in Prepare and the stage re-entered: no window now + stage.state.tracedata = _as_shg(td, lams_lims=None) + stage.on_enter() + assert not stage.pf_lam.isEnabled() + # and added back + stage.state.tracedata = _as_shg(td, lams_lims=(700e-9, 900e-9)) + stage.on_enter() + assert stage.pf_lam.isEnabled() diff --git a/tests/test_marginal_checks.py b/tests/test_marginal_checks.py index 2755435..89f2da8 100644 --- a/tests/test_marginal_checks.py +++ b/tests/test_marginal_checks.py @@ -181,9 +181,9 @@ def test_auto_exponent_no_signal_raises(): marg = np.exp(-((lam - 800e-9) ** 2) / (2 * (25e-9) ** 2)) spec = np.ones_like(lam) # a trust window with no overlap leaves nothing to fit - with pytest.raises(ValueError, match="no signal in the trust window"): + with pytest.raises(ValueError, match="no signal in the trace's trust window"): mc.auto_third_order_exponent( - lam, marg, lam, spec, "pg", window=(200e-9, 300e-9) + lam, marg, lam, spec, "pg", trace_window=(200e-9, 300e-9) ) @@ -225,7 +225,13 @@ def test_auto_exponent_target_override_still_needs_marginal_signal(): lam, spec, marg = _tilted_marginal() with pytest.raises(ValueError, match="trace marginal has no signal"): mc.auto_third_order_exponent( - lam, marg, lam, spec, "pg", window=(200e-9, 300e-9), target_centroid=1e15 + lam, + marg, + lam, + spec, + "pg", + trace_window=(200e-9, 300e-9), + target_centroid=1e15, ) @@ -245,3 +251,43 @@ def test_auto_exponent_root_at_a_bound_returns_that_bound(): # the base marginal is spec / (lam/um)^2: n = 2 restores spec exactly n = mc.auto_third_order_exponent(lam, marg, lam, spec, "pg", bounds=(2.0, 5.0)) assert n == pytest.approx(2.0, abs=1e-9) + + +def test_auto_exponent_spectrum_window_without_signal_raises(): + lam, spec, marg = _tilted_marginal() + with pytest.raises(ValueError, match="no signal in the spectrum's trust window"): + mc.auto_third_order_exponent( + lam, marg, lam, spec, "pg", spectrum_window=(200e-9, 300e-9) + ) + + +def test_auto_exponent_windows_the_spectrum_and_the_trace_independently(): + """SHG: the spectrum (fundamental) and the marginal (second harmonic) sit an + octave apart, so one shared window cannot hold both; each gets its own.""" + lam_spec = np.linspace(1400e-9, 1800e-9, 400) + spec = np.exp(-((lam_spec - 1600e-9) ** 2) / (2 * (40e-9) ** 2)) + omega_spec = wlfreq(lam_spec) + target = mc.predicted_centroid(omega_spec, spec * lam_spec**2, "shg") + lam = np.linspace(700e-9, 900e-9, 400) + marg_true = np.exp(-((lam - wlfreq(target)) ** 2) / (2 * (20e-9) ** 2)) + base = marg_true / (lam / 1e-6) ** 2.0 + n_ref = mc.auto_third_order_exponent( + lam, base, lam_spec, spec, "shg", target_centroid=target + ) + n = mc.auto_third_order_exponent( + lam, + base, + lam_spec, + spec, + "shg", + trace_window=(700e-9, 900e-9), + spectrum_window=(1400e-9, 1800e-9), + ) + # Same target either way: the spectrum window covers the whole spectrum. + assert n == pytest.approx(n_ref, abs=1e-9) + # A trace-domain window applied to the spectrum (the old shared window) + # finds no spectrum at all. + with pytest.raises(ValueError, match="independent spectrum has no signal"): + mc.auto_third_order_exponent( + lam, base, lam_spec, spec, "shg", spectrum_window=(700e-9, 900e-9) + ) diff --git a/tests/test_pipeline_simulated.py b/tests/test_pipeline_simulated.py index 0e167c7..4ea3376 100644 --- a/tests/test_pipeline_simulated.py +++ b/tests/test_pipeline_simulated.py @@ -553,3 +553,19 @@ def test_uniform_delay_core_drops_the_wings(tmp_path, simulated_truth): np.testing.assert_allclose(d, d[0], rtol=1e-9) # the kept columns are the core's, not the zero-padded wings assert core_only["trace"].max() > 0 + + +def test_native_grid_load_windows_the_field_to_the_signal_band( + tmp_path, simulated_truth +): + from conftest import write_simulated_h5 + + path = write_simulated_h5(tmp_path / "w.h5", simulated_truth) + with_spec = assemble_simulated_tracedata(_params(path, use_spectrum=True)) + # the spectrum is not windowed: its window is its full extent + assert with_spec.lam_spec is not None + assert with_spec.lams_lims == (with_spec.lam_spec.min(), with_spec.lam_spec.max()) + # PG: the field is windowed to the trace's signal band, as before + assert with_spec.fundamental_window == (with_spec.lam_min, with_spec.lam_max) + without = assemble_simulated_tracedata(_params(path, use_spectrum=False)) + assert without.lams_lims is None diff --git a/tests/test_preprocess.py b/tests/test_preprocess.py index 1309a2b..cdb9f2b 100644 --- a/tests/test_preprocess.py +++ b/tests/test_preprocess.py @@ -1237,3 +1237,150 @@ def test_load_and_clean_carries_the_independent_spectrum(): # the 1600 nm fundamental peaks at the pulse carrier (grid centre), within # 5 % of the grid's half-span assert abs(td.grid.omega[int(np.argmax(td.Iomega))]) < 0.05 * td.grid.omega.max() + + +def _shg_clean(**kw): + """``load_and_clean`` of the SHG experiment with its fundamental spectrum.""" + exp, lam_spec, Ilam_spec, lam_min, lam_max = _shg_experiment() + return preprocess.load_and_clean( + exp.trace, + exp.lam_nm * 1e-9, + exp.delay_fs * 1e-15, + "shg", + lam_min=lam_min, + lam_max=lam_max, + input_unit="delay", + taper_warn_level=0.0, + lam_spec=lam_spec, + Ilam_spec=Ilam_spec, + **kw, + ) + + +def test_load_and_clean_spectrum_window_defaults_to_the_spectrum_extent(): + _, lam_spec, _, _, _ = _shg_experiment() + td = _shg_clean() + assert td.lams_lims == pytest.approx((lam_spec.min(), lam_spec.max())) + + +def test_load_and_clean_trace_window_leaves_the_spectrum_alone(): + # A narrow SHG trace window (around 800 nm) used to be doubled onto the + # fundamental spectrum (1600 nm) and cut it; the two are now independent. + # 700-850 nm doubled is 1400-1700 nm, which would cut the 1600 nm spectrum. + narrow = (700e-9, 850e-9) + full = _shg_clean() + cut = _shg_clean(lamm_lims=narrow) + assert cut.Ilam_spec_filt is not None and full.Ilam_spec_filt is not None + np.testing.assert_array_equal(cut.Ilam_spec_filt, full.Ilam_spec_filt) + assert not np.array_equal(cut.Ifrog_filt, full.Ifrog_filt) + + +def test_load_and_clean_spectrum_window_leaves_the_trace_alone(): + lams = (1500e-9, 1700e-9) + full = _shg_clean() + cut = _shg_clean(lams_lims=lams) + assert cut.lams_lims == lams + np.testing.assert_array_equal(cut.trace, full.trace) + np.testing.assert_array_equal(cut.Ifrog_filt, full.Ifrog_filt) + # outside the window the spectrum is tapered to zero; inside it is kept + assert cut.Ilam_spec_filt is not None and cut.lam_spec is not None + outside = (cut.lam_spec < 1400e-9) | (cut.lam_spec > 1800e-9) + assert np.all(cut.Ilam_spec_filt[outside] == 0.0) + assert cut.Ilam_spec_filt.max() == pytest.approx(1.0) + + +def test_load_and_clean_spectrum_window_without_a_spectrum_is_none(experiment): + exp = experiment + td = preprocess.load_and_clean( + exp.trace, + exp.lam_nm * 1e-9, + exp.delay_fs * 1e-15, + exp.interaction, + lam_min=exp.lam_min, + lam_max=exp.lam_max, + input_unit="delay", + ) + assert td.lams_lims is None + + +def test_load_and_clean_rejects_a_spectrum_window_without_a_spectrum(experiment): + exp = experiment + with pytest.raises(ValueError, match="lams_lims needs an independent spectrum"): + preprocess.load_and_clean( + exp.trace, + exp.lam_nm * 1e-9, + exp.delay_fs * 1e-15, + exp.interaction, + lam_min=exp.lam_min, + lam_max=exp.lam_max, + input_unit="delay", + lams_lims=(700e-9, 900e-9), + ) + + +def test_fundamental_window_of_shg_is_the_spectrum_window(): + td = _shg_clean(lams_lims=(1500e-9, 1700e-9)) + assert td.fundamental_window == (1500e-9, 1700e-9) + + +def test_fundamental_window_of_pg_is_the_trace_window(experiment): + # PG: the trace is at the fundamental, so its λm is the signal's measured + # support, and a spectrum window does not replace it. + exp = experiment + kw = dict(lam_min=exp.lam_min, lam_max=exp.lam_max, input_unit="delay") + args = (exp.trace, exp.lam_nm * 1e-9, exp.delay_fs * 1e-15) + lamm = (exp.lam_min * 1.05, exp.lam_max * 0.95) + pg = preprocess.load_and_clean(*args, "pg", lamm_lims=lamm, **kw) + assert pg.fundamental_window == lamm + lam = exp.lam_nm * 1e-9 + with_spectrum = preprocess.load_and_clean( + *args, + "pg", + lamm_lims=lamm, + lam_spec=lam, + Ilam_spec=np.ones_like(lam), + lams_lims=(exp.lam_min, exp.lam_max), + **kw, + ) + assert with_spectrum.fundamental_window == lamm + # SHG without a spectrum: λm is at the second harmonic, so there is no + # fundamental window to offer (a ×2 map of it is not a support bound). + shg = preprocess.load_and_clean( + *args, "shg", lamm_lims=lamm, taper_warn_level=0.0, **kw + ) + assert shg.fundamental_window is None + + +@pytest.mark.parametrize( + ("lams", "match"), + [ + ((1700e-9, 1500e-9), "low edge below its high edge"), + ((1600e-9, 1600e-9), "low edge below its high edge"), + ((2300e-9, 3000e-9), "holds no sample of the spectrum"), + # the spectrum is sampled every ~1.25 nm from 1200 nm: a window between + # two samples holds none strictly inside it + ((1200.1e-9, 1201.0e-9), "holds no sample of the spectrum"), + ], +) +def test_load_and_clean_rejects_an_unusable_spectrum_window(lams, match): + with pytest.raises(ValueError, match=match): + _shg_clean(lams_lims=lams) + + +def test_load_and_clean_rejects_a_spectrum_window_without_signal(): + exp, lam_spec, _, lam_min, lam_max = _shg_experiment() + dark = np.where(lam_spec < 1300e-9, 0.0, 1.0) + with pytest.raises(ValueError, match="no positive spectral intensity"): + preprocess.load_and_clean( + exp.trace, + exp.lam_nm * 1e-9, + exp.delay_fs * 1e-15, + "shg", + lam_min=lam_min, + lam_max=lam_max, + input_unit="delay", + taper_warn_level=0.0, + lam_spec=lam_spec, + Ilam_spec=dark, + lams_lims=(1210e-9, 1290e-9), + ) diff --git a/tests/test_session.py b/tests/test_session.py index 535fd1e..db5f19b 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -669,3 +669,97 @@ def test_fit_thickness_requires_dispersive_in_kwargs(): p = RetrieveParams(fit_thickness=True, dispersive=False) assert covariance_kwargs(p)["fit_thickness"] is False + + +def _captured_load_and_clean(monkeypatch) -> dict: + """Patch ``load_and_clean`` to record its keyword arguments.""" + seen: dict = {} + monkeypatch.setattr( + croak.preprocess, "load_and_clean", lambda *a, **kw: seen.update(kw) + ) + return seen + + +def _data_with_spectrum(experiment) -> dict: + lam = experiment.lam_nm * 1e-9 + return dict( + trace=experiment.trace, + lam=lam, + scanaxis=experiment.delay_fs * 1e-15, + interaction=experiment.interaction, + input_unit="delay", + lam_spec=lam, + Ilam_spec=np.ones_like(lam), + ) + + +def test_build_tracedata_passes_the_spectrum_window(monkeypatch, experiment): + seen = _captured_load_and_clean(monkeypatch) + p = PreprocParams(lams_min_nm=700.0, lams_max_nm=900.0) + build_tracedata(p, _data_with_spectrum(experiment)) + assert seen["lams_lims"] == pytest.approx((700e-9, 900e-9)) + + +def test_build_tracedata_unset_spectrum_window_means_full_extent( + monkeypatch, experiment +): + seen = _captured_load_and_clean(monkeypatch) + build_tracedata(PreprocParams(), _data_with_spectrum(experiment)) + assert seen["lams_lims"] is None + + +def test_build_tracedata_ignores_the_spectrum_window_without_a_spectrum( + monkeypatch, experiment +): + # A window left over from an earlier spectrum must not make load_and_clean + # raise once the spectrum is gone. + seen = _captured_load_and_clean(monkeypatch) + data = _data_with_spectrum(experiment) | dict(lam_spec=None, Ilam_spec=None) + build_tracedata(PreprocParams(lams_min_nm=700.0, lams_max_nm=900.0), data) + assert seen["lams_lims"] is None + + +def test_an_options_file_without_the_spectrum_window_loads_it_unset(tmp_path): + # options.toml files written before the spectrum window existed lack its + # keys; they must read back as "full extent", and None must round-trip. + SessionOptions().to_toml(str(tmp_path / "options.toml")) + back = SessionOptions.from_toml(str(tmp_path / "options.toml")) + assert back.preproc.lams_min_nm is None and back.preproc.lams_max_nm is None + old = SessionOptions.from_dict({"preproc": {"lamm_min_nm": 400.0}}) + assert old.preproc.lams_min_nm is None and old.preproc.lams_max_nm is None + + +def test_build_tracedata_rejects_a_half_set_spectrum_window(experiment): + with pytest.raises(ValueError, match="both lams_min_nm and lams_max_nm"): + build_tracedata( + PreprocParams(lams_min_nm=700.0), _data_with_spectrum(experiment) + ) + + +def test_apply_post_filter_keeps_an_shg_field(experiment_h5, experiment): + # Regression for issue #22, through the real pipeline: the SHG trace window + # (around 400 nm for an 800 nm pulse) used to be passed to post_filter, whose + # taper then removed the whole field. + from dataclasses import replace + + from croak.session.pipeline import apply_post_filter + + options = _options_for(experiment_h5, experiment) + td = build_tracedata(options.preproc, assemble_load_data(options.load)) + result = run_retrieval( + RetrieveParams(solver="copra", maxiters=3), td, rng=np.random.default_rng(0) + ) + shg = replace( + td, + interaction="shg", + omega0_trace=2.0 * td.omega0_pulse, + lamm_lims=(350e-9, 450e-9), + lams_lims=(700e-9, 900e-9), + ) + params = RetrieveParams(post_filter_lam=True) + kept = apply_post_filter(params, result, shg).spectrum + energy = float(np.sum(np.abs(kept) ** 2)) / float( + np.sum(np.abs(result.spectrum) ** 2) + ) + # a 6 fs pulse at 800 nm keeps most of its energy inside 700-900 nm + assert energy > 0.5 diff --git a/tests/test_session_pipeline.py b/tests/test_session_pipeline.py index 0b40804..ef98378 100644 --- a/tests/test_session_pipeline.py +++ b/tests/test_session_pipeline.py @@ -714,7 +714,7 @@ def test_apply_post_filter_windows_only_when_toggled(monkeypatch, lam, tau, expe "post_filter", lambda result, *, lam_lims, tau_lims: calls.append((lam_lims, tau_lims)), ) - td = SimpleNamespace(lamm_lims=(1e-7, 2e-7), tau_lims=(-5e-14, 5e-14)) + td = SimpleNamespace(fundamental_window=(1e-7, 2e-7), tau_lims=(-5e-14, 5e-14)) p = RetrieveParams(post_filter_lam=lam, post_filter_tau=tau) out = apply_post_filter(p, "result", td) if expected is None: