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

increased soil moisture tests test coverage

parent 2319b322
No related branches found
No related tags found
No related merge requests found
...@@ -265,17 +265,17 @@ def flagSoilMoistureByConstantsDetection( ...@@ -265,17 +265,17 @@ def flagSoilMoistureByConstantsDetection(
plateau_window_min="12h", plateau_window_min="12h",
plateau_var_limit=0.0005, plateau_var_limit=0.0005,
rainfall_window_range="12h", rainfall_window_range="12h",
filter_window_size="3h", filter_window_size=None,
var_total_nans=np.inf, var_total_nans=np.inf,
var_consec_nans=np.inf, var_consec_nans=np.inf,
derivative_maximum_lb=0.0025, derivative_maximum_lb=0.0025,
derivative_minimum_ub=0, derivative_minimum_ub=0,
data_max_tolerance=0.95, data_max_tolerance=0.95,
smooth_poly_order=2,
**kwargs **kwargs
): ):
"""Function is not ready to use yet: we are waiting for response from the author of [Paper] in order of getting """
able to exclude some sources of confusion.
Note, function has to be harmonized to equidistant freq_grid Note, function has to be harmonized to equidistant freq_grid
...@@ -322,13 +322,27 @@ def flagSoilMoistureByConstantsDetection( ...@@ -322,13 +322,27 @@ def flagSoilMoistureByConstantsDetection(
# 2. extend forwards: # 2. extend forwards:
if period_diff > 0: if period_diff > 0:
cond1_sets = cond1_sets.replace(1, method='ffill', limit=period_diff) cond1_sets = cond1_sets.replace(1, method='ffill', limit=period_diff)
# get first derivative # get first derivative
if filter_window_size is None:
filter_window_size = 3 * pd.Timedelta(moist_rate)
else:
filter_window_size = pd.Timedelta(filter_window_size)
first_derivative = dataseries.diff() first_derivative = dataseries.diff()
# cumsum trick to seperate continous plateau groups from each other filter_window_seconds = filter_window_size.seconds
smoothing_periods = int(np.ceil((filter_window_seconds / moist_rate.n)))
first_derivate = savgol_filter(
dataseries,
window_length=smoothing_periods,
polyorder=smooth_poly_order,
deriv=1,
)
first_derivate = pd.Series(data=first_derivate, index=dataseries.index, name=dataseries.name)
# cumsumming to seperate continous plateau groups from each other:
group_counter = cond1_sets.cumsum() group_counter = cond1_sets.cumsum()
group_counter = group_counter[group_counter.diff() == 0] group_counter = group_counter[group_counter.diff() == 0]
group_counter.name = 'group_counter' group_counter.name = 'group_counter'
group_frame = pd.merge(group_counter, first_derivative, left_index=True, right_index=True, how='inner') group_frame = pd.merge(group_counter, first_derivate, left_index=True, right_index=True, how='inner')
group_frame = group_frame.groupby('group_counter') group_frame = group_frame.groupby('group_counter')
condition_passed = group_frame.filter( condition_passed = group_frame.filter(
lambda x: (x[field].max() >= derivative_maximum_lb) & (x[field].min() <= derivative_minimum_ub)) lambda x: (x[field].max() >= derivative_maximum_lb) & (x[field].min() <= derivative_minimum_ub))
......
...@@ -8,9 +8,10 @@ import pandas as pd ...@@ -8,9 +8,10 @@ import pandas as pd
from saqc.funcs.soil_moisture_tests import ( from saqc.funcs.soil_moisture_tests import (
flagSoilMoistureBySoilFrost, flagSoilMoistureBySoilFrost,
flagSoilMoistureByPrecipitationEvents, flagSoilMoistureByPrecipitationEvents,
flagSoilMoistureByConstantsDetection
) )
from test.common import TESTFLAGGER from test.common import TESTFLAGGER, initData
@pytest.mark.parametrize("flagger", TESTFLAGGER) @pytest.mark.parametrize("flagger", TESTFLAGGER)
...@@ -58,6 +59,22 @@ def test_flagSoilMoisturePrecipitationEvents(flagger): ...@@ -58,6 +59,22 @@ def test_flagSoilMoisturePrecipitationEvents(flagger):
test_sum = (flag_result[flag_assertion] == flagger.BAD).sum() test_sum = (flag_result[flag_assertion] == flagger.BAD).sum()
assert test_sum == len(flag_assertion) assert test_sum == len(flag_assertion)
@pytest.mark.parametrize("flagger", TESTFLAGGER)
def test_flagSoilMoistureByConstantsDetection(flagger):
data = initData(
1, start_date="2011-01-01 00:00:00", end_date="2011-01-02 00:00:00", freq="5min"
)
data.iloc[5:25] = 0
data.iloc[100:120] = data.max()[0]
field = data.columns[0]
flagger = flagger.initFlags(data)
data, flagger = flagSoilMoistureByConstantsDetection(data, field, flagger, plateau_window_min='1h',
rainfall_window_range='1h')
assert ~(flagger.isFlagged()[5:25]).all()[0]
assert (flagger.isFlagged()[100:120]).all()[0]
if __name__ == "__main__": if __name__ == "__main__":
flagger = TESTFLAGGER[2] flagger = TESTFLAGGER[2]
test_flagSoilMoistureBySoilFrost(flagger) test_flagSoilMoistureByConstantsDetection(flagger)
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