Skip to content
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

Compute Head Direction Vector #276

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

b-peri
Copy link
Collaborator

@b-peri b-peri commented Aug 22, 2024

Description

This PR introduces the navigation module and adds compute_head_direction_vector() - a function for calculating an animal's 2D head direction vector - to movement. As described in #238, this function takes two keypoints (corresponding to the left and right of the head) and computes the vector perpendicular to the line connecting them, pointing in the animal's rostral direction.

Since compute_head_direction_vector() expects the input dataset to have specific dimensions (and specific keypoints), I've taken some notes from the kinematics module here and added some validation to the beginning of the function. I expect that some of this will be factored out into a separate function as we continue to add to the navigation module, but I've kept everything under the main function itself for the time being.

What is this PR

  • Bug fix
  • Addition of a new feature
  • Other

References

Checklist:

  • The code has been tested locally
  • Tests have been added to cover all new functionality
  • The documentation has been updated to reflect any changes
  • The code has been formatted with pre-commit

@b-peri b-peri requested review from niksirbi and sfmig August 22, 2024 13:24
Copy link

codecov bot commented Aug 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.77%. Comparing base (644c1b1) to head (98312d9).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #276   +/-   ##
=======================================
  Coverage   99.77%   99.77%           
=======================================
  Files          14       14           
  Lines         884      904   +20     
=======================================
+ Hits          882      902   +20     
  Misses          2        2           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@niksirbi niksirbi left a comment

Choose a reason for hiding this comment

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

Thanks @b-peri! This is well implemented and well tested with the known values.

Some thoughts (apart from the things I point out in individual comments):

  1. would be nice to expand the tests a bit by checking for what happens if there is a NaN value. For example, you can make one of the ears NaN for a particular timepoint and test whether the head vector for that exact timepoint (and no other) is NaN.
  2. As we discussed today, you may move the new funciton to the kinematics module (anticipating our planned removal of the analysis level.
  3. Once you compelete the above, it would be good to use this function within the polar vectors example. For that you'd need to modify the code to use your function for computing the head vector (instead of midpoint + snout) and update the plots accordingly. I think it's worth doing that in this PR, as that example will serve as the perfect documentation for the new feature. Btw, becasue the example scripts are broken down into cells (via # %%), you can run them independently via ipython, which is helpful for debugging and editing that script. Let me know if you need help with that.

movement/analysis/navigation.py Outdated Show resolved Hide resolved
movement/analysis/navigation.py Outdated Show resolved Hide resolved
movement/analysis/navigation.py Outdated Show resolved Hide resolved
movement/analysis/navigation.py Outdated Show resolved Hide resolved
movement/analysis/navigation.py Outdated Show resolved Hide resolved
tests/test_unit/test_navigation.py Outdated Show resolved Hide resolved
tests/test_unit/test_navigation.py Outdated Show resolved Hide resolved
tests/test_unit/test_navigation.py Outdated Show resolved Hide resolved
tests/test_unit/test_navigation.py Outdated Show resolved Hide resolved
tests/test_unit/test_navigation.py Outdated Show resolved Hide resolved
Copy link
Contributor

@sfmig sfmig left a comment

Choose a reason for hiding this comment

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

sorry for the delay @b-peri !

From the longer discussion in zulip, I detail here what I think would be the simplest implementation.

I also added some comments about the tests, let me know if you have any questions. The basic idea is to split them based on their goals (e.g. split them if they test the error-catching, the behaviour with nans, or the 'ideal' behaviour) and parametrise them when possible.

Let me know if you'd like to discuss anything offline or if you have questions!

movement/analysis/kinematics.py Outdated Show resolved Hide resolved
@@ -161,6 +163,94 @@ def _compute_approximate_time_derivative(
return result


def compute_head_direction_vector(
Copy link
Contributor

@sfmig sfmig Sep 10, 2024

Choose a reason for hiding this comment

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

Suggested change
def compute_head_direction_vector(
def compute_2D_head_direction_vector(

or something of the like to make it clear that we are focusing on the top-down / bottom-up view case?

Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be more general than head direction?

It would work to compute any forward direction perpendicular to a line connecting two symmetrical points about the sagittal plane, right? For example, we could compute a forward vector that is always perpendicular to the line connecting both hips.

Maybe it's worth renaming?

movement/analysis/kinematics.py Outdated Show resolved Hide resolved
movement/analysis/kinematics.py Outdated Show resolved Hide resolved
movement/analysis/kinematics.py Outdated Show resolved Hide resolved
tests/test_unit/test_kinematics.py Outdated Show resolved Hide resolved
tests/test_unit/test_kinematics.py Outdated Show resolved Hide resolved
tests/test_unit/test_kinematics.py Outdated Show resolved Hide resolved
tests/test_unit/test_kinematics.py Outdated Show resolved Hide resolved
tests/test_unit/test_kinematics.py Outdated Show resolved Hide resolved
@lochhh
Copy link
Collaborator

lochhh commented Sep 16, 2024

Quality Gate Passed Quality Gate passed

Issues 3 New issues 0 Accepted issues

Measures 0 Security Hotspots 0.0% Coverage on New Code 0.0% Duplication on New Code

See analysis details on SonarCloud

Hey @b-peri , just a note to also remove any uppercase letters in function names.

@b-peri
Copy link
Collaborator Author

b-peri commented Sep 16, 2024

Quality Gate Passed Quality Gate passed

Issues 3 New issues 0 Accepted issues
Measures 0 Security Hotspots 0.0% Coverage on New Code 0.0% Duplication on New Code
See analysis details on SonarCloud

Hey @b-peri , just a note to also remove any uppercase letters in function names.

Hi @lochhh, thanks for the heads-up, that's my bad! The function names are fixed now!

Copy link

sonarcloud bot commented Sep 17, 2024

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.

Compute Head Direction and Angular Head Velocity
4 participants