-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
|
||
def KGE(simulations, evaluation): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
b[0] = b_LH[0] | ||
for i in range(Q.shape[0] - 1): | ||
b[i + 1] = (1 - e) * b[i] + e * Q[i + 1] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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]) / ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
# first pass | ||
b[0] = Q[0] / 2 | ||
for i in range(Q.shape[0] - 1): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
inN = np.ceil(2 * N) | ||
if np.mod(inN, 2) == 0: | ||
inN = np.ceil(2 * N) - 1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
n = 0 | ||
for i in range(idx_turn[0], idx_turn[-1] + 1): | ||
if i == idx_turn[n + 1]: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
b[0] = b_LH[0] | ||
v = (1 - w) * (1 - a) / (2 * w) | ||
for i in range(Q.shape[0] - 1): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
def moving_average(x, w): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
@program.command() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
self.use_median = use_median | ||
self.water_year = water_year | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
else: | ||
flag = 0 | ||
if nnp[group_name] > 0: | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return 0 | ||
|
||
def DL19(self): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return stat | ||
|
||
def DH2(self): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
ndf = pd.DataFrame() | ||
for indx, df in enumerate(table): | ||
|
@@ -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", | ||
|
There was a problem hiding this comment.
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:inline-immediately-returned-variable
)This removes the following comments ( why? ):