pytomography.utils.helper_functions — PyTomography 0.8.0 documentation

Skip to main content
`Ctrl`+`K`

# `pytomography.utils.helper_functions`

## Module Contents

### Functions

`rev_cumsum`(x) | Reverse cumulative sum along the first axis of a tensor of shape [batch_size, Lx, Ly, Lz].
`rotate_detector_z`(x, angles[, mode, negative]) | Returns an object tensor in a rotated reference frame such that the scanner is located at the +x axis. Note that the scanner angle \(\beta\) is related to \(\phi\) (azimuthal angle) by \(\phi = 3\pi/2 - \beta\).
`get_distance`(Lx, r, dx) | Given the radial distance to center of object space from the scanner, computes the distance
`compute_pad_size`(width) | Computes the pad width required such that subsequent rotation retains the entire image
`compute_pad_size_padded`(width) | Computes the width by which an object was padded, given its padded size.
`pad_object`(object[, mode]) | Pads object tensors by enough pixels in the xy plane so that subsequent rotations don't crop out any of the object
`unpad_object`(object) | Unpads a padded object tensor in the xy plane back to its original dimensions
`pad_image`(image[, mode, value]) | Pads an image along the Lr axis
`unpad_image`(image) | Unpads the image back to original Lr dimensions
`pad_object_z`(object, pad_size[, mode]) | Pads an object tensor along z. Useful for PSF modeling
`unpad_object_z`(object, pad_size) | Unpads an object along the z dimension
`get_object_nearest_neighbour`(object, shifts) |
`get_blank_below_above`(image) | 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.
`bilinear_transform`(arr, a1, b1, a2, b2) | 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.helper_functions.rev_cumsum(x)

  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.helper_functions.rotate_detector_z(x, angles, mode='bilinear', negative=False)

  Returns an object tensor in a rotated reference frame such that the scanner is located at the +x axis. Note that the scanner angle \(\beta\) is related to \(\phi\) (azimuthal angle) by \(\phi = 3\pi/2 - \beta\).

  Parameters:

    - x (torch.tensor[batch_size, Lx, Ly, Lz]) – Tensor aligned with cartesian coordinate system specified

    - manual. (by the) –

    - angles (torch.Tensor) – The angles \(\beta\) 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

  :param x is an object in a coordinate system aligned with \(\beta\): :param and the function rotates the: :param x back to the original cartesian coordinate system specified by the users manual. In particular: :param if one: :param uses this function on a tensor with negative=False: :param then applies this function to that returned: :param tensor with negative=True: :param it should return the same tensor. Defaults to False.:

  Returns:

    Rotated tensor.

  Return type:

    torch.tensor[batch_size, Lx, Ly, Lz]

  Parameters:

    - x (torch.Tensor) –

    - angles (torch.tensor) –

    - mode (str) –

    - negative (bool) –

pytomography.utils.helper_functions.get_distance(Lx, r, dx)
  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.helper_functions.compute_pad_size(width)

  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.helper_functions.compute_pad_size_padded(width)

  Computes the width by which an object was padded, given its padded size.

  Parameters:

    width (int) – width of the corresponding axis (i.e. number of elements in the dimension)

  Returns:

    the number of pixels by which the object was padded to get to this width

  Return type:

    int

pytomography.utils.helper_functions.pad_object(object, mode='constant')

  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.helper_functions.unpad_object(object)

  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.helper_functions.pad_image(image, mode='constant', value=0)

  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.helper_functions.unpad_image(image)

  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.helper_functions.pad_object_z(object, pad_size, mode='constant')

  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.helper_functions.unpad_object_z(object, pad_size)

  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.helper_functions.get_object_nearest_neighbour(object, shifts)
  Parameters:

    - object (torch.Tensor) –

    - shifts (list[int]) –

pytomography.utils.helper_functions.get_blank_below_above(image)

  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.helper_functions.bilinear_transform(arr, a1, b1, a2, b2)

  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