1
+ from contextlib import nullcontext as does_not_raise
2
+
1
3
import numpy as np
2
4
import pytest
3
5
import xarray as xr
@@ -46,24 +48,56 @@ def _expected_dataarray(property):
46
48
47
49
return _expected_dataarray
48
50
49
- def test_displacement (self , valid_pose_dataset , expected_dataarray ):
51
+ kinematic_test_params = [
52
+ ("valid_pose_dataset" , does_not_raise ()),
53
+ ("valid_pose_dataset_with_nan" , does_not_raise ()),
54
+ ("missing_dim_dataset" , pytest .raises (ValueError )),
55
+ ]
56
+
57
+ @pytest .mark .parametrize ("ds, expected_exception" , kinematic_test_params )
58
+ def test_displacement (
59
+ self , ds , expected_exception , expected_dataarray , request
60
+ ):
50
61
"""Test displacement computation."""
51
- result = kinematics .compute_displacement (
52
- valid_pose_dataset .pose_tracks
53
- )
54
- xr .testing .assert_allclose (result , expected_dataarray ("displacement" ))
62
+ ds = request .getfixturevalue (ds )
63
+ with expected_exception :
64
+ result = kinematics .compute_displacement (ds .pose_tracks )
65
+ expected = expected_dataarray ("displacement" )
66
+ if ds .pose_tracks .isnull ().any ():
67
+ expected .loc [
68
+ {"individuals" : "ind1" , "time" : [3 , 4 , 7 , 8 , 9 ]}
69
+ ] = np .nan
70
+ xr .testing .assert_allclose (result , expected )
55
71
56
- def test_velocity (self , valid_pose_dataset , expected_dataarray ):
72
+ @pytest .mark .parametrize ("ds, expected_exception" , kinematic_test_params )
73
+ def test_velocity (
74
+ self , ds , expected_exception , expected_dataarray , request
75
+ ):
57
76
"""Test velocity computation."""
58
- result = kinematics .compute_velocity (valid_pose_dataset .pose_tracks )
59
- xr .testing .assert_allclose (result , expected_dataarray ("velocity" ))
77
+ ds = request .getfixturevalue (ds )
78
+ with expected_exception :
79
+ result = kinematics .compute_velocity (ds .pose_tracks )
80
+ expected = expected_dataarray ("velocity" )
81
+ if ds .pose_tracks .isnull ().any ():
82
+ expected .loc [
83
+ {"individuals" : "ind1" , "time" : [2 , 4 , 6 , 7 , 8 , 9 ]}
84
+ ] = np .nan
85
+ xr .testing .assert_allclose (result , expected )
60
86
61
- def test_acceleration (self , valid_pose_dataset , expected_dataarray ):
87
+ @pytest .mark .parametrize ("ds, expected_exception" , kinematic_test_params )
88
+ def test_acceleration (
89
+ self , ds , expected_exception , expected_dataarray , request
90
+ ):
62
91
"""Test acceleration computation."""
63
- result = kinematics .compute_acceleration (
64
- valid_pose_dataset .pose_tracks
65
- )
66
- xr .testing .assert_allclose (result , expected_dataarray ("acceleration" ))
92
+ ds = request .getfixturevalue (ds )
93
+ with expected_exception :
94
+ result = kinematics .compute_acceleration (ds .pose_tracks )
95
+ expected = expected_dataarray ("acceleration" )
96
+ if ds .pose_tracks .isnull ().any ():
97
+ expected .loc [
98
+ {"individuals" : "ind1" , "time" : [1 , 3 , 5 , 6 , 7 , 8 , 9 ]}
99
+ ] = np .nan
100
+ xr .testing .assert_allclose (result , expected )
67
101
68
102
@pytest .mark .parametrize ("order" , [0 , - 1 , 1.0 , "1" ])
69
103
def test_approximate_derivative_with_invalid_order (self , order ):
@@ -74,13 +108,3 @@ def test_approximate_derivative_with_invalid_order(self, order):
74
108
)
75
109
with pytest .raises (expected_exception ):
76
110
kinematics .compute_approximate_derivative (data , order = order )
77
-
78
- def test_compute_with_missing_time_dimension (
79
- self , missing_dim_dataset , kinematic_property
80
- ):
81
- """Test that computing a property of a pose dataset with
82
- missing 'time' dimension raises the appropriate error."""
83
- with pytest .raises (ValueError ):
84
- eval (f"kinematics.compute_{ kinematic_property } " )(
85
- missing_dim_dataset
86
- )
0 commit comments