ModelOperator#

class blochsim.ModelOperator[source]#

Bases: Module

A signal model over parameter maps, with its derivatives.

Maps are stacked on the last axis: (..., channels) in, (..., contrasts) out, every leading axis a voxel axis. Only physics() moves the channel axis to the front, as deepinv expects.

Parameters:
  • acquisition (Simulator) – Sequence being inverted, with every property not solved for already bound. A property bound as a map is one value per voxel.

  • *unknown (str) – Names of the properties solved for, in channel order. At least one.

  • bounds (mapping, default=None) – {name: (low, high)} in the property’s units; either end may be None, and None leaves every unknown unbounded. Enforced by solving for a transformed variable, so no iterate leaves the bounds.

  • scale (mapping, default=None) – {name: value}, step scale of an unbounded parameter in its own units; unlisted ones use 1. Bounded parameters are scaled by their interval.

  • amplitude (bool, default=True) – Carry a complex amplitude multiplying the model output, as two extra channels (real, imaginary).

  • subspace (Subspace, default=None) – Basis the prediction is projected onto, to solve in the temporal basis rather than in the contrasts.

  • execution ({"auto", "cpu"}, torch.device or sequence of device, default=None) – Where the per-voxel work runs. "auto", which None stands for, runs a small volume where the maps are and cuts a larger one into chunks of voxels spread over every CUDA device; the answer comes back where the maps are. "cpu" or named devices force the choice.

Raises:

ValueError – If nothing is unknown, a bound or scale names a property that is not unknown, a bound is not increasing, or a scale is not positive.

Notes

The operator follows the device of the maps it is called with; to() moves nothing.

An equality constraint is imposed by writing the model on the remaining parameters. For fat-water fractions that sum to one, make the fat fraction the only unknown and write water as 1 - f inside the model; the constraint then holds exactly at every iterate.

Examples

operator = ModelOperator(
    MultiEchoSimulator(TE=echo_times, T1=1000.0),
    "T2",
    bounds={"T2": (10.0, 300.0)},
)
maps = operator.initial((128, 128), T2=80.0)
images = operator.A(maps)          # (128, 128, echoes), complex

Methods

A

The name a reconstruction knows the forward operator by.

A_jvp

Directional derivative J d by one forward-mode pass.

A_vjp

Adjoint product J^H v by one reverse-mode pass.

initial

Build starting variables from property values.

jacobian

Build the derivative block of every voxel.

physics

Wrap the operator as a deepinv.physics.Physics.

select

Restrict the operator to a subset of the voxels.

split

Convert solved-for variables to properties in their own units.

property channels#

Number of map channels of x.

property names#

Channel names of x, in order.

split()[source]#

Convert solved-for variables to properties in their own units.

Parameters:

x (torch.Tensor) – (..., channels), the variables.

Returns:

One entry per unknown, inside its bound, plus complex "amplitude" where carried.

Return type:

dict

initial()[source]#

Build starting variables from property values.

Inverse of split().

Parameters:
  • shape (sequence of int, default=()) – Voxel shape. Empty gives one voxel.

  • **values (float or array-like) – {name: value} in the property’s units, scalar or broadcastable to shape. An unknown left out starts at the middle of its two-sided bound. amplitude (complex) defaults to 1.

Returns:

(*shape, channels).

Return type:

torch.Tensor

Raises:

ValueError – If an unknown with no two-sided bound is left out, or a value is not strictly inside its bound.

select()[source]#

Restrict the operator to a subset of the voxels.

Properties bound as per-voxel maps are indexed by keep; other bound values are unchanged.

Parameters:

keep (torch.Tensor) – Index or boolean mask over the voxel axis.

Returns:

Copy over those voxels; self if no bound property is a map.

Return type:

ModelOperator

forward()[source]#

Predict the contrasts of the maps.

Parameters:

x (torch.Tensor) – (..., channels).

Returns:

(..., contrasts), complex.

Return type:

torch.Tensor

A()#

The name a reconstruction knows the forward operator by.

A_jvp()[source]#

Directional derivative J d by one forward-mode pass.

Parameters:
  • x (torch.Tensor) – (..., channels), the point and the direction.

  • d (torch.Tensor) – (..., channels), the point and the direction.

Returns:

(..., contrasts), complex.

Return type:

torch.Tensor

A_vjp()[source]#

Adjoint product J^H v by one reverse-mode pass.

The result is the real part of the conjugate product, the adjoint of A_jvp() under the real inner product.

Parameters:
  • x (torch.Tensor) – (..., channels), the point.

  • v (torch.Tensor) – (..., contrasts), the cotangent.

Returns:

(..., channels), real.

Return type:

torch.Tensor

jacobian()[source]#

Build the derivative block of every voxel.

Used by voxel-wise fits; use A_jvp() and A_vjp() under an encoding operator.

Parameters:

x (torch.Tensor) – (..., channels).

Returns:

(..., channels, contrasts), complex.

Return type:

torch.Tensor

physics()[source]#

Wrap the operator as a deepinv.physics.Physics.

The channel axis moves to the front: (batch, channels, *xyz) in, (batch, contrasts, *xyz) out. encoding * operator.physics() composes with an encoding operator.

Parameters:

**kwargs – Passed to deepinv.physics.Physics, e.g. a noise model.

Return type:

deepinv.physics.Physics

Raises:

ImportError – If deepinv is not installed.

Notes

A deepinv ComposedPhysics differentiates the chain by autograd, not by the analytic products of this class; a Gauss-Newton solver such as bartorch’s nlop.IRGNM chains the analytic products.