-
Notifications
You must be signed in to change notification settings - Fork 0
/
usgs_returns_from_training_length.py
284 lines (259 loc) · 13.1 KB
/
usgs_returns_from_training_length.py
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
import scipy.stats as stats
import os
import pandas as pd
import numpy as np
pd.set_option("display.precision", 3)
import matplotlib.pyplot as plt
dataset = "usgs"
folder_path = str("G:/My Drive/PhD Admin and Notes/paper1/revisions-code/" + dataset + "_modpods_results")
# choose to see shifted or not shifted
data_width = 11 # include training_length_days and training_time_minutes
shifted = True # True or False
data = []
df = pd.DataFrame(data)
p1t1 = pd.DataFrame(pd.np.empty((0, data_width)))
p1t2 = pd.DataFrame(pd.np.empty((0, data_width)))
p2t1 = pd.DataFrame(pd.np.empty((0, data_width)))
p2t2 = pd.DataFrame(pd.np.empty((0, data_width)))
p3t1 = pd.DataFrame(pd.np.empty((0, data_width)))
p3t2 = pd.DataFrame(pd.np.empty((0, data_width)))
performance_summaries = dict()
shifted_eval = dict()
for subdir, dirs, files in os.walk(folder_path):
#print(subdir)
for file in files:
if("error_metrics" in str(os.path.join(subdir, file))):
name_idx = str(subdir).find("results") + 8
site_id = str(subdir)[name_idx:str(subdir).find("_",name_idx)]
#print(site_id)
if ("eval" in str(os.path.join(subdir, file))):
if ( (shifted and "no_shift" not in str(os.path.join(subdir, file))) or
(not shifted and "no_shift" in str(os.path.join(subdir, file))) ):
#print(str(os.path.join(subdir, file)))
if ("po_1" in str(os.path.join(subdir, file))):
shifted_eval[site_id] = pd.read_csv(str(os.path.join(subdir, file)),index_col=0)
#print(site_id)
#print(shifted_eval[site_id])
if p1t1.empty:
p1t1.columns = shifted_eval[site_id].columns[:data_width]
p1t1.loc[site_id,:] = shifted_eval[site_id].loc[1,:]
#print(p1t1)
#try:
if p1t2.empty:
p1t2.columns = shifted_eval[site_id].columns[:data_width]
p1t2.loc[site_id,:] = shifted_eval[site_id].loc[2,:]
#except:
# pass
elif ("po_2" in str(os.path.join(subdir, file))):
shifted_eval[site_id] = pd.read_csv(str(os.path.join(subdir, file)),index_col=0)
#print(shifted_eval[site_id])
#try:
if p2t1.empty:
p2t1.columns = shifted_eval[site_id].columns[:data_width]
p2t1.loc[site_id,:] = shifted_eval[site_id].loc[1,:]
#except:
#pass # that model config wasn't trained, not an error
#try:
if p2t2.empty:
p2t2.columns = shifted_eval[site_id].columns[:data_width]
p2t2.loc[site_id,:] = shifted_eval[site_id].loc[2,:]
#except:
#pass
elif ("po_3" in str(os.path.join(subdir, file))):
shifted_eval[site_id] = pd.read_csv(str(os.path.join(subdir, file)),index_col=0)
#print(shifted_eval[site_id])
#try:
if p3t1.empty:
p3t1.columns = shifted_eval[site_id].columns[:data_width]
p3t1.loc[site_id,:] = shifted_eval[site_id].loc[1,:]
#except:
#pass # that model config wasn't trained, not an error
#try:
if p3t2.empty:
p3t2.columns = shifted_eval[site_id].columns[:data_width]
p3t2.loc[site_id,:] = shifted_eval[site_id].loc[2,:]
#except:
#pass
print("p1t1")
print(p1t1.to_string())
print("p1t2")
print(p1t2.to_string())
print("p2t1")
print(p2t1.to_string())
print("p2t2")
print(p2t2.to_string())
print("p3t1")
print(p3t1.to_string())
print("p3t2")
print(p3t2.to_string())
train_p1t1 = pd.DataFrame(pd.np.empty((0, data_width)))
train_p1t2 = pd.DataFrame(pd.np.empty((0, data_width)))
train_p2t1 = pd.DataFrame(pd.np.empty((0, data_width)))
train_p2t2 = pd.DataFrame(pd.np.empty((0, data_width)))
train_p3t1 = pd.DataFrame(pd.np.empty((0, data_width)))
train_p3t2 = pd.DataFrame(pd.np.empty((0, data_width)))
# make a dataframe that identifies the model with the best training NSE for each site
best_train_NSE = pd.DataFrame()
# make the best_NSE index the union of the indices of the p1t1, p1t2, p2t1, p2t2, p3t1, p3t2 dataframes
best_train_NSE.index = p1t1.index.union(p1t2.index).union(p2t1.index).union(p2t2.index).union(p3t1.index).union(p3t2.index)
best_train_NSE['NSE'] = np.nan
best_train_NSE['model'] = np.nan
shifted_train = dict()
for subdir, dirs, files in os.walk(folder_path):
#print(subdir)
for file in files:
if("error_metrics" in str(os.path.join(subdir, file))):
name_idx = str(subdir).find("results") + 8
site_id = str(subdir)[name_idx:str(subdir).find("_",name_idx)]
#print(site_id)
if ("train" in str(os.path.join(subdir, file))):
if ( (shifted and "no_shift" not in str(os.path.join(subdir, file))) or
(not shifted and "no_shift" in str(os.path.join(subdir, file))) ):
#print(str(os.path.join(subdir, file)))
max_so_far = -10e6
if ("po_1" in str(os.path.join(subdir, file))):
shifted_train[site_id] = pd.read_csv(str(os.path.join(subdir, file)),index_col=0)
#print(site_id)
#print(shifted_train[site_id])
if train_p1t1.empty:
train_p1t1.columns = shifted_train[site_id].columns[:data_width]
train_p1t1.loc[site_id,:] = shifted_train[site_id].loc[1,:]
if (train_p1t1.NSE[site_id] > max_so_far):
best_train_NSE.loc[site_id,'NSE'] = train_p1t1.NSE[site_id]
best_train_NSE.loc[site_id,'model'] = 'p1t1'
max_so_far = train_p1t1.NSE[site_id]
#print(p1t1)
#try:
if train_p1t2.empty:
train_p1t2.columns = shifted_train[site_id].columns[:data_width]
train_p1t2.loc[site_id,:] = shifted_train[site_id].loc[2,:]
if (train_p1t2.NSE[site_id] > max_so_far):
best_train_NSE.loc[site_id,'NSE'] = train_p1t2.NSE[site_id]
best_train_NSE.loc[site_id,'model'] = 'p1t2'
max_so_far = train_p1t2.NSE[site_id]
#except:
# pass
elif ("po_2" in str(os.path.join(subdir, file))):
shifted_train[site_id] = pd.read_csv(str(os.path.join(subdir, file)),index_col=0)
#print(shifted_train[site_id])
#try:
if train_p2t1.empty:
train_p2t1.columns = shifted_train[site_id].columns[:data_width]
train_p2t1.loc[site_id,:] = shifted_train[site_id].loc[1,:]
if (train_p2t1.NSE[site_id] > max_so_far):
best_train_NSE.loc[site_id,'NSE'] = train_p2t1.NSE[site_id]
best_train_NSE.loc[site_id,'model'] = 'p2t1'
max_so_far = train_p2t1.NSE[site_id]
#except:
#pass # that model config wasn't trained, not an error
#try:
if train_p2t2.empty:
train_p2t2.columns = shifted_train[site_id].columns[:data_width]
train_p2t2.loc[site_id,:] = shifted_train[site_id].loc[2,:]
if (train_p2t2.NSE[site_id] > max_so_far):
best_train_NSE.loc[site_id,'NSE'] = train_p2t2.NSE[site_id]
best_train_NSE.loc[site_id,'model'] = 'p2t2'
max_so_far = train_p2t2.NSE[site_id]
#except:
#pass
elif ("po_3" in str(os.path.join(subdir, file))):
shifted_train[site_id] = pd.read_csv(str(os.path.join(subdir, file)),index_col=0)
#print(shifted_train[site_id])
#try:
if train_p3t1.empty:
train_p3t1.columns = shifted_train[site_id].columns[:data_width]
train_p3t1.loc[site_id,:] = shifted_train[site_id].loc[1,:]
if (train_p3t1.NSE[site_id] > max_so_far):
best_train_NSE.loc[site_id,'NSE'] = train_p3t1.NSE[site_id]
best_train_NSE.loc[site_id,'model'] = 'p3t1'
max_so_far = train_p3t1.NSE[site_id]
#except:
#pass # that model config wasn't trained, not an error
#try:
if train_p3t2.empty:
train_p3t2.columns = shifted_train[site_id].columns[:data_width]
train_p3t2.loc[site_id,:] = shifted_train[site_id].loc[2,:]
if (train_p3t2.NSE[site_id] > max_so_far):
best_train_NSE.loc[site_id,'NSE'] = train_p3t2.NSE[site_id]
best_train_NSE.loc[site_id,'model'] = 'p3t2'
max_so_far = train_p3t2.NSE[site_id]
# ensemble is taking the best training NSE for each site and putting it in a dataframe
ensemble = pd.DataFrame(columns=p1t1.columns,index=p1t1.index.union(p1t2.index).union(p2t1.index).union(p2t2.index).union(p3t1.index).union(p3t2.index))
# make the best_NSE index the union of the indices of the p1t1, p1t2, p2t1, p2t2, p3t1, p3t2 dataframes
#ensemble.index = p1t1.index.union(p1t2.index).union(p2t1.index).union(p2t2.index).union(p3t1.index).union(p3t2.index)
#ensemble.columns = p1t1.columns
ensemble['model'] = np.nan
for site in best_train_NSE.index: # take the eval nse corresponding to the model with the best training nse
if best_train_NSE.model[site] == 'p1t1':
ensemble.loc[site,:-1] = p1t1.loc[site,:]
ensemble.model[site] = 'p1t1'
elif best_train_NSE.model[site] == 'p1t2':
ensemble.loc[site,:-1] = p1t2.loc[site,:]
ensemble.model[site] = 'p1t2'
elif best_train_NSE.model[site] == 'p2t1':
ensemble.loc[site,:-1] = p2t2.loc[site,:]
ensemble.model[site] = 'p2t1'
elif best_train_NSE.model[site] == 'p2t2':
ensemble.loc[site,:-1] = p2t2.loc[site,:]
ensemble.model[site] = 'p2t2'
elif best_train_NSE.model[site] == 'p3t1':
ensemble.loc[site,:-1] = p3t1.loc[site,:]
ensemble.model[site] = 'p3t1'
elif best_train_NSE.model[site] == 'p3t2':
ensemble.loc[site,:-1] = p3t2.loc[site,:]
ensemble.model[site] = 'p3t2'
# drop rows which have NaNs
ensemble.dropna(axis=0,how='any',inplace=True)
p1t1.dropna(axis=0,how='any',inplace=True)
p1t2.dropna(axis=0,how='any',inplace=True)
p2t1.dropna(axis=0,how='any',inplace=True)
p2t2.dropna(axis=0,how='any',inplace=True)
p3t1.dropna(axis=0,how='any',inplace=True)
p3t2.dropna(axis=0,how='any',inplace=True)
print("ensemble")
print(ensemble.to_string())
print("p1t1 correlations")
print(p1t1.corr().iloc[9,:])
print("p1t2 correlations")
print(p1t2.corr().iloc[9,:])
print("p2t1 correlations")
print(p2t1.corr().iloc[9,:])
print("p2t2 correlations")
print(p2t2.corr().iloc[9,:])
print("p3t1 correlations")
print(p3t1.corr().iloc[9,:])
print("p3t2 correlations")
print(p3t2.corr().iloc[9,:])
# print the correlation between training_length_days and every other column for ensemble
print("ensemble correlations")
print(ensemble.iloc[:,:-1].corr(numeric_only=False).iloc[9,:])
# drop rows in ensemble which have NSE less than -1 (decent performance)
ensemble = ensemble[ensemble.NSE > -2]
# correlation between training_length_days and NSE for ensemble
r = ensemble.iloc[:,:-1].corr(numeric_only=False).iloc[9,2]
# make a scatter plot of training_length_days vs NSE for each model
fig, ax = plt.subplots()
#ax.scatter(p1t1.training_length_days,p1t1.NSE,label='p1t1')
#ax.scatter(p1t2.training_length_days,p1t2.NSE,label='p1t2')
#ax.scatter(p2t1.training_length_days,p2t1.NSE,label='p2t1')
#ax.scatter(p2t2.training_length_days,p2t2.NSE,label='p2t2')
#ax.scatter(p3t1.training_length_days,p3t1.NSE,label='p3t1')
#ax.scatter(p3t2.training_length_days,p3t2.NSE,label='p3t2')
ax.scatter(ensemble.training_length_days/365,ensemble.NSE,label='ensemble')
ax.set_xlabel('Training Record Length [Years]',fontsize='large')
ax.set_ylabel('Evaluation Nash Sutcliffe Efficiency',fontsize='large')
ax.set_title("Models with NSE < -2 excluded",fontsize='large')
#ax.set_xlim(0, 3000)
#ax.set_ylim(-10,1)
# plot the line of best fit between ensemble.training_length_days and ensemble.NSE
training_days = ensemble.training_length_days.values.astype(float)
nse = ensemble.NSE.values.astype(float)
m, b = np.polyfit(training_days, nse, 1)
print("gain in NSE per extra year of training")
print(m*365)
line_label = str('Best Fit | r = ' + str(np.round(r,decimals=2)) + ' | NSE/year = ' + str(np.round(m*365,decimals=2)))
ax.plot(training_days/365, m*training_days + b, color='red',label=line_label)
ax.legend(loc='lower center',fontsize='large')
plt.tight_layout()
plt.savefig(str(folder_path + '/returns_from_training_length.png'),format='png',dpi=600)
plt.show()