-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhelpers.py
More file actions
301 lines (251 loc) · 8.02 KB
/
helpers.py
File metadata and controls
301 lines (251 loc) · 8.02 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
"""
Test helper utilities
This module provides a collection of utility functions and CLI wrappers for
handling NetCDF data, pandas DataFrames, and various performance and
perturbation operations.
It includes functions to load and process data, perform statistical analysis,
and run CLI commands related to performance, tolerance, perturbation, and
selection of ensemble members.
"""
import logging
import os
import random
import shutil
from math import sin
import numpy as np
import pandas as pd
import xarray as xr
from click.testing import CliRunner
from engine.cdo_table import cdo_table
from engine.performance import performance
from engine.perturb import perturb
from engine.select_members import select_members
from engine.stats import stats
from engine.tolerance import tolerance
def load_netcdf(path):
return xr.load_dataset(path)
def load_pandas(file, index_col=None, header=None):
if index_col is None:
index_col = [0, 1, 2]
if header is None:
header = [0, 1]
return pd.read_csv(file, index_col=index_col, header=header)
def store_timings_as_potential_new_ref(timings, dst):
# get all files ending with *.json
for root, _, files in os.walk(timings):
for file in files:
if file.endswith(".json"):
store_as_potential_new_ref(os.path.join(root, file), dst)
def store_as_potential_new_ref(src, dst):
filename = os.path.basename(src)
shutil.copyfile(src, os.path.join(dst, filename))
def pandas_error(df_ref, df_cur):
diff = (df_ref - df_cur).abs()
err_mask = (diff > 0).any(axis=1)
return diff[err_mask]
def check_netcdf(data_ref, data_cur):
diff_keys = set(data_ref.keys()) - set(data_cur.keys())
same_keys = set(data_ref.keys()).intersection(set(data_cur.keys()))
err = []
for key in same_keys:
diff = np.fabs(data_ref[key] - data_cur[key])
if np.sum(diff) > 0:
err.append(key)
return list(diff_keys), err
def assert_empty_list(lst, msg):
assert lst == [], f"{msg}:\n{lst}"
def assert_empty_df(df, msg):
assert len(df.values) == 0, f"{msg}:\n{df}"
def run_performance_cli(log_file, timing_database):
args = [
"--log_file",
log_file,
"--timing-database",
timing_database,
]
run_cli(performance, args)
def run_tolerance_cli(
ensemble_files,
tolerance_files,
member_type=None,
member_ids="1,2,3,4,5,6,7,8,9,10",
fof_type="AIREP",
minimum_tolerance=0.0,
): # pylint: disable=too-many-positional-arguments
args = [
"--ensemble-files",
ensemble_files,
"--tolerance-files",
tolerance_files,
"--member-ids",
member_ids,
"--fof-types",
fof_type,
"--minimum-tolerance",
minimum_tolerance,
]
if member_type is not None:
args.append("--member-type")
args.append(member_type)
run_cli(tolerance, args)
def generate_ensemble(tmp_path, filename, perturb_amplitude):
return run_perturb_cli(tmp_path, filename, perturb_amplitude)
def run_perturb_cli(model_input_dir, files, perturb_amplitude, member_ids=range(1, 11)):
perturbed_model_input_dir = f"{model_input_dir}/experiments/{{member_id}}"
args = [
"--model-input-dir",
model_input_dir,
"--perturbed-model-input-dir",
perturbed_model_input_dir,
"--files",
files,
"--member-ids",
",".join(map(str, member_ids)),
"--member-type",
"dp",
"--variable-names",
"U,V",
"--perturb-amplitude",
f"{perturb_amplitude}",
"--no-copy-all-files",
]
run_cli(perturb, args)
return perturbed_model_input_dir
def run_stats_cli(
model_output_dir, stats_file_name, ensemble, perturbed_model_output_dir=None
):
args = [
"--model-output-dir",
model_output_dir,
"--stats-file-name",
stats_file_name,
"--member-type",
"dp",
"--file-id",
"NetCDF",
"*.nc",
"--file-specification",
[
{
"NetCDF": {
"format": "netcdf",
"time_dim": "time",
"horizontal_dims": ["lat", "lon"],
}
}
],
]
args += (
["--perturbed-model-output-dir", perturbed_model_output_dir]
if perturbed_model_output_dir
else []
)
args.append("--ensemble" if ensemble else "--no-ensemble")
run_cli(stats, args)
def run_cdo_table_cli(model_output_dir, cdo_table_file, perturbed_model_output_dir):
args = [
"--model-output-dir",
model_output_dir,
"--cdo-table-file",
cdo_table_file,
"--member-type",
"dp",
"--file-id",
"NetCDF",
"*.nc",
"--perturbed-model-output-dir",
perturbed_model_output_dir,
"--file-specification",
[
{
"NetCDF": {
"format": "netcdf",
"time_dim": "time",
"horizontal_dims": ["lat", "lon"],
}
}
],
]
run_cli(cdo_table, args)
def run_select_members_cli(
ensemble_files,
selected_members_file_name,
tolerance_files,
enable_check_only=False,
max_member_count=15,
min_factor=5.0,
max_factor=50.0,
total_member_count=20,
log=None,
): # pylint: disable=too-many-positional-arguments
args = [
"--ensemble-files",
str(ensemble_files),
"--selected-members-file-name",
selected_members_file_name,
"--tolerance-files",
str(tolerance_files),
"--max-member-count",
str(max_member_count),
"--min-factor",
str(min_factor),
"--max-factor",
str(max_factor),
"--total-member-count",
str(total_member_count),
]
if enable_check_only:
args.append("--enable-check-only")
return run_cli(select_members, args, log)
def run_cli(command, args, log=None):
if log:
log.set_level(logging.INFO)
runner = CliRunner()
result = runner.invoke(command, args)
if not log:
catch_error(result)
else:
return log.text
def catch_error(result):
if result.exit_code != 0:
error_message = "Error executing command:\n" + result.output
if result.exception:
error_message += "\nException: " + str(result.exception)
raise Exception(error_message)
def create_random_stats_file(filename, configurations, seed, perturbation):
random.seed(seed)
max_time_dim = max(config["time_dim"] for config in configurations)
time_header = ",".join(f"{t},{t},{t}" for t in range(max_time_dim))
header = [
f"time,,,{time_header}",
"statistic,," + ",max,mean,min" * max_time_dim,
"file_ID,variable,height,,,,,,,,,",
]
data = []
for config in configurations:
time_dim = config["time_dim"]
height_dim = config["height_dim"]
variable = config["variable"]
file_format = config["file_format"]
for h in range(height_dim):
row = f"{file_format},{variable},{h}.0"
for t in range(time_dim):
base_mean = round((h - 2.0) * sin(t), 5)
mean = base_mean + (t + 1.0) * ((seed % 2) == 0) * round(
random.uniform(-perturbation, perturbation), 5
)
max_val = 2.0 * abs(base_mean) + (t + 1.0) * ((seed % 3) == 0) * round(
random.uniform(-perturbation, perturbation), 5
)
min_val = -2.0 * abs(base_mean) + (t + 1.0) * ((seed % 5) == 0) * round(
random.uniform(-perturbation, perturbation), 5
)
row += f",{max_val},{mean},{min_val}"
for _ in range(time_dim, max_time_dim):
row += ",,,"
data.append(row)
with open(filename, "w", encoding="utf-8") as f:
for line in header:
f.write(line + "\n")
for row in data:
f.write(row + "\n")