Basic Workflow
Full-batch inversion with DevitoPhysics + SPG
This example performs a small full-batch inversion. It is intentionally explicit—no hidden classes, just Dataset, DevitoPhysics, and the SPG optimizer.
1. Load Data and Physics
from devitohorizon.data import Dataset
from devitohorizon.physics import DevitoPhysics
pre_cfg = {
"filter": {"type": "bandpass", "fmin": 0.005, "fmax": 0.020},
"mute": {"type": "top", "v": 1500.0, "taper": 10},
}
dataset = Dataset.from_segy("data/marmousi_shots.segy")
physics = DevitoPhysics(
model={"m0": "models/marmousi_initial.h5"},
nbl=40,
space_order=8,
wavelet={"type": "ricker", "f0": 0.015},
gradient={"mute": {"type": "datum", "cone_angle": 65.0}},
preprocess=pre_cfg,
source_estimation=True,
)The datum surface is inferred automatically from the first few shots—no extra calls required.
2. QC a Single Shot
import numpy as np
shot = dataset[0]
result = physics.compute_shot_gradient(shot)
print(f"Shot 0 misfit = {result['misfit']:.3e}")
print(f"Gradient norm = {np.linalg.norm(result['gradients']['m']):.3e}")This step is invaluable: it confirms the wavelet, preprocessing (which lives in DevitoPhysics), and geometry are consistent before we let an optimizer loose.
3. Run SPG
from devitohorizon.optimization import SPG
optimizer = SPG(
maxiter=6,
ftol=1e-6,
gtol=1e-8,
verbose=True,
)
model_opt, f_opt, info = optimizer.fwi(
physics=physics,
dataset=dataset,
model0=physics.model,
bounds={'vp': (1.4, 5.0)}, # clip velocities to physical range
)Because the dataset is passed in directly, SPG automatically iterates over every shot each time (full-batch). To use a subset, configure shot sampling via SPG(batch_size=8) or pass a SamplingConfig to optimizer.fwi(sampling=...).
4. Visualise Results
import matplotlib.pyplot as plt
vp = model_opt.vp.data # includes absorbing boundaries
crop = (slice(40, -40), slice(40, -40))
fig, ax = plt.subplots(figsize=(10, 6))
im = ax.imshow(vp[crop].T, cmap='gist_ncar', aspect='auto')
ax.set_title('Optimized velocity (km/s)')
ax.set_xlabel('Inline (grid cells)')
ax.set_ylabel('Depth (grid cells)')
fig.colorbar(im, ax=ax, label='km/s')
plt.show()Optional: Manual Loop
Prefer to script your own gradient descent? Use physics.objective(...):
model = physics.model
lr = 5e-4
for it in range(4):
fval, grads = physics.objective(dataset, model)
model.m.data[:] -= lr * grads['m']
print(f"iter {it} misfit={fval:.3e}")