deepali.core.pointset#

Utility functions for point sets.

Module Contents#

Functions#

bounding_box(→ Tuple[torch.Tensor, torch.Tensor])

Compute corners of minimum axes-aligned bounding box of given points.

distance_matrix(→ torch.Tensor)

Compute squared Euclidean distances between all pairs of points.

closest_point_distances(→ torch.Tensor)

Compute minimum Euclidean distance from each point in x to point set y.

closest_point_indices(→ torch.Tensor)

Determine indices of points in y with minimum Euclidean distance from each point in x.

normalize_grid(→ torch.Tensor)

Map unnormalized grid coordinates to normalized grid coordinates.

denormalize_grid(→ torch.Tensor)

Map normalized grid coordinates to unnormalized grid coordinates.

polyline_directions(→ torch.Tensor)

Compute proximal to distal facing tangent vectors.

polyline_tangents(→ torch.Tensor)

Compute distal to proximal facing tangent vectors.

transform_grid(→ torch.Tensor)

Transform undeformed grid by a spatial transformation.

transform_points(→ torch.Tensor)

Transform set of points by a tensor of non-rigid flow fields.

bounding_box(points: torch.Tensor) Tuple[torch.Tensor, torch.Tensor][source]#

Compute corners of minimum axes-aligned bounding box of given points.

distance_matrix(x: torch.Tensor, y: torch.Tensor) torch.Tensor[source]#

Compute squared Euclidean distances between all pairs of points.

Parameters
  • x – Point set of shape (N, X, D).

  • y – Point set of shape (N, Y, D).

Returns

Tensor of distance matrices of shape (N, X, Y).

closest_point_distances(x: torch.Tensor, y: torch.Tensor, split_size: int = 10000) torch.Tensor[source]#

Compute minimum Euclidean distance from each point in x to point set y.

Parameters
  • x – Point set of shape (N, X, D).

  • y – Point set of shape (N, Y, D).

  • split_size – Maximum number of points in x to consider each time when computing the full distance matrix between these points in x and every point in y. This is required to limit the size of the distance matrix.

Returns

Tensor of shape (N, X) with minimum distances from points in x to points in y.

closest_point_indices(x: torch.Tensor, y: torch.Tensor, split_size: int = 10000) torch.Tensor[source]#

Determine indices of points in y with minimum Euclidean distance from each point in x.

Parameters
  • x – Point set of shape (N, X, D).

  • y – Point set of shape (N, Y, D).

  • split_size – Maximum number of points in x to consider each time when computing the full distance matrix between these points in x and every point in y. This is required to limit the size of the distance matrix.

Returns

Tensor of shape (N, X) with indices of closest points in y.

normalize_grid(grid: torch.Tensor, size: Optional[Union[torch.Tensor, torch.Size]] = None, side_length: float = 2, align_corners: bool = ALIGN_CORNERS, channels_last: bool = True) torch.Tensor[source]#

Map unnormalized grid coordinates to normalized grid coordinates.

denormalize_grid(grid: torch.Tensor, size: Optional[Union[torch.Tensor, torch.Size]] = None, side_length: float = 2, align_corners: bool = ALIGN_CORNERS, channels_last: bool = True) torch.Tensor[source]#

Map normalized grid coordinates to unnormalized grid coordinates.

polyline_directions(points: torch.Tensor, normalize: bool = False, repeat_last: bool = True) torch.Tensor[source]#

Compute proximal to distal facing tangent vectors.

polyline_tangents(points: torch.Tensor, normalize: bool = False, repeat_first: bool = True) torch.Tensor[source]#

Compute distal to proximal facing tangent vectors.

transform_grid(transform: torch.Tensor, grid: torch.Tensor, align_corners: bool = ALIGN_CORNERS) torch.Tensor[source]#

Transform undeformed grid by a spatial transformation.

