Repository navigation
FEAT: detector response map - #98
Conversation
stevehenke
left a comment
There was a problem hiding this comment.
Summary
The forward model y = m·blur(crop(Σ|ψ_far|²)) + b is correct, and the order (crop, then blur, then pixel response) is right. Autodiff applies the map exactly, and LSQML applies it approximately but converges to the right answer. The other analytic reconstructors accept the map but ignore it in their updates.
1. Most analytic solvers ignore the map
- PIE, rPIE, mPIE, GM-rPIE, MAGPIE, GM-MAGPIE, DM and RAAR all update through
replace_propagated_exit_wave_magnitude(psi_far, y_true)(reconstructors/base.py:727). It sets the amplitude tosqrt(d)and ignoresmandb. - BH's
gradientF/hessianF(reconstructors/bh.py:157) comparesqrt(y_true)against|ψ_far|, so BH ignores the map too. - In all of these,
yis used only for loss tracking. The object and probe come out identical to a run without the map, while the reported loss changes.
Suggested fix: either reject the map for these solvers, as api/options/magpie.py:141 already does for diffraction_pattern_blur_sigma, or project with the ratio ψ·sqrt(d / y_pred).
- The ratio reduces to today's behavior when the map is the identity and blur is off.
- It matches what LSQML-Gaussian already does (below).
- BH would need its gradient and Hessian re-derived, or a rejection.
2. LSQML: approximate, right fixed point
The true gradient carries a factor m that backward_to_psi_far omits: ∂L/∂ψ* ∝ m·(1 − sqrt(d/y))·ψ for Gaussian, m·(1 − d/y)·ψ for Poisson. The step is still zero exactly at y = d.
- Gaussian. With the default σ = 0.5 and α = 0.5, the step is
ψ' = sqrt(d/y)·ψ, soI' = d·I/(m·I + b).- The fixed point is
I* = (d − b)/m. - With
b = 0it reaches the answer in one step. - With
b > 0the reciprocal-space step multiplies the error byb/deach iteration, so convergence is slower where the background dominates.
- The fixed point is
- Poisson. The step-size search assumes
y(α) = y·(1 − αξ)², but with the offset the true value is(y − b)·(1 − αξ)² + b. - Epoch-0 probe rescale. It uses
c² = Σd / Σy, where exact would be(Σd − Σb) / Σ(m·I).
The step-size search and the rescale are both exact for m alone and approximate with b ≠ 0.
3. Validate the maps
- Require finite values. One NaN in
mpoisons autodiff gradients even at masked pixels, because0 × NaN = NaNthroughm·I + b. In LSQML a NaN makes the loss NaN, and through the epoch-0 rescale also the probe. - Require real values.
to_tensorkeeps complex input complex, and validation accepts it. - Mask pixels where
m ≤ floor. In LSQML-Gaussian, a pixel withm = 0has its intensity multiplied byd/bon every step. Ifb = 0too, the step divides byeps = 1e-9and blows up. The ratio projection in item 1 would inherit this.
4. The maps cannot be refined
detector_response_multiplier and detector_response_offset are registered as fixed buffers, not as a ReconstructParameter. No reconstructor, autodiff included, can refine them, and get_data cannot return them. For integrating detectors this is limiting: the pedestal drifts, and dark frames are noisy estimates of it.
Suggestions:
- Make the offset a
ReconstructParameterwith its ownOptimizationPlanand options (optimizable, start epoch, step size), likeOPRModeWeightsorProbePositions, and add it toget_data.- Autodiff then refines it for free.
- Analytic solvers need an update rule. For fixed predicted intensities
I_k, the least-squares offset per pixel isb = mean_k(d_k − m·I_k)over all positions, using valid pixels only. Accumulate it across the epoch's batches and apply it once per epoch.
- Refine the multiplier only with a constraint.
- It has a global scale degeneracy with the probe:
(m, P) → (c·m, P/sqrt(c))leavesyunchanged, so it needs a normalization such asmean(m) = 1over valid pixels, or a fixed probe power. - Per pixel,
mandbare identifiable together by regression across positions:m = cov_k(I, d)/var_k(I)andb = mean_k(d) − m·mean_k(I). This is well conditioned only whereI_kvaries across the scan, so the multiplier is best left fixed by default.
- It has a global scale degeneracy with the probe:
- Keep
m > 0. An offset may legitimately be negative after pedestal subtraction, so a positivity constraint onbshould be optional.
5. Smaller points
- Tests. The new tests check
forward()only, including for EPIE and BH. Add at least one update-step test per reconstructor family; it would have caught item 1. - Silent no-op on old versions. In ≤ 2.4.0,
Task.__init__(options, *args, **kwargs)swallows unknown keywords, so code written for this feature silently does nothing there. Consider rejecting unknown keyword arguments. - Threshold meaning.
exclude_measured_pixels_belowstill compares raw measured values; with a pedestal in the offset, the threshold no longer means what it used to. - Integrating detectors in ADU. With
mas the gaingandbas the pedestal, the mean is right, but the Poisson NLL is misspecified: ADU variance isg²·I + σ_read², while Poisson assumesg·I + b. Worth documenting that the Gaussian model, or converting to photons first, is the safer choice. - Nits. Trailing whitespace in the new docstrings (
api/task.py).
Account for gain and fixed offsets in magnitude projections, BH derivatives, LSQML reciprocal steps, and initial probe rescaling. This addresses the analytical-reconstructor and LSQML review points in PR #98. Validate response gradients, fixed-point steps, masked pixels, and reconstruction updates; 351 targeted tests pass.
Account for gain and fixed offsets in magnitude projections, BH derivatives, LSQML reciprocal steps, and initial probe rescaling. This addresses the analytical-reconstructor and LSQML review points in PR #98. Validate response gradients, fixed-point steps, masked pixels, and reconstruction updates; 351 targeted tests pass.
658b8ef to
fc96936
Compare
Account for gain and fixed offsets in magnitude projections, BH derivatives, LSQML reciprocal steps, and initial probe rescaling. This addresses the analytical-reconstructor and LSQML review points in PR #98. Validate response gradients, fixed-point steps, masked pixels, and reconstruction updates; 351 targeted tests pass.
fc96936 to
c784cda
Compare
|
@stevehenke Thanks for the review. I've addressed the points below and pushed the changes. I also renamed “multiplier” to “gain” in the code and docs.
A few additional changes came out of this work:
|
Minor
|
Use the exact crop transpose for mixed odd/even grids and the transpose of replicated-edge Gaussian blur in likelihood gradients. Account for crop and blur in LSQML reciprocal-space steps, with adjoint and legacy-reference tests.
Apply shared detector gain and offset across reconstruction algorithms and support optional analytical and autodiff refinement. Include validation, consistent pixel masks, prediction-preserving gain normalization, optimizer closures, documentation, and regression tests.
10de448 to
ad6f1dd
Compare
|
@stevehenke Thanks for the follow-up review. I’ve pushed the changes below.
I also consolidated response-fitting ownership in Validation included 127 refinement and feasibility tests, including CUDA buffer movement and empty-rank handling. For the ownership refactor, all 48 saved arrays—fitted responses, probes, and predictions from 16 fits across SGD/LBFGS, both noise models, and float32/float64—matched the previous implementation exactly. Lint also passed. Finally, I consolidated the history into two commits:
The standalone adjoint commit passed 360 tests and lint. Before rewriting the history, I saved a snapshot and verified that the consolidated head has an identical Git tree. |
Features/fixes
Added a
detector_response_map=(multiplier, offset)argument in thePtychographyTaskAPI. In the forward model, predicted intensity is transformed asintensity * multiplier + offsetbefore it is compared with the measured intensity.Related issues
#92
Mentions
@stevehenke
Checklist (Don't skip!)
To check a checklist item, change the space in the backets with an
x, i.e.,[ ] -> [x].Have you...