From 00666bb2edbce23a1575910c0118e40a6e7003d4 Mon Sep 17 00:00:00 2001 From: Baptiste Vandecrux <35140661+BaptisteVandecrux@users.noreply.github.com> Date: Mon, 13 May 2024 12:48:16 +0200 Subject: [PATCH] removing count_consecutive_true + fixed types in duration_consecutive_true --- src/pypromice/process/L1toL2.py | 2 +- src/pypromice/qc/persistence.py | 40 +++++---------------------------- 2 files changed, 6 insertions(+), 36 deletions(-) diff --git a/src/pypromice/process/L1toL2.py b/src/pypromice/process/L1toL2.py index 9b74f3ef..73ca1a7d 100644 --- a/src/pypromice/process/L1toL2.py +++ b/src/pypromice/process/L1toL2.py @@ -272,7 +272,7 @@ def smoothTilt(da: xr.DataArray, threshold=0.2): xarray.DataArray either X or Y smoothed tilt inclinometer measurements ''' - # we caluclate the moving standard deviation over a 3-day sliding window + # we calculate the moving standard deviation over a 3-day sliding window # hourly resampling is necessary to make sure the same threshold can be used # for 10 min and hourly data moving_std_gap_filled = da.to_series().resample('H').median().rolling( diff --git a/src/pypromice/qc/persistence.py b/src/pypromice/qc/persistence.py index e0a41830..20ceb60c 100644 --- a/src/pypromice/qc/persistence.py +++ b/src/pypromice/qc/persistence.py @@ -133,44 +133,14 @@ def count_consecutive_persistent_values( ) -> pd.Series: diff = data.ffill().diff().abs() # forward filling all NaNs! mask: pd.Series = diff < max_diff - # return count_consecutive_true(mask) return duration_consecutive_true(mask) -def count_consecutive_true( - series: Union[pd.Series, pd.DataFrame] -) -> Union[pd.Series, pd.DataFrame]: - """ - Convert boolean series to integer series where the values represent the number of connective true values. - - Examples - -------- - >>> count_consecutive_true(pd.Series([False, True, False, False, True, True, True, False, True])) - pd.Series([0, 1, 0, 0, 1, 2, 3, 0, 1]) - - Parameters - ---------- - series - Boolean pandas Series or DataFrame - - Returns - ------- - consecutive_true_count - Integer pandas Series or DataFrame with values representing the number of connective true values. - - """ - # assert series.dtype == bool - cumsum = series.cumsum() - is_first = series.astype("int").diff() == 1 - offset = (is_first * cumsum).replace(0, np.nan).fillna(method="ffill").fillna(0) - return ((cumsum - offset + 1) * series).astype("int") - - def duration_consecutive_true( - series: Union[pd.Series, pd.DataFrame] -) -> Union[pd.Series, pd.DataFrame]: + series: pd.Series, +) -> pd.Series: """ - Froma a boolean series, calculates the duration, in hours, of the periods with connective true values. + From a boolean series, calculates the duration, in hours, of the periods with connective true values. Examples -------- @@ -179,12 +149,12 @@ def duration_consecutive_true( Parameters ---------- - series + pd.Series Boolean pandas Series or DataFrame Returns ------- - consecutive_true_duration + pd.Series Integer pandas Series or DataFrame with values representing the number of connective true values. """