Skip to content
Snippets Groups Projects
Commit 119875ce authored by Bert Palm's avatar Bert Palm 🎇
Browse files

refactored _maxConsecutiveNan and added test.

parent ec62df83
No related branches found
No related tags found
1 merge request!462More tests
Pipeline #93416 passed with stage
in 2 minutes and 4 seconds
......@@ -139,9 +139,22 @@ def maxGap(in_arr):
@nb.njit
def _maxConsecutiveNan(arr, max_consec):
# checks if arr (boolean array) has not more then "max_consec" consecutive True
# values
def _exceedConsecutiveNanLimit(arr, max_consec):
"""
Check if array has more consecutive NaNs than allowed.
Parameters
----------
arr : bool array
boolean array
max_consec : int
maximum allowed consecutive `True`s
Returns
-------
exceeded: bool
True if more than allowed consecutive NaNs appear, False otherwise.
"""
current = 0
idx = 0
while idx < arr.size:
......@@ -149,10 +162,10 @@ def _maxConsecutiveNan(arr, max_consec):
current += 1
idx += 1
if current > max_consec:
return False
return True
current = 0
idx += 1
return True
return False
def validationTrafo(data, max_nan_total, max_nan_consec):
......@@ -160,22 +173,17 @@ def validationTrafo(data, max_nan_total, max_nan_consec):
# True-array of input array size for invalid input arrays False array for valid
# ones
data = data.copy()
if (max_nan_total is np.inf) & (max_nan_consec is np.inf):
if max_nan_total is np.inf and max_nan_consec is np.inf:
return data
# nan_mask = np.isnan(data)
if data.sum() <= max_nan_total:
if max_nan_consec is np.inf:
data[:] = False
return data
elif _maxConsecutiveNan(np.asarray(data), max_nan_consec):
data[:] = False
else:
data[:] = True
if data.sum() > max_nan_total:
value = True
elif max_nan_consec is np.inf:
value = False
else:
data[:] = True
value = _exceedConsecutiveNanLimit(np.asarray(data), max_nan_consec)
data[:] = value
return data
......@@ -257,7 +265,7 @@ def interpolateNANs(
except (NotImplementedError, ValueError):
warnings.warn(
f"Interpolation with method {method} is not supported at order "
f"{wrap_order}. and will be performed at order {wrap_order-1}"
f"{wrap_order}. and will be performed at order {wrap_order - 1}"
)
return _interpolWrapper(x, int(wrap_order - 1), wrap_method)
elif x.size < 3:
......
# SPDX-FileCopyrightText: 2021 Helmholtz-Zentrum für Umweltforschung GmbH - UFZ
#
# SPDX-License-Identifier: GPL-3.0-or-later
import numpy as np
import pytest
import saqc.lib.ts_operators as tsops
......@@ -10,9 +10,38 @@ import pandas as pd
def test_butterFilter():
assert (
tsops.butterFilter(pd.Series([1, -1] * 100), cutoff=0.1) - pd.Series([1, -1] * 100)
tsops.butterFilter(pd.Series([1, -1] * 100), cutoff=0.1)
- pd.Series([1, -1] * 100)
).mean() < 0.5
def test_identity():
assert tsops.identity(99) == 99
T = True
F = False
@pytest.mark.parametrize(
"arr,maxc,expected",
[
(np.array([]), 1, False),
(np.array([F]), 1, False),
(np.array([F, F, F]), 1, False),
#
(np.array([T]), 0, True),
(np.array([T]), 1, False),
#
(np.array([F, T, F]), 0, True),
(np.array([F, T, F]), 1, False),
#
(np.array([F, T, T, T, T, F]), 0, True),
(np.array([F, T, T, T, T, F]), 1, True),
(np.array([F, T, T, T, T, F]), 2, True),
(np.array([F, T, T, T, T, F]), 3, True),
(np.array([F, T, T, T, T, F]), 4, False),
(np.array([F, T, T, T, T, F]), 5, False),
#
(np.array([F, T, T, F, T, T, F]), 2, False),
],
)
def test__maxConsecutiveNan(arr, maxc, expected):
result = tsops._exceedConsecutiveNanLimit(arr, maxc)
assert result is expected
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment