Skip to content
Snippets Groups Projects
Commit d9636c53 authored by Peter Lünenschloß's avatar Peter Lünenschloß
Browse files

fixed tests

parent ebd1dc6c
No related branches found
No related tags found
1 merge request!850Horizontal axis rolling
......@@ -42,7 +42,9 @@ class RollingMixin:
"""
Calculate a rolling-window function on the data.
Note, that the data gets assigned the worst flag present in the original data.
Note, that the new data gets assigned the worst flag present in the window it was aggregated from.
Note, That you also can select multiple fields to get a rolling calculation over those.
Parameters
----------
......@@ -178,9 +180,24 @@ def _hroll(
center: bool = True,
**kwargs,
):
if isinstance(window, str):
freq = getFreqDelta(data[field].to_pandas().index)
if freq is None:
raise ValueError(
f"Rolling over more than one column is only supported if either the data has a unitary"
f'sampling rate, or window is an Integer. "{window}" was passed and combined {field} '
f"index is not unitarily sampled"
)
else:
window = int(np.floor(pd.Timedelta(window) / freq))
views = np.lib.stride_tricks.sliding_window_view(
data[field].to_pandas(), (window, len(field))
)
f_views = np.lib.stride_tricks.sliding_window_view(
pd.DataFrame({f: flags[f] for f in field}), (window, len(field))
)
frame = pd.DataFrame(
views.reshape(views.shape[0], views.shape[1] * views.shape[2] * views.shape[3])
)
......@@ -191,20 +208,12 @@ def _hroll(
insuff_periods_mask = ~(~frame.isna()).sum(axis=1) >= min_periods
result[insuff_periods_mask] = np.nan
out = pd.Series(np.nan, index=data[field].to_pandas().index)
out[window - 1 :] = result
if center:
out = out.shift(-window // 2)
f_views = np.lib.stride_tricks.sliding_window_view(
pd.DataFrame({f: flags[f] for f in field}), (window, len(field))
)
f_result = f_views.max(axis=(2, 3)).squeeze()
d_out = pd.Series(np.nan, index=data[field].to_pandas().index)
d_out[window - 1 :] = result
if center:
d_out = d_out.shift(-window // 2)
d_out = d_out.shift(-int(np.floor(window / 2)))
f_out = pd.Series(np.nan, index=data[field].to_pandas().index)
f_out[window - 1 :] = f_result
......
......@@ -20,6 +20,27 @@ from saqc.lib.ts_operators import linearInterpolation, polynomialInterpolation
from tests.fixtures import char_dict, course_3, course_5 # noqa, todo: fix fixtures
@pytest.mark.parametrize(
("window", "center", "expected"),
[
(1, True, [3, 2, 3, 2]),
(2, False, [np.nan, 5, 5, 5]),
(3, True, [np.nan, 8, 7, np.nan]),
("20min", True, [5, 5, 5, np.nan]),
],
)
def test_multivariateRolling(window, center, expected):
data = pd.DataFrame(
{"a": [1, np.nan, 3, 4], "b": [1, 2, 3, 4], "c": [1, 2, 3, np.nan]},
index=pd.date_range("2000", periods=4, freq="10min"),
)
qc = saqc.SaQC(data)
qc = qc.rolling(
["a", "b", "c"], func="count", target="count", window=window, center=center
)
assert np.array_equal(qc.data["count"].values, expected, equal_nan=True)
def test_rollingInterpolateMissing(course_5):
data, characteristics = course_5(periods=10, nan_slice=[5, 6])
field = data.columns[0]
......
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