pytomography.utils
#
This module contains utility functions used in the other modules of PyTomography
Submodules#
Package Contents#
Functions#
|
Reverse cumulative sum along the first axis of a tensor of shape [batch_size, Lx, Ly, Lz]. |
|
Returns an object tensor in a rotated reference frame such that the scanner is located at the +x axis. Note that the scanner angle $eta$ is related to $phi$ (azimuthal angle) by $phi = 3pi/2 - eta$. |
|
Given the radial distance to center of object space from the scanner, computes the distance |
|
Computes the pad width required such that subsequent rotation retains the entire image |
|
Pads an image along the Lr axis |
|
Pads object tensors by enough pixels in the xy plane so that subsequent rotations don't crop out any of the object |
|
Unpads the image back to original Lr dimensions |
|
Unpads a padded object tensor in the xy plane back to its original dimensions |
|
Pads an object tensor along z. Useful for PSF modeling |
|
Unpads an object along the z dimension |
|
|
|
Obtains the number of blank z-slices at the sup ( |
|
Converts an array of Hounsfield Units into linear attenuation coefficient using the bilinear transformation \(f(x)=a_1x+b_1\) for positive \(x\) and \(f(x)=a_2x+b_2\) for negative \(x\). |
- pytomography.utils.rev_cumsum(x)[source]#
Reverse cumulative sum along the first axis of a tensor of shape [batch_size, Lx, Ly, Lz]. since this is used with CT correction, the initial voxel only contributes 1/2.
- Parameters:
x (torch.tensor[batch_size,Lx,Ly,Lz]) – Tensor to be summed
- Returns:
The cumulatively summed tensor.
- Return type:
torch.tensor[batch_size, Lx, Ly, Lz]
- pytomography.utils.rotate_detector_z(x, angles, mode='bilinear', negative=False)[source]#
Returns an object tensor in a rotated reference frame such that the scanner is located at the +x axis. Note that the scanner angle $eta$ is related to $phi$ (azimuthal angle) by $phi = 3pi/2 - eta$.
- Parameters:
x (torch.tensor[batch_size, Lx, Ly, Lz]) – Tensor aligned with cartesian coordinate system specified
manual. (by the) –
angles (torch.Tensor) – The angles $eta$ where the scanner is located for each element in the batch x.
mode (str, optional) – Method of interpolation used to get rotated image. Defaults to bilinear.
negative (bool, optional) – If True, applies an inverse rotation. In this case, the tensor
$eta$ (x is an object in a coordinate system aligned with) –
the (and the function rotates) –
particular (x back to the original cartesian coordinate system specified by the users manual. In) –
one (if) –
negative=False (uses this function on a tensor with) –
returned (then applies this function to that) –
negative=True (tensor with) –
False. (it should return the same tensor. Defaults to) –
- Returns:
Rotated tensor.
- Return type:
torch.tensor[batch_size, Lx, Ly, Lz]
- pytomography.utils.get_distance(Lx, r, dx)[source]#
- Given the radial distance to center of object space from the scanner, computes the distance
between each parallel plane (i.e. (y-z plane)) and a detector located at +x. This function is used for point spread function (PSF) blurring where the amount of blurring depends on thedistance from the detector.
- Parameters:
Lx (int) – The number of y-z planes to compute the distance of
r (float) – The radial distance between the central y-z plane and the detector at +x.
dx (float) – The spacing between y-z planes in Euclidean distance.
- Returns:
An array of distances for each y-z plane to the detector.
- Return type:
np.array[Lx]
- pytomography.utils.compute_pad_size(width)[source]#
Computes the pad width required such that subsequent rotation retains the entire image
- Parameters:
width (int) – width of the corresponding axis (i.e. number of elements in the dimension)
- Returns:
the number of pixels by which the axis needs to be padded on each side
- Return type:
int
- pytomography.utils.pad_image(image, mode='constant', value=0)[source]#
Pads an image along the Lr axis
- Parameters:
image (torch.Tensor[batch_size, Ltheta, Lr, Lz]) – Image tensor.
mode (str, optional) – Padding mode to use. Defaults to ‘constant’.
value (float, optional) – If padding mode is constant, fill with this value. Defaults to 0.
- Returns:
Padded image tensor.
- Return type:
torch.Tensor[batch_size, Ltheta, Lr’, Lz]
- pytomography.utils.pad_object(object, mode='constant')[source]#
Pads object tensors by enough pixels in the xy plane so that subsequent rotations don’t crop out any of the object
- Parameters:
object (torch.Tensor[batch_size, Lx, Ly, Lz]) – object tensor to be padded
mode (str, optional) – _description_. Defaults to ‘constant’.
- Returns:
_description_
- Return type:
_type_
- pytomography.utils.unpad_image(image)[source]#
Unpads the image back to original Lr dimensions
- Parameters:
image (torch.Tensor[batch_size, Ltheta, Lr', Lz]) – Padded image tensor
- Returns:
Unpadded image tensor
- Return type:
torch.Tensor[batch_size, Ltheta, Lr, Lz]
- pytomography.utils.unpad_object(object)[source]#
Unpads a padded object tensor in the xy plane back to its original dimensions
- Parameters:
object (torch.Tensor[batch_size, Lx', Ly', Lz]) – padded object tensor
- Returns:
Object tensor back to it’s original dimensions.
- Return type:
torch.Tensor[batch_size, Lx, Ly, Lz]
- pytomography.utils.pad_object_z(object, pad_size, mode='constant')[source]#
Pads an object tensor along z. Useful for PSF modeling
- Parameters:
object (torch.Tensor[batch_size, Lx, Ly, Lz]) – Object tensor
pad_size (int) – Amount by which to pad in -z and +z
mode (str, optional) – Padding mode. Defaults to ‘constant’.
- Returns:
Padded object tensor along z.
- Return type:
torch.Tensor[torch.Tensor[batch_size, Lx, Ly, Lz’]]
- pytomography.utils.unpad_object_z(object, pad_size)[source]#
Unpads an object along the z dimension
- Parameters:
object (torch.Tensor[batch_size, Lx, Ly, Lz']) – Padded object tensor along z.
pad_size (int) – Amount by which the padded tensor was padded in the z direcion
- Returns:
Unpadded object tensor.
- Return type:
torch.Tensor[batch_size, Lx, Ly, Lz]
- pytomography.utils.get_object_nearest_neighbour(object, shifts)[source]#
- Parameters:
object (torch.Tensor) –
shifts (list[int]) –
- pytomography.utils.get_blank_below_above(image)[source]#
Obtains the number of blank z-slices at the sup (
blank_above
) and inf (blank_below
) of the projection data. This method is entirely empircal, and looks for z slices where there are zero detected counts.- Parameters:
image (torch.tensor) – Image data from a scanner
- Returns:
A tuple of two elements corresponding to the number of blank slices at the inf, and the number of blank slices at the sup.
- Return type:
Sequence[int]
- pytomography.utils.bilinear_transform(arr, a1, b1, a2, b2)[source]#
Converts an array of Hounsfield Units into linear attenuation coefficient using the bilinear transformation \(f(x)=a_1x+b_1\) for positive \(x\) and \(f(x)=a_2x+b_2\) for negative \(x\).
- Parameters:
arr (np.array) – Array to be transformed using bilinear transformation
a1 (float) – Bilinear slope for negative input values
b1 (float) – Bilinear intercept for negative input values
a2 (float) – Bilinear slope for positive input values
b2 (float) – Bilinear intercept for positive input values
- Returns:
Transformed array.
- Return type:
np.array