Handle null vectors explicitly in convert_to_unit for Cartesian coordinates#900
Open
swagat-mishra28 wants to merge 3 commits into
Open
Handle null vectors explicitly in convert_to_unit for Cartesian coordinates#900swagat-mishra28 wants to merge 3 commits into
swagat-mishra28 wants to merge 3 commits into
Conversation
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Closes #899
Summary
While reviewing
convert_to_unit()inmovement/utils/vector.py, I noticed that the Cartesian branch relies on implicit division-by-zero behaviour when encountering null vectors.For vectors with norm = 0, the operation
produces
NaNvalues due to NumPy division-by-zero. However, this behaviour is implicit, whereas the polar-coordinate branch explicitly handles null vectors by assigningNaN.This PR makes the Cartesian branch consistent with the polar branch by explicitly handling null vectors.
Changes
compute_norm(data)NaNto vectors whose norm is zero usingxr.whereThis avoids relying on implicit division-by-zero behaviour and makes the logic clearer and more robust.
Testing
I ran the full test suite locally:
pytest --ignore=tests/test_unit/test_napari_plugin
Result:
The change does not affect existing behaviour and remains fully compatible with the current test suite.
Moreover, it improves clarity and consistency between Cartesian and polar coordinate handling when dealing with null vectors, making the behaviour explicit instead of relying on NumPy's division-by-zero behaviour.