POI Strategies¶
Internal modules that implement the various strategies for computing Points of Interest from segmentation volumes.
Ray Casting¶
TPTBox.core.poi_fun.ray_casting
¶
unit_vector
¶
trilinear_interpolate
¶
Perform trilinear interpolation of a 3D volume at a sub-voxel coordinate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
ndarray
|
3D array to interpolate. |
required |
x
|
float
|
Sub-voxel coordinate along the first axis. |
required |
y
|
float
|
Sub-voxel coordinate along the second axis. |
required |
z
|
float
|
Sub-voxel coordinate along the third axis. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Interpolated scalar value at (x, y, z), or 0.0 if the point is outside |
float
|
the valid interior of the volume. |
Source code in TPTBox/core/poi_fun/ray_casting.py
max_distance_ray_cast_convex_npfast
¶
max_distance_ray_cast_convex_npfast(region_array: ndarray, start_coord: ndarray, direction_vector: ndarray, acc_delta: float = 5e-05) -> np.ndarray
Find the exit point of a ray inside a convex region using bisection (fast path).
Uses trilinear interpolation and bisection search to locate the last point
inside the region along the given direction. Prints a debug line at each
bisection step (development helper — see max_distance_ray_cast_convex_np
for the production version without debug output).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region_array
|
ndarray
|
Binary 3D numpy array where nonzero voxels define the region. |
required |
start_coord
|
ndarray
|
Starting coordinate |
required |
direction_vector
|
ndarray
|
Direction of the ray; need not be a unit vector. |
required |
acc_delta
|
float
|
Bisection stops when the bracket width falls below this value. Defaults to 0.00005. |
5e-05
|
Returns:
| Type | Description |
|---|---|
ndarray
|
3-element array with the approximate exit coordinate along the ray. |
ndarray
|
If |
ndarray
|
returned unchanged. |
Source code in TPTBox/core/poi_fun/ray_casting.py
max_distance_ray_cast_convex_np
¶
max_distance_ray_cast_convex_np(region: ndarray, start_coord: COORDINATE | ndarray, direction_vector: ndarray, acc_delta: float = 5e-05, max_v: int | None = None) -> np.ndarray | None
Find the exit point of a ray inside a convex region (numpy array input).
Uses RegularGridInterpolator and bisection to locate the boundary of a
convex binary region along the given direction vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
ndarray
|
Binary 3D numpy array where values > 0.5 are considered inside. |
required |
start_coord
|
COORDINATE | ndarray
|
Starting coordinate |
required |
direction_vector
|
ndarray
|
Direction of the ray; will be normalised internally. |
required |
acc_delta
|
float
|
Bisection convergence threshold in voxel units. Defaults to 0.00005. |
5e-05
|
max_v
|
int | None
|
Upper bound for the bisection search (in voxels along the ray).
Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
3-element numpy array with the approximate exit coordinate, or the start |
ndarray | None
|
coordinate if it lies outside the region. Returns |
ndarray | None
|
|
Source code in TPTBox/core/poi_fun/ray_casting.py
max_distance_ray_cast_convex
¶
max_distance_ray_cast_convex(region: NII, start_coord: COORDINATE | ndarray, direction_vector: ndarray, acc_delta: float = 5e-05) -> np.ndarray | None
Find the exit point of a ray inside a convex NII region.
Wraps the underlying numpy logic by extracting the voxel array from an
NII object and delegating to bisection-based ray casting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
NII
|
|
required |
start_coord
|
COORDINATE | ndarray
|
Starting coordinate |
required |
direction_vector
|
ndarray
|
Direction of the ray; will be normalised internally. |
required |
acc_delta
|
float
|
Bisection convergence threshold in voxel units. Defaults to 0.00005. |
5e-05
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
3-element numpy array with the approximate exit coordinate, or the start |
ndarray | None
|
coordinate if it lies outside the region. Returns |
ndarray | None
|
|
Source code in TPTBox/core/poi_fun/ray_casting.py
ray_cast_pixel_lvl
¶
ray_cast_pixel_lvl(start_point_np: ndarray, normal_vector: ndarray, shape: ndarray | tuple[int, ...], two_sided: bool = False) -> tuple[np.ndarray | None, np.ndarray | None]
Cast a ray at pixel/voxel resolution and return all integer voxel coordinates along it.
Traces the ray starting at start_point_np in the direction of
normal_vector until it exits the volume defined by shape. When
two_sided is True, the ray is also cast in the opposite direction and
the two halves are concatenated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_point_np
|
ndarray
|
Starting voxel coordinate |
required |
normal_vector
|
ndarray
|
Direction vector of the ray; will be normalised to a unit vector internally. |
required |
shape
|
ndarray | tuple[int, ...]
|
Volume shape |
required |
two_sided
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
A tuple |
ndarray | None
|
|
tuple[ndarray | None, ndarray | None]
|
|
Source code in TPTBox/core/poi_fun/ray_casting.py
add_ray_to_img
¶
add_ray_to_img(start_point: ndarray | COORDINATE, normal_vector: ndarray, seg: NII, add_to_img: bool = True, inplace: bool = False, value: int = 0, dilate: int = 1) -> NII | None
Paint a ray into a segmentation NII image.
Casts a ray from start_point along normal_vector at voxel
resolution, fills the visited voxels with distance values (or a fixed
value), optionally dilates the ray, and optionally composites it onto
the source segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_point
|
ndarray | COORDINATE
|
Starting voxel coordinate of the ray. |
required |
normal_vector
|
ndarray
|
Direction of the ray. |
required |
seg
|
NII
|
Segmentation |
required |
add_to_img
|
bool
|
If |
True
|
inplace
|
bool
|
Modify |
False
|
value
|
int
|
Fixed voxel value written along the ray. When 0, distance values along the ray are used instead. Defaults to 0. |
0
|
dilate
|
int
|
Morphological dilation radius applied to the ray image. Set to 0 to skip dilation. Defaults to 1. |
1
|
Returns:
| Type | Description |
|---|---|
NII | None
|
The modified |
NII | None
|
valid coordinates. |
Source code in TPTBox/core/poi_fun/ray_casting.py
add_spline_to_img
¶
add_spline_to_img(seg: NII, poi: POI, location: int = 50, add_to_img: bool = True, override_seg: bool = True, value: int = 100, dilate: int = 2) -> NII
Fit and paint a POI spline onto a segmentation NII image.
Computes a cubic spline through the POI centroids at location,
converts it to voxel coordinates, paints each spline point with value,
dilates the result, and (optionally) composites it onto seg.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seg
|
NII
|
Base segmentation |
required |
poi
|
POI
|
|
required |
location
|
int
|
Subregion ID used as the spline anchor points.
Defaults to 50 ( |
50
|
add_to_img
|
bool
|
If |
True
|
override_seg
|
bool
|
When overlaying, whether to overwrite existing nonzero
voxels in |
True
|
value
|
int
|
Voxel label written along the spline. Defaults to 100. |
100
|
dilate
|
int
|
Morphological dilation radius. Defaults to 2. |
2
|
Returns:
| Type | Description |
|---|---|
NII
|
The composite or standalone spline |
Source code in TPTBox/core/poi_fun/ray_casting.py
shift_point
¶
shift_point(poi: POI, vert_id: int, bb: tuple[slice, slice, slice] | None, start_point: Location = Location.Vertebra_Corpus, direction: DIRECTIONS | None = 'R', log: Logger_Interface = _log) -> np.ndarray | None
Shift a POI start point laterally by a fraction of the vertebra width.
Estimates the vertebra width from articular-process or costal-process POIs
and returns a new coordinate displaced from start_point along
direction by a scaled fraction of that width. Sacrum vertebrae without
arcus are skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
vert_id
|
int
|
Vertebra identifier (integer label). |
required |
bb
|
tuple[slice, slice, slice] | None
|
Bounding-box slices used to convert global POI coordinates to local coordinates within the cropped sub-volume. |
required |
start_point
|
Location
|
Location enum member or already-resolved numpy coordinate
from which the shift originates. Defaults to
|
Vertebra_Corpus
|
direction
|
DIRECTIONS | None
|
Anatomical direction letter ( |
'R'
|
log
|
Logger_Interface
|
Logger used for warning and error messages. |
_log
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Shifted voxel coordinate as a 3-element numpy array, or |
ndarray | None
|
required POIs are missing or the vertebra is excluded. |
Source code in TPTBox/core/poi_fun/ray_casting.py
max_distance_ray_cast_convex_poi
¶
max_distance_ray_cast_convex_poi(poi: POI, region: NII, vert_id: int, bb: tuple[slice, slice, slice] | None, normal_vector_points: tuple[Location, Location] | DIRECTIONS = 'R', start_point: Location | ndarray = Location.Vertebra_Corpus, log: Logger_Interface = _log, acc_delta: float = 5e-05) -> np.ndarray | None
Find the boundary point of a convex region along a direction derived from POI landmarks.
Resolves the ray start coordinate and direction vector from the POI
object, then delegates to :func:max_distance_ray_cast_convex to locate
the exit point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
region
|
NII
|
|
required |
vert_id
|
int
|
Vertebra identifier (integer label). |
required |
bb
|
tuple[slice, slice, slice] | None
|
Bounding-box slices that map between global POI coordinates and the local sub-volume coordinate system. |
required |
normal_vector_points
|
tuple[Location, Location] | DIRECTIONS
|
Either a |
'R'
|
start_point
|
Location | ndarray
|
Starting point of the ray: a |
Vertebra_Corpus
|
log
|
Logger_Interface
|
Logger used for warning and error messages. |
_log
|
acc_delta
|
float
|
Bisection convergence threshold in voxel units. Defaults to 0.00005. |
5e-05
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
3-element numpy array with the approximate exit coordinate in the local |
ndarray | None
|
sub-volume coordinate system, or |
ndarray | None
|
direction cannot be resolved. |
Source code in TPTBox/core/poi_fun/ray_casting.py
calculate_pca_normal_np
¶
calculate_pca_normal_np(segmentation: ndarray, pca_component: int, zoom: tuple[float, ...] | ndarray | None = None, verbose: bool = False) -> np.ndarray
Computes the normal vector of a segmented region using Principal Component Analysis (PCA).
Parameters:¶
segmentation : np.ndarray
A binary mask where nonzero values indicate the segmented region.
pca_component : int, optional
The principal component index to return as the normal vector.
- 0: The primary axis (direction of greatest variance, often the main elongation).
- 1: The secondary axis (orthogonal to the primary, capturing the second-most variance).
- 2: The third axis (typically the normal direction to the structure in 3D).
zoom : tuple or array-like, optional
If provided, scales the normal vector by the inverse of the voxel size to account for anisotropic resolution.
verbose : bool, optional
If True, prints the principal component vectors for debugging.
Returns:¶
normal_vector : np.ndarray The selected principal component as a normal vector.
Usage:¶
Use pca_component=2 when you want the normal to a surface-like structure.
If analyzing an elongated structure (e.g., a vessel or bone), pca_component=0 gives the primary axis,
while pca_component=1 provides the secondary direction.
Source code in TPTBox/core/poi_fun/ray_casting.py
set_label_above_3_point_plane
¶
set_label_above_3_point_plane(array: NII | ndarray, p1: ndarray | list[float], p2: ndarray | list[float], p3: ndarray | list[float], value: float = 0, invert: Literal[-1, 1] = 1, mask: ndarray | NII | bool = True, inplace: bool = False) -> NII | np.ndarray
Set all values in a 3D array above a plane defined by three non-collinear points to a specified value.
Parameters:¶
array : NII | np.ndarray
A 3D NumPy array or an NII object representing the volume data.
p1, p2, p3 : array-like
Three (x, y, z) points defining the plane.
value : int or float, optional (default=0)
The value to set for all elements above the plane.
inf : Literal[-1, 1], optional (default=1)
Controls the direction of "above":
- 1 means values superior to the plane (default).
- -1 means values inferior to the plane.
If the input is an NII object with an inferior-superior orientation, this will be adjusted accordingly.
Returns:¶
np.ndarray The modified 3D array with values set above the plane.
Notes:¶
- The plane is defined by the equation
ax + by + cz + d = 0, where(a, b, c)is the normal vector. - Uses
meshgridto construct a 3D grid and determine which values lie above the plane.
Example:¶
import numpy as np
data = np.random.rand(300, 300, 300)
p1 = [100, 100, 50]
p2 = [100, 50, 100]
p3 = [50, 100, 100]
result = set_above_3_point_plane(data, p1, p2, p3, value=0)
Source code in TPTBox/core/poi_fun/ray_casting.py
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 | |
Vertebra Direction¶
TPTBox.core.poi_fun.vertebra_direction
¶
calc_orientation_of_vertebra_PIR
¶
calc_orientation_of_vertebra_PIR(poi: POI | None, vert: NII, subreg: NII, spline_subreg_point_id=Location.Vertebra_Corpus, source_subreg_point_id=Location.Vertebra_Corpus, subreg_id=Location.Spinal_Canal, do_fill_back: bool = False, spine_plot_path: None | str = None, save_normals_in_info=False, _orientation_version=0) -> tuple[POI, NII | None]
Calculate the orientation of vertebrae using PIR (Posterior, Inferior, Right) DIRECTIONS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI | None
|
Point of interest. If None, computed from |
required |
vert
|
NII
|
Vertebra (full). |
required |
subreg
|
NII
|
Subregion (full). |
required |
spline_subreg_point_id
|
Location
|
Subregion point ID for spline computation. Defaults to Location.Vertebra_Corpus. |
Vertebra_Corpus
|
source_subreg_point_id
|
Location
|
Source subregion point ID. Defaults to Location.Vertebra_Corpus. |
Vertebra_Corpus
|
subreg_id
|
Location
|
Subregion ID. Defaults to Location.Spinal_Canal. |
Spinal_Canal
|
do_fill_back
|
bool
|
Whether to fill back. Defaults to False. |
False
|
spine_plot_path
|
None | str
|
Path to spine plot. Defaults to None. |
None
|
save_normals_in_info
|
bool
|
Whether to save normals in info. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[POI, NII | None]
|
Tuple[POI, NII | None]: Point of interest and filled back NII. |
Source code in TPTBox/core/poi_fun/vertebra_direction.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
get_direction
¶
Return the unit vector for an anatomical direction of a vertebra.
Retrieves the pre-computed PIR orientation vectors for vertebra vert_id
from poi and returns the one corresponding to direction d. C1 has
no independent direction and falls back to C2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
DIRECTIONS
|
Anatomical direction letter ( |
required |
poi
|
POI
|
|
required |
vert_id
|
int
|
Vertebra identifier. If 1 (C1), direction is taken from C2. |
required |
verbose
|
bool
|
Emit a warning when C1 falls back to C2. Defaults to |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Unit vector (3-element numpy array) pointing in direction |
ndarray
|
image coordinate frame. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If orientation data for the requested vertebra is absent. |
Source code in TPTBox/core/poi_fun/vertebra_direction.py
get_vert_direction_PIR
¶
get_vert_direction_PIR(poi: POI, vert_id: int, do_norm: bool = True, to_pir: bool = True) -> Vertebra_Orientation
Retrieve the vertebra orientation vectors from the POI.
Must be called after :func:calc_orientation_of_vertebra_PIR has populated
the Vertebra_Direction_* POI entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
vert_id
|
int
|
Vertebra identifier (integer label). |
required |
do_norm
|
bool
|
Normalise each direction vector to unit length.
Defaults to |
True
|
to_pir
|
bool
|
Rescale and reorient the POI to isotropic PIR space before
computing the directions. When |
True
|
Returns:
| Type | Description |
|---|---|
Vertebra_Orientation
|
Tuple of three unit vectors |
Vertebra_Orientation
|
Inferior, and Right directions of the vertebra in the (optionally |
Vertebra_Orientation
|
reoriented) coordinate frame. |
Source code in TPTBox/core/poi_fun/vertebra_direction.py
get_vert_direction_matrix
¶
get_vert_direction_matrix(poi: POI, vert_id: int, to_pir: bool = False) -> tuple[np.ndarray, np.ndarray]
Return the change-of-basis matrices between the image frame and the vertebra PIR frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
vert_id
|
int
|
Vertebra identifier (integer label). |
required |
to_pir
|
bool
|
Whether to convert the POI to isotropic PIR space before
computing. Defaults to |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
A tuple |
ndarray
|
|
tuple[ndarray, ndarray]
|
|
Source code in TPTBox/core/poi_fun/vertebra_direction.py
calc_center_spinal_cord
¶
calc_center_spinal_cord(poi: POI, subreg: NII, spline_subreg_point_id: Location | list[Location] = Location.Vertebra_Corpus, source_subreg_point_id: Location = Location.Vertebra_Corpus, subreg_id=Location.Spinal_Canal, intersection_target: list[Location] | None = None, _fill_inplace: NII | None = None, add_dense=False) -> POI
Calculate the center of the spinal cord within a specified region.
Parameters: - poi (POI): Point of Interest object containing relevant data. - subreg (NII): Neuroimaging subregion data for analysis. - spline_subreg_point_id (int, optional): The height of the region to analyze (default is Location.Vertebra_Corpus). - subreg_id (int, optional): The identifier for the subregion (default is Location.Spinal_Canal). - intersection_target (List[Location], optional): A list of locations for intersection analysis (default is [Location.Spinal_Cord, Location.Spinal_Canal]).
Returns: - POI: Point of Interest object with calculated centroids.
This function calculates the center of the spinal cord within a specified region using a spline fit to the given Point of Interest (POI) data and a NII. It first extracts a subregion of interest based on the provided spline_subreg_point_id and then calculates the center using geometric operations.
Note:
- The poi object should contain the relevant data for spline fitting.
- The subreg object should be a NII subregion containing spine with number 60 and 61.
- The subreg_id parameter is an optional identifier for the subregion.
- The intersection_target parameter allows you to specify the locations to intersect (e.g., spinal cord and spinal canal).
Example usage:
poi = POI(...)
subreg = NII(...)
updated_poi = calc_center_spinal_cord(
poi,
subreg,
spline_subreg_point_id=Location.Vertebra_Corpus,
subreg_id=Location.Spinal_Canal,
intersection_target=[Location.Spinal_Cord, Location.Spinal_Canal],
)
Source code in TPTBox/core/poi_fun/vertebra_direction.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
Vertebra Non-Centroid POIs¶
TPTBox.core.poi_fun.vertebra_pois_non_centroids
¶
Strategy_Pattern
¶
Implements the Strategy design pattern by encapsulating different strategies as callable objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Location
|
The target location for which this strategy is defined. |
required |
strategy
|
Callable
|
The strategy function that implements the desired behavior. |
required |
prerequisite
|
set[Location] | None
|
A set of prerequisite locations that must be satisfied before applying this strategy. Defaults to None. |
None
|
**args
|
Additional keyword arguments to be passed to the strategy function. |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
target |
Location
|
The target location for which this strategy is defined. |
args |
dict
|
Additional keyword arguments to be passed to the strategy function. |
prerequisite |
set[Location]
|
A set of prerequisite locations that must be satisfied before applying this strategy. |
strategy |
Callable
|
The strategy function that implements the desired behavior. |
Note
The strategy function should accept the following arguments: - poi (POI): The point of interest. - current_subreg (NII): The current subregion. - vert_id (int): The vertex ID. - bb: The bounding box. - log (Logger_Interface, optional): The logger interface. Defaults to _log, which should be defined globally.
Example
def strategy_function(poi, current_subreg, location, log, vert_id, bb, **kwargs): ... # Strategy implementation ... pass strategy = Strategy_Pattern(target_location, strategy_function, prerequisite={prerequisite_location}, additional_arg=value) result = strategy(poi, current_subreg, vert_id, bb)
Source code in TPTBox/core/poi_fun/vertebra_pois_non_centroids.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
prority
¶
Strategy_Pattern_Side_Effect
¶
Bases: Strategy_Pattern
Strategy marker for POIs computed as a side effect of another strategy.
Registers target as a POI that does not need its own compute call
because it is produced implicitly when prerequisite is computed. The
callable is a no-op; the dependency is tracked in
:data:pois_computed_by_side_effect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Location
|
The |
required |
prerequisite
|
Location
|
The |
required |
**args
|
Additional keyword arguments forwarded to
:class: |
{}
|
Source code in TPTBox/core/poi_fun/vertebra_pois_non_centroids.py
Strategy_Computed_Before
¶
Bases: Strategy_Pattern
Strategy marker for POIs that must be computed before the pipeline runs.
Use this when a Location is computed by an earlier, separate step
(e.g. spinal canal centroids) rather than by a generic strategy function.
The callable is a no-op; the class simply registers the prerequisite
dependencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Location
|
The |
required |
*prerequisite
|
Location
|
One or more |
()
|
**args
|
Additional keyword arguments forwarded to
:class: |
{}
|
Source code in TPTBox/core/poi_fun/vertebra_pois_non_centroids.py
run_poi_pipeline
¶
Compute all POIs from vertebra and subregion segmentations and save them to disk.
Wraps :func:~TPTBox.calc_poi_from_subreg_vert for every Location
member and writes the result as a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vert
|
NII
|
Vertebra instance segmentation |
required |
subreg
|
NII
|
Subregion segmentation |
required |
poi_path
|
Path
|
Output path for the JSON POI file. Used as a buffer file so that an existing file is read and extended rather than recomputed from scratch. |
required |
logger
|
Logger_Interface
|
Logger instance for progress and error messages. |
_log
|
Source code in TPTBox/core/poi_fun/vertebra_pois_non_centroids.py
add_prerequisites
¶
Expand a list of Location members with all transitive prerequisites.
Walks the :data:all_poi_functions registry and iteratively adds any
Location entries that are required by the provided list (and their
requirements, and so on) until no new entries are found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
locs
|
Sequence[Location]
|
Initial sequence of |
required |
Returns:
| Type | Description |
|---|---|
list[Location]
|
Sorted list of |
list[Location]
|
every transitive prerequisite, ordered by |
Raises:
| Type | Description |
|---|---|
UserWarning
|
If a dependency cycle is detected (loop limit of 1000 iterations reached). |
Source code in TPTBox/core/poi_fun/vertebra_pois_non_centroids.py
compute_non_centroid_pois
¶
compute_non_centroid_pois(poi: POI, locations: Sequence[Location] | Location, vert: NII, subreg: NII, _vert_ids: Sequence[int] | None = None, log: Logger_Interface = _log, verbose: bool | None = True, _orientation_version: int = 0) -> None
Compute all requested non-centroid anatomical landmarks and store them in poi in place.
Runs the full non-centroid POI pipeline:
- Vertebra orientation (PIR direction vectors) — always computed first if
Location.Vertebra_Direction_Inferioris requested. - Global landmarks: spinal canal / cord centres, intervertebral disc POIs, dense-axis tip.
- Per-vertebra landmarks via the registered :class:
Strategy_Patternfunctions (extreme points, ray casts, corner finders, etc.). - Intervertebral disc (IVD) POIs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
locations
|
Sequence[Location] | Location
|
One or more |
required |
vert
|
NII
|
Vertebra instance segmentation |
required |
subreg
|
NII
|
Spine subregion segmentation |
required |
_vert_ids
|
Sequence[int] | None
|
Explicit list of vertebra integer labels to process.
When |
None
|
log
|
Logger_Interface
|
Logger for progress and error messages. |
_log
|
verbose
|
bool | None
|
Verbosity passed to logging calls. |
True
|
_orientation_version
|
int
|
Internal version selector for the vertebra orientation algorithm; keep at 0 (default) for production use. |
0
|
Source code in TPTBox/core/poi_fun/vertebra_pois_non_centroids.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
Pixel-Based Point Finder¶
TPTBox.core.poi_fun.pixel_based_point_finder
¶
get_nearest_neighbor
¶
Return the voxel in a label mask that is closest to a query point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
ndarray
|
Query point as a 1-D or column array of shape |
required |
sr_msk
|
ndarray
|
3-D integer array containing segmentation labels. |
required |
region_label
|
int
|
Label value whose voxels are searched for the nearest neighbour. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
1-D integer array |
ndarray
|
|
Source code in TPTBox/core/poi_fun/pixel_based_point_finder.py
max_distance_ray_cast_pixel_level
¶
max_distance_ray_cast_pixel_level(poi: POI, region: NII, vert_id: int, bb: tuple[slice, slice, slice], normal_vector_points: tuple[Location, Location] | DIRECTIONS = 'R', start_point: Location | ndarray = Location.Vertebra_Corpus, two_sided=False, log: Logger_Interface = _log) -> tuple[int, ...] | None
Calculate the maximum distance ray cast in a region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
Point of interest. |
required |
region
|
NII
|
Region to cast rays in. |
required |
vert_id
|
int
|
Label of the region in |
required |
bb
|
Tuple[slice, slice, slice]
|
Bounding box coordinates. |
required |
normal_vector_points
|
Union[Tuple[Location, Location], DIRECTIONS]
|
Points defining the normal vector or the direction. Defaults to "R". |
'R'
|
start_point
|
Location
|
Starting point of the ray. Defaults to Location.Vertebra_Corpus. |
Vertebra_Corpus
|
log
|
Logger_Interface
|
Logger interface. Defaults to _log. |
_log
|
Returns:
| Type | Description |
|---|---|
tuple[int, ...] | None
|
Tuple[int, int, int]: The coordinates of the maximum distance ray cast. |
Source code in TPTBox/core/poi_fun/pixel_based_point_finder.py
ray_cast_pixel_level_from_poi
¶
ray_cast_pixel_level_from_poi(poi: POI, region: NII, vert_id: int, bb: tuple[slice, slice, slice], normal_vector_points: tuple[Location, Location] | DIRECTIONS = 'R', start_point: Location | ndarray = Location.Vertebra_Corpus, log: Logger_Interface = _log, two_sided: bool = False) -> tuple[np.ndarray | None, np.ndarray | None]
Perform pixel-level ray casting in a region using POI-derived direction.
Resolves the ray start coordinate and direction vector from the POI
object, then delegates to :func:~TPTBox.core.poi_fun.ray_casting.ray_cast_pixel_lvl
to enumerate all voxels along the ray at integer resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
region
|
NII
|
|
required |
vert_id
|
int
|
Vertebra identifier (integer label). |
required |
bb
|
tuple[slice, slice, slice]
|
Bounding-box slices mapping global POI coordinates to local sub-volume indices. |
required |
normal_vector_points
|
tuple[Location, Location] | DIRECTIONS
|
Ray direction — a |
'R'
|
start_point
|
Location | ndarray
|
Ray origin — a |
Vertebra_Corpus
|
log
|
Logger_Interface
|
Logger for warning and error messages. |
_log
|
two_sided
|
bool
|
Cast the ray in both directions. Defaults to |
False
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
|
ndarray | None
|
shape |
tuple[ndarray | None, ndarray | None]
|
array of distance values, or |
Source code in TPTBox/core/poi_fun/pixel_based_point_finder.py
get_extreme_point_by_vert_direction
¶
get_extreme_point_by_vert_direction(poi: POI, region: NII, vert_id: int, direction: Sequence[DIRECTIONS] | DIRECTIONS | tuple[DIRECTIONS, float] | Sequence[tuple[DIRECTIONS, float]] = 'I') -> np.ndarray | None
Find the voxel in a binary region that is most extreme along a vertebra-relative direction.
Projects all nonzero voxels in region into the vertebra PIR frame and
returns the voxel index with the maximum weighted score along the requested
direction(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
region
|
NII
|
Binary |
required |
vert_id
|
int
|
Vertebra identifier used to look up the orientation matrix. |
required |
direction
|
Sequence[DIRECTIONS] | DIRECTIONS | tuple[DIRECTIONS, float] | Sequence[tuple[DIRECTIONS, float]]
|
Anatomical direction(s) to optimise. May be:
Defaults to |
'I'
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Integer voxel index array |
ndarray | None
|
|
Note
region must contain binary values (1 for foreground, 0 for
background). Zoom scaling from poi is applied to ensure
physically meaningful distances.
Source code in TPTBox/core/poi_fun/pixel_based_point_finder.py
project_pois_onto_segmentation_surface
¶
project_pois_onto_segmentation_surface(poi: POI, seg: NII, connectivity: int = 1, dilated_surface: bool = False) -> POI
Projects points of interest (POI) onto a segmentation surface.
This function computes the surface points of a segmentation volume and projects the given points of interest (POI) onto these computed surface points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
The points of interest to be projected. |
required |
seg
|
NII
|
A segmentation volume object containing the target surface. |
required |
connectivity
|
int
|
The connectivity level for defining the surface. Default is 1, where 1 denotes face-connectivity. |
1
|
dilated_surface
|
bool
|
Whether to compute a dilated version of the surface, expanding the surface area. Default is False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
POI |
POI
|
The points of interest projected onto the segmentation surface. |
Source code in TPTBox/core/poi_fun/pixel_based_point_finder.py
project_pois_onto_set_of_points
¶
Projects points of interest (POI) onto the nearest points in a given set.
For each point in the POI, this function finds the closest point in the provided point set and updates the POI coordinates to align with the nearest points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
The points of interest to be projected. |
required |
point_set
|
list[COORDINATE]
|
A list of coordinates representing the target points for projection. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
POI |
POI
|
The updated POI with coordinates projected onto the nearest points in |
POI
|
the provided point set. |
Source code in TPTBox/core/poi_fun/pixel_based_point_finder.py
cdist_to_point
¶
Compute Euclidean distances from a single point to every row in an array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
COORDINATE | ndarray
|
Query point as a sequence or 1-D array of length 3. |
required |
a
|
ndarray
|
Array of shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
1-D float array of shape |
ndarray
|
|
Source code in TPTBox/core/poi_fun/pixel_based_point_finder.py
Strategies¶
TPTBox.core.poi_fun.strategies
¶
strategy_extreme_points
¶
strategy_extreme_points(poi: POI, current_subreg: NII, location: Location, direction: Sequence[DIRECTIONS] | DIRECTIONS, vert_id: int, subreg_id: Location | list[Location], bb: tuple[slice, slice, slice] | None, log: Logger_Interface = _log) -> None
Strategy: place a POI at the anatomically extreme voxel of a subregion.
Extracts the label(s) subreg_id from current_subreg, finds the
voxel most extreme in direction relative to the vertebra orientation,
and stores the result in poi.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
current_subreg
|
NII
|
Cropped subregion |
required |
location
|
Location
|
Target |
required |
direction
|
Sequence[DIRECTIONS] | DIRECTIONS
|
Anatomical direction(s) to optimise. Accepts a single
direction string, a sequence of strings, or weighted
|
required |
vert_id
|
int
|
Vertebra identifier (integer label). |
required |
subreg_id
|
Location | list[Location]
|
Label(s) to extract from |
required |
bb
|
tuple[slice, slice, slice] | None
|
Bounding-box slices mapping local to global voxel coordinates. |
required |
log
|
Logger_Interface
|
Logger for warning and error messages. |
_log
|
Source code in TPTBox/core/poi_fun/strategies.py
strategy_line_cast
¶
strategy_line_cast(poi: POI, vert_id: int, current_subreg: NII, location: Location, start_point: Location | ndarray, regions_loc: list[Location] | Location, normal_vector_points: tuple[Location, Location] | DIRECTIONS, bb: tuple[slice, slice, slice] | None, log: Logger_Interface = _log) -> None
Strategy: place a POI at the ray-cast exit point of a subregion surface.
Extracts the combined label mask for regions_loc, then casts a ray
from start_point along normal_vector_points using
:func:~TPTBox.core.poi_fun.ray_casting.max_distance_ray_cast_convex_poi
and stores the resulting boundary coordinate in poi.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
vert_id
|
int
|
Vertebra identifier (integer label). |
required |
current_subreg
|
NII
|
Cropped subregion |
required |
location
|
Location
|
Target |
required |
start_point
|
Location | ndarray
|
Ray origin — a |
required |
regions_loc
|
list[Location] | Location
|
Label(s) to extract from |
required |
normal_vector_points
|
tuple[Location, Location] | DIRECTIONS
|
Ray direction, given either as a |
required |
bb
|
tuple[slice, slice, slice] | None
|
Bounding-box slices mapping local to global voxel coordinates. |
required |
log
|
Logger_Interface
|
Logger for warning and error messages. |
_log
|
Source code in TPTBox/core/poi_fun/strategies.py
strategy_find_corner
¶
strategy_find_corner(poi: POI, current_subreg: NII, vert_id: int, bb: tuple[slice, slice, slice], location: Location, vec1: Location, vec2: Location, start_point: Location | ndarray = Location.Vertebra_Corpus, log: Logger_Interface = _log, shift_direction: DIRECTIONS | None = None) -> None
Strategy: place a POI at the corner of a vertebral body defined by two direction vectors.
Shifts the start point laterally if shift_direction is given, then
uses a 2-D bisection search within the vertebral body (Vertebra_Corpus
and Vertebra_Corpus_border) to find the furthest reachable corner in
the directions of vec1 and vec2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
current_subreg
|
NII
|
Cropped subregion |
required |
vert_id
|
int
|
Vertebra identifier (integer label). |
required |
bb
|
tuple[slice, slice, slice]
|
Bounding-box slices mapping local to global voxel coordinates. |
required |
location
|
Location
|
Target |
required |
vec1
|
Location
|
First direction |
required |
vec2
|
Location
|
Second direction |
required |
start_point
|
Location | ndarray
|
Ray origin — a |
Vertebra_Corpus
|
log
|
Logger_Interface
|
Logger for warning and error messages. |
_log
|
shift_direction
|
DIRECTIONS | None
|
Optional lateral shift direction applied to
|
None
|
Source code in TPTBox/core/poi_fun/strategies.py
strategy_ligament_attachment_point_flava
¶
strategy_ligament_attachment_point_flava(poi: POI, current_subreg: NII, vert_id: int, bb: tuple[slice, slice, slice], location: Location, goal: Location | ndarray, log: Logger_Interface = _log, delta: float = 1e-07, shift_direction: Literal['S', 'I'] = 'S', dir2: Literal['A'] = 'A') -> None
Strategy: place a ligamentum-flavum attachment POI on the arcus / spinosus surface.
Starting from the spinosus process (or arcus if spinosus is absent),
performs a 2-D bisection search moving in shift_direction and dir2
while staying inside the arcus/spinosus mask and approaching goal.
The search stops when the bracket width falls below delta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
current_subreg
|
NII
|
Cropped subregion |
required |
vert_id
|
int
|
Vertebra identifier (integer label). |
required |
bb
|
tuple[slice, slice, slice]
|
Bounding-box slices mapping local to global voxel coordinates. |
required |
location
|
Location
|
Target |
required |
goal
|
Location | ndarray
|
Target |
required |
log
|
Logger_Interface
|
Logger for warning and error messages. |
_log
|
delta
|
float
|
Convergence threshold for the bisection loop. Defaults to 0.0000001. |
1e-07
|
shift_direction
|
Literal['S', 'I']
|
Primary direction of search along the arcus
( |
'S'
|
dir2
|
Literal['A']
|
Secondary search direction (always anterior, |
'A'
|
Source code in TPTBox/core/poi_fun/strategies.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
strategy_shifted_line_cast
¶
strategy_shifted_line_cast(poi: POI, current_subreg: NII, location: Location, vert_id: int, bb: tuple[slice, slice, slice] | None, regions_loc: list[Location] | Location, normal_vector_points: tuple[Location, Location] | DIRECTIONS, start_point: Location = Location.Vertebra_Corpus, log: Logger_Interface = _log, direction: DIRECTIONS = 'R', do_shift: bool = True) -> None
Strategy: lateral-shift a start point then ray-cast to the subregion surface.
Optionally displaces start_point laterally by a vertebra-width fraction
via :func:~TPTBox.core.poi_fun.ray_casting.shift_point, then delegates to
:func:strategy_line_cast to find the boundary along normal_vector_points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poi
|
POI
|
|
required |
current_subreg
|
NII
|
Cropped subregion |
required |
location
|
Location
|
Target |
required |
vert_id
|
int
|
Vertebra identifier (integer label). |
required |
bb
|
tuple[slice, slice, slice] | None
|
Bounding-box slices mapping local to global voxel coordinates. |
required |
regions_loc
|
list[Location] | Location
|
Label(s) to extract from |
required |
normal_vector_points
|
tuple[Location, Location] | DIRECTIONS
|
Ray direction — a |
required |
start_point
|
Location
|
Ray origin before shifting. Defaults to
|
Vertebra_Corpus
|
log
|
Logger_Interface
|
Logger for warning and error messages. |
_log
|
direction
|
DIRECTIONS
|
Lateral direction for the shift ( |
'R'
|
do_shift
|
bool
|
When |
True
|
Source code in TPTBox/core/poi_fun/strategies.py
Save / Load¶
TPTBox.core.poi_fun.save_load
¶
save_poi
¶
save_poi(poi: POI | POI_Global, out_path: Path | str, make_parents=False, additional_info: dict | None = None, resample_reference: Has_Grid | None = None, verbose: logging = True, save_hint=2) -> None
Saves the POIs to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out_path
|
Path | str
|
The path where the JSON file will be saved. |
required |
make_parents
|
bool
|
If True, create any necessary parent directories for the output file. Defaults to False. |
False
|
verbose
|
bool
|
If True, print status messages to the console. Defaults to True. |
True
|
save_hint
|
0 Default, 1 Gruber, 2 POI (readable), 10 ISO-POI (outdated) |
2
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any of the POIs have an invalid type. |
Example
POIs = Centroids(...) POIs.save("output/POIs.json")
Source code in TPTBox/core/poi_fun/save_load.py
load_poi
¶
Load POIs from a file or a BIDS file object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctd_path
|
Centroid_Reference
|
Path to a file or BIDS file object from which to load POIs. Alternatively, it can be a tuple containing the following items: - vert: str, the name of the vertebra. - subreg: str, the name of the subregion. - ids: list[int | Location], a list of integers and/or Location objects used to filter the POIs. |
required |
Returns:
| Type | Description |
|---|---|
POI | POI_Global
|
A Centroids object containing the loaded POIs. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in TPTBox/core/poi_fun/save_load.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |