Skip to content

BUG: get_neighborhood_at_voxel treated the radius as a flattened kernel - #963

Open
Srizdebnath wants to merge 1 commit into
ANTsX:mainfrom
Srizdebnath:fix/neighborhood-at-voxel-radius
Open

Srizdebnath wants to merge 1 commit into
ANTsX:mainfrom
Srizdebnath:fix/neighborhood-at-voxel-radius

Conversation

@Srizdebnath

Copy link
Copy Markdown

get_neighborhood_at_voxel documents kernel as either a radius per axis or a binary array the shape of the neighborhood, but neither works:

import ants
img = ants.image_read(ants.get_ants_data("r16"))

r = ants.get_neighborhood_at_voxel(img, (100, 100), (3, 3))
print(r["values"].shape)   # (2,): expected 7 x 7 = 49

r = ants.get_neighborhood_at_voxel(img, (100, 100), (2, 2))
print(r["indices"])        # [[100. 100.] [132. 2213.]]: second row is garbage

The Python wrapper passes the radius list straight to getNeighborhood as the flattened kernel mask and computes radius = (k-1)/2 from it.

  • (3, 3) becomes a 3x3 neighborhood (radius 1), and only the first two offsets are returned, because the "mask" has two entries.
  • (2, 2) becomes radius 0, a 1-voxel neighborhood. The C++ loop then asks the iterator for offset 1, outside the neighborhood, so the second value and index are read from out-of-range memory.
  • A binary kernel ndarray is turned into a nested list with tolist(). It is then rejected by the length check, or, when its first axis happens to equal the image dimension, misread as a radius.

This follows ANTsR's getNeighborhoodAtVoxel:

  • Radius: expanded into a (2*radius+1) hypercube of ones. It must be non-negative integers, one per dimension.
  • Binary kernel: any array with image.dimension dimensions and odd sizes. It is flattened in Fortran order, which is the order the ITK neighborhood iterator walks offsets (first axis fastest). I checked this with a one-voxel asymmetric kernel.

center is unchanged. It is 1-based, like ANTsR, because the C++ code subtracts 1, and the returned indices are 1-based too. The docstring now says so.

Two new tests in tests/test_utils.py compare the returned values against the numpy array for a 2-D radius, a 3-D radius, a zero radius, a cross-shaped kernel and a one-voxel asymmetric kernel, and check the new ValueErrors. Both fail on main and pass here. The rest of test_utils.py passes.

A radius such as (3, 3) was passed to the C++ code as the kernel mask
and turned into radius (k-1)/2, so it returned only len(kernel) voxels
of a smaller neighborhood. Radii where (k-1)/2 gives a neighborhood with
fewer voxels than the list length, e.g. (2, 2), read past the end of the
kernel. Binary kernel arrays were converted with tolist() and rejected
or misread as a radius.

Expand a radius into a (2*radius+1) hypercube of ones, as ANTsR does,
and flatten binary kernel arrays in the order the ITK neighborhood
iterator walks them.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant