AGENT SKILL

solve-inverse-problem

Use this skill whenever the user has (or can build) a PyLops operator and data and wants to invert it - load it before choosing a solver or writing any inversion code, even when the right solver or regularisation seems obvious. It sets up and runs the inversion in the user's project - choose the solver (lsqr, cgls, cg, regularized_inversion, preconditioned_inversion, fista, spgl1, omp, irls, splitbregman), the regularisation and the stopping parameters, and write a runnable script that reports convergence. E.g. "invert my data with this operator", "which PyLops solver should I use", "add Tikhonov / smoothness / sparsity regularisation to my inversion", "solve y = Op x". Not for an inversion that already misbehaves (use debug-inversion) or for choosing regularisation weights with an L-curve or sweep (use tune-regularization).

License 许可
LGPL-2.1
最近更新
2026-11-02
Stars
★ 1
Forks
0

01 前言 Front Matter

name
solve-inverse-problem
description
Use this skill whenever the user has (or can build) a PyLops operator and data and wants to invert it - load it before choosing a solver or writing any inversion code, even when the right solver or regularisation seems obvious. It sets up and runs the inversion in the user's project - choose the solver (lsqr, cgls, cg, regularized_inversion, preconditioned_inversion, fista, spgl1, omp, irls, splitbregman), the regularisation and the stopping parameters, and write a runnable script that reports convergence. E.g. "invert my data with this operator", "which PyLops solver should I use", "add Tikhonov / smoothness / sparsity regularisation to my inversion", "solve y = Op x". Not for an inversion that already misbehaves (use debug-inversion) or for choosing regularisation weights with an L-curve or sweep (use tune-regularization).

02 资源文件 2

03 SKILL.md 正文

Goal: a runnable, reproducible inversion script in the user's project that solves their problem with a solver and regularisation chosen for a stated reason, and shows whether it converged. Do not modify the installed pylops package.

If the user has no operator yet, use the wrap-operator skill first. If they already have an inversion that misbehaves, use debug-inversion instead. To choose regularisation weights, hand over to tune-regularization once the setup below works.

0. Check the environment

  • Find how the project runs Python (uv.lock, pyproject.toml, environment.yml, requirements*.txt, active venv) and use that runner.
  • Check PyLops and its version: python -c "import pylops; print(pylops.__version__)". Solver signatures and return tuples changed across versions: inspect the installed ones (help(pylops.optimization.basic.cgls)) rather than trusting memory. reference/solver_guide.md describes PyLops 2.x.
  • Optional packages: spgl1 is needed by pylops.optimization.sparsity.spgl1; PyProximal is needed for non-smooth problems beyond what PyLops offers. Ask before installing anything.

1. Characterise the problem

Before picking a solver, write down (and confirm with the user if unclear):

  • Op.shape = (N, M), Op.dims, Op.dimsd, dtype (real/complex);
  • whether the problem is square/symmetric positive definite (e.g. already normal equations), overdetermined (N > M), or underdetermined (N < M, e.g. interpolation, compressive sensing);
  • the noise level (known sigma? SNR?) and whether data are available on a subset only (then the operator should include a Restriction);
  • the prior the user expects on the model: smooth, blocky/piecewise constant, sparse in some transform, bounded, positive, low-rank;
  • the size: can Op.todense() be afforded for a sanity check (M, N up to a few thousand)?

Run pylops.utils.dottest(Op, *Op.shape, complexflag=...) once. If it fails, stop and switch to debug-inversion / wrap-operator: no solver will fix a wrong adjoint.

2. Pick the solver

Use reference/solver_guide.md for the full decision table and return values. In short:

Prior / structure Start with
well-posed, overdetermined, no prior lsqr or cgls (damp for zero-order Tikhonov)
symmetric positive (semi)definite Op cg
smooth model regularized_inversion with Regs=[SecondDerivative/Laplacian], epsRs=[...]
model best expressed in another basis preconditioned_inversion with P (e.g. smoothing or a synthesis transform)
sparse in the model or in a transform T (signal → coefficients) fista (SOp=T.H) or spgl1 (SOp=T, note the opposite convention) if a residual target sigma is known – see pick-sparsifying-transform
few non-zeros, explicit dictionary omp
robust to outliers (L1 data misfit) irls with kind="data"
blocky / TV-like, mixed L1 + L2 terms splitbregman with RegsL1=[FirstDerivative/Gradient]
constraints (bounds, positivity), TV with better control, other non-smooth terms PyProximal: use the pyproximal plugin skill solve-nonsmooth-problem

Avoid Op / y in production code: it is a convenience wrapper with a fixed 100 iterations and no control over stopping or regularisation.

State which solver was picked and why (one or two sentences) before writing code.

3. Write the script

