Skip to content

Commit

Permalink
Added validator (and test) to ensure input is 2D
Browse files Browse the repository at this point in the history
  • Loading branch information
b-peri committed Aug 22, 2024
1 parent bd1228f commit 10342e9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions movement/analysis/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def compute_head_direction_vector(
raise log_error(
ValueError, "The left and right keypoints may not be identical."
)
if len(data.space) != 2:
raise log_error(
ValueError,
"Input data must have 2 (and only 2) spatial dimensions, but "
f"currently has {len(data.space)}.",
)

# Select the right and left keypoints
head_left = data.sel(keypoints=left_keypoint, drop=True)
Expand Down
27 changes: 24 additions & 3 deletions tests/test_unit/test_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_compute_head_direction_vector(mock_dataset):
are computed from a basic mock dataset.
"""
# Test that validators work
with pytest.raises(TypeError):
with pytest.raises(TypeError): # Catch incorrect datatype
np_array = [
[[[1, 0], [-1, 0], [0, -1]]],
[[[0, 1], [0, -1], [1, 0]]],
Expand All @@ -47,17 +47,38 @@ def test_compute_head_direction_vector(mock_dataset):
np_array, "left_ear", "right_ear"
)

with pytest.raises(AttributeError):
with pytest.raises(AttributeError): # Catch incorrect dimensions
mock_dataset_keypoint = mock_dataset.sel(keypoints="nose", drop=True)
navigation.compute_head_direction_vector(
mock_dataset_keypoint, "left_ear", "right_ear"
)

with pytest.raises(ValueError):
with pytest.raises(ValueError): # Catch identical left and right keypoints
navigation.compute_head_direction_vector(
mock_dataset, "left_ear", "left_ear"
)

with pytest.raises(ValueError): # Catch incorrect spatial dimensions
time = np.array([0])
individuals = np.array(["individual_0"])
keypoints = np.array(["left", "right"])
space = np.array(["x", "y", "z"])

ds = xr.DataArray(
[
[[[1, 0, 0], [-1, 0, 0]]],
],
dims=["time", "individuals", "keypoints", "space"],
coords={
"time": time,
"individuals": individuals,
"keypoints": keypoints,
"space": space,
},
)

navigation.compute_head_direction_vector(ds, "left", "right")

# Test that output contains correct datatype, dimensions, and values
head_vector = navigation.compute_head_direction_vector(
mock_dataset, "left_ear", "right_ear"
Expand Down

0 comments on commit 10342e9

Please sign in to comment.