New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
_streamlines_in_mask bounds check #2183
_streamlines_in_mask bounds check #2183
Conversation
Looks legit to me. Could you maybe add a test case along the lines of what you had in #2182 ? |
I have updated that based on my comment #2182 (comment) |
That's a good check to have as a general case. However, when calling |
Hi @jchoude,
I mean using a mask taken on a anatomical e.g. T1 image of the same subject in the same scanner that might have a smaller bounding box than the DWI image. The affine provided in the
This is what I try to fix with this PR. The points reaching In that way you can still pass streamlines that may completely or partially fall out of masks's shape to
I think that what was happening was worse than wraparound because when out of bounds indices were passed here for example it was even returning random values other than 0 and 1 that were only included in the mask array. I was even getting segmentation fault at some cases as I state in the issue. I think it was reading out of bounds as it is possible to do in C language and since it is Cython this is possible I guess. Do you think after the changes in this PR, out of bounds points still create an issue for this function? If so can you please provide me with an example? |
Hi! Any updates on that? |
@jchoude can you please suggest further steps ? |
In fact, the fix works. I was just wary of people getting edge cases when misusing the internal function. Code looks good to me. Just not sure why CICD is not working. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks good to me. I just would add these comments.
dipy/tracking/vox2track.pyx
Outdated
x = <cnp.npy_intp>floor(current_pt[0] + half_ratio * direction[0]) | ||
y = <cnp.npy_intp>floor(current_pt[1] + half_ratio * direction[1]) | ||
z = <cnp.npy_intp>floor(current_pt[2] + half_ratio * direction[2]) | ||
if x >= 0 and y >= 0 and z >= 0 and x < mask.shape[0] and y < mask.shape[1] and z < mask.shape[2]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be expressed in a more pythonic way as follows:
if 0 <= x < mask.shape[0] and 0 <= y < mask.shape[1] and 0 <= z < mask.shape[2]:
dipy/tracking/tests/test_utils.py
Outdated
(np.array([[1, -10, 0], [1, 10, 0]]), 1), | ||
]) | ||
def test_target_line_based_out_of_bounds(streamline, expected_matched): | ||
# Ensures https://github.com/dipy/dipy/issues/2182 doesn't happen |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please remove this comment?
dipy/tracking/tests/test_utils.py
Outdated
@@ -313,6 +314,24 @@ def _target(target_f, streamlines, voxel_both_true, voxel_one_true, | |||
assert_true(exclude[0] is streamlines[1]) | |||
|
|||
|
|||
@pytest.mark.parametrize("streamline, expected_matched", [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you can see, the rest of the test cases don't utilize this way to pass parameters, mainly because we don't want to have pytest as a dependency. Therefore, could you please change this and remove the import.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@takhs91 thanks for the update. LGTM!
Hi @takhs91, Can you rebase your PR to be sure that everything is ok? Thank you. After the rebase and If everything is ok, we will go ahead and merge it. Thank you |
222b013
to
66da528
Compare
Codecov Report
@@ Coverage Diff @@
## master #2183 +/- ##
==========================================
+ Coverage 89.37% 91.37% +2.00%
==========================================
Files 251 251
Lines 32507 32514 +7
Branches 3422 3423 +1
==========================================
+ Hits 29052 29709 +657
+ Misses 2736 2057 -679
- Partials 719 748 +29
|
Thank you very much @takhs91 for the rebase and the update. merging |
Closes #2182
Adds bound checking in _streamlines_in_mask when accessing the mask array.
I also changed it to explicitly return 1 or 0 for the final point to avoid returning unwanted values given in the mask array