Start from reference/inversion_template.py and adapt it:

  • build (or import) the operator, load the data, reshape to Op.dimsd;
  • scale data and model sensibly: if |y| is ~1e-8 or ~1e8 normalise it and undo the scaling on the result; badly scaled problems stall Krylov solvers;
  • set x0 (zero, or a smooth background model when the user has one);
  • choose niter from the problem size and condition, not "1000 to be safe": start with 20–100 for Krylov solvers and 100–500 for (F)ISTA, then look at the cost curve. Use tol / rtol to stop early;
  • for regularised solvers, set initial weights from a scale argument (e.g. eps ≈ ||Op.H y|| / ||Reg.H Reg x|| order-of-magnitude) and mark them as "to be tuned" – do not claim they are optimal;
  • for fista/ista, let alpha=None so PyLops estimates the step from the largest eigenvalue, or pass eigsdict={"niter": ..., "tol": ...} for big operators; eps is the threshold weight;
  • keep the whole solver return tuple and name its entries (they differ between solvers – see reference/solver_guide.md);
  • save outputs (np.save / the project's I/O) rather than only plotting.

Put the script where the user keeps analysis code (ask if ambiguous) and use the project's plotting conventions if any.

4. Check the result

Run the script and report, with numbers:

  • solver exit status (istop where available) and iterations used;
  • the cost curve (log scale) – flat from iteration 1 means a wrong setup, still decreasing at niter means increase it;
  • data residual ||y - Op x|| / ||y|| compared with the expected noise level (much lower than the noise ⇒ overfitting; much higher ⇒ under-fitting or a modelling error);
  • if the user has a true model (synthetic), pylops.utils.metrics.snr(xtrue, xinv);
  • a plot of the model (and data, modelled data, residual).

If something looks wrong, switch to debug-inversion rather than randomly changing parameters.

5. Hand off

Report: the solver and regularisation chosen and why, the file path, the convergence summary above, and the parameters that should be tuned next (pointing to tune-regularization).

04 同仓库更多 Skills

PyLops
PyLops/pylops-agent-skills

solve-nonsmooth-problem

Use this skill whenever the user wants to solve an inverse problem on PyLops operators with a non-smooth, constrained or non-convex term - load it before writing the objective, choosing a solver or writing optimisation code, even when a SciPy bounded solver or a hand-written FISTA/ADMM loop would also work. Covers total variation, L1/sparsity in a transform, joint sparsity across signals sharing the same components, bounds/positivity/ball/affine constraints, low-rank matrix completion (nuclear norm) and plug-and-play priors with a denoiser (median filter, BM3D), solved with PyProximal (ProximalGradient, ADMM, PrimalDual, ...), step sizes from operator norms. E.g. "add TV regularisation", "keep the model within physical bounds", "recover several signals that share their components", "fill in missing entries of a low-rank matrix", "use PyProximal". Not for purely quadratic regularisation (Tikhonov, damping, derivative smoothing), which is plain least squares - use the pylops skill solve-inverse-problem.

★ 1
PyLops
PyLops/pylops-agent-skills

wrap-operator

Wrap a user's own forward model as a PyLops linear operator inside their project (not inside the PyLops library), with a verified adjoint, a dot-test and a small test suite. Use when the user wants to turn existing forward/adjoint code, a script at a URL, or a mathematical description into a PyLops operator they can pass to PyLops solvers (e.g. "make my blurring function a PyLops operator", "wrap this forward model so I can invert it with lsqr", "I have a forward and adjoint, plug them into PyLops").

★ 1
PyLops
PyLops/pylops-agent-skills

tune-regularization

Choose regularisation weights and thresholds for a PyLops inversion (epsRs in regularized_inversion, damp in cgls/lsqr, eps in ista/fista, epsRL1s in splitbregman, sigma/tau in spgl1, niter as implicit regularisation) with a principled, reproducible sweep - L-curve, discrepancy principle, GCV for small problems, or a synthetic test with a known model. Use when the user asks "how do I choose epsilon / lambda / damping", "my inversion is too smooth / too noisy", "make an L-curve", or after solve-inverse-problem produced a working but untuned inversion.

★ 1
PyLops
PyLops/pylops-agent-skills

sliding-patch-processing

Set up PyLops windowed operators - Sliding1D, Sliding2D, Sliding3D, Patch2D, Patch3D - to apply a local transform or operator over overlapping windows of 1-D/2-D/3-D data, choosing window size, overlap, taper and model size with the *_design and *_pad_to_next helpers, checking coverage and the dot-test, and using them for local denoising, interpolation or inversion. Use when the user asks "apply FFT/Radon/any operator in overlapping windows", "local transform / patching with PyLops", "Patch2D gives artefacts at the edges", "how do I pick nwin, nover, nop".

★ 1
PyLops
PyLops/pylops-agent-skills

pick-sparsifying-transform

Choose a sparsifying transform for a signal or image in a PyLops sparsity-promoting inversion (FFT/FFT2D/FFTND, DCT, DWT/DWT2D/DWTND, DTCWT, Seislet, curvelets via UDCT, Radon/FourierRadon/ChirpRadon, windowed versions with Sliding/Patch), check its optional dependencies and frame properties, compare candidates quantitatively with a best-k-term compressibility test, and wire the winner into fista/spgl1 in synthesis or analysis form. Use when the user asks "which transform should I use for sparsity", "wavelets or curvelets for my data", "make my model sparse for fista/spgl1", "compressive sensing / interpolation with PyLops".

★ 1
PyLops
PyLops/pylops-agent-skills

debug-inversion

Use this skill whenever a user shares PyLops code or results that misbehave and asks what is wrong - load it first, before running the user's code, testing a hypothesis or answering, even when the bug looks obvious from the code. Covers a crash or traceback (dtype/casting errors, shape mismatches), a solver (lsqr, cgls, cg, fista, ista, spgl1, regularized_inversion, splitbregman) that stalls, diverges, returns NaNs, zeros, ringing or a wrong model, one dataset or block being ignored in a stacked/joint inversion, or a custom FunctionOperator/LinearOperator that may have a wrong adjoint. Diagnoses with evidence (block-by-block dot-tests incl. complex, dtype and dims checks, operator/data scaling, solver and eps/damp choice), then proposes the smallest fix and verifies it. Not for choosing a solver for a new, working setup (use solve-inverse-problem) or picking regularisation weights (use tune-regularization).

★ 1