Skip to content

Commit

Permalink
Raise errors for invalid rolling windows arguments (#280)
Browse files Browse the repository at this point in the history
Raise a specific error if no spacing or shape is passed to
rolling_window(). Raise a specific error if window size is larger than
the smaller dimension of the region. Add tests for these new methods
that include matching error messages to identify which error has been
raised.
  • Loading branch information
santisoler authored Sep 11, 2020
1 parent b807423 commit 8302ca3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions verde/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,11 +1015,21 @@ def rolling_window(
[20. 20. 20. 20. 20. 20. 20. 20. 20.]
"""
# Check if shape or spacing were passed
if shape is None and spacing is None:
raise ValueError("Either a shape or a spacing must be provided.")
# Select the coordinates after checking to make sure indexing will still
# work on the ignored coordinates.
coordinates = check_coordinates(coordinates)[:2]
if region is None:
region = get_region(coordinates)
# Check if window size is bigger than the minimum dimension of the region
region_min_width = min(region[1] - region[0], region[3] - region[2])
if region_min_width < size:
raise ValueError(
"Window size '{}' is larger ".format(size)
+ "than dimensions of the region '{}'.".format(region)
)
# Calculate the region spanning the centers of the rolling windows
window_region = [
dimension + (-1) ** (i % 2) * size / 2 for i, dimension in enumerate(region)
Expand Down
34 changes: 34 additions & 0 deletions verde/tests/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,40 @@ def test_rolling_window_warnings():
assert str(userwarnings[-1].message).split()[0] == "Rolling"


def test_rolling_window_no_shape_or_spacing():
"""
Check if error is raise if no shape or spacing is passed
"""
coords = grid_coordinates((-5, -1, 6, 10), spacing=1)
err_msg = "Either a shape or a spacing must be provided."
with pytest.raises(ValueError, match=err_msg):
rolling_window(coords, size=2)


def test_rolling_window_oversized_window():
"""
Check if error is raised if size larger than region is passed
"""
oversize = 5
regions = [
(-5, -1, 6, 20), # window larger than west-east
(-20, -1, 6, 10), # window larger than south-north
(-5, -1, 6, 10), # window larger than both dims
]
for region in regions:
coords = grid_coordinates(region, spacing=1)
# The expected error message with regex
# (the long expression intends to capture floats and ints)
float_regex = r"[+-]?([0-9]*[.])?[0-9]+"
err_msg = (
r"Window size '{}' is larger ".format(float_regex)
+ r"than dimensions of the region "
+ r"'\({0}, {0}, {0}, {0}\)'.".format(float_regex)
)
with pytest.raises(ValueError, match=err_msg):
rolling_window(coords, size=oversize, spacing=2)


def test_spacing_to_shape():
"Check that correct spacing and region are returned"
region = (-10, 0, 0, 5)
Expand Down

0 comments on commit 8302ca3

Please sign in to comment.