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

Reorganise test fixtures #380

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

Reorganise test fixtures #380

wants to merge 31 commits into from

Conversation

lochhh
Copy link
Collaborator

@lochhh lochhh commented Jan 20, 2025

Description

What is this PR

  • Bug fix
  • Addition of a new feature
  • Other

Why is this PR needed?
This PR closes #336 and #222.

What does this PR do?
This PR

  • modularises fixtures in conftest.py by grouping these as either data.py, files.py, or helpers.py in tests/fixtures/; where data contains fixtures for valid and invalid movement datasets and arrays, dataframes, and the movement_dataset_asserts.
  • ensures file fixtures returning file paths are suffixed with "_file" to enable devs to easily identify file fixtures and search for existing ones
  • prefixes VIA tracks-related file content fixtures with via_
  • replaces the old and simpler valid_poses_dataset, valid_poses_dataset_with_nan, valid_position_array fixtures with the newer uniform_linear_motion fixtures and updates the affected tests
  • updates poses_with_nan fixture to match that of bboxes (i.e. both centroids have nans at the same time points)
  • ensures data fixtures containing nans are consistently suffixed with _with_nan (before we had a mix of _with_nans and _with_nan)
  • groups tests using the same "parametrize" into Classes, e.g. TestComputeKinematics, TestFilteringValidDataset
  • removes unused fixtures
  • standardises the "individuals" IDs across fixtures to start from id_0 (before, poses ids start from id_1); semi-related note: movement by default sets "individuals" names to begin with "individual_0" if none are provided upon import.

I haven't changed the vector-related tests in test_kinematics.py as there is ongoing work in PR #385.

References

#336 #222

How has this PR been tested?

Tests passing

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 Jan 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.79%. Comparing base (22fe3e1) to head (510489a).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #380   +/-   ##
=======================================
  Coverage   99.79%   99.79%           
=======================================
  Files          14       14           
  Lines         969      969           
=======================================
  Hits          967      967           
  Misses          2        2           

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

@niksirbi niksirbi linked an issue Jan 21, 2025 that may be closed by this pull request
@niksirbi
Copy link
Member

FYI @lochhh, @sfmig has arrived at a nice structure in ethology, by storing fixtures in separate files, see this PR.

I don't know of you were planning to go this deep into reorganising, leaving this reference here just in case its useful.

@lochhh lochhh requested a review from sfmig January 24, 2025 18:00
@lochhh lochhh marked this pull request as ready for review January 24, 2025 18:00
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.

thanks @lochhh it looks fantastic!

I really like the consistency (such as the _file endings, the via_ prefixes, with_nan suffixes etc), it makes a lot of it much more readable. I followed some of these ideas and suggested a few invalid_ prefixes for some dataset-related fixtures.

The only larger thing is that I had a go at moving the movement_dataset_asserts fixture to be under the helpers fixtures, since the class that wrapped it only had that one method - it's here, but you can revert if you disagree.

Re PR #385, since that is now merged, if you want to deal with that in this PR I am happy to review any additions. Otherwise we can deal with it separately in another PR/issue.

tests/conftest.py Show resolved Hide resolved
tests/conftest.py Show resolved Hide resolved
tests/fixtures/data.py Outdated Show resolved Hide resolved
tests/fixtures/data.py Outdated Show resolved Hide resolved
tests/fixtures/data.py Outdated Show resolved Hide resolved
* expected_n_nans_in_position
@pytest.mark.parametrize(
"window",
[3, 5, 6, 10], # data is nframes = 10
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
[3, 5, 6, 10], # data is nframes = 10
[3, 5, 6, 10], # input data has 10 frames

- id_1 moves along x=-y line from the origin
- they both move one unit (pixel) along each axis in each frame
class TestComputeKinematics:
"""Test ``compute_[kinematic_variable]`` with valid and invalid inputs."""
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"""Test ``compute_[kinematic_variable]`` with valid and invalid inputs."""
"""Test ``compute_[kinematic_variable]`` with valid and invalid inputs.
When valid, the expected kinematics are a uniform linear motion case.
Uniform linear motion means the individuals move along a line at constant
velocity.
We consider 2 individuals ("id_0" and "id_1"), tracked for 10 frames,
along x and y:
- Individual 0 ("id_0") moves along x=y line from the origin
- Individual 1 ("id_1") moves along x=-y line from the origin
- they both move one unit (pixel) along each axis in each frame
"""

I think this description may have got lost somewhere

"""Test kinematics computation with an invalid dataset."""
with expected_exception:
position = request.getfixturevalue(invalid_dataset).position
getattr(kinematics, f"compute_{kinematic_variable}")(position)


@pytest.mark.parametrize("order", [0, -1, 1.0, "1"])
Copy link
Contributor

Choose a reason for hiding this comment

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

Below a suggestion I realise that is out of scope for this PR - if you want it can be dealt separately. It involves:

  • renaming the test (it refers to the old name of the function I think)
  • include the expected exception in the parametrisation for readability
@pytest.mark.parametrize(
    "order, expected_exception",
    [
        (0, pytest.raises(ValueError)),
        (-1, pytest.raises(ValueError)),
        (1.0, pytest.raises(TypeError)),
        ("1", pytest.raises(TypeError)),
    ],
)
def test_time_derivative_with_invalid_order(order, expected_exception):
    """Test that an error is raised when the order is non-positive."""
    data = np.arange(10)
    with expected_exception:
        kinematics.compute_time_derivative(data, order=order)

tests/fixtures/data.py Outdated Show resolved Hide resolved
@niksirbi
Copy link
Member

Re PR #385, since that is now merged, if you want to deal with that in this PR I am happy to review any additions. Otherwise we can deal with it separately in another PR/issue.

Oops sorry, we merged it without looking here.
Whatever scheme you agree on should be also applied to the kinematics tests (after merging/rebasing from main).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants