Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from sf_runoff_lt import create_it_matrix_lt
from climatology_ensemble import daily_climatology_p_et_ensemble
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import numpy as np
from sklearn.svm import SVR
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.compose import TransformedTargetRegressor
from sklearn.model_selection import GridSearchCV,TimeSeriesSplit
from sklearn.metrics import mean_squared_error, r2_score
from sf_runoff import smape
import pdb
import seaborn as sns
def nested_CV_SVR_predict(daily_input, C, eps, t_length, t_unit, n_splits, test_size, radius_for_ensemble,lt):
#compute the daily climatology and the quantile analysis
daily_clim = daily_climatology_p_et_ensemble(daily_input,t_unit,radius_for_ensemble)
#get the input-target matrix
it_matrix=create_it_matrix_lt(daily_input,t_length,t_unit, lt)
#split in train-test sets
tscv = TimeSeriesSplit(gap=t_unit ,n_splits=n_splits, test_size=test_size)
sets = tscv.split(it_matrix.index)
j=0;
prediction=pd.DataFrame(data=None)
for train_index, test_index in sets:
#set up training features
X = it_matrix.drop(columns='Q').iloc[train_index]
y = it_matrix['Q'].iloc[train_index]
#set up the model according to the parameters
svr_model_tuned = SVR(kernel='rbf', gamma='scale', C=C, epsilon=eps, cache_size=1000)
svr_model_tuned = make_pipeline(StandardScaler(),
TransformedTargetRegressor(regressor=svr_model_tuned, transformer=StandardScaler()))
#fit the model
svr_model_tuned.fit(X, y)
# get the test dates (end of the month)
test_dates = it_matrix.index[test_index]#[daily_input.index.is_month_end]
# and their day of the year
doy_test_dates = test_dates.day_of_year
# Save the true runoff values (with t_unit days rolling average)
target = {}
target['true_runoff'] = daily_input.Q.rolling(t_unit, min_periods=t_unit).mean().loc[test_dates]
# Compute runoff monthly climatology using the whole dataset
runoff_daily_clim = daily_input.Q.rolling(t_unit, min_periods=t_unit).mean()
target['runoff_clim'] = [runoff_daily_clim.loc[runoff_daily_clim.index.day_of_year == d].mean() for d in doy_test_dates]
X_trueTP = it_matrix.loc[test_dates, :].drop(columns='Q')
target['trueTP'] = svr_model_tuned.predict(X_trueTP)
# Predict using temperature and precipitation climatology
# predict also for 25th and 75th quantile situations.
X_climTP = X_trueTP.copy()
X_climTP_Q25=X_trueTP.copy()
X_climTP_Q75=X_trueTP.copy()
"""
#predict till 6 months of advance
lead_times = range(1,6)
for lt in lead_times:
# modify the X matrix by substituting the climatology to the real meteo vars for lt months.
change_dest = [c for c in X_climTP.columns if c.split('_')[1] == str(-lt + 1)]
change_source = [c.split('_')[0] for c in change_dest]
X_climTP.loc[:, change_dest]=daily_clim.loc[(test_dates-np.timedelta64(t_unit*(lt-1),'D')).day_of_year][change_source].values
#predict
target[f'climTP_lt{lt}'] = svr_model_tuned.predict(X_climTP)
# modify the X matrix by substituting the climatology to the extreme (25th and 75th quantiles) meteo vars for lt months.
change_source_25 = []
change_source_75 = []
#modify the source, by taking the daily climathological data referred to the quantiles situations
for i in change_source:
change_source_25.append(i+'_Q25')
change_source_75.append((i+'_Q75'))
X_climTP_Q25.loc[:, change_dest]=daily_clim.loc[(test_dates-np.timedelta64(t_unit*(lt-1),'D')).day_of_year][change_source_25].values
target[f'climTP_lt{lt}_Q25'] = svr_model_tuned.predict(X_climTP_Q25)
X_climTP_Q75.loc[:, change_dest]=daily_clim.loc[(test_dates-np.timedelta64(t_unit*(lt-1),'D')).day_of_year][change_source_75].values
target[f'climTP_lt{lt}_Q75'] = svr_model_tuned.predict(X_climTP_Q75)
#pdb.set_trace()
"""
target['split']= np.repeat(j,test_size)
#add this split prediction to the
prediction=prediction.append(pd.DataFrame(data=target, index=test_dates))
j=j+1
return prediction
def nested_CV_PCA_SVR_predict(daily_input, C, eps, n, t_length, t_unit, n_splits, test_size, radius_for_ensemble, lt):
#compute the daily climatology and the quantile analysis
daily_clim = daily_climatology_p_et_ensemble(daily_input,t_unit,radius_for_ensemble)
#get the input-target matrix
it_matrix=create_it_matrix_lt(daily_input,t_length,t_unit, lt)
#split in train-test sets
tscv = TimeSeriesSplit(gap=t_unit ,n_splits=n_splits, test_size=test_size)
sets = tscv.split(it_matrix.index)
j=0;
prediction=pd.DataFrame(data=None)
for train_index, test_index in sets:
#set up training features
X = it_matrix.drop(columns='Q').iloc[train_index]
y = it_matrix['Q'].iloc[train_index]
#set up the model according to the parameters
svr_model_tuned = SVR(kernel='rbf', gamma='scale', C=C, epsilon=eps, cache_size=1000)
svr_model_tuned = make_pipeline(StandardScaler(),
PCA(n_components=n),
TransformedTargetRegressor(regressor=svr_model_tuned, transformer=StandardScaler()))
#fit the model
svr_model_tuned.fit(X, y)
# get the test dates (end of the month)
test_dates = it_matrix.index[test_index]#[daily_input.index.is_month_end]
# and their day of the year
doy_test_dates = test_dates.day_of_year
# Save the true runoff values (with t_unit days rolling average)
target = {}
target['true_runoff'] = daily_input.Q.rolling(t_unit, min_periods=t_unit).mean().loc[test_dates]
# Compute runoff monthly climatology using the whole dataset
runoff_daily_clim = daily_input.Q.rolling(t_unit, min_periods=t_unit).mean()
#pdb.set_trace()
target['runoff_clim'] = [runoff_daily_clim.loc[runoff_daily_clim.index.day_of_year == d].mean() for d in doy_test_dates]
X_trueTP = it_matrix.loc[test_dates, :].drop(columns='Q')
target['trueTP'] = svr_model_tuned.predict(X_trueTP)
"""
# Predict using temperature and precipitation climatology
# predict also for 25th and 75th quantile situations.
X_climTP = X_trueTP.copy()
X_climTP_Q25=X_trueTP.copy()
X_climTP_Q75=X_trueTP.copy()
#predict till 6 months of advance
lead_times = range(1,6)
for lt in lead_times:
# modify the X matrix by substituting the climatology to the real meteo vars for lt months.
change_dest = [c for c in X_climTP.columns if c.split('_')[1] == str(-lt + 1)]
change_source = [c.split('_')[0] for c in change_dest]
X_climTP.loc[:, change_dest]=daily_clim.loc[(test_dates-np.timedelta64(t_unit*(lt-1),'D')).day_of_year][change_source].values
#predict
target[f'climTP_lt{lt}'] = svr_model_tuned.predict(X_climTP)
# modify the X matrix by substituting the climatology to the extreme (25th and 75th quantiles) meteo vars for lt months.
change_source_25 = []
change_source_75 = []
#modify the source, by taking the daily climathological data referred to the quantiles situations
for i in change_source:
change_source_25.append(i+'_Q25')
change_source_75.append((i+'_Q75'))
X_climTP_Q25.loc[:, change_dest]=daily_clim.loc[(test_dates-np.timedelta64(t_unit*(lt-1),'D')).day_of_year][change_source_25].values
target[f'climTP_lt{lt}_Q25'] = svr_model_tuned.predict(X_climTP_Q25)
X_climTP_Q75.loc[:, change_dest]=daily_clim.loc[(test_dates-np.timedelta64(t_unit*(lt-1),'D')).day_of_year][change_source_75].values
target[f'climTP_lt{lt}_Q75'] = svr_model_tuned.predict(X_climTP_Q75)
#pdb.set_trace()
"""
target['split']= np.repeat(j,test_size)
#add this split prediction to the
prediction = prediction.append(pd.DataFrame(data=target, index=test_dates))
#pdb.set_trace()
j=j+1
return prediction
def plot_prediction(prediction,t_unit):
splits=prediction['split'].max()
for i in range(splits+1):
query=f'split=={i}'
#query='split==' + str(i)
pred=prediction.query(query)
pred.loc[:,'date']= pred.index
ax,fig=plt.subplots(figsize=(20,10))
#plot the real
sns.lineplot(y=("true_runoff"),x="date",data=pred,color='red',linewidth=1.3,legend='auto')
sns.lineplot(y=("runoff_clim"),x="date",data=pred,color='yellow',linewidth=1.3,legend='auto')
sns.lineplot(y=("trueTP"),x="date",data=pred,color='green',linewidth=1.3,legend='auto')
"""
#plot the lead_time_
lt1=pred[["climTP_lt1","climTP_lt1_Q25","climTP_lt1_Q75"]]
#lt1.columns=np.repeat('climatologia_lt1_ensemple_prec',3)
sns.lineplot(data=lt1["climTP_lt1"],legend='auto')
plt.fill_between(x=lt1.index, y1=lt1['climTP_lt1_Q25'], y2=lt1['climTP_lt1_Q75'], alpha=0.2)
#plot the lead_time_
lt4=pred[["climTP_lt4","climTP_lt4_Q25","climTP_lt4_Q75"]]
#lt4.columns=np.repeat('climatologia_lt4_ensemple_prec',3)
sns.lineplot(data=lt4["climTP_lt4"], palette=['green'],legend='auto')
plt.fill_between(x=lt4.index, y1=lt4['climTP_lt4_Q25'], y2=lt4['climTP_lt4_Q75'], alpha=0.2)
"""
plt.ylabel('{t_unit} days discharge average [m^3/sec]')
plt.legend(['TRUE DISCHARGE', 'DISCHARGE CLIMATOLOGY', 'LEAD TIME = 0', 'LEAD TIME = 1'])
plt.title("Precipitation variability(Q= 25 and 75) mapped on the prediction discharge")
return;
def plot_anomalies(prediction, t_unit):
splits=prediction['split'].max()
for i in range(splits+1):
query=f'split=={i}'
#query='split==' + str(i)
pred=prediction.query(query)
pred.loc[:,'date']= pred.index
ax,fig=plt.subplots(figsize=(20,10))
#plot the real
sns.lineplot(y=("true_runoff"),x="date",data=pred,color='red',linewidth=1.3,legend='auto')
sns.lineplot(y=("trueTP"),x="date",data=pred,color='green',linewidth=1.3,legend='auto')
"""
#plot the lead_time_
lt1=pred[["climTP_lt1","climTP_lt1_Q25","climTP_lt1_Q75"]]
#lt1.columns=np.repeat('climatologia_lt1_ensemple_prec',3)
sns.lineplot(data=lt1["climTP_lt1"],legend='auto')
plt.fill_between(x=lt1.index, y1=lt1['climTP_lt1_Q25'], y2=lt1['climTP_lt1_Q75'], alpha=0.2)
#plot the lead_time_
lt4=pred[["climTP_lt4","climTP_lt4_Q25","climTP_lt4_Q75"]]
#lt4.columns=np.repeat('climatologia_lt4_ensemple_prec',3)
sns.lineplot(data=lt4["climTP_lt4"], palette=['green'],legend='auto')
plt.fill_between(x=lt4.index, y1=lt4['climTP_lt4_Q25'], y2=lt4['climTP_lt4_Q75'], alpha=0.2)
"""
plt.axhline(0,ls='--')
plt.ylabel('{t_unit} days averaged discharge anomaly [m^3/sec]')
plt.legend(['TRUE DISCHARGE','LEAD TIME = 0','LEAD TIME = 1','LEAD TIME = 4'])
plt.title("Anomalies plotting with precipitation variability(Q= 25 and 75) mapped on the prediction discharge")
return;
def evaluate_prediction(prediction):
to_drop=[]
for c in prediction.columns:
if c[-3] == 'Q':
to_drop.append(c)
to_drop.append('split')
runoff=prediction.drop(columns=to_drop)
runoff_error_r2=runoff.apply(lambda y_pred: r2_score(runoff['true_runoff'], y_pred), axis=0)
#runoff_error_smape=runoff.apply(lambda y_pred: smape(runoff['true_runoff'], y_pred), axis=0)
#runoff_error_smape.plot.bar(ylabel="SMAPE score [/]")
runoff_error_r2.plot.bar(ylabel="R^2 score [/]",)
return runoff_error_r2