Inverse solvers

Inverters to calculate current distributions from SOT signals.

This module contains various classes to parametrize, accelerate and regularize the inversion of SOT signals to current distributions. Two key points set the approach of this module apart from other SOT inversion schemes:

1. The inversion is done in real space using iterative optimization of a regularized loss function. 2. When working in 2D, the inversion is done onto a mesh rather than a grid. This makes it much easier to incorporate the shape of the sample into the inversion.

class sottools.backward.FitState1D(step, total_steps, loss, fidelity, regularization, current, predicted_signal, to_fit_signal, xaxis_current, xaxis_signal)

Bases: object

State of the 1D fitting process at one particular point.

Can be used to communicate intermediate results to a user through a callback function.

Parameters:
  • step (int) – Current step number in the fitting process.

  • total_steps (int) – Total number of steps in the fitting process.

  • loss (float) – Current value of the loss function.

  • fidelity (float) – Current value of the fidelity term in the loss function.

  • regularization (float) – Current value of the regularization term in the loss function.

  • current (np.ndarray) – Current array of size (Ndevice,) representing the current density on the device.

  • predicted_signal (np.ndarray) – Predicted signal array of size (Nscan,) representing the predicted signal based on the current density.

  • to_fit_signal (np.ndarray) – Signal array of size (Nscan,) representing the signal to fit.

  • xaxis_current (np.ndarray) – X-axis array of size (Ndevice,) representing the positions of the current density.

  • xaxis_signal (np.ndarray) – X-axis array of size (Nscan,) representing the positions of the signal.

step: int
total_steps: int
loss: float
fidelity: float
regularization: float
current: ndarray | None
predicted_signal: ndarray | None
to_fit_signal: ndarray | None
xaxis_current: ndarray | None
xaxis_signal: ndarray | None
class sottools.backward.FitCallback(*args, **kwargs)

Bases: Protocol

Protocol for callback functions.

This protocol generalizes to both 1D and 2D inverters.

on_step(state)

Call at each (callback) step of the fit.

Parameters:

state (FitState1D)

Return type:

None

on_finish(state)

Call at the end of the fit.

Parameters:

state (FitState1D)

Return type:

None

class sottools.backward.PrintCallback1D(bar_width=30)

Bases: object

In-shell printed callback for 1D inverters.

Displays a progress bar and the current loss, fidelity, and regularization for a 1D current fit.

Parameters:

bar_width (int) – Width of the progress bar in characters.

on_step(state)

Call at every (callback) step of a 1D fit.

Parameters:

state (FitState1D) – State of the 1D fitting process at this step.

Return type:

None

on_finish(state)

Call after the last step of a 1D fit.

Parameters:

state (FitState1D) – State of the 1D fitting process at this step.

Return type:

None

class sottools.backward.JupyterCallback

Bases: object

Jupyter notebook callback for 1D inverters.

Displays the current, predicted signal, and loss as plots underneath a notebook cell.

on_step(state)

Call at every (callback) step of a 1D fit.

Parameters:

state (FitState1D) – State of the 1D fitting process at this step.

Return type:

None

on_finish(state)

Call after the last step of a 1D fit.

Parameters:

state (FitState1D) – State of the 1D fitting process at this step.

Return type:

None

class sottools.backward.Inverter1D(forward, data, lam1=0.0, lam2=0.0, lam3=0.0, lam4=0.0, n_edge=3, device=None)

Bases: object

Inverts a 1D SOT signal to find the corresponding current distribution.

Uses the Forward1D model in forward.py to relate a 1D SOT scan to a 1D current distribution over an infinite bar. Because, especially with noise, an infinite number of distributions give rise to (approximately) the same SOT signal, regularizers are used. The current implementation of the class uses TV-1, TV-2, and positivity regularizers. Perhaps, in the future, Tikhonov can be added.

Parameters:
  • forward (Forward1D) – The forward model to use for the inversion.

  • data (np.ndarray) – The SOT signal to fit, of shape (Nscan,).

  • lam1 (float) – Regularization weight for 1st order TV regularization. Default is 0.0.

  • lam2 (float) – Regularization weight for 2nd order TV regularization. Default is 0.0.

  • lam3 (float) – Regularization weight for positivity regularization. Default is 0.0.

  • lam4 (float) – Regularization weight for edge ringing suppression (through further requirement of positivity). Default is 0.0.

  • n_edge (int) – Number of points on each edge to apply the edge ringing suppression.

  • device (torch.device) – The device to use for the inversion. Default is CPU.

fit(lr=0.001, n_steps=20000, callback=None, callback_every=500)

Fits the current distribution to the data using gradient descent.

Performs ADAM fit of the current distribution to the data using the forward model while continually regularizing. Can be restarted if parameter variation is required.

Parameters:
  • lr (float) – Learning rate for the ADAM optimizer. Default is 1e-3.

  • n_steps (int) – Number of steps to run the optimizer. Default is 20000.

  • callback (FitCallback) – Callback function to call at each (callback) step. Default is None.

  • callback_every (int) – Number of steps between callback calls. Default is 500.

Returns:

  • yarray_current (np.ndarray) – The y-axis of the current distribution, of shape (Ndevice,).

  • j_recovered (np.ndarray) – The recovered current distribution, of shape (Ndevice,).

  • loss_history (np.ndarray) – The history of the loss function, of shape (n_steps,).

Return type:

tuple[ndarray, ndarray, ndarray]

