Skip to content

Commit

Permalink
Simplify test_kinematics_uniform_linear_motion with suggestion from r…
Browse files Browse the repository at this point in the history
…eview
  • Loading branch information
sfmig committed Sep 2, 2024
1 parent 6136fe0 commit 61e8432
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions tests/test_unit/test_kinematics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pytest
import xarray as xr

from movement.analysis import kinematics

Expand All @@ -12,40 +13,40 @@
],
)
@pytest.mark.parametrize(
"kinematic_variable, expected_2D_array_per_individual_and_kpt",
"kinematic_variable, expected_kinematics",
[
(
"displacement",
{
0: np.vstack(
[np.zeros((1, 2)), np.ones((9, 2))]
), # at t=0 displacement is (0,0)
1: np.multiply(
[
np.vstack([np.zeros((1, 2)), np.ones((9, 2))]), # Individual 0
np.multiply(
np.vstack([np.zeros((1, 2)), np.ones((9, 2))]),
np.array([1, -1]),
),
},
), # Individual 1
],
),
(
"velocity",
{
0: np.ones((10, 2)),
1: np.multiply(np.ones((10, 2)), np.array([1, -1])),
},
[
np.ones((10, 2)), # Individual 0
np.multiply(
np.ones((10, 2)), np.array([1, -1])
), # Individual 1
],
),
(
"acceleration",
{
0: np.zeros((10, 2)),
1: np.zeros((10, 2)),
},
[
np.zeros((10, 2)), # Individual 0
np.zeros((10, 2)), # Individual 1
],
),
],
)
def test_kinematics_uniform_linear_motion(
valid_dataset_uniform_linear_motion,
kinematic_variable,
expected_2D_array_per_individual_and_kpt, # 2D: n_frames, n_space_dims
expected_kinematics, # 2D: n_frames, n_space_dims
request,
):
"""Test computed kinematics for a uniform linear motion case.
Expand All @@ -63,25 +64,30 @@ def test_kinematics_uniform_linear_motion(
(centroid, left, right), that are always in front of the centroid keypoint
at 45deg from the trajectory.
"""
# Compute kinematic array from input dataset
position = request.getfixturevalue(
valid_dataset_uniform_linear_motion
).position
kinematic_array = getattr(kinematics, f"compute_{kinematic_variable}")(
position
)

for ind in expected_2D_array_per_individual_and_kpt:
if "keypoints" in position.coords:
for k in range(position.coords["keypoints"].size):
assert np.allclose(
kinematic_array.isel(individuals=ind, keypoints=k).values,
expected_2D_array_per_individual_and_kpt[ind],
)
else:
assert np.allclose(
kinematic_array.isel(individuals=ind).values,
expected_2D_array_per_individual_and_kpt[ind],
)
# Build expected data array from the expected numpy array
expected_array = xr.DataArray(
np.stack(expected_kinematics, axis=1),
# Stack along the "individuals" axis
dims=["time", "individuals", "space"],
)
if "keypoints" in position.coords:
expected_array = expected_array.expand_dims(
{"keypoints": position.coords["keypoints"].size}
)
expected_array = expected_array.transpose(
"time", "individuals", "keypoints", "space"
)

# Compare the values of the kinematic_array against the expected_array
np.testing.assert_allclose(kinematic_array.values, expected_array.values)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 61e8432

Please sign in to comment.