Skip to content

Commit

Permalink
removing count_consecutive_true + fixed types in duration_consecutive…
Browse files Browse the repository at this point in the history
…_true
  • Loading branch information
BaptisteVandecrux committed May 13, 2024
1 parent 705de5f commit 00666bb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/pypromice/process/L1toL2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
40 changes: 5 additions & 35 deletions src/pypromice/qc/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand All @@ -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.
"""
Expand Down

0 comments on commit 00666bb

Please sign in to comment.