-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_importance.py
316 lines (263 loc) · 13.5 KB
/
feature_importance.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#====================
# Make deterministic
#====================
from mingpt.utils import set_seed
set_seed(42)
#==========================
# Standard library imports
#==========================
import warnings
# Silence FutureWarnings (something with my numpy version)
warnings.simplefilter(action='ignore', category=FutureWarning)
import datetime
import math
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import pickle
import sys
import time
import torch
import torch.nn as nn
from torch.nn import functional as F
from torch.utils.data import Dataset
#===============
# Local imports
#===============
from Dataset import TestDataset
from general import GetDataVariables, SpecifyFeatures, \
SpecifyModel, SpecifyDatasetFile, SpecifyPredictionFile
from global_parameters import *
from mingpt.model import GPT, GPTConfig
from mingpt.trainer import Trainer, TrainerConfig
from mingpt.utils import predict
from read_weather_data import read_auxiliary_data, read_test_datasets
if __name__ == '__main__':
# Logicals
Plot_gradient_continuously = False
# Plot settings
colors = ['b','orange','g','r','m','k','y','c', \
'b','orange','g','r','m','k','y','c', \
'b','orange','g','r','m','k','y','c', \
'b','orange','g','r','m','k','y','c']
markers = ['x','o','^','v','*','+','p','d', \
'o','^','v','*','+','p','d','x', \
'^','v','*','+','p','d','x','o', \
'v','*','+','p','d','x','o','^',]
linestyles = ['--','--','--','--','--','--','--','--', \
'dotted','dotted','dotted','dotted','dotted','dotted','dotted','dotted', \
'dashdot','dashdot','dashdot','dashdot','dashdot','dashdot','dashdot','dashdot', \
'-.','-.','-.','-.','-.','-.','-.','-.']
# Read station IMEI, time period and lat,lon from file
station_file = list_dir + 'station_list.csv'
df = pd.read_csv(station_file)
# Number of stations
nstation = df.shape[0]
station_idx = np.arange(nstation)
# Get IMEI, start and end dates and lat,lon for all stations/locations
imei = df['imei'].values
start_dates = df['start_date'].values
end_dates = df['end_date'].values
latitudes = df['lat'].values
longitudes = df['lon'].values
# Get dates in datetime format
start_datetimes = [datetime.datetime.strptime(start_date,fmt_strp) for start_date in start_dates]
end_datetimes = [datetime.datetime.strptime(end_date,fmt_strp) for end_date in end_dates]
# Loicals for loading
Load_data = False
# Logicals for saving
Save_data = True
# Logicals for plotting
Plot_dydx = False
Plot_dydx_mean = False
Plot_dydx_mean_all = True
# Feature inclusion
include_features = {}
include_features['OBS_t2m'] = True
include_features['NWP_t2m'] = True
include_features['NWP_u10m'] = True
include_features['NWP_v10m'] = True
include_features['NWP_wspd10m'] = True
include_features['NWP_sin_wsdir10m'] = True
include_features['NWP_cos_wsdir10m'] = True
include_features['NWP_rh2m'] = True
include_features['NWP_q2m'] = True
include_features['NWP_td2m'] = True
include_features['NWP_mslp'] = True
include_features['NWP_lhtfl'] = True
include_features['NWP_shtfl'] = True
include_features['NWP_nswrf'] = True
include_features['NWP_nlwrf'] = True
include_features['NWP_tcc'] = True
include_features['NWP_tp'] = True
include_features['NWP_pwat'] = True
include_features['NWP_gh500'] = True
include_features['NWP_gh700'] = True
include_features['NWP_gh850'] = True
include_features['NWP_t500'] = True
include_features['NWP_t700'] = True
include_features['NWP_t850'] = True
include_features['NWP_u500'] = True
include_features['NWP_u700'] = True
include_features['NWP_u850'] = True
include_features['NWP_v500'] = True
include_features['NWP_v700'] = True
include_features['NWP_v850'] = True
include_features['NWP_w700'] = True
include_features['TIME_sin_doy'] = False
include_features['TIME_cos_doy'] = False
include_features['TIME_sin_hod'] = False
include_features['TIME_cos_hod'] = False
include_features['NWP_wspd500'] = False
include_features['NWP_wspd700'] = False
include_features['NWP_wspd850'] = False
include_features['NWP_sin_wsdir500'] = False
include_features['NWP_cos_wsdir500'] = False
include_features['NWP_sin_wsdir700'] = False
include_features['NWP_cos_wsdir700'] = False
include_features['NWP_sin_wsdir850'] = False
include_features['NWP_cos_wsdir850'] = False
# Feature indices
feature_indices = [i for i,key in enumerate(include_features) if include_features[key] == True]
# Specify features
features, Nfeatures = SpecifyFeatures(include_features)
# Specify dataset file name
dataset_file_generic, dataset_file_train, dataset_file_val, dataset_file_test = \
SpecifyDatasetFile(data_dir,include_features,nstation_train,nstation_val,nstation_test,input_days)
# Specify the predictions file
predictions_file = SpecifyPredictionFile(data_dir,include_features,nstation_train,nstation_val,nstation_test,n_epochs, \
batch_size,input_days,loss_metrics)
# Specify model
model_name = SpecifyModel(model_dir,include_features,features,nstation_train,nstation_val,n_epochs, \
batch_size,input_days,loss_metrics)
if Load_data:
print('Load already-calculated gradients')
ofile = data_dir + 'gradients.pickle'
outfile = open(ofile,'rb')
save_tuple = pickle.load(outfile)
dydx = save_tuple
outfile.close()
del save_tuple
else:
print('Load the model')
model = torch.load(model_name,map_location=torch.device('cpu'))
print('Load Datasets')
print(' ---Generic data')
mu, std, station_train, station_val, station_test = read_auxiliary_data(dataset_file_generic)
print('Load test dataset')
read_dict={'data_test':True,'data_test_1d':True,'extra_data_test':True, \
'data_raw_test':True,'extra_data_raw_test':True}
data_test, data_test_1d, extra_data_test, data_raw_test, extra_data_raw_test = read_test_datasets(dataset_file_test,read_dict)
# Create test dataset
t0 = time.time()
test_dataset, analysis_dataset, extra_analysis_dataset\
= TestDataset(data_test,data_raw_test,extra_data_raw_test,data_test_1d,feature_indices,nfc_input,fc_update, \
fc_output_interval,prediction_window,block_size,station_test,verbose=True)
# Delete already-used data
del data_test, data_test_1d, extra_data_test, data_raw_test, extra_data_raw_test
print('Calculate gradients')
dydx = []
for istation in range(nstation_test):
print('Working on station no ' + str(istation) + ' of ' + str(nstation_test))
# Number of data points to make predictions for
npredict = test_dataset[istation].shape[0]
dydx_forecast = np.zeros((prediction_window,block_size,Nfeatures))
for ipredict in range(npredict):
# print('--- Prediction ' + str(ipredict+1) + ' out of ' + str(npredict))
dydx_tmp = np.zeros((prediction_window,block_size,Nfeatures))
x_obs = torch.tensor(test_dataset[istation][ipredict,:,0], dtype=torch.float).unsqueeze(-1)
x_model = torch.tensor(test_dataset[istation][ipredict,:,1:], dtype=torch.float)
x = torch.cat((x_obs[:block_size,:],x_model[:block_size,:]), dim=1)
x = x.unsqueeze(0)
x_model = x_model.unsqueeze(0)
# Loop over prediction window - Number of forecast outputs (+3h - +54h)
for k in range(prediction_window):
x_cond = x.clone().detach() if x.size(1) <= block_size else x[:, -block_size:].clone().detach() # crop the sequence if needed
x_cond.requires_grad = True # set pytorch to compute gradients for input
outputs, _ = model.forward(x_cond)
max_prediction = outputs.mean() # must be scalar to compute gradient - mean seems to be the best
# max: Gives zeros for a lot of the input days and a sharp spike otherwise,
# Starts with most important for recent days and then descends
# min: Gives zeros for a lot of the input days and a sharp spike otherwise
# Starts with most important for oldest days and then descends - until it starts from most recent days
# median: Gives zeros for a lot of the input days and a sharp spike otherwise
# mean: Gives continuous importance/gradients
if k == 0:
max_prediction.backward(retain_graph=True)
else:
max_prediction.backward()
# Take the absolute of the gradient since I want to take the average later and gradients
# might cancel if they're opposite signs
dydx_tmp[k,:,:] = np.abs(x_cond.grad[0,:,:]) # same shape as input, telling you the "importance" of each dimension
if Plot_gradient_continuously:
fig, ax = plt.subplots(figsize=[10,8])
for i in range(Nfeatures):
ax.plot(dydx_tmp[k,:,i], color=colors[i], marker=markers[i], linestyle=linestyles[i], label=features[i])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.set_title(str(k) + ' of ' + str(prediction_window))
plt.subplots_adjust(right=0.8)
plt.show()
# pluck the logits at the final step
outputs = outputs[:, -1, :]
# Concatenate the predicted value(s) and the forecast for the next step,
# since we don't want to predict the model data, but use the current NWP forecast
x_new = torch.cat((outputs,x_model[:,block_size+k]), dim=1)
x_new = x_new.unsqueeze(0)
# Append to the sequence and continue to predict the next step using the newest prediction and the next NWP forecast step
x_tmp = torch.tensor(x[:,:,:].detach().clone().numpy(), dtype=torch.float)
x_new_tmp = torch.tensor(x_new[:,:,:].clone().detach().numpy(), dtype=torch.float)
x = torch.cat((x_tmp, x_new_tmp), dim=1)
x.requires_grad = True
# Want to take the average over each prediction step
dydx_forecast += dydx_tmp
# Calculate the mean by dividing with the number of prediction steps
# Append to dydx list
dydx.append(dydx_forecast/npredict)
# Delete data to save memory
del x, x_obs, x_model, x_cond, x_new, x_tmp, x_new_tmp
if Save_data:
ofile = data_dir + 'gradients.pickle'
outfile = open(ofile,'wb')
save_tuple = (dydx)
pickle.dump(save_tuple,outfile)
outfile.close()
# Convert to an array
dydx = np.array(dydx)
# Average over all stations
dydx_mean = np.nanmean(dydx, axis=0)
# Average over all stations and prediction steps
dydx_mean_all = np.nanmean(np.nanmean(dydx, axis=0), axis=0)
# # TEMPORARILY REMOVE THE SIN/COS features
# features_new = [feature for feature in features if ('cos' not in feature)]
# features = [feature if 'sin' not in feature else feature[4:] for feature in features_new]
# Nfeatures = len(features)
if Plot_dydx_mean_all:
fig, ax = plt.subplots(figsize=[12,8])
for i in range(Nfeatures):
ax.plot(dydx_mean_all[:,i], color=colors[i], marker=markers[i], linestyle=linestyles[i], label=features[i])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=6)
# ax.set_title(str(k) + ' of ' + str(prediction_window))
plt.subplots_adjust(right=0.8)
plt.show()
if Plot_dydx_mean:
for k in range(prediction_window):
fig, ax = plt.subplots(figsize=[12,8])
for i in range(Nfeatures):
ax.plot(dydx_mean[k,:,i], color=colors[i], marker=markers[i], linestyle=linestyles[i], label=features[i])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
# ax.set_title(str(k) + ' of ' + str(prediction_window))
plt.subplots_adjust(right=0.8)
plt.show()
if Plot_dydx:
k = 0
# for k in range(prediction_window):
for istation in range(nstation_test):
fig, ax = plt.subplots(figsize=[12,8])
for i in range(Nfeatures):
ax.plot(dydx[istation][k,:,i], color=colors[i], marker=markers[i], linestyle=linestyles[i], label=features[i])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5),fontsize=8)
# ax.set_title('Lead time ' + str(k) + ' of ' + str(prediction_window))
ax.set_title('Station no. ' + str(istation) + ' of ' + str(nstation_test))
plt.subplots_adjust(right=0.8)
plt.show()