This function applies a spatial transformation to map a tensor of undeformed grid points to a tensor of deformed grid points with the same shape as the input tensor. The input points must be the positions of undeformed spatial grid points, because in case of a non-rigid transformation, this function uses interpolation to resize the vector fields to the size of the input grid. This assumes that input points x are the coordinates of points located on a regularly spaced undeformed grid which is aligned with the borders of the grid domain on which the vector fields of the non-rigid transformations are sampled, i.e., y = x + u.

In case of a linear transformation y = Ax + t.

If in doubt whether the input points will be sampled regularly at grid points of the domain of the spatial transformation, use transform_points() instead.

Parameters
  • transform – Tensor representation of spatial transformation, where the shape of the tensor determines the type of transformation. A translation-only transformation must be given as tensor of shape (N, D, 1). An affine-only transformation without translation can be given as tensor of shape (N, D, D), and an affine transformation with translation as tensor of shape (N, D, D + 1). Flow fields of non-rigid transformations, on the other hand, are tensors of shape (N, D, ..., X), i.e., linear transformations are represented by 3-dimensional tensors, and non-rigid transformations by tensors of at least 4 dimensions. If batch size is one, but the batch size of points is greater than one, all point sets are transformed by the same non-rigid transformation.

  • grid – Coordinates of undeformed grid points as tensor of shape (N, ..., D) or (1, ..., D). If batch size is one, but multiple flow fields are given, this single point set is transformed by each non-rigid transformation to produce N output point sets.

  • align_corners – Whether flow vectors in case of a non-rigid transformation are with respect to Axes.CUBE (False) or Axes.CUBE_CORNERS (True). The input grid points must be with respect to the same spatial grid domain as the input flow fields. This option is in particular passed on to the grid_reshape() function used to resize the flow fields to the shape of the input grid.

Returns

Tensor of shape (N, ..., D) with coordinates of spatially transformed points.

transform_points(transform: torch.Tensor, points: torch.Tensor, align_corners: bool = ALIGN_CORNERS) torch.Tensor[source]#

Transform set of points by a tensor of non-rigid flow fields.

This function applies a spatial transformation to map a tensor of points to a tensor of transformed points of the same shape as the input tensor. Unlike transform_grid(), it can be used to spatially transform any set of points which are defined with respect to the grid domain of the spatial transformation, including a tensor of shape (N, M, D), i.e., a batch of N point sets with cardianality M. It can also be applied to a tensor of grid points of shape (N, ..., X, D) regardless if the grid points are located at the undeformed grid positions or an already deformed grid. Therefore, in case of a non-rigid transformation, the given flow fields are sampled at the input points x using linear interpolation. The flow vectors u(x) are then added to the input points, i.e., y = x + u(x).

In case of a linear transformation y = Ax + t.

Parameters
  • transform – Tensor representation of spatial transformation, where the shape of the tensor determines the type of transformation. A translation-only transformation must be given as tensor of shape (N, D, 1). An affine-only transformation without translation can be given as tensor of shape (N, D, D), and an affine transformation with translation as tensor of shape (N, D, D + 1). Flow fields of non-rigid transformations, on the other hand, are tensors of shape (N, D, ..., X), i.e., linear transformations are represented by 3-dimensional tensors, and non-rigid transformations by tensors of at least 4 dimensions. If batch size is one, but the batch size of points is greater than one, all point sets are transformed by the same non-rigid transformation.

  • points – Coordinates of points given as tensor of shape (N, ..., D) or (1, ..., D). If batch size is one, but multiple flow fields are given, this single point set is transformed by each non-rigid transformation to produce N output point sets.

  • align_corners – Whether flow vectors in case of a non-rigid transformation are with respect to Axes.CUBE (False) or Axes.CUBE_CORNERS (True). The input points must be with respect to the same spatial grid domain as the input flow fields. This option is in particular passed on to the grid_sample() function used to sample the flow vectors at the input points.

Returns

Tensor of shape (N, ..., D) with coordinates of spatially transformed points.