Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored main branch #1

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/hydrotoolbox/baseflow/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ def strict_baseflow(Q):
# 4. Flow data followed by a data point with a larger value of -dQ/dt.
wet4 = np.concatenate([[True], dQdt[1:] - dQdt[:-1] < 0, [True, True]])

# Dry points, namely strict baseflow.
dry = ~(wet1 + wet2 + wet3 + wet4)

return dry
return ~(wet1 + wet2 + wet3 + wet4)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function strict_baseflow refactored with the following changes:

This removes the following comments ( why? ):

# Dry points, namely strict baseflow.



def KGE(simulations, evaluation):
Expand Down Expand Up @@ -73,7 +70,4 @@ def KGE(simulations, evaluation):
beta = np.sum(simulations, axis=0, dtype=np.float64) / (
np.sum(evaluation, axis=0, dtype=np.float64) + 1e-10
)
# calculate the Kling-Gupta Efficiency KGE
kge_ = 1 - np.sqrt((r - 1) ** 2 + (alpha - 1) ** 2 + (beta - 1) ** 2)

return kge_
return 1 - np.sqrt((r - 1) ** 2 + (alpha - 1) ** 2 + (beta - 1) ** 2)
Comment on lines -76 to +73
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function KGE refactored with the following changes:

This removes the following comments ( why? ):

# calculate the Kling-Gupta Efficiency KGE

5 changes: 1 addition & 4 deletions src/hydrotoolbox/baseflow/methods/CM.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ def CM(Q, b_LH, a, return_exceed=False):
Q (np.array): streamflow
a (float): recession coefficient
"""
if return_exceed:
b = np.zeros(Q.shape[0] + 1)
else:
b = np.zeros(Q.shape[0])
b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CM refactored with the following changes:

b[0] = b_LH[0]
for i in range(Q.shape[0] - 1):
b[i + 1] = a / (2 - a) * b[i] + (1 - a) / (2 - a) * Q[i + 1]
Expand Down
5 changes: 1 addition & 4 deletions src/hydrotoolbox/baseflow/methods/EWMA.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ def EWMA(Q, b_LH, e, return_exceed=False):
Q (np.array): streamflow
e (float): smoothing parameter
"""
if return_exceed:
b = np.zeros(Q.shape[0] + 1)
else:
b = np.zeros(Q.shape[0])
b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function EWMA refactored with the following changes:

b[0] = b_LH[0]
for i in range(Q.shape[0] - 1):
b[i + 1] = (1 - e) * b[i] + e * Q[i + 1]
Expand Down
5 changes: 1 addition & 4 deletions src/hydrotoolbox/baseflow/methods/Eckhardt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ def Eckhardt(Q, b_LH, a, BFImax, return_exceed=False):
a (float): recession coefficient
BFImax (float): maximum value of baseflow index (BFI)
"""
if return_exceed:
b = np.zeros(Q.shape[0] + 1)
else:
b = np.zeros(Q.shape[0])
b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Eckhardt refactored with the following changes:

b[0] = b_LH[0]
for i in range(Q.shape[0] - 1):
b[i + 1] = ((1 - BFImax) * a * b[i] + (1 - a) * BFImax * Q[i + 1]) / (
Expand Down
5 changes: 1 addition & 4 deletions src/hydrotoolbox/baseflow/methods/Furey.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ def Furey(Q, b_LH, a, A, return_exceed=False):
a (float): recession coefficient
A (float): calibrated in baseflow.param_estimate
"""
if return_exceed:
b = np.zeros(Q.shape[0] + 1)
else:
b = np.zeros(Q.shape[0])
b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Furey refactored with the following changes:

b[0] = b_LH[0]
for i in range(Q.shape[0] - 1):
b[i + 1] = (a - A * (1 - a)) * b[i] + A * (1 - a) * Q[i]
Expand Down
6 changes: 1 addition & 5 deletions src/hydrotoolbox/baseflow/methods/LH.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ def LH(Q, beta=0.925, return_exceed=False):
Q (np.array): streamflow
beta (float): filter parameter, 0.925 recommended by (Nathan & McMahon, 1990)
"""
if return_exceed:
b = np.zeros(Q.shape[0] + 1)
else:
b = np.zeros(Q.shape[0])

b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LH refactored with the following changes:

# first pass
b[0] = Q[0] / 2
for i in range(Q.shape[0] - 1):
Expand Down
5 changes: 1 addition & 4 deletions src/hydrotoolbox/baseflow/methods/Local.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ def hysep_interval(area):
# and A is the drainage area in square miles (Linsley and others, 1982, p. 210).
# The interval 2N* used for hydrograph separations is the odd integer between
# 3 and 11 nearest to 2N (Pettyjohn and Henning, 1979, p. 31).
if area is None:
N = 5
else:
N = np.power(0.3861022 * area, 0.2)
N = 5 if area is None else np.power(0.3861022 * area, 0.2)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function hysep_interval refactored with the following changes:

inN = np.ceil(2 * N)
if np.mod(inN, 2) == 0:
inN = np.ceil(2 * N) - 1
Expand Down
6 changes: 1 addition & 5 deletions src/hydrotoolbox/baseflow/methods/UKIH.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ def UKIH_turn(Q, idx_min):


def linear_interpolation(Q, idx_turn, return_exceed=False):
if return_exceed:
b = np.zeros(Q.shape[0] + 1)
else:
b = np.zeros(Q.shape[0])

b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function linear_interpolation refactored with the following changes:

n = 0
for i in range(idx_turn[0], idx_turn[-1] + 1):
if i == idx_turn[n + 1]:
Expand Down
5 changes: 1 addition & 4 deletions src/hydrotoolbox/baseflow/methods/Willems.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ def Willems(Q, b_LH, a, w, return_exceed=False):
w (float): case-specific average proportion of the quick flow
in the streamflow, calibrated in baseflow.param_estimate
"""
if return_exceed:
b = np.zeros(Q.shape[0] + 1)
else:
b = np.zeros(Q.shape[0])
b = np.zeros(Q.shape[0] + 1) if return_exceed else np.zeros(Q.shape[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Willems refactored with the following changes:

b[0] = b_LH[0]
v = (1 - w) * (1 - a) / (2 * w)
for i in range(Q.shape[0] - 1):
Expand Down
4 changes: 1 addition & 3 deletions src/hydrotoolbox/baseflow/param_estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@ def maxmium_BFI(Q, b_LH, a, date=None):
idx_end = b.shape[0] // 365 * 365
annual_b = np.mean(b[:idx_end].reshape(-1, 365), axis=1)
annual_Q = np.mean(Q[:idx_end].reshape(-1, 365), axis=1)
annual_BFI = annual_b / annual_Q
else:
idx_year = date.Y - date.Y.min()
counts = np.bincount(idx_year)
idx_valid = counts > 0
annual_b = np.bincount(idx_year, weights=b)[idx_valid] / counts[idx_valid]
annual_Q = np.bincount(idx_year, weights=Q)[idx_valid] / counts[idx_valid]
annual_BFI = annual_b / annual_Q

annual_BFI = annual_b / annual_Q
Comment on lines -76 to +82
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function maxmium_BFI refactored with the following changes:

BFI_max = np.max(annual_BFI)
BFI_max = BFI_max if BFI_max < 0.9 else np.sum(annual_b) / np.sum(annual_Q)
return BFI_max
Expand Down
6 changes: 1 addition & 5 deletions src/hydrotoolbox/baseflow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ def clean_streamflow(date, Q):


def clean_streamflow_jit(year, year_unique, Q):
year_delete = []
for y in year_unique:
if (Q[year == y] >= 0).sum() < 120:
year_delete.append(y)
return year_delete
return [y for y in year_unique if (Q[year == y] >= 0).sum() < 120]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function clean_streamflow_jit refactored with the following changes:



def moving_average(x, w):
Expand Down
5 changes: 1 addition & 4 deletions src/hydrotoolbox/hydrotoolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,10 +1402,7 @@ def indices(
lu[(sclass, None)] = lu[(sclass, None)].union(lu[(sclass, fcomp)])
lu[(None, fcomp)] = lu[(None, fcomp)].union(lu[(sclass, fcomp)])

hi = {}
for icode in indice_codes:
hi[icode] = getattr(indice_class, icode)()
return hi
return {icode: getattr(indice_class, icode)() for icode in indice_codes}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function indices refactored with the following changes:



@program.command()
Expand Down
56 changes: 24 additions & 32 deletions src/hydrotoolbox/indices/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@

class Indices:
def __init__(self, data, use_median=False, water_year="A-SEP"):
if isinstance(data, pd.DataFrame):
if len(data.columns) != 1:
raise ValueError(
tsutils.error_wrapper(
f"""
if isinstance(data, pd.DataFrame) and len(data.columns) != 1:
raise ValueError(
tsutils.error_wrapper(
f"""
Can only calculate indices on 1 series, you gave {len(data.columns)}.
"""
)
)
)
Comment on lines -10 to +17
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Indices.__init__ refactored with the following changes:


self.use_median = use_median
self.water_year = water_year
Expand Down Expand Up @@ -384,9 +383,9 @@ def _lf(self, thresh, than):
if eval(f"value {than} thresh"):
pdur = pdur + 1
flag = flag + 1
allnp[group_name] = allnp[group_name] + 1
allnp[group_name] += 1
if flag == 1:
nnp[group_name] = nnp[group_name] + 1
nnp[group_name] += 1
Comment on lines -387 to +388
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Indices._lf refactored with the following changes:

  • Replace assignment with augmented assignment [×2] (aug-assign)

else:
flag = 0
if nnp[group_name] > 0:
Expand Down Expand Up @@ -571,9 +570,7 @@ def DL18(self):
self.data[self.data == 0].groupby(pd.Grouper(freq=self.water_year)).count()
)
if any(stat):
if self.use_median is True:
return stat.median()
return stat.mean()
return stat.median() if self.use_median is True else stat.mean()
Comment on lines -574 to +573
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Indices.DL18 refactored with the following changes:

return 0

def DL19(self):
Expand All @@ -583,15 +580,11 @@ def DL19(self):
return stat.std() / stat.mean() * 100

def DL20(self):
stat = self.data_monthly_mean[self.data_monthly_mean == 0].count()
return stat
return self.data_monthly_mean[self.data_monthly_mean == 0].count()
Comment on lines -586 to +583
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Indices.DL20 refactored with the following changes:


def DH1(self):
stat = self.data_yearly.max()
if self.use_median:
stat = stat.median()
else:
stat = stat.mean()
stat = stat.median() if self.use_median else stat.mean()
Comment on lines -591 to +587
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Indices.DH1 refactored with the following changes:

return stat

def DH2(self):
Expand Down Expand Up @@ -701,18 +694,19 @@ def _pre_ta1_ta2(self):

lq[self.data == 0.0] = np.log10(0.01)

table = []
table.append(lq[lq < 0.1 * lma1])
table.append(lq[(lq >= 0.1 * lma1) & (lq < 0.25 * lma1)])
table.append(lq[(lq >= 0.25 * lma1) & (lq < 0.5 * lma1)])
table.append(lq[(lq >= 0.5 * lma1) & (lq < 0.75 * lma1)])
table.append(lq[(lq >= 0.75 * lma1) & (lq < lma1)])
table.append(lq[(lq >= lma1) & (lq < 1.25 * lma1)])
table.append(lq[(lq >= 1.25 * lma1) & (lq < 1.5 * lma1)])
table.append(lq[(lq >= 1.5 * lma1) & (lq < 1.75 * lma1)])
table.append(lq[(lq >= 1.75 * lma1) & (lq < 2.0 * lma1)])
table.append(lq[(lq >= 2.0 * lma1) & (lq < 2.25 * lma1)])
table.append(lq[(lq >= 2.25 * lma1)])
table = [
lq[lq < 0.1 * lma1],
lq[(lq >= 0.1 * lma1) & (lq < 0.25 * lma1)],
lq[(lq >= 0.25 * lma1) & (lq < 0.5 * lma1)],
lq[(lq >= 0.5 * lma1) & (lq < 0.75 * lma1)],
lq[(lq >= 0.75 * lma1) & (lq < lma1)],
lq[(lq >= lma1) & (lq < 1.25 * lma1)],
lq[(lq >= 1.25 * lma1) & (lq < 1.5 * lma1)],
lq[(lq >= 1.5 * lma1) & (lq < 1.75 * lma1)],
lq[(lq >= 1.75 * lma1) & (lq < 2.0 * lma1)],
lq[(lq >= 2.0 * lma1) & (lq < 2.25 * lma1)],
lq[lq >= 2.25 * lma1],
]
Comment on lines -704 to +709
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Indices._pre_ta1_ta2 refactored with the following changes:


ndf = pd.DataFrame()
for indx, df in enumerate(table):
Expand All @@ -727,9 +721,7 @@ def _pre_ta1_ta2(self):
axis="columns",
)
continue
ldf = []
for day in range(1, 366):
ldf.append(df[df.index.dayofyear == day].count())
ldf = [df[df.index.dayofyear == day].count() for day in range(1, 366)]
ndf = pd.concat(
[ndf, pd.DataFrame(data=ldf, index=range(1, 366), columns=[indx])],
axis="columns",
Expand Down