Skip to content

Add verification of close-to-bound voxel with map_coordinates - #1270

Open
EmmaRenauld wants to merge 3 commits into
scilus:masterfrom
EmmaRenauld:fix_issue_1102
Open

Add verification of close-to-bound voxel with map_coordinates#1270
EmmaRenauld wants to merge 3 commits into
scilus:masterfrom
EmmaRenauld:fix_issue_1102

Conversation

@EmmaRenauld

@EmmaRenauld EmmaRenauld commented Oct 31, 2025

Copy link
Copy Markdown
Contributor

In PR #1102, we changed the padding to nearest. Antoine suggested to add a verification for out-of-bound values. It was not done in PR1102 to keep it simple, but was added as issue #1206. This finishes PR1102.

@codecov

codecov Bot commented Oct 31, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.32258% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.61%. Comparing base (31cf4e5) to head (62d396f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1270      +/-   ##
==========================================
+ Coverage   72.59%   72.61%   +0.01%     
==========================================
  Files         301      301              
  Lines       26283    26304      +21     
  Branches     3700     3703       +3     
==========================================
+ Hits        19080    19100      +20     
+ Misses       5651     5650       -1     
- Partials     1552     1554       +2     
Flag Coverage Δ
smoketests 69.76% <80.64%> (+<0.01%) ⬆️
unittests 14.26% <64.51%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Scripts 75.32% <71.42%> (+<0.01%) ⬆️
Library 69.36% <95.83%> (+0.04%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

arnaudbore
arnaudbore previously approved these changes Oct 31, 2025

@arnaudbore arnaudbore left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick fix and LGTM

Comment thread src/scilpy/cli/scil_tractogram_assign_custom_color.py Outdated
Comment thread src/scilpy/cli/scil_viz_bundle_screenshot_mni.py Outdated

@AntoineTheb AntoineTheb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment. I would strongly recommend we actually deal with out of bounds coordinates in the function. Otherwise, we'll redo the same logic every time we call our own map_coordinates.

if (np.any(np.logical_or(points[0] < 0, points[0] > data.shape[0])) or
np.any(np.logical_or(points[1] < 0, points[1] > data.shape[1])) or
np.any(np.logical_or(points[2] < 0,points[2] > data.shape[2]))) :
logging.warning("Careful! You are interpolating outside of boundaries "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this as it deviates from the expected behavior of the function. I would instead try to match the expected behavior of map_coordinates and return a cval if the indices are actually out of bounds instead of logging a message (which may be swallowed).

@AntoineTheb

Copy link
Copy Markdown
Contributor
image

In this case, we would expect "E" to return an "out-of-bounds" value that the user can choose, not what is in the nearest voxel.

@EmmaRenauld

Copy link
Copy Markdown
Contributor Author

Summarizing because it has been a long time since we managed this:

We have an issue with 3D images (with voxels) when using interpolation with scipy. map_coordinates manages coordinates with a origin=center manner: the first voxel (coordinate [0, 0, 0]) spans from [-0.5, -0.5, -0.5] to [0.5, 0.5, 0.5]. But scipy also considers that everything between -0.5 and 0 is outside the image, and then scipy pads the outside of the image to 0 by default. Si if we want to find the image value there, we will always get 0 if default values as used.

With mode='nearest': (i.e. padding the outside values to the nearest voxel that is in the volume): we're ok.

So here: splitting the voxels on the limit (the half voxels all around) and using them with nearest. Other voxels are used as usual with user choices.

(I also reversed changes we made in #1102, where we added mode 'nearest' everywhere). Now it's only used for voxels on the limit.

@EmmaRenauld EmmaRenauld changed the title Add verification of out-of-bound with map_coordinates Add verification of close-to-bound voxel with map_coordinates Aug 4, 2026
@arnaudbore
arnaudbore requested a review from AntoineTheb August 27, 2026 18:57

@AntoineTheb AntoineTheb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to hammer on this, but I think there's still a problem with this function. IMO it should behave exactly as map_coordinate should, just with voxels instead of points, but if that's outside of the scope, it should at least let the user know if coordinates are out of bounds.

axis=0)

# Points correctly managed by scipy
good_points = ~limit_points

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I missed something, this will also include points that are actually outside the volume, not just at the border. These "bad" points should return a cval imo

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.

3 participants