Skip to content

pysrx.solvers

pysrx.solvers

Reconstruction solvers: Richardson-Lucy (+TV) and autograd MAP.

tv3d(x, dz_rel=1.0, eps=0.001)

Isotropic 3D total variation (smoothed), z differences scaled by the voxel aspect.

richardson_lucy(views, psf, iterations=17, background=0.0, tv=0.0, z_chunk=16, x0=None, callback=None, shift_px=None, weights=None, cache_otf=None, trace=None, tol=None, patience=3, min_iter=5, accelerate=False)

views (V, Y, X) raw counts; psf (V, Z, h, w) from optics.psf_stack, or the centred psf + shift_px from optics.psf_model -> volume (Z, Y, X). weights (V,) optional per-view gain (flat-field / vignetting); background a scalar or per-view (V,) counts. trace {"iters": [...], "view": v, "plane": z}: for those iterations, every RL step at one view / one plane is appended to trace["frames"] as numpy (x, Hx+b, y, ratio, H^T ratio, H^T 1, x_new) -- for figures; with "x_every": True the plane after every iteration is appended to trace["x_series"] (animations). tol: stop once the relative update ||x_new - x|| / ||x|| stays below tol for patience iterations (after min_iter); iterations is then the cap. ReconResult.iterations is the number actually run. accelerate: Biggs-Andrews vector extrapolation (1997): each RL step starts from x + a (x - x_prev), with a = / clipped to [0, 1), g = RL update - start point.

map

MAP light-field reconstruction with PyTorch autograd (an alternative to fixed-step Richardson-Lucy).

Model: y_v ~ Poisson( g_v (H x)_v + B_v ), x >= 0 (volume), B_v = smooth per-view haze map Loss: mean( mu - y + y log(y / mu) ) + lam_tv * TV_3D(x / mean(x)) (Poisson deviance + total variation prior)

H (forward projection) is the calibrated FFT projector of forward.Projector; autograd supplies H^T (back projection), so the same operator serves RL and this solver. The haze B_v absorbs out-of-volume light that the 121-plane model cannot explain (otherwise RL pushes it into the volume as blur). Optimised with Adam on unconstrained parameters (x = s * softplus(u), B = softplus(upsampled low-res map)); stops when the loss changes by < tol (relative) over window steps. RL -> MAP: RL is the EM algorithm for the same Poisson likelihood without prior and without haze.

tv3d(x, dz_rel=1.0, eps=0.001)

Isotropic 3D total variation (smoothed), z differences scaled by the voxel aspect.

opsolve

Operator-generic reconstruction on the GPU: any linear forward model op with

op.forward(x)  (obj_shape) -> data (any shape),   op.backward(r)  exact adjoint,   op.obj_shape

e.g. forward.sim.SimOperator (lattice SIM, data (H, Z, Y, X)). All three solvers fit y ~ A x + b:

  • tikhonov_op min ||A x + b - y||^2 + lam ||x||^2 by conjugate gradients on (A^T A + lam I) x = A^T (y - b): the Gaussian-MAP / Wiener-equivalent linear baseline (no positivity).
  • rl_op Poisson Richardson-Lucy x <- x A^T(y / (A x + b)) / (A^T 1 - tv div(grad x / |grad x|)), optionally Biggs-Andrews accelerated; history = mean Poisson deviance per iteration.
  • fit_field_op the NeRF world model: a hash-grid neural field (field.hashgrid.NeuralField) over the object grid, fitted by the Poisson deviance through op. The gradient w.r.t. the rendered volume is the exact adjoint A^T(1 - y / mu) / N (no autograd through the operator); the field is back-propagated in plane chunks (two-pass, as field.hashgrid.fit_field), so VRAM stays at one chunk of field activations.

poisson_deviance(y, mu)

Mean Poisson deviance 2 [mu - y + y log(y / mu)] (0 for a perfect fit).

tikhonov_op(y, op, lam=0.01, iterations=30, background=0.0, x0=None, callback=None)

Conjugate gradients on (A^T A + lam I) x = A^T (y - b). history: relative normal-equation residual.

rl_op(y, op, iterations=30, background=0.0, tv=0.0, x0=None, accelerate=False, tol=None, patience=3, min_iter=5, callback=None)

Poisson Richardson-Lucy (+TV) through op; history = mean Poisson deviance before each update.

fit_field_op(y, op, spacing_um, cfg=None, background=0.0, x_init=None, callback=None)

NeRF world model through op: a NeuralField over op.obj_shape with voxel spacing spacing_um (z, y, x), fitted to y by the mean Poisson deviance (+ optional TV / L1 on x / s). x_init (optional, e.g. an RL volume) is distilled into the field first (cfg.distill_steps); with cfg.init_rl > 0 and no x_init, that many RL iterations through op are run to make one. Returns InrResult (volume on the object grid, history (step, deviance, loss), state for re-rendering at any resolution).

rl

Richardson-Lucy (+ total variation) multi-view deconvolution; the operator is forward.lightfield.Projector.

Multi-view Richardson-Lucy (+ total variation) light-field deconvolution on the GPU.

Model (equation 1): y_v = sum_z H_{v,z} * x_z + b (* = 2D convolution, v = view, z = depth) Update (equation 2): x <- x [ H^T( y / (H x + b) ) / H^T 1 ] TV (Dey et al. 2006): denominator becomes H^T 1 - lambda . div( grad x / |grad x| )

GPU layout: everything lives in the Fourier domain. The PSF stack is turned once into OTFs (V, Z, Y, X/2+1); a forward projection is a batched complex multiply-and-sum over z followed by V inverse FFTs, a back projection the conjugate multiply-and-sum over v followed by Z inverse FFTs. Depth is processed in chunks of z_chunk planes so the OTF cache fits in VRAM (37 x 99 x 512 x 257 complex64 = 3.9 GB for the full 20x grid).

richardson_lucy(views, psf, iterations=17, background=0.0, tv=0.0, z_chunk=16, x0=None, callback=None, shift_px=None, weights=None, cache_otf=None, trace=None, tol=None, patience=3, min_iter=5, accelerate=False)

views (V, Y, X) raw counts; psf (V, Z, h, w) from optics.psf_stack, or the centred psf + shift_px from optics.psf_model -> volume (Z, Y, X). weights (V,) optional per-view gain (flat-field / vignetting); background a scalar or per-view (V,) counts. trace {"iters": [...], "view": v, "plane": z}: for those iterations, every RL step at one view / one plane is appended to trace["frames"] as numpy (x, Hx+b, y, ratio, H^T ratio, H^T 1, x_new) -- for figures; with "x_every": True the plane after every iteration is appended to trace["x_series"] (animations). tol: stop once the relative update ||x_new - x|| / ||x|| stays below tol for patience iterations (after min_iter); iterations is then the cap. ReconResult.iterations is the number actually run. accelerate: Biggs-Andrews vector extrapolation (1997): each RL step starts from x + a (x - x_prev), with a = / clipped to [0, 1), g = RL update - start point.