Skip to content

Latest commit

History

101 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Numerical optimization of gradient waveforms (NOW) for tensor-valued dMRI

A package for optimization of gradient waveforms that yield b-tensors of arbitrary shape that can be tailored to pulse sequence timing and hardware restrictions. Available in MATLAB and Python.

The optimizer supports the following:

  • Arbitrary b-tensor shape
  • Variable raster resolution
  • Asymmetric sequence timing
  • Control for 0th-moment balance
  • Control for energy consumption/heating
  • L2 and max-norm amplitude constraints
  • Nulling of concomitant gradient effects
  • Nulling of motion encoding

Python

Installation

Requires Python ≥ 3.10. Install dependencies:

pip install numpy scipy jax matplotlib
pip install pytest # for running tests

Quick start

importnumpyasnpfromnowimportNOW_config, now_optimizefromnow.visualizationimportplot_result# 1. Create a configurationconfig=NOW_config(
gMax=80, # max gradient amplitude [mT/m]sMax=100, # max slew rate [T/m/s]N=50, # number of discretization pointsdurationFirstPartRequested=32, # duration before pause [ms]durationSecondPartRequested=27, # duration after pause [ms]durationZeroGradientRequested=8, # pause duration [ms]targetTensor=np.eye(3), # spherical tensor encoding (STE)eta=0.9, # energy/efficacy balance (0, 1]
)
# 2. Run optimizationresult, config=now_optimize(config)
# 3. Inspect resultsprint(f"b-value: {result.b:.2f} s/mm²")
print(f"B-tensor:\n{result.B}")
# 4. Plotplot_result(result, config)

A complete example is provided in now_example.py.

Configuration parameters

ParameterDefaultDescription
targetTensornp.eye(3)3×3 target encoding tensor. eye(3) = STE, diag([1,0,0]) = LTE, diag([1,1,0]) = PTE
N77Number of discretization points. More = smoother waveforms, slower optimization
gMax80Maximum gradient amplitude [mT/m]
sMax100Maximum slew rate [T/m/s]
durationFirstPartRequested28Duration before the zero-gradient pause [ms]
durationSecondPartRequested22Duration after the zero-gradient pause [ms]
durationZeroGradientRequested8Duration of the zero-gradient pause [ms]
eta1Heat dissipation / energy balance in (0, 1]
useMaxNormFalseUse max-norm instead of L2-norm for gradient amplitude
doMaxwellCompTrueEnable Maxwell (concomitant gradient) compensation
MaxwellIndex100Threshold for Maxwell terms [(mT/m)² ms]
enforceSymmetryFalseForce waveform symmetry about the pause
motionCompensationNoneDict with keys 'order' and 'maxMagnitude' (see below)
doBackgroundCompensation00 = off, 1 = general timing, 2 = specific timing
startTime0Time from excitation to first sample [ms] (for doBackgroundCompensation=2)

Motion compensation

config=NOW_config(
motionCompensation={
'order': [1, 2], # compensate 1st and 2nd order motion'maxMagnitude': [0, 1e-4], # 0 = exact nulling (linear constraint),
}, # >0 = allowed deviation (nonlinear constraint)
)

Result fields

FieldUnitsDescription
result.bs/mm²b-value
result.Bs/m²Full 3×3 b-tensor
result.gmT/mGradient waveform, shape (N+2, 3)
result.q1/mq-space trajectory, shape (N, 3)
result.slewT/m/sSlew rate, shape (N+2, 3)
result.kappaEncoding efficiency
result.gwfT/mGradient waveform, md-dMRI compatible
result.rfSpin dephasing direction
result.dtsTime step

Optimization methods

result, config=now_optimize(config, method='SLSQP') # default, fastestresult, config=now_optimize(config, method='trust-constr') # interior point

Solver backends

The optimization is dispatched through a solver registry. Custom backends can be registered:

fromnowimportget_solver, register_solver# Use a built-in solver directlysolver=get_solver('SLSQP')
# Register a custom solver (must implement the SolverProtocol)register_solver('my-solver', lambda: MySolver())
result, config=now_optimize(config, method='my-solver')

Config serialization

