:py:mod:`operator2d` ==================== .. py:module:: operator2d Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: operator2d.Operator operator2d.CombinedOperator operator2d.Rotate1DConvOperator operator2d.RotateSeperable2DConvOperator operator2d.Kernel2DOperator operator2d.NearestKernelOperator operator2d.GaussianOperator .. py:class:: Operator Base class for operators; operators are used to apply linear shift invariant operations to a sequence of 2D images. .. py:method:: __call__(input, xv, yv, a) Evaluates the operator on the input. The meshgrid xv and yv is used to compute the kernel size; it is assumed that the spacing in xv and yv is the same as that in input. The output is multiplied by the area of a pixel in the meshgrid. :param input: Input 3D map to be operated on :type input: torch.Tensor[Ld,Li,Lj] :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Output of the operator :rtype: torch.Tensor[Ld,Li,Lj] .. py:method:: _area(xv, yv) Compute pixel volume in meshgrid :param xv: Meshgrid x coordinates :type xv: torch.Tensor :param yv: Meshgrid y coordinates :type yv: torch.Tensor :returns: Are :rtype: float .. py:method:: set_device(device) Sets the device of all parameters in the operator :param device: Device to set parameters to :type device: str .. py:method:: detach() Detaches all parameters from autograd. .. py:method:: set_requires_grad() Sets all parameters to require grad .. py:method:: save(path) Saves the operator :param path: Path where to save the operator :type path: str .. py:method:: normalization_constant(xv, yv, a) Computes the normalization constant of the operator :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Normalization constant at each source-detector distance :rtype: torch.Tensor[Ld] .. py:method:: normalize(input, xv, yv, a) Normalizes the input by the normalization constant. This ensures that the operator maintains the total sum of the input at each source-detector distance. :param input: Input to be normalized :type input: torch.Tensor[Ld,Li,Lj] :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Normalized input :rtype: torch.Tensor[Ld,Li,Lj] .. py:method:: __add__(other) Implementation of addition to allow for adding operators. Addition of two operators yields a new operator that corresponds to the sum of two linear operators :param other: Operator to add :type other: Operator :returns: New operator corresponding to the sum of the two operators :rtype: Operator .. py:method:: __mul__(other) Implementation of multiplication to allow for multiplying operators. Multiplication of two operators yields a new operator that corresponds to the composition of the two operators :param other: Operator to use in composition :type other: Operator :returns: Composed operators :rtype: Operator .. py:class:: CombinedOperator(func, operators, type) Bases: :py:obj:`Operator` Operator that has been constructed using two other operators :param func: Function that specifies how the two operators are combined :type func: Callable :param operators: Sequence of operators :type operators: Sequence[Operator] :param type: Type of operator: either 'sequential' or 'additive' :type type: str .. py:method:: set_device(device) Sets the device of all the parameters in the composed operator :param device: Device to set parameters to :type device: str .. py:method:: detach() Detaches all parameters of the composed operator .. py:method:: normalization_constant(xv, yv, a) Computes the normalization constant of the combined operator using the normalization constants of its components :param xv: Meshgrid x coordinates :type xv: torch.Tensor :param yv: Meshgrid y coordinates :type yv: torch.Tensor :param a: Source-detector distances :type a: torch.Tensor :returns: Normalization constant :rtype: torch.Tensor .. py:method:: __call__(input, xv, yv, a, normalize = False) Computes the output of the combined operator :param input: Input to the operator :type input: torch.Tensor[Ld,Li,Lj] :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :param normalize: Whether to normalize the output. Defaults to False. :type normalize: bool, optional :returns: Output of the operator :rtype: torch.Tensor[Ld,Li,Lj] .. py:class:: Rotate1DConvOperator(kernel1D, N_angles, additive = False, use_fft_conv = False, rot = 0) Bases: :py:obj:`Operator` Operator that functions by rotating the input by a number of angles and applying a 1D convolution at each angle :param kernel1D: 1D kernel to apply at each rotation angle :type kernel1D: Kernel1D :param N_angles: Number of angles to convolve at. Evenly distributes these angles between 0 and 180 degrees (2 angles would be 0, 90 degrees) :type N_angles: int :param additive: Use in additive mode; in this case, the initial input is used at each rotation angle. If False, then output from each previous angle is used in succeeding angles. Defaults to False. :type additive: bool, optional :param use_fft_conv: Whether or not to use FFT based convolution. Defaults to False. :type use_fft_conv: bool, optional :param rot: Initial angle offset. Defaults to 0. :type rot: float, optional .. py:method:: _conv(input) Applies convolution to the input :param input: Input tensor :type input: torch.Tensor :returns: Convolved input tensor :rtype: torch.Tensor .. py:method:: normalization_constant(xv, yv, a) Computes the normalization constant of the operator :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Normalization constant at each source-detector distance :rtype: torch.Tensor[Ld] .. py:method:: _rotate(input, angle) Rotates the input at the desired angle :param input: Input tensor :type input: torch.Tensor :param angle: Angle to rotate by :type angle: float :returns: Rotated input :rtype: torch.Tensor .. py:method:: _apply_additive(input) Applies the operator in additive mode :param input: Input tensor :type input: torch.Tensor :returns: Output tensor, which is rotated + convolved input tensor :rtype: torch.Tensor .. py:method:: _apply_regular(input) Applies operator in non-additive mode :param input: Input tensor :type input: torch.Tensor :returns: Output tensor, which is rotated + convolved input tensor :rtype: torch.Tensor .. py:method:: __call__(input, xv, yv, a, normalize = False) Evaluates the operator on the input. The meshgrid xv and yv is used to compute the kernel size; it is assumed that the spacing in xv and yv is the same as that in input. The output is multiplied by the area of a pixel in the meshgrid. :param input: Input 3D map to be operated on :type input: torch.Tensor[Ld,Li,Lj] :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Output of the operator :rtype: torch.Tensor[Ld,Li,Lj] .. py:class:: RotateSeperable2DConvOperator(kernel1D, N_angles, additive = False, use_fft_conv = False, rot = 0) Bases: :py:obj:`Operator` Operator that applies rotations followed by convolutions with two perpendicular 1D kernels (x/y) at each angle :param kernel1D: Kernel1D to use for convolution :type kernel1D: Kernel1D :param N_angles: Number of angles to rotate at :type N_angles: int :param additive: Use in additive mode; in this case, the initial input is used at each rotation angle. If False, then output from each previous angle is used in succeeding angles. Defaults to False. :type additive: bool, optional :param use_fft_conv: Whether or not to use FFT based convoltution. Defaults to False. :type use_fft_conv: bool, optional :param rot: Initial rotation angle. Defaults to 0. :type rot: float, optional .. py:method:: _conv(input) Applies convolution :param input: Input tensor to convole :type input: torch.Tensor :returns: Convolved input tensor :rtype: torch.Tensor .. py:method:: normalization_constant(xv, yv, a) Computes the normalization constant of the operator :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Normalization constant at each source-detector distance :rtype: torch.Tensor[Ld] .. py:method:: _rotate(input, angle) Applies rotation to input tensor :param input: Input tensor to be rotated :type input: torch.Tensor :param angle: Rotation angle :type angle: float :returns: Rotated input tensor :rtype: torch.Tensor .. py:method:: _apply_additive(input) Applies operator in additive mode :param input: Input tensor :type input: torch.Tensor :returns: Output tensor :rtype: torch.Tensor .. py:method:: _apply_regular(input) Applies operator in non-additive mode :param input: Input tensor :type input: torch.Tensor :returns: Output tensor :rtype: torch.Tensor .. py:method:: __call__(input, xv, yv, a, normalize = False) Evaluates the operator on the input. The meshgrid xv and yv is used to compute the kernel size; it is assumed that the spacing in xv and yv is the same as that in input. The output is multiplied by the area of a pixel in the meshgrid. :param input: Input 3D map to be operated on :type input: torch.Tensor[Ld,Li,Lj] :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Output of the operator :rtype: torch.Tensor[Ld,Li,Lj] .. py:class:: Kernel2DOperator(kernel2D, use_fft_conv = False) Bases: :py:obj:`Operator` Operator built using a general 2D kernel; the output of this operator is 2D convolution with the Kernel2D instance :param kernel2D: Kernel2D instance used for obtaining the generic 2D kernel :type kernel2D: Kernel2D :param use_fft_conv: Whether or not to use FFT based convolution. Defaults to False. :type use_fft_conv: bool, optional .. py:method:: _conv(input) Applies convolution to the input :param input: Input :type input: torch.Tensor :returns: Output :rtype: torch.Tensor .. py:method:: normalization_constant(xv, yv, a) Computes the normalization constant of the operator :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Normalization constant at each source-detector distance :rtype: torch.Tensor[Ld] .. py:method:: __call__(input, xv, yv, a, normalize = False) Evaluates the operator on the input. The meshgrid xv and yv is used to compute the kernel size; it is assumed that the spacing in xv and yv is the same as that in input. The output is multiplied by the area of a pixel in the meshgrid. :param input: Input 3D map to be operated on :type input: torch.Tensor[Ld,Li,Lj] :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Output of the operator :rtype: torch.Tensor[Ld,Li,Lj] .. py:class:: NearestKernelOperator(psf_data, distances, dr0, use_fft_conv = True, grid_sample_mode = 'bilinear') Bases: :py:obj:`Operator` Operator that uses a set of PSFs and distances to compute the output of the operator. The PSF is obtained by selecting the nearest PSF to each distance provided in __call__ so that each plane in input is convolved with the appropriate kernel. :param psf_data: Provided PSF data :type psf_data: torch.Tensor[LD,LX,LY] :param distances: Source-detector distance for each PSF :type distances: torch.Tensor[LD] :param dr0: Spacing in the PSF data :type dr0: float :param use_fft_conv: Whether or not to use FFT based convolutions. Defaults to True. :type use_fft_conv: bool, optional :param grid_sample_mode: How to sample the PSF when the input spacing is not the same as the PSF. Defaults to 'bilinear'. :type grid_sample_mode: str, optional .. py:method:: set_device(device) Sets the device of all parameters in the operator :param device: Device to set parameters to :type device: str .. py:method:: _conv(input, kernel) Performs convolution on the input data :param input: Input data :type input: torch.Tensor :param kernel: Kernel to convolve with :type kernel: torch.Tensor :returns: Convolved input data :rtype: torch.Tensor .. py:method:: _get_nearest_distance_idxs(distances) Obtains the indices of the nearest PSF to each distance :param distances: Distances to find the nearest PSF for :type distances: torch.Tensor :returns: Array of indices of the nearest PSF :rtype: torch.Tensor .. py:method:: _get_kernel(xv, yv, a) Obtains the kernel by sampling the nearest PSF at the appropriate location :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Kernel obtained by sampling the nearest PSF :rtype: torch.Tensor[Ld,Lx,Ly] .. py:method:: __call__(input, xv, yv, a, normalize = False) Evaluates the operator on the input. The meshgrid xv and yv is used to compute the kernel size; it is assumed that the spacing in xv and yv is the same as that in input. The output is multiplied by the area of a pixel in the meshgrid. :param input: Input 3D map to be operated on :type input: torch.Tensor[Ld,Li,Lj] :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Output of the operator :rtype: torch.Tensor[Ld,Li,Lj] .. py:class:: GaussianOperator(amplitude_fn, sigma_fn, amplitude_params, sigma_params, a_min = -torch.inf, a_max = torch.inf, use_fft_conv = False) Bases: :py:obj:`Operator` Gaussian operator; works by convolving the input with two perpendicular 1D kernels. This is implemented seperately from the Kernel2DOperator since it is more efficient to convolve with two 1D kernels than a 2D kernel. :param amplitude_fn: Amplitude function for 1D Gaussian kernel :type amplitude_fn: Callable :param sigma_fn: Scale function for 1D Gaussian kernel :type sigma_fn: Callable :param amplitude_params: Amplitude hyperparameters :type amplitude_params: torch.Tensor :param sigma_params: Scaling hyperparameters :type sigma_params: torch.Tensor :param a_min: Minimum source-detector distance for the kernel; any distance values passed to __call__ below this value will be clamped to this value. Defaults to -torch.inf. :type a_min: float, optional :param a_max: Minimum source-detector distance for the kernel; any distance values passed to __call__ below this value will be clamped to this value. Defaults to torch.inf. :type a_max: float, optional :param use_fft_conv: Whether or not to use FFT based convolution. Defaults to False. :type use_fft_conv: bool, optional .. py:method:: normalization_constant(xv, yv, a) Computes the normalization constant of the operator :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Normalization constant at each source-detector distance :rtype: torch.Tensor[Ld] .. py:method:: _conv(input, kernel1D) Performs convolution on input :param input: Input tensor :type input: torch.Tensor :param kernel1D: Gaussian 1D kernel :type kernel1D: torch.Tensor :returns: Output convolved tensor :rtype: torch.Tensor .. py:method:: __call__(input, xv, yv, a, normalize = False) Evaluates the operator on the input. The meshgrid xv and yv is used to compute the kernel size; it is assumed that the spacing in xv and yv is the same as that in input. The output is multiplied by the area of a pixel in the meshgrid. :param input: Input 3D map to be operated on :type input: torch.Tensor[Ld,Li,Lj] :param xv: Meshgrid x coordinates :type xv: torch.Tensor[Lx,Ly] :param yv: Meshgrid y coordinates :type yv: torch.Tensor[Lx,Ly] :param a: Source-detector distances :type a: torch.Tensor[Ld] :returns: Output of the operator :rtype: torch.Tensor[Ld,Li,Lj]