Skip to content

Commit c5c32c3

Browse files
committed
raise error when select on bad patch coord
1 parent 018d807 commit c5c32c3

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

dascore/proc/coords.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
from dascore.constants import PatchType, select_values_description
1212
from dascore.core.coords import BaseCoord
13-
from dascore.exceptions import CoordError, ParameterError, PatchError
13+
from dascore.exceptions import (
14+
CoordError,
15+
ParameterError,
16+
PatchCoordinateError,
17+
PatchError,
18+
)
1419
from dascore.utils.docs import compose_docstring
1520
from dascore.utils.misc import get_parent_code_name, iterate
1621
from dascore.utils.patch import patch_function
@@ -497,6 +502,15 @@ def select(
497502
See [`Patch.order`](`dascore.Patch.order`).
498503
499504
"""
505+
# Check for and raise on invalid kwargs.
506+
if invalid_coords := set(kwargs) - set(patch.coords.coord_map):
507+
invalid_list = sorted(invalid_coords)
508+
valid_list = sorted(patch.coords.coord_map)
509+
msg = (
510+
f"Coordinate(s) {invalid_list} not found in patch coordinates: {valid_list}"
511+
)
512+
raise PatchCoordinateError(msg)
513+
500514
new_coords, data = patch.coords.select(
501515
**kwargs,
502516
array=patch.data,

tests/test_proc/test_proc_coords.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
CoordError,
1515
ParameterError,
1616
PatchBroadcastError,
17+
PatchCoordinateError,
1718
PatchError,
1819
)
1920
from dascore.units import get_quantity
@@ -321,6 +322,33 @@ def test_patch_non_coord(self, random_patch):
321322
new = patch.select(face_angle=(face_angle.min(), face_angle.max()))
322323
assert new == patch
323324

325+
def test_select_nonexistent_coordinate_raises_error(self, random_patch):
326+
"""
327+
Test that selecting on a non-existing coordinate
328+
raises PatchCoordinateError.
329+
"""
330+
# Try to select on a coordinate that doesn't exist
331+
with pytest.raises(PatchCoordinateError, match="nonexistent_coord"):
332+
random_patch.select(nonexistent_coord=(0, 10))
333+
334+
def test_select_multiple_nonexistent_coordinates_raises_error(self, random_patch):
335+
"""
336+
Test that selecting on multiple non-existing coordinates raises
337+
PatchCoordinateError.
338+
"""
339+
# Try to select on multiple coordinates that don't exist
340+
with pytest.raises(PatchCoordinateError, match="coord1.*coord2"):
341+
random_patch.select(bad_coord1=(0, 10), bad_coord2=(5, 15))
342+
343+
def test_select_mix_valid_invalid_coordinates_raises_error(self, random_patch):
344+
"""
345+
Test that mixing valid and invalid coordinates raises
346+
PatchCoordinateError.
347+
"""
348+
# Try to select on a mix of valid and invalid coordinates
349+
with pytest.raises(PatchCoordinateError, match="invalid_coord"):
350+
random_patch.select(time=(0, 1), invalid_coord=(0, 10))
351+
324352

325353
class TestOrder:
326354
"""Tests for ordering Patches."""

0 commit comments

Comments
 (0)