Configs can be saved to and loaded from JSON (or YAML if pyyaml is installed), enabling shared experiment definitions between MATLAB and Python:

fromnowimportNOW_config, save_config, load_configconfig=NOW_config(N=50, gMax=80, targetTensor=np.eye(3))
save_config(config, 'my_config.json')
# Later, or in another session:config=load_config('my_config.json')

Problem export

The complete optimization problem (constraint matrices, sample evaluations) can be exported for analysis or use by external solvers:

fromnowimportbuild_problem, export_problemproblem=build_problem(config)
export_problem(problem, 'problem.npz') # NumPy formatexport_problem(problem, 'problem.mat', fmt='mat') # MATLAB-compatible

Running tests

cd NOW
python -m pytest tests/ -v

The test suite validates the Python implementation against MATLAB reference data at every intermediate computation step: config values, constraint matrices, nonlinear constraint values, and analytical Jacobians. Jacobians are additionally verified against JAX automatic differentiation.

Correspondence with MATLAB

MATLABPython
optimizationProblem()NOW_config()
NOW_RUN(problem)now_optimize(config)
result.gwfresult.gwf
result.bresult.b
fmincon (SQP)scipy.optimize.minimize (SLSQP)
build_problem(config)OptimizationProblem
get_solver('SLSQP')SolverProtocol
save_config / load_config (JSON/YAML)
export_problem (npz/mat)

Because the MATLAB and Python solvers are different implementations, optimized waveforms will generally differ (different local optima), but both satisfy the same constraints and produce similar b-values.

The Python implementation adds a solver-independent problem layer (OptimizationProblem) that separates constraint assembly from solver dispatch, enabling custom solver backends. Config files (JSON/YAML) can be shared between MATLAB and Python.

MATLAB

Getting started

First, download (clone or fork) this repository and open it in MATLAB. You can generate waveforms in a graphical interface by calling NOW_GUI.m. To access all optimization controls, you can run the optimization via a script. An example script is provided in scripted_NOW_example.m.

Setting up the optimizer always follows these steps:

  1. Create object that specifies optimization problem by calling pObj = optimizationProblem()
  2. Modify pObj to your specification
  3. Update the derived parameters of pObj by feeding it through the function pObj = optimizationProblem(pObj)
  4. Call the optimizer run function using pObj as input, according to [result, pObj] = NOW_RUN(pObj)

The result structure contains several fields; among them is the gradient waveform. Notably, the fields gwf, rf and dt, are compatible with the multidimensional diffusion (md-dMRI) framework format. With the md-dMRI framework installed, an overview of the resulting gradient waveform can be plotted by calling gwf_plot_all(result.gwf, result.rf, result.dt).

References to NOW and its components and extensions

The optimization framework contains contains several sub-functions, all of which are part of the master branch of this repository. Please consider citing the following papers if you use NOW in your research or applications.

Review paper on gradient waveform design

Gradient waveforms can be intended for many kinds of purposes and deployed on vastly different hardware. The general design of gradient waveforms for dMRI, with special focus on tensor-valued encoding, has been described in:

Free waveform encoding pulse sequence

To run user-defined gradient waveforms a special MRI pulse sequence is usually required. Pulse sequences are available for multiple vendors and scanner software versions. Please refer to the free waveform (FWF) encoding resource site for more information.

Help improve NOW

We welcome contributions from anyone! NOW is an open-source project that aspires to be community-driven. If you have a feature request or discover a bug, please submit an issue or - even better - fix it and make a pull request. If you plan to make major changes, it might be a good idea to first submit an issue to discuss what you would like to change.

MATLAB dependencies

The following toolboxes are called during optimization:

  • Curve Fitting Toolbox
  • System Identification Toolbox
  • Optimization Toolbox
  • Simulink Control Design
  • Statistics and Machine Learning Toolbox
  • Computer Vision Toolbox

List was derived from pList.Name by calling: [fList,pList] = matlab.codetools.requiredFilesAndProducts('scripted_NOW_Example.m');

License

This work is published under the BSD 3-Clause License.

About

NOW - Numerical Optimization of gradient Waveforms

Topics

Resources

Stars

20 stars

Watchers

4 watching

Forks

Releases

Packages

Contributors

Languages