Customization Overview
Extending DevitoHorizon with custom components
DevitoHorizon is built from composable, first-class objects with well-defined interfaces. This makes it straightforward to extend without touching core code.
Extension Points
| Component | Base Class | Guide |
|---|---|---|
| Misfit Functions | Misfit |
Custom Misfits |
| Optimizers | Optimizer |
Custom Optimizers |
| Data I/O | DataManager |
Custom Data I/O |
| Data Preprocessing | step callable | Custom Data Preprocessing |
| Gradient Processing | GradientProcessor |
Custom Gradient Processing |
| Schedulers | Scheduler |
Custom Schedulers |
| Wave Propagator | recipes.Propagator |
Custom Solvers |
Note on Storage: Data I/O customization is for file formats (HDF5, NetCDF, etc.), not storage backends. Storage backends (S3, Azure Blob, GCS) are handled automatically via
fsspec.
Quick Examples
Custom Misfit: Cross-Correlation
from devitohorizon.misfits import Misfit
import numpy as np
class CrossCorrelationMisfit(Misfit):
name = "xcorr"
def value(self, synthetic, observed):
num = np.sum(synthetic * observed)
denom = np.linalg.norm(synthetic) * np.linalg.norm(observed) + 1e-12
return 1.0 - num / denom
def gradient(self, synthetic, observed):
norm_s = np.linalg.norm(synthetic) + 1e-12
norm_o = np.linalg.norm(observed) + 1e-12
corr = np.sum(synthetic * observed) / (norm_s * norm_o)
return -(observed / (norm_s * norm_o) - corr * synthetic / (norm_s**2))
physics = DevitoPhysics(misfit=CrossCorrelationMisfit())Custom Optimizer: Adam
from devitohorizon.optimization import Optimizer
from devitohorizon.config import OptimizationResult
class Adam(Optimizer):
def __init__(self, lr=1e-3, beta1=0.9, beta2=0.999, **kwargs):
super().__init__(**kwargs)
self.lr, self.beta1, self.beta2 = lr, beta1, beta2
def minimize(self, fun_and_grad, x0, bounds=None, callback=None):
x, m, v = x0.copy(), np.zeros_like(x0), np.zeros_like(x0)
lower = np.array([b[0] for b in bounds]) if bounds else -np.inf
upper = np.array([b[1] for b in bounds]) if bounds else np.inf
for it in range(self.maxiter):
f, g = fun_and_grad(x)
m = self.beta1 * m + (1 - self.beta1) * g
v = self.beta2 * v + (1 - self.beta2) * g**2
m_hat, v_hat = m / (1 - self.beta1**(it+1)), v / (1 - self.beta2**(it+1))
x = np.clip(x - self.lr * m_hat / (np.sqrt(v_hat) + 1e-8), lower, upper)
if callback: callback(x)
return OptimizationResult(x=x, fun=f, nit=self.maxiter, success=True)Custom Solver: alternate imaging condition
from recipes import AcousticIsotropic
from devitohorizon.physics import DevitoPhysics
class MySolver(AcousticIsotropic):
def sensitivity(self, params=('m',)):
# Custom imaging condition — see Custom Solvers guide for details
return super().sensitivity(params=params)
physics = DevitoPhysics(solver=MySolver)Custom Data Manager: HDF5
from devitohorizon.data import DataManager, SeismicData
class HDF5DataManager(DataManager):
def discover_shots(self):
import h5py
with h5py.File(self.data_path, 'r') as f:
return sorted([int(k) for k in f['shots'].keys()])
def load_shot_raw(self, shot_id):
import h5py
with h5py.File(self.data_path, 'r') as f:
grp = f['shots'][str(shot_id)]
return SeismicData(
shot_id=shot_id,
data=np.array(grp['data']),
src_pos=np.array(grp['src_coords']),
rec_pos=np.array(grp['rec_coords']),
dt=float(grp.attrs['dt']),
)Testing Custom Components
Always verify custom misfit gradients:
def taylor_test(misfit, shape=(500, 100)):
x, y = np.random.randn(*shape), np.random.randn(*shape)
f0, g = misfit.value(x, y), misfit.gradient(x, y)
dx = np.random.randn(*shape); dx /= np.linalg.norm(dx)
errors = [abs(misfit.value(x + h*dx, y) - f0 - h*np.sum(g*dx))
for h in [1e-3, 5e-4, 2.5e-4]]
ratios = [errors[i]/errors[i+1] for i in range(len(errors)-1)]
return all(r > 3.5 for r in ratios) # Should be ~4 for O(h^2)See Also
- API Reference - Complete API documentation
- Architecture - System design and data flow
- Examples - Practical usage patterns