Skip to content

Commit c6071f6

Browse files
authored
Merge pull request #370 from EIT-ALIVE/fix_filter_nan_values
Add exception when trying to filter data with NaN-values
2 parents bf8a284 + a75680a commit c6071f6

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

eitprocessing/filters/butterworth_filters.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from dataclasses import InitVar, dataclass
23
from typing import Literal
34

@@ -142,6 +143,15 @@ def apply_filter(self, input_data: npt.ArrayLike, axis: int = -1) -> np.ndarray:
142143
Returns:
143144
The filtered output with the same shape as the input data.
144145
"""
146+
if np.any(np.isnan(input_data)):
147+
msg = "Input data contains NaN-values."
148+
exc = ValueError(msg)
149+
if sys.version_info >= (3, 11):
150+
exc.add_note(
151+
"Butterworth filters can't handle data containing NaN-values. "
152+
"You can fill in gaps using `np.nan_to_num(...)` or interpolation."
153+
)
154+
raise exc
145155
sos = signal.butter(
146156
N=self.order,
147157
Wn=self.cutoff_frequency,

tests/test_butterworth_filter.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,14 @@ def compare_filters(
286286
signal_,
287287
axis,
288288
)
289+
290+
291+
def test_nan_values(filter_arguments: dict):
292+
filter_ = ButterworthFilter(**filter_arguments)
293+
data = np.random.default_rng().random(1000)
294+
295+
_ = filter_.apply_filter(data)
296+
297+
data[100] = np.nan
298+
with pytest.raises(ValueError):
299+
_ = filter_.apply_filter(data)

0 commit comments

Comments
 (0)