AbstractModel#

class torchsim.base.AbstractModel(diff=None, chunk_size=None, device=None, *args, **kwargs)[source]#

Bases: ABC

Abstract base class for MRI simulation models with automated parameter handling.

Methods

__init__

Initialize the model with automatic parameter segregation and engine setup.

forward

Get forward method.

jacobian

Get Jacobian method.

set_properties

Define broadcastable spin/environment parameters.

set_sequence

Define sequence parameters.

Parameters:
  • diff (str | tuple[str] | None)

  • chunk_size (int | None)

  • device (str | device | None)

abstract set_properties(*args, **kwargs)[source]#

Define broadcastable spin/environment parameters.

abstract set_sequence(*args, **kwargs)[source]#

Define sequence parameters.

abstract static _engine(*args, **kwargs)[source]#

Core computational function for the model.

static _jacobian_engine(*args, **kwargs)[source]#

Manual Jacobian engine.

_get_func(_func, *args, **kwargs)[source]#

Dynamically split parameters into broadcastable and non-broadcastable groups.

Parameters:
  • _engine (Callable) – Function to be wrapped.

  • *args (tuple[Any, ...]) – Positional arguments provided by the user.

  • **kwargs (dict[str, Any]) – Keyword arguments provided by the user.

Returns:

  • Callable – A new engine accepting broadcastable parameters.

  • dict[str, Any] – Captured non-broadcastable parameters.

Return type:

tuple[Callable, dict[str, Any]]

_forward(*args, **kwargs)[source]#

Return a callable for forward computation. Useful for sequence optimization.

Parameters:
  • *args (Any) – Positional arguments for the simulation.

  • **kwargs (Any) – Keyword arguments for the simulation.

Returns:

A function that evaluates the forward model with the specified arguments.

Return type:

callable

_jacobian(*args, **kwargs)[source]#

Return a callable for the Jacobian computation. Useful for sequence optimization.

Parameters:
  • *args (Any) – Positional arguments for the simulation.

  • **kwargs (Any) – Keyword arguments for the simulation.

Returns:

A function that computes the Jacobian with respect to specified arguments.

Return type:

callable

__call__()[source]#

Calls both forward and jacobian methods, returning output and jacobian for sequence optimization.

Returns:

A tuple containing the forward output and jacobian output.

Return type:

tuple

forward(compile=False)[source]#

Get forward method.

Parameters:

compile (bool, optional) – Compile function using torch.compile. The default is False.

Returns:

Forward method.

Return type:

Callable

jacobian(compile=False)[source]#

Get Jacobian method.

Parameters:

compile (bool, optional) – Compile function using torch.compile. The default is False.

Returns:

Jacobian method.

Return type:

Callable