-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsa_sandbox.py
More file actions
executable file
·329 lines (274 loc) · 9.65 KB
/
Copy pathsa_sandbox.py
File metadata and controls
executable file
·329 lines (274 loc) · 9.65 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
#!/usr/bin/env python3
"""
Perform sensitivity analysis on synthetic test functions using GP surrogates.
This script trains a GP surrogate on test function data and computes Sobol
sensitivity indices to identify important input variables. Supports variable
exclusion and customizable test functions.
Usage examples:
./sa_sandbox.py --help
./sa_sandbox.py
./sa_sandbox.py --test-function otlcircuit --n-train 200
./sa_sandbox.py --test-function otlcircuit --n-train 200 --exclude Beta
./sa_sandbox.py -f parabola --b1 2 --b2 1 --b12 0.5
./sa_sandbox.py -f wingweight -tr 150 -e S_w A
./sa_sandbox.py -f otlcircuit -tr 200 -e R_b1 R_f
./sa_sandbox.py -f otlcircuit -tr 200 -e R_b1 R_f --no-scale-x
"""
import argparse
import time
from datetime import datetime
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
from SALib.analyze import sobol
from SALib.sample import saltelli
from sklearn.metrics import mean_absolute_error, root_mean_squared_error
from surmod import sensitivity_analysis as sa
from surmod.gaussian_process import GPSurrogate
from surmod.test_functions import get_input_spec, get_variable_names
from surmod.utils import log_results
def parse_arguments():
"""Get command line arguments."""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Perform sensitivity analysis on synthetic test functions using GP surrogates.",
)
parser.add_argument(
"-s",
"--seed",
type=int,
default=42,
help="Random seed for reproducibility.",
)
experiment = parser.add_argument_group("experiment options")
gp_options = parser.add_argument_group("GP options")
parabola = parser.add_argument_group("parabola options")
experiment.add_argument(
"-f",
"--test-function",
type=str,
choices=["parabola", "otlcircuit", "piston", "wingweight", "borehole"],
default="parabola",
help="Choose test function.",
)
experiment.add_argument(
"-tr",
"--n-train",
type=int,
default=100,
help="Number of training samples.",
)
experiment.add_argument(
"-te",
"--n-test",
type=int,
default=100,
help="Number of points to have in testing data set.",
)
experiment.add_argument(
"-e",
"--exclude",
type=str,
nargs="+",
help="Variable names to exclude from fitting the surrogate model",
)
gp_options.add_argument(
"-k",
"--kernel",
type=str,
choices=["rbf", "matern", "periodic"],
default="matern",
help="GP kernel function.",
)
gp_options.add_argument(
"-i",
"--isotropic",
action="store_true",
help="Use isotropic kernel (single lengthscale for all inputs).",
)
gp_options.add_argument(
"--scale-x",
action=argparse.BooleanOptionalAction,
default=True,
help="Scale the input values to [0,1] per dimension using training data.",
)
gp_options.add_argument(
"-ny",
"--normalize-y",
action="store_true",
default=False,
help="Standardize outputs (maps to GPSurrogate.scale_outputs).",
)
gp_options.add_argument(
"--fixed-nugget",
type=float,
default=None,
metavar="VALUE",
help="Set the white-noise variance (nugget) to VALUE instead of learning it.",
)
gp_options.add_argument(
"--lengthscale-bounds",
type=float,
nargs=2,
default=(1e-2, 100.0),
metavar=("LOW", "HIGH"),
help="Bounds for kernel lengthscale constraint.",
)
gp_options.add_argument(
"--noise-bounds",
type=float,
nargs=2,
default=(1e-8, 1e-1),
metavar=("LOW", "HIGH"),
help="Bounds for likelihood noise constraint.",
)
parabola.add_argument(
"--b1",
type=float,
default=1,
help="Parabola coefficient for the x1^2 term.",
)
parabola.add_argument(
"--b2",
type=float,
default=1,
help="Parabola coefficient for the x2^2 term.",
)
parabola.add_argument(
"--b12",
type=float,
default=1,
help="Parabola coefficient for the interaction term.",
)
return parser.parse_args()
def main():
"""Run surrogate-based sensitivity analysis on a synthetic test function."""
args = parse_arguments()
test_function = args.test_function
n_train = args.n_train
n_test = args.n_test
b1 = args.b1
b2 = args.b2
b12 = args.b12
exclude = args.exclude
kernel = args.kernel
isotropic = args.isotropic
lengthscale_bounds = tuple(args.lengthscale_bounds)
noise_bounds_arg = tuple(args.noise_bounds)
seed = args.seed
# Set output directories relative to this script
plots_dir = Path(__file__).parent / "plots"
results_dir = Path(__file__).parent / "results"
_, _, bounds_list = get_input_spec(test_function)
bounds = np.array(bounds_list, dtype=float)
x_train, x_test, y_train, y_test = sa.simulate_data(
test_function, n_train, n_test, b1, b2, b12, seed=seed
)
# Get variable names from test_functions module
variable_names = get_variable_names(test_function)
# Apply exclusions by converting variable names to indices
if exclude is not None:
exclude_indices = []
for var_name in exclude:
if var_name not in variable_names:
raise ValueError(
f"Variable '{var_name}' not found in {test_function}. "
f"Valid variables: {variable_names}"
)
exclude_indices.append(variable_names.index(var_name))
x_train = np.copy(np.delete(x_train, exclude_indices, axis=1))
x_test = np.copy(np.delete(x_test, exclude_indices, axis=1))
bounds = np.delete(bounds, exclude_indices, axis=0)
variable_names = [name for name in variable_names if name not in exclude]
dim = x_train.shape[1]
# Handle fixed nugget
fixed_noise = args.fixed_nugget
noise_bounds_to_use = None if fixed_noise is not None else noise_bounds_arg
gp_model = GPSurrogate(
x_train=x_train,
y_train=y_train,
x_test=x_test,
y_test=y_test,
kernel=kernel,
isotropic=isotropic,
scale_inputs=args.scale_x,
scale_outputs=args.normalize_y,
fixed_noise=fixed_noise,
lengthscale_bounds=lengthscale_bounds,
noise_bounds=noise_bounds_to_use,
seed=seed,
)
start_time = time.perf_counter()
gp_model.fit()
elapsed_time = time.perf_counter() - start_time
pred_train, _ = gp_model.predict(x_train)
pred_test, _ = gp_model.predict(x_test)
train_mae = mean_absolute_error(y_train, pred_train)
test_mae = mean_absolute_error(y_test, pred_test)
train_rmse = root_mean_squared_error(y_train, pred_train)
test_rmse = root_mean_squared_error(y_test, pred_test)
train_max_abserr, train_max_input = GPSurrogate.compute_max_error(
pred_train, y_train, x_train
)
test_max_abserr, test_max_input = GPSurrogate.compute_max_error(
pred_test, y_test, x_test
)
problem = {
"num_vars": dim,
"names": variable_names,
"bounds": bounds.tolist(),
}
param_values = saltelli.sample(problem, 2**13, calc_second_order=False)
Y_mean, _ = gp_model.predict(param_values)
Y = np.asarray(Y_mean).reshape(-1)
Si = sobol.analyze(problem, Y, calc_second_order=False)
timestamp = datetime.now().strftime("%m%d_%H%M%S")
log_message = (
f"Run timestamp (%m%d_%H%M%S): {timestamp}\n"
f"Test Function: {test_function}\n"
f"Number of training points: {n_train}\n"
f"Number of testing points: {n_test}\n"
f"Kernel: {kernel}\n"
f"Isotropic: {isotropic}\n"
f"Fixed nugget: {args.fixed_nugget}\n"
f"Lengthscale bounds: {lengthscale_bounds}\n"
f"Noise bounds: {noise_bounds_to_use if fixed_noise is None else 'N/A (fixed)'}\n"
f"Train RMSE: {train_rmse:.3e}\n"
f"Test RMSE: {test_rmse:.3e}\n"
f"Train Max abs err: {train_max_abserr:.3e} | Location: {train_max_input}\n"
f"Test Max abs err: {test_max_abserr:.3e} | Location: {test_max_input}\n"
f"Train MAE: {train_mae:.3e}\n"
f"Test MAE: {test_mae:.3e}\n"
f"Elapsed time for training GP: {elapsed_time:.3f} seconds\n"
)
print(log_message)
log_results(
log_message,
path_to_log=results_dir / f"{test_function}.txt",
)
# Assumes sa.plot_test_predictions was updated earlier to use gp_model.predict(x)->(mean,std)
sa.plot_test_predictions(x_test, y_test, gp_model, test_function)
sa.sobol_plot(
Si["S1"],
Si["ST"],
problem["names"],
Si["S1_conf"],
Si["ST_conf"],
test_function,
)
if test_function == "parabola":
input1 = np.linspace(bounds[0, 0], bounds[0, 1], 100)
input2 = np.linspace(bounds[1, 0], bounds[1, 1], 100)
grid_input1, grid_input2 = np.meshgrid(input1, input2)
x_grid = np.column_stack((grid_input1.flatten(), grid_input2.flatten()))
preds_mean, _ = gp_model.predict(x_grid)
plt.figure()
plt.tricontourf(
x_grid[:, 0], x_grid[:, 1], preds_mean, levels=50, cmap="viridis"
)
plt.title("GP Model Prediction for Parabola")
plots_dir.mkdir(exist_ok=True)
timestamp = datetime.now().strftime("%m%d_%H%M%S")
plt.savefig(plots_dir / f"{b1}_{b2}_{b12}_{test_function}_{timestamp}.png")
if __name__ == "__main__":
main()