Skip to content

Commit

Permalink
add jit compiling and make lower level filter method
Browse files Browse the repository at this point in the history
  • Loading branch information
jarq6c committed Jan 16, 2025
1 parent 925f654 commit e22ee66
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
20 changes: 11 additions & 9 deletions python/events/src/hydrotools/events/baseflow/eckhardt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import numpy as np
import numpy.typing as npt
from numba import jit, float64

def linear_recession_analysis(
series: npt.ArrayLike,
Expand Down Expand Up @@ -67,8 +68,9 @@ def maximum_baseflow_analysis(
"""
return 0.5

@jit(float64[:](float64[:], float64, float64), nogil=True)
def separate_baseflow(
series: npt.ArrayLike,
series: npt.NDArray,
recession_constant: float,
maximum_baseflow_index: float
) -> npt.NDArray:
Expand All @@ -77,8 +79,8 @@ def separate_baseflow(
Parameters
----------
series: array-like, required
An array of streamflow values. Assumes first value in series is baseflow.
series: array-type, required
A numpy array of streamflow values. Assumes first value in series is baseflow.
recession_constant: float, required
Linear reservoir recession constant, a, from Eckhardt (2005, 2008).
maximum_baseflow_index: float
Expand All @@ -96,10 +98,10 @@ def separate_baseflow(

# Instantiate baseflow series
# Assume first value is baseflow
streamflow = np.asarray(series)
baseflow = np.empty(len(series))
baseflow[0] = streamflow[0]
for i in range(1, len(series)):
baseflow[i] = A * baseflow[i-1] + B * streamflow[i]
baseflow = np.empty(series.size)
baseflow[0] = series[0]

return np.minimum(baseflow, streamflow)
# Apply filter and return result
for i in range(1, len(series)):
baseflow[i] = min(series[i], A * baseflow[i-1] + B * series[i])
return baseflow
14 changes: 5 additions & 9 deletions python/events/tests/test_baseflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ def test_maximum_baseflow_analysis():

def test_separate_baseflow():
rng = np.random.default_rng()
s = rng.normal(100.0, 10.0, 1000)
s = rng.normal(100.0, 10.0, 100)

# Test numpy
from time import perf_counter
start = perf_counter()
b = bf.separate_baseflow(s, 0.9, 0.5)
assert b[0] == s[0]

# Test list
b = bf.separate_baseflow(s.tolist(), 0.9, 0.5)
assert b[0] == s[0]

# Test pandas
b = bf.separate_baseflow(pd.Series(s), 0.9, 0.5)
end = perf_counter()
print(f"{end-start:.6f} s")
assert b[0] == s[0]

0 comments on commit e22ee66

Please sign in to comment.