Functional
Low-level functional interfaces for core operations. These functions operate directly on tensors
without the NamedLinop abstraction, and are used internally by the linop classes.
torchlinops.functional
array_to_blocks
array_to_blocks(
input,
block_shape: tuple[int, ...],
stride: Optional[tuple[int, ...]] = None,
mask: Optional[Bool[Tensor, ...]] = None,
out: Optional[Tensor] = None,
)
Wrapper for default arguments
Source code in src/torchlinops/functional/_unfold/array_to_blocks.py
blocks_to_array
blocks_to_array(
input,
im_size: tuple,
block_shape: tuple,
stride: Optional[tuple] = None,
mask: Optional[Bool[Tensor, ...]] = None,
out: Optional[Tensor] = None,
)
Wrapper for default arguments
Source code in src/torchlinops/functional/_unfold/array_to_blocks.py
canonicalize_idx
| PARAMETER | DESCRIPTION |
|---|---|
idx
|
TYPE:
|
dim
|
The dimension of idx to tuple-ify
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
D-tuple of [B1... B2...] tensors
|
|
Note
|
TYPE:
|
Source code in src/torchlinops/functional/_index/index.py
center_crop
Center crops the input tensor to the specified size.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
The input tensor to be cropped.
TYPE:
|
im_size
|
The original size of the input tensor.
TYPE:
|
crop_im_size
|
The desired size after cropping.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The cropped tensor. |
Notes
This function uses the Pad operator to perform the cropping operation.
It calculates the necessary cropping dimensions and applies the cropping
from the center of the original tensor to achieve the specified crop_im_size.
Source code in src/torchlinops/functional/_pad.py
center_pad
Center pads the input tensor to the specified size.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
The input tensor to be padded.
TYPE:
|
im_size
|
The original size of the input tensor.
TYPE:
|
pad_im_size
|
The desired size after padding.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The padded tensor. |
Notes
This function uses the Pad operator to perform the padding operation.
It calculates the necessary padding dimensions and applies the padding
to ensure that the output tensor matches the pad_im_size while preserving
the center of the original tensor.
Source code in src/torchlinops/functional/_pad.py
ensure_tensor_indexing
Convert any slice()-type indexes to tensor indexes.
Also broadcasts by appending slice(None) to the front of idx
| PARAMETER | DESCRIPTION |
|---|---|
idx
|
Tuple of torch.Tensor (integer-valued) index tensors or slice() objects
TYPE:
|
tshape
|
Target size, should have length greater than or equal to that of idx |
Source code in src/torchlinops/functional/_index/index.py
fold
fold(
x,
im_size: tuple,
block_size: tuple,
stride: tuple,
mask: Optional[Bool[Tensor, ...]] = None,
output: Optional[Tensor] = None,
) -> Tensor
Accumulate an array of blocks into a full array
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Shape [B..., blocks, block_size]
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
If mask is not None, block_size will be an int equal to the number of True elements in the mask Otherwise it will be the full block shape.
TYPE:
|
Source code in src/torchlinops/functional/_unfold/fold.py
get_nblocks
get_nblocks(
im_size: tuple[int, ...],
block_size: tuple[int, ...],
block_stride: Optional[tuple[int, ...]] = None,
) -> tuple[int, ...]
Given an image and a block size, returns the number of valid blocks in each direction.
Blocks may overlap
Examples:
>>> get_nblocks((5, 5), (3, 3), (1, 1))
(3, 3)
>>> get_nblocks((5, 5), (3, 3), (2, 2))
(2, 2)
>>> get_nblocks((6, 6), (3, 3), (2, 2))
(2, 2)
>>> get_nblocks((7, 7), (3, 3), (2, 2))
(3, 3)
>>> get_nblocks((10, 10), (8, 8), (4, 4))
(1, 1)
Source code in src/torchlinops/functional/_unfold/nblocks.py
grid
grid(
vals: Inexact[Tensor, ...],
locs: Float[Tensor, "... D"],
grid_size: tuple[int, ...],
width: float | tuple[float, ...],
kernel: str = "kaiser_bessel",
norm: int = 1,
pad_mode: Literal["zero", "circular"] = "circular",
kernel_params: dict = None,
)
Interpolate from off-grid values to on-grid locations.
grid_size : tuple[int, ...] Shape of output array, excluding batch dimensions For example, if the gridded output should have shape [3, 64, 64], where 3 is the batch size, then grid_size would be [64, 64]
norm: int, 1 or 2 if 2, uses Euclidean norm to grid points to compute weights if 1, computes weights as product of axis-aligned norm weights - Same as sigpy
Source code in src/torchlinops/functional/_interp/grid.py
index
| PARAMETER | DESCRIPTION |
|---|---|
idx
|
Index
TYPE:
|
Source code in src/torchlinops/functional/_index/index.py
index_adjoint
index_adjoint(
vals: Shaped[Tensor, ...],
idx: tuple[Integer[Tensor, ...], ...],
grid_size: tuple[int, ...],
) -> Tensor
| PARAMETER | DESCRIPTION |
|---|---|
vals
|
Batch size of vals is used to determine batch size of output
TYPE:
|
idx
|
Use ensure_tensor_indexing to guarantee this
TYPE:
|
grid_size
|
The shape of the output tensor, excluding batch dimensions
TYPE:
|
Source code in src/torchlinops/functional/_index/index.py
interpolate
interpolate(
vals: Inexact[Tensor, ...],
locs: Float[Tensor, "... D"],
width: float | tuple[float, ...],
kernel="kaiser_bessel",
norm: int = 1,
pad_mode: str = "circular",
kernel_params: dict = None,
)
Interpolate from a regular grid to scattered locations (ungridding).
Evaluates values on a uniform grid at arbitrary non-uniform locations using kernel-based interpolation. This is the forward NUFFT interpolation step. Gradients are computed via the adjoint (gridding) operation.
| PARAMETER | DESCRIPTION |
|---|---|
vals
|
Values on a regular grid. The last
TYPE:
|
locs
|
Non-uniform target locations, with coordinates in the range
TYPE:
|
width
|
Interpolation kernel width (in grid units) for each spatial dimension. A scalar applies the same width to all dimensions.
TYPE:
|
kernel
|
Interpolation kernel type. Default is
TYPE:
|
norm
|
Kernel normalization order. Default is 1.
TYPE:
|
pad_mode
|
Padding mode for out-of-bounds access. Default is
TYPE:
|
kernel_params
|
Additional parameters passed to the interpolation kernel
(e.g.,
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Interpolated values at the non-uniform locations. |
Source code in src/torchlinops/functional/_interp/interp.py
interpolate_adjoint
interpolate_adjoint(
vals: Inexact[Tensor, ...],
locs: Float[Tensor, "... D"],
grid_size: tuple[int, ...],
width: float | tuple[float, ...],
kernel: str = "kaiser_bessel",
norm: int = 1,
pad_mode: str = "circular",
kernel_params: dict = None,
)
Adjoint of interpolation (gridding) from scattered locations to a regular grid.
Scatters values from non-uniform locations back onto a regular grid
using kernel-based gridding. This is the adjoint of the interpolate
operation and corresponds to the gridding step in an adjoint NUFFT.
| PARAMETER | DESCRIPTION |
|---|---|
vals
|
Values at non-uniform locations to be gridded.
TYPE:
|
locs
|
Non-uniform source locations, with coordinates in the range
TYPE:
|
grid_size
|
Output grid size for each spatial dimension.
TYPE:
|
width
|
Interpolation kernel width (in grid units) for each spatial dimension. A scalar applies the same width to all dimensions.
TYPE:
|
kernel
|
Interpolation kernel type. Default is
TYPE:
|
norm
|
Kernel normalization order. Default is 1.
TYPE:
|
pad_mode
|
Padding mode for out-of-bounds access. Default is
TYPE:
|
kernel_params
|
Additional parameters passed to the interpolation kernel
(e.g.,
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Gridded values on a regular grid of shape |
Source code in src/torchlinops/functional/_interp/interp.py
mask2idx
Converts an n-dimensional boolean tensor into an n-tuple of integer tensors indexing the True elements of the tensor.
| PARAMETER | DESCRIPTION |
|---|---|
mask
|
A boolean tensor.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[torch.Tensor]:
|
A tuple of integer tensors indexing the True elements. |
Source code in src/torchlinops/functional/_index/index.py
nufft
Functional interface for the Non-Uniform Fast Fourier Transform.
Computes the forward NUFFT of input data at specified non-uniform locations. Internally applies apodization, zero-padding, FFT, and interpolation.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Input data on a regular grid. The last
TYPE:
|
locs
|
Non-uniform sample locations. Each entry along the last dimension
corresponds to a spatial axis and should lie in
TYPE:
|
oversamp
|
Oversampling factor for the padded FFT grid. Default is 1.25.
TYPE:
|
width
|
Interpolation kernel width. Default is 4.0.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
NUFFT values evaluated at the non-uniform locations. |
Source code in src/torchlinops/functional/_nufft.py
nufft_adjoint
nufft_adjoint(
x: Tensor,
locs: Float[Tensor, "... D"],
grid_size: tuple[int, ...],
oversamp: float = 1.25,
width: float = 4.0,
)
Functional interface for the adjoint NUFFT.
Grids non-uniformly sampled data back onto a regular grid. Internally applies adjoint interpolation (gridding), inverse FFT, cropping, and apodization correction.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Non-uniformly sampled data to be gridded.
TYPE:
|
locs
|
Non-uniform sample locations. Each entry along the last dimension
corresponds to a spatial axis and should lie in
TYPE:
|
grid_size
|
Desired output grid size for each spatial dimension.
TYPE:
|
oversamp
|
Oversampling factor for the padded FFT grid. Default is 1.25.
TYPE:
|
width
|
Interpolation kernel width. Default is 4.0.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Gridded data on a regular grid of shape |
Source code in src/torchlinops/functional/_nufft.py
slice2range
Convert a slice object to a range object given the array size
Examples:
>>> tuple(slice2range(slice(None, None, None), 4))
(0, 1, 2, 3)
>>> tuple(slice2range(slice(None, None, -1), 3))
(2, 1, 0)
Source code in src/torchlinops/functional/_index/index.py
unfold
unfold(
x: Shaped[Tensor, ...],
block_size: tuple,
stride: Optional[tuple] = None,
mask: Optional[Bool[Tensor, ...]] = None,
output: Optional[Tensor] = None,
) -> Tensor
Wrapper that dispatches complex and real tensors Also precomputes some shapes
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Shape [B..., *im_size]
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
If mask is not None, block_size will be an int equal to the number of True elements in the mask Otherwise it will be the full block shape.
TYPE:
|
Source code in src/torchlinops/functional/_unfold/unfold.py
ungrid
ungrid(
vals: Inexact[Tensor, ...],
locs: Float[Tensor, "... D"],
width: float | tuple[float, ...],
kernel: str = "kaiser_bessel",
norm: int = 1,
pad_mode: Literal["zero", "circular"] = "circular",
kernel_params: dict = None,
)
Interpolate from on-grid values to off-grid locations.
norm: int, 1 or 2 if 1, computes weights as product of axis-aligned norm weights - Same as sigpy if 2, uses Euclidean norm to grid points to compute weights