Skip to content

Commit 3b6a7c4

Browse files
committed
use ValueError if moving_average window size is out of bounds
1 parent e2084c0 commit 3b6a7c4

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

ratapi/utils/plotting.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,6 @@ def validate_dens_type(dens_type: str | None, param: str):
11321132
results,
11331133
i,
11341134
smooth=smooth,
1135-
sigma=sigma,
11361135
estimated_density=estimated_density.get(i),
11371136
axes=ax,
11381137
**hist_settings,
@@ -1247,14 +1246,17 @@ def moving_average(data: np.ndarray, window_size: int = 8) -> list[float]:
12471246
computing an average over the elements within each window.
12481247
12491248
"""
1250-
assert 0 <= window_size <= len(data)
1249+
if not 0 <= window_size <= len(data):
1250+
raise ValueError(
1251+
"The moving average window size is out of range. Please change to a positive integer which "
1252+
"does not exceed the number of histogram bins."
1253+
)
12511254
moving_averages = []
12521255

12531256
for i in range(len(data)):
12541257
start_window_ind = floor(float(i - window_size / 2)) if i - window_size / 2 > 0 else 0
12551258
end_window_ind = floor(float(i + window_size / 2)) if i + window_size / 2 < len(data) else len(data)
12561259
window_average = np.sum(data[start_window_ind:end_window_ind]) / (end_window_ind + 0 - start_window_ind)
12571260
moving_averages.append(window_average)
1258-
i += 1
12591261

12601262
return moving_averages

tests/test_plotting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,8 @@ def test_moving_average() -> None:
548548
18.5,
549549
]
550550

551-
with pytest.raises(AssertionError):
551+
with pytest.raises(ValueError):
552552
RATplot.moving_average(data_to_average, window_size=-1)
553553

554-
with pytest.raises(AssertionError):
554+
with pytest.raises(ValueError):
555555
RATplot.moving_average(data_to_average, window_size=len(data_to_average) + 1)

0 commit comments

Comments
 (0)