property lam1: float

Weight of 1st order TV regularization.

property lam2: float

Weight of 2nd order TV regularization.

property lam3: float

Weight of positivity regularization.

property lam4: float

Weight of edge ringing suppression regularization.

property n_edge: int

Number of points on each edge to apply the edge ringing suppression.

reset()

Reset the inversion to the initial conditions.

Return type:

None

class sottools.backward.Inverter1DOversampled(forward, xdata, data, lam1=0.0, lam2=0.0, lam3=0.0, lam4=0.0, n_edge=3, device=None)

Bases: Inverter1D

1D inverter that allows for oversampling the current distribution.

Inverter that releases the constraint that the output of the forward model must be sampled on the same grid as the data. Use this class when the forward model has a finer grid than the data, this allows for the reconstruction of smaller features. To make up for the difference, every step, the output of the forward model gets resampled on the data grid to compute the fidelity term. The regularization is still computed on the finer grid. This adds a small overhead (on top of the forward model generally being more expensive due to the finer grid).

Parameters:
  • forward (Forward1D) – The forward model to use for the inversion.

  • xdata (np.ndarray) – The x-axis on which the data is sampled, of shape (Nscan,).

  • data (np.ndarray) – The SOT signal to fit, of shape (Nscan,).

  • lam1 (float) – Regularization weight for 1st order TV regularization. Default is 0.0.

  • lam2 (float) – Regularization weight for 2nd order TV regularization. Default is 0.0.

  • lam3 (float) – Regularization weight for positivity regularization. Default is 0.0.

  • lam4 (float) – Regularization weight for edge ringing suppression (through further requirement of positivity). Default is 0.0.

  • n_edge (int) – Number of points on each edge to apply the edge ringing suppression.

  • device (torch.device) – The device to use for the inversion. Default is CPU.

fit(lr=0.001, n_steps=20000, callback=None, callback_every=500)

Fits the current distribution to the data using gradient descent.

Performs ADAM fit of the current distribution to the data using the forward model while continually regularizing. Can be restarted if parameter variation is required.

Parameters:
  • lr (float) – Learning rate for the ADAM optimizer. Default is 1e-3.

  • n_steps (int) – Number of steps to run the optimizer. Default is 20000.

  • callback (FitCallback) – Callback function to call at each (callback) step. Default is None.

  • callback_every (int) – Number of steps between callback calls. Default is 500.

Returns:

  • yarray_current (np.ndarray) – The y-axis of the current distribution, of shape (Ndevice,).

  • j_recovered (np.ndarray) – The recovered current distribution, of shape (Ndevice,).

  • loss_history (np.ndarray) – The history of the loss function, of shape (n_steps,).

Return type:

tuple[ndarray, ndarray, ndarray]

class sottools.backward.Inverter2D(meshtogrid, currentforward, data, lam1=1e-07, lam2=1e-09, huber_e=0.001, device=None)

Bases: object

Invert a 2D SOT signal to the underlying current distribution.

Inverter for full 2D images. Unlike the 1D inverter, 2D inversion uses a mesh to represent the sample. This enables accurate modeling of sample geometry, which can be extracted from AFM, SEM, litho pattern, etc.

Parameters:
  • meshtogrid (MeshToGrid) – The mesh to grid converter that encapsulates the mesh and defines the operation that converts the mesh to a grid for the forward model. It is the user’s responsibility to ensure the output of meshtogrid is compatible with the forward model.

  • currentforward (Forward2DCurrent) – The forward model that computes the SOT signal from the current distribution on the mesh. It is the user’s responsibility to ensure the output of meshtogrid is compatible with the forward model.

  • data (np.ndarray) – The SOT signal to fit, of shape (Nscan_y, Nscan_x).

  • lam1 (float) – Regularization weight for Tikhonov regularization on the streamfunction.

  • lam2 (float) – Regularization weight for total variation regularization on the current density.

  • huber_e (float) – Huber parameter for total variation regularization. Default is 1e-3.

  • device (torch.device) – The device to use for the inversion. Default is CPU.

fit(lr=0.01, n_steps=20000, callback=None, callback_every=500)

Fit the current distribution to the data using gradient descent.

Performs ADAM fit of the current distribution to the data using the forward model while continually regularizing. Can be restarted if parameter variation is required.

Parameters:
  • lr (float) – Learning rate for the ADAM optimizer. Default is 1e-2.

  • n_steps (int) – Number of steps to run the optimizer. Default is 20000.

  • callback (FitCallback) – Callback function to call at each (callback) step. Default is None.

  • callback_every (int) – Number of steps between callback calls. Default is 500.

Returns:

res – Dictionary containing the following keys: - “streamfunction”: np.ndarray of shape (N_points,) representing

the recovered streamfunction on the mesh.

  • ”Kx”: np.ndarray of shape (N_triangles,) representing the recovered current density in the x-direction on the cell centers.

  • ”Ky”: np.ndarray of shape (N_triangles,) representing the recovered current density in the y-direction on the cell centers.

  • ”mesh”: Mesh object representing the mesh used for the inversion.

  • ”loss_history”: np.ndarray of shape (n_steps,) representing the history of the loss function during the fit.

  • ”fidelity_history”: np.ndarray of shape (n_steps,) representing the history of the fidelity term during the fit.

Return type:

dict

property lam1: float

Weight of Tikhonov regularization.

property lam2: float

Weight of total variation regularization.