spyrit.core.meas.Linear

class spyrit.core.meas.Linear(H: tensor, pinv: bool = False, rtol: float = None, Ord: tensor = None, meas_shape: tuple = None)[source]

Bases: _Base

Simulates linear measurements \(y = Hx\).

Computes linear measurements from incoming images: \(y = Hx\), where \(H\) is a given linear operator (matrix) and \(x\) is a vectorized image or batch of images.

The class is constructed from a matrix \(H\) of shape \((M,N)\), where \(N\) represents the number of pixels in the image and \(M\) the number of measurements.

Args:

H (torch.tensor): measurement matrix (linear operator) with shape \((M, N)\). Only real values are supported.

pinv (bool): Whether to store the pseudo inverse of the measurement matrix \(H\). If True, the pseudo inverse is initialized as \(H^\dagger\) and stored in the attribute H_pinv. It is alwats possible to compute and store the pseudo inverse later using the method build_H_pinv(). Defaults to False.

rtol (float, optional): Cutoff for small singular values (see torch.linalg.pinv). Only relevant when pinv is True.

Ord (torch.tensor, optional): Order matrix used to reorder the rows of the measurement matrix \(H\). The first new row of \(H\) will correspond to the highest value in \(Ord\). Must contain \(M\) values. If some values repeat, the order is kept. Defaults to None.

meas_shape (tuple, optional): Shape of the image \(x\). Must be a tuple of two integers representing the height and width of the image. If not specified, the image is suppposed to be a square. If not, an error is raised. Defaults to None.

Attributes:

H (torch.tensor): The learnable measurement matrix of shape \((M, N)\) initialized as \(H\).

H_static (torch.tensor): alias for H.

H_pinv (torch.tensor, optional): The learnable pseudo inverse measurement matrix \(H^\dagger\) of shape \((N, M)\).

M (int): Number of measurements performed by the linear operator.

N (int): Number of pixels in the image.

h (int): Measurement pattern height.

w (int): Measurement pattern width.

meas_shape (tuple): Shape of the image \(x\) (height, width). Is equal to (self.h, self.w).

indices (torch.tensor): Indices used to sort the rows of H. It is used by the method reindex().

Ord (torch.tensor): Order matrix used to sort the rows of H. It is used by sort_by_significance().

Note

If you know the pseudo inverse of \(H\) and want to store it, it is best to initialize the class with pinv set to False and then call build_H_pinv() to store the pseudo inverse.

Example 1:
>>> H = torch.rand([400, 1600])
>>> meas_op = Linear(H, pinv=False)
>>> print(meas_op)
Linear(
  (M): 400
  (N): 1600
  (H.shape): torch.Size([400, 1600])
  (meas_shape): (40, 40)
  (H_pinv): False
)
Example 2:
>>> H = torch.rand([400, 1600])
>>> meas_op = Linear(H, True)
>>> print(meas_op)
Linear(
  (M): 400
  (N): 1600
  (H.shape): torch.Size([400, 1600])
  (meas_shape): (40, 40)
  (H_pinv): True
)

Methods

adjoint(y)

Applies adjoint transform to incoming measurements \(x = H^{T}y\)

build_H_pinv([reg, eta])

Used to set the pseudo inverse of the measurement matrix \(H\) using torch.linalg.pinv.

forward(x)

Applies linear transform to incoming images: \(y = Hx\).

get_H()

Deprecated method.

pinv(x[, reg, eta, diff])

Computes the pseudo inverse solution \(y = H^\dagger x\).

reindex(x[, axis, inverse_permutation])

Sorts a tensor along a specified axis using the indices tensor.

unvectorize(input)

Reshape a vectorized tensor to the measurement shape (heigth, width).

vectorize(input)

Vectorize an image-shaped tensor.