Skip to content

Handle null vectors explicitly in convert_to_unit for Cartesian coordinates#900

Open
swagat-mishra28 wants to merge 3 commits into
neuroinformatics-unit:mainfrom
swagat-mishra28:fix-convert-to-unit-null
Open

Handle null vectors explicitly in convert_to_unit for Cartesian coordinates#900
swagat-mishra28 wants to merge 3 commits into
neuroinformatics-unit:mainfrom
swagat-mishra28:fix-convert-to-unit-null

Conversation

@swagat-mishra28
Copy link
Copy Markdown

Closes #899

Summary

While reviewing convert_to_unit() in movement/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

data / compute_norm(data)

produces NaN values due to NumPy division-by-zero. However, this behaviour is implicit, whereas the polar-coordinate branch explicitly handles null vectors by assigning NaN.

This PR makes the Cartesian branch consistent with the polar branch by explicitly handling null vectors.

Changes

  • Compute the vector norm first using compute_norm(data)
  • Perform normalization using the computed norm
  • Explicitly assign NaN to vectors whose norm is zero using xr.where

This 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:

  • 1027 tests passed
  • 0 failures

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.

@sonarqubecloud
Copy link
Copy Markdown

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.

Possible inconsistency in how convert_to_unit handles null vectors in Cartesian vs polar coordinates

1 participant