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

Simplify and expand kinematic tests for bboxes #265

Merged
merged 35 commits into from
Sep 6, 2024

Conversation

sfmig
Copy link
Contributor

@sfmig sfmig commented Aug 7, 2024

Description

What is this PR

  • Bug fix
  • Addition of a new feature
  • Other

Why is this PR needed?
To ensure our existing kinematics methods play nicely with bboxes datasets.

What does this PR do?

  • Simplifies kinematics tests
    • Refactored tests for displacement, velocity and acceleration into a single parametrised test for computing kinematic variables
    • Separated tests for valid and invalid datasets
    • Added a test for a simple motion for which kinematics can be easily derived.
    • Removed some hardcoded bits from the fixtures
  • Expands kinematics tests to valid and invalid bboxes datasets
  • Renames internal method for explicitness _compute_approximate_derivative ---> _compute_approximate_time_derivative
    • the reasoning being this makes it a bit more clear to developers and contributors how our wrapper to compute the approximate derivate and xarray's method differentiate are distinct. With differentiate you can compute the derivative along any single-coordinate dimension, not necessarily time.

Question

I am not sure if it existed or we were aware of it when we implemented these methods. If we decide to use it, we may need to change the tests, so that we continue to compute the expected kinematic variables with an 'independent' method.

The simplest way would be to check the kinematic variables for a very simple motion case, for which the velocity, acceleration and displacement are very easy to infer / derive by hand. In a way this is similar to the tests we currently have, except that we can more easily infer which values are expected. I added an example test like that called test_kinematics_uniform_linear_motion for reference, but right now it takes only the valid bboxes dataset.

References

Overflow from #246

How has this PR been tested?

Tests pass locally and in CI.

Is this a breaking change?

No.

Does this PR require an update to the documentation?

I updated the docstrings for the kinematics module so that the API reference is no longer specific to poses datasets.

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

Copy link

codecov bot commented Aug 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.77%. Comparing base (a98ff45) to head (fad3257).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #265   +/-   ##
=======================================
  Coverage   99.77%   99.77%           
=======================================
  Files          14       14           
  Lines         883      883           
=======================================
  Hits          881      881           
  Misses          2        2           

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

@sfmig sfmig marked this pull request as ready for review August 8, 2024 12:56
@sfmig sfmig requested a review from lochhh August 8, 2024 12:56
@sfmig sfmig changed the title Simplify and expand kinematic tests Simplify and expand kinematic tests for bboxes Aug 8, 2024
@sfmig sfmig mentioned this pull request Aug 8, 2024
6 tasks
@lochhh
Copy link
Collaborator

lochhh commented Aug 9, 2024

Cool! I wasn't aware of xr.differentiate(), which is much simpler in code for our use case:

result = data.differentiate("time")

vs.

result = xr.apply_ufunc(
               np.gradient,
               data,
               data.coords["time"].values,
               kwargs={"axis": 0},
         )
result = result.reindex_like(data)

And in both cases, we no longer need to assume equidistant time-spacing. (Opened #268 for this)

tests/test_unit/test_kinematics.py Show resolved Hide resolved
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
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
@lochhh lochhh mentioned this pull request Aug 12, 2024
7 tasks
@sfmig sfmig linked an issue Aug 27, 2024 that may be closed by this pull request
@sfmig sfmig force-pushed the smg/extend-kinematics-tests-bboxes branch from b0c42a7 to 963d423 Compare August 28, 2024 09:55
@sfmig sfmig force-pushed the smg/extend-kinematics-tests-bboxes branch from 5ba43ba to 9ba34a9 Compare August 28, 2024 15:52
@sfmig
Copy link
Contributor Author

sfmig commented Aug 29, 2024

thanks for the review @lochhh. A summary of the main changes below.

In the tests:

  • I added a fixture for a poses dataset with 2 individuals, each one with 3 keypoints, moving in a linear uniform motion.
  • The name of the added fixture is valid_poses_dataset_uniform_linear_motion, which doesn't really match the equivalent bboxes fixture but I didn't want to mess up existing tests that use valid_poses_dataset or valid_bboxes_dataset. I leave that to the refactoring fixtures PR (issue Reorganising pytest fixtures #222 ) if that is alright.
  • I added a test for datasets with nans.

In the docstrings of the kinematics functions:

  • I used monospace formatting for when we refer to arrays in a movement dataset.
  • I removed (x,y) from cartesian, as the functions should work just fine if the trajectory is in 3D (x,y,z).
  • I replaced some references to "vector" by the term "array" where possible, else I clarified the relationship between them (e.g., a position array is made up of position vectors across time). Let me know if this is clear enough.
  • I specified that the input position arrays should be in cartesian (even though this is not enforced in the implementation for now).
  • I added Notes to clarify what would happen when different position arrays are fed to each function. I am not sure if this is overkill / helpful, maybe we should remove it. I don't feel strongly either way.

Will re request review now

@sfmig sfmig marked this pull request as ready for review August 30, 2024 18:58
@sfmig sfmig mentioned this pull request Aug 30, 2024
7 tasks
Copy link
Collaborator

@lochhh lochhh left a comment

Choose a reason for hiding this comment

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

Thanks so much for this @sfmig ! ❤️ the restructured tests! I'm happy to have the additional notes in the Notes section - I guess people can read or ignore. Mostly minor comments and suggestions, feel free to take or leave. I also tried to avoid multiple assertions in a for loop where possible, as this makes the code inefficient to debug - the test will fail and terminate on the first failure and we'd need to re-run the test to uncover issues one-by-one.

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
movement/analysis/kinematics.py Outdated Show resolved Hide resolved
movement/analysis/kinematics.py Outdated Show resolved Hide resolved
tests/conftest.py Outdated Show resolved Hide resolved
tests/test_unit/test_kinematics.py Show resolved Hide resolved
tests/test_unit/test_kinematics.py 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
Copy link

sonarcloud bot commented Sep 6, 2024

@sfmig sfmig added this pull request to the merge queue Sep 6, 2024
Merged via the queue into main with commit 95965f8 Sep 6, 2024
14 of 15 checks passed
@sfmig sfmig deleted the smg/extend-kinematics-tests-bboxes branch September 6, 2024 09:10
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.

Extend tests to cover bounding boxes datasets
2 participants