-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc_optimal_weights.py
More file actions
334 lines (277 loc) · 10.2 KB
/
Copy pathcalc_optimal_weights.py
File metadata and controls
334 lines (277 loc) · 10.2 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import os
import numpy as np
import argparse
from itertools import product
import csv
import logging
# SKLEARN modules
from sklearn.cluster import KMeans
from sklearn.linear_model import Ridge
from sklearn.metrics import r2_score
from sklearn.gaussian_process import GaussianProcessRegressor, kernels
from scipy.optimize import minimize
import pdb
from src.utils import (
load_dataset,
create_folds,
standardize_data,
generate_data,
create_weight_matrix,
)
def main(args):
# Create the output directory if it doesn't exist
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
# Initialize a logger
logging.basicConfig(
filename=os.path.join(args.output_dir, "output.log"),
filemode="w",
level=args.log_level,
format="%(asctime)s - %(levelname)s - %(message)s",
)
logging.info(args)
# Initialize the length scale values to consider
# NLCD_LS = range(args.nlcd_ls_vals[0], args.nlcd_ls_vals[1])
NDVI_LS = range(args.ndvi_ls_vals[0], args.ndvi_ls_vals[1])
ALBEDO_LS = range(args.albedo_ls_vals[0], args.albedo_ls_vals[1])
# Read in the data
data = load_dataset(args.data_dir, args.window_size, args.city)
# Create K-folds of the data
folds = create_folds(data, args.k_folds * args.k_folds_size)
# Create the headers to use for the CSV output file
headers = [
"fold",
"iter",
"ndvi_ls",
"albedo_ls",
"train_ridge_score",
"train_gp_score",
"train_score",
"val_ridge_score",
"val_gp_score",
"val_score",
]
# Create headings for coefs
for i in range(19):
headers.append(f"beta_{i}")
headers.append("intercept")
headers.extend(["matern_const", "matern_ls", "dp_const", "dot_prod_sigma"])
# Create header in the cSV
with open(os.path.join(args.output_dir, "results.csv"), "w") as f:
writer = csv.writer(f)
writer.writerow(headers)
# For each of the fold
for fold in range(0, args.k_folds):
# Split into a training and validation set
low_bound = fold * args.k_folds_size
upper_bound = (fold + 1) * args.k_folds_size
train_idx = np.where((folds < low_bound) | (folds >= upper_bound))[0]
val_idx = np.where((folds >= low_bound) & (folds < upper_bound))[0]
# val_idx = np.where(folds == fold)[0]
# Record the fold used
logging.info(f"Starting fold {fold}.")
# For each of the possible combinations of weights
for ndvi_ls, albedo_ls in product(NDVI_LS, ALBEDO_LS):
logging.debug(
f"Starting fold {fold} with ndvi_ls={ndvi_ls}, albedo_ls={albedo_ls}"
)
# Create the datasets using the given weights
logging.debug("Creating datasets.")
X_train, y_train = generate_data(
data, train_idx, ndvi_ls, albedo_ls, args.window_size, args.use_coords
)
X_val, y_val = generate_data(
data, val_idx, ndvi_ls, albedo_ls, args.window_size, args.use_coords
)
logging.debug("Standardizing datasets")
# Standardize the data
X_train, X_val, y_train, y_val, _, _, _, _ = standardize_data(
X_train, X_val, y_train, y_val, scaler=args.scaler
)
logging.debug("Fitting model.")
ridge_output_train = y_train
for i in range(args.max_iter):
row = [fold, i, ndvi_ls, albedo_ls]
# Fit the model to the training set
lm = Ridge(alpha=args.l2_alpha, fit_intercept=False)
lm.fit(X_train, ridge_output_train)
logging.debug("Calculating R2 scores.")
# Calculate the train and validation score
train_ridge_score = lm.score(X_train, y_train)
val_ridge_score = lm.score(X_val, y_val)
train_preds = lm.predict(X_train)
val_preds = lm.predict(X_val)
train_residuals = y_train - train_preds
val_residuals = y_val - val_preds
sample_points, sample_residuals = generate_samples(
data.coords[train_idx], train_residuals, args.n_samples
)
gp = fit_gp(
sample_points,
sample_residuals,
args.gp_constant_1,
args.gp_length_scale,
args.gp_constant_2,
args.gp_sigma_0,
args.gp_noise,
)
U_train = gp.predict(data.coords[train_idx])
U_val = gp.predict(data.coords[val_idx])
row.extend(
[
train_ridge_score,
r2_score(train_residuals, U_train),
r2_score(y_train, train_preds + U_train),
val_ridge_score,
r2_score(val_residuals, U_val),
r2_score(y_val, val_preds + U_val),
]
)
# Add coefficients
row.extend(lm.coef_)
# Add intercept
row.append(lm.intercept_)
# Add GP parameters
kp = gp.kernel_.get_params()
# pdb.set_trace()
row.extend(
[
np.sqrt(kp["k1__k1__constant_value"]),
kp["k1__k2__length_scale"],
np.sqrt(kp["k2__k1__constant_value"]),
kp["k2__k2__sigma_0"],
]
)
# Report the validation set error along with the parameters and the fold value
logging.debug("Writing row to CSV.")
# Write the row to the CSV file
with open(os.path.join(args.output_dir, "results.csv"), "a") as f:
writer = csv.writer(f)
writer.writerow(row)
# Update the residuals
ridge_output_train = y_train - U_train
def fit_gp(points, residuals, constant_1, length_scale, constant_2, sigma_0, noise):
kernel = kernels.ConstantKernel(
constant_1, constant_value_bounds="fixed"
) * kernels.Matern(
length_scale=length_scale, nu=0.5, length_scale_bounds="fixed"
) + kernels.ConstantKernel(
constant_2, constant_value_bounds="fixed"
) * kernels.DotProduct(
sigma_0, sigma_0_bounds="fixed"
)
gpr = GaussianProcessRegressor(kernel=kernel, optimizer=optimizer, alpha=noise)
gpr.fit(points, residuals)
return gpr
def optimizer(obj_func, initial_theta, bounds):
opt_res = minimize(
obj_func,
initial_theta,
method="L-BFGS-B",
bounds=bounds,
jac=True,
options={"maxiter": 1000},
)
return opt_res.x, opt_res.fun
def generate_samples(coords, residuals, N):
sample_idx = np.random.choice(np.arange(coords.shape[0]), N, replace=False)
sample_points = coords[sample_idx, :]
sample_residuals = residuals[sample_idx]
return sample_points, sample_residuals
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Calculate the optimal weights for the model"
)
# Create data arguments
parser.add_argument("--data_dir", type=str, help="Path to the data directory")
parser.add_argument(
"--window_size", type=int, default=20, help="Size of the window "
)
parser.add_argument("--use_coords", action="store_true")
# Create the model arguments
parser.add_argument(
"--l2_alpha", type=float, default=1e1, help="L2 regularization parameter"
)
# Create the training arguments
parser.add_argument(
"--k_folds",
type=int,
default=5,
help="Number of folds to use for cross validation",
)
parser.add_argument(
"--k_folds_size", type=int, default=1, help="How big the k-folds should be."
)
parser.add_argument(
"--scaler", choices=["minmax", "standard"], default="standard", help="Scaler"
)
parser.add_argument("--max_iter", type=int, default=20, help="Max iterations")
# Add the start and end ints for the NDVI length scale range
parser.add_argument(
"--ndvi_ls_vals",
nargs="+",
type=int,
help="The start and end values for the NDVI length scale range",
default=[1, 10],
)
# Add the start and end ints for the albedo length scale range
parser.add_argument(
"--albedo_ls_vals",
nargs="+",
type=int,
help="The start and end values for the albedo length scale range",
default=[1, 10],
)
# Add the GP arguments
parser.add_argument(
"--gp_constant_1",
type=float,
default=0.5,
help="The constant parameter for the Gaussian Process.",
)
parser.add_argument(
"--gp_constant_2",
type=float,
default=0.001,
help="The constant parameter for the Gaussian Process.",
)
# Add a term for the noise
parser.add_argument(
"--gp_noise",
type=float,
default=0.1,
help="The noise parameter for the Gaussian Process.",
)
parser.add_argument(
"--gp_length_scale",
type=float,
default=1000,
help="The length scale for the Gaussian Process.",
)
parser.add_argument(
"--gp_sigma_0",
type=float,
default=0.001,
help="The sigma_0 parameter for the Gaussian Process.",
)
# N samples to use for GP
parser.add_argument(
"--n_samples",
type=int,
default=1000,
help="The number of samples to use for the GP.",
)
# Create the output arguments
parser.add_argument("--output_dir", type=str, help="Path to the output directory")
parser.add_argument("--log_level", type=str, default="INFO", help="Logging level")
# Add an argument for the city name
parser.add_argument(
"--city",
type=str,
default="boston",
help="The city where the data is being collected from.",
)
# Parse the arguments
args = parser.parse_args()
print(args)
main(args)