-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_prms_python.py
More file actions
476 lines (352 loc) · 14 KB
/
test_prms_python.py
File metadata and controls
476 lines (352 loc) · 14 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
from copy import copy
import json
import glob
import numpy as np
import os
import re
import shutil
import unittest
from difflib import Differ
from numpy.testing import assert_array_almost_equal
from prms_python import (
modify_params, Parameters, Scenario, ScenarioSeries, Simulation,
SimulationSeries, Data
)
OPJ = os.path.join
class TestSimulationSeries(unittest.TestCase):
'''
Expect that a SimulationSeries
'''
def setUp(self):
self.test_model_data_dir = os.path.join(
'test', 'data', 'models', 'lbcd'
)
self.simulation_dir = os.path.join(self.test_model_data_dir, 'tmp_sim')
def tearDown(self):
for g in glob.glob(OPJ(self.simulation_dir, '*')):
shutil.rmtree(g)
def test_simulation_series(self):
tdd = self.test_model_data_dir
data = Data(OPJ(tdd, 'data'))
base_parameters = Parameters(OPJ(tdd, 'parameters'))
def _copy_mod(base_parameters, val):
ret = copy(base_parameters)
ret['dday_intcp'][:] = val
return ret
parameters_gen = (
(val, _copy_mod(base_parameters, val))
for val in (-50, -40, -30)
)
control_path = OPJ(tdd, 'control')
series = SimulationSeries(
Simulation.from_data(
data, parameters, control_path,
self.simulation_dir + str(val)
)
for val, parameters in parameters_gen
)
outputs = list(series.run().outputs_iter())
self.assertEqual(len(series), 3)
self.assertEqual(len(outputs), 3)
for out in outputs:
sdir = out['simulation_dir']
assert os.path.isdir(out['simulation_dir'])
assert os.path.exists(OPJ(sdir, 'outputs', 'statvar.dat'))
assert os.path.exists(OPJ(sdir, 'inputs', 'data'))
assert os.path.exists(OPJ(sdir, 'inputs', 'parameters'))
shutil.rmtree(sdir)
class TestSimulation(unittest.TestCase):
"""
Simulations should take a base directory and return a simulation directory
"""
def setUp(self):
self.test_data_dir = os.path.join('test', 'data')
self.test_model_data_dir = os.path.join(
'test', 'data', 'models', 'lbcd'
)
self.simulation_dir = os.path.join(self.test_data_dir, 'tmp_sim')
def tearDown(self):
if os.path.exists(self.simulation_dir):
shutil.rmtree(self.simulation_dir)
def test_simulation_no_simdir(self):
"Simulation should run and write outputs to input directory when simulation_dir is not specified"
s = Simulation(self.test_model_data_dir)
s.run()
g = [
os.path.basename(f)
for f in glob.glob(os.path.join(self.test_model_data_dir, '*'))
]
self.assertIn('prms_ic.out', g)
self.assertIn('prms.out', g)
self.assertIn('statvar.dat', g)
def test_simulation_w_simdir(self):
"Simulation should create sim dir with inputs and outputs directory when simulation_dir is specified"
s = Simulation(self.test_model_data_dir, self.simulation_dir)
s.run()
gs = [
os.path.basename(f)
for f in glob.glob(os.path.join(self.simulation_dir, '*'))
]
self.assertIn('inputs', gs)
self.assertIn('outputs', gs)
assert_valid_input_dir(
self, os.path.join(self.simulation_dir, 'inputs')
)
assert_valid_output_dir(
self, os.path.join(self.simulation_dir, 'outputs')
)
def test_simulation_from_data(self):
"""
Use @classmethod from_data to build a simulation from Parameters and Data instances
"""
tdd = self.test_model_data_dir
data = Data(OPJ(tdd, 'data'))
parameters = Parameters(OPJ(tdd, 'parameters'))
ctrl = OPJ(tdd, 'control')
test_dir = OPJ(tdd, 'test-sim-dir')
if os.path.isdir(test_dir):
shutil.rmtree(test_dir)
s = Simulation.from_data(data, parameters, ctrl, test_dir)
s.run()
g = [
os.path.basename(f)
for f in glob.glob(OPJ(test_dir, 'outputs', '*'))
]
self.assertIn('prms_ic.out', g)
self.assertIn('prms.out', g)
self.assertIn('statvar.dat', g)
# clean up
shutil.rmtree(test_dir)
class TestScenario(unittest.TestCase):
def setUp(self):
self.test_data_dir = os.path.join('test', 'data')
self.test_model_data_dir = os.path.join(
'test', 'data', 'models', 'lbcd'
)
self.scenario_dir = os.path.join(self.test_data_dir, 'tmp_scenario')
def tearDown(self):
if os.path.exists(self.scenario_dir):
shutil.rmtree(self.scenario_dir)
def test_create_scenario(self):
"""
a simulation setup should create a simulation directory with correct scenario data
"""
s = Scenario(
self.test_model_data_dir, self.scenario_dir,
title='Scenario Uno', description='test scenario for prms_python'
)
param_mods = {
'snow_adj': lambda x: 1.1*x,
'rad_trncf': lambda x: 0.9*x
}
s.build(param_mod_funs=param_mods)
assert_valid_input_dir(self, self.scenario_dir) # os.path.join(self.scenario_dir, 'inputs'))
s.run()
assert_valid_input_dir(
self, os.path.join(self.scenario_dir, 'inputs')
)
assert_valid_output_dir(
self, os.path.join(self.scenario_dir, 'outputs')
)
md_json_path = os.path.join(self.scenario_dir, 'metadata.json')
assert os.path.isfile(md_json_path)
md_json = json.loads(open(md_json_path).read())
assert md_json['title'] == 'Scenario Uno'
assert md_json['description'] == 'test scenario for prms_python'
assert 'start_datetime' in md_json
assert 'end_datetime' in md_json
assert 'mod_funs_dict' in md_json
p_base = Parameters(
os.path.join(self.test_model_data_dir, 'parameters')
)
p_scen = Parameters(
os.path.join(self.scenario_dir, 'inputs', 'parameters')
)
assert_array_almost_equal(p_base['snow_adj']*1.1, p_scen['snow_adj'])
assert_array_almost_equal(p_base['rad_trncf']*0.9, p_scen['rad_trncf'])
class TestScenarios(unittest.TestCase):
def setUp(self):
self.test_data_dir = os.path.join('test', 'data')
self.test_model_data_dir = os.path.join(
self.test_data_dir, 'models', 'lbcd'
)
self.scenarios_dir = os.path.join(self.test_data_dir, 'tmp_scenarios')
def tearDown(self):
if os.path.exists(self.scenarios_dir):
shutil.rmtree(self.scenarios_dir)
def test_scenario_series(self):
"create_many_simulations should create many simulation directories and correct data"
s = ScenarioSeries(
self.test_model_data_dir, self.scenarios_dir,
title='scenario series uno',
description='''
Each scenario is given a title with the schema
'"<param_name1>":<scale_value1>|"<param_name2>":<scale_value2>', which can be
easily parsed into a Python dictionary later. We use the pipe instead of
comma for easier visual inspection, though this is entirely up to the
user/developer. The scenario consists of parameters scaled for each of the
specified parameters.
'''
)
def _scale_param(val):
def scale_by_val(x):
return x*val
return scale_by_val
scale_arange = np.arange(0.7, 0.9, 0.1)
series_funs = [
{
'title': '"rad_trncf":{0:.1f}|"snow_adj":{0:.1f}'.format(val),
'rad_trncf': _scale_param(val),
'snow_adj': _scale_param(val)
}
for val in scale_arange
]
s.build(series_funs)
g_series = glob.glob(os.path.join(self.scenarios_dir, '*'))
# this should be 5 because of series_metadata.json and inputs_dir
assert len(g_series) == 5, g_series
uuid_pattern = re.compile(
r'^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
)
uuid_dir_count = 0
non_uuid_dir_count = 0
series_dirs = []
found_metadata = False
for d in g_series:
# XXX TODO this is in flux. at this point maybe all will be dirs
if os.path.isdir(d):
# at this point the files are in the base scenario dir;
# they will be moved later when the scenario is run
assert_valid_input_dir(self, os.path.join(d))
if re.match(uuid_pattern, os.path.basename(d)):
uuid_dir_count += 1
assert re.match(uuid_pattern, os.path.basename(d)), os.path.basename(d)
else:
non_uuid_dir_count += 1
else:
found_metadata = True
series_dirs.append(d)
assert uuid_dir_count == 3
assert non_uuid_dir_count == 1
assert found_metadata
s.run()
g_series = glob.glob(os.path.join(self.scenarios_dir, '*'))
series_md_path = os.path.join(
self.scenarios_dir, 'series_metadata.json'
)
self.assertIn(series_md_path, g_series)
series_md = json.loads(open(series_md_path).read())
dir_titles = series_md['uuid_title_map']
titles = dir_titles.values()
assert '"rad_trncf":0.7|"snow_adj":0.7' in titles
assert '"rad_trncf":0.8|"snow_adj":0.8' in titles
assert '"rad_trncf":0.9|"snow_adj":0.9' in titles
p_base = Parameters(
os.path.join(self.test_model_data_dir, 'parameters')
)
for g in g_series:
if os.path.isdir(g) and 'input' not in g:
assert_valid_output_dir(self, os.path.join(g, 'outputs'))
md = json.loads(open(os.path.join(g, 'metadata.json')).read())
title = md['title']
scale_vals = eval('{' + title.replace('|', ',') + '}')
p_scen = Parameters(os.path.join(g, 'inputs', 'parameters'))
rad_base = p_base['rad_trncf']
rad_scen = p_scen['rad_trncf']
snow_base = p_base['snow_adj']
snow_scen = p_scen['snow_adj']
assert_array_almost_equal(
rad_base*scale_vals['rad_trncf'], rad_scen)
assert_array_almost_equal(
snow_base*scale_vals['snow_adj'], snow_scen)
def assert_valid_input_dir(test_case, d):
gs = [
os.path.basename(f)
for f in glob.glob(os.path.join(d, '*'))
]
test_case.assertIn('control', gs, d)
test_case.assertIn('parameters', gs, d)
test_case.assertIn('data', gs, d)
def assert_valid_output_dir(test_case, d):
go = [
os.path.basename(f)
for f in
glob.glob(os.path.join(d, '*'))
]
test_case.assertIn('prms_ic.out', go)
test_case.assertIn('prms.out', go)
test_case.assertIn('statvar.dat', go)
class TestParameters(unittest.TestCase):
def setUp(self):
self.test_data_dir = os.path.join('test', 'data')
self.test_param = os.path.join(self.test_data_dir, 'parameters')
self.temp_dir = os.path.join('test', 'data', 'tmp')
if os.path.isdir(self.temp_dir):
shutil.rmtree(self.temp_dir)
os.mkdir(self.temp_dir)
def tearDown(self):
if os.path.isdir(self.temp_dir):
shutil.rmtree(self.temp_dir)
def test_faithful_copy(self):
"""
Parameters class should load and write identical file
"""
test_copy = os.path.join(self.temp_dir, 'param_copy_test')
p = Parameters(self.test_param)
p.write(test_copy)
expected = open(self.test_param)
generated = open(test_copy)
# ignore first two lines ignore now date written on Parameters.write
expected = expected.readlines()[2:]
generated = generated.readlines()[2:]
assert expected == generated
def test_modify_params(self):
"parameter and data file modification access should work as expected"
# test parameters mod with 1d rad_trncf
params_out = os.path.join(self.temp_dir, 'modify_params_out')
mod_d = {'rad_trncf': lambda x: 1.1*x}
modify_params(self.test_param, params_out, mod_d)
generated = open(params_out).readlines()[2:]
expected = open(
os.path.join(
self.test_data_dir, 'expected_rad_trncf_1.1'
)
).readlines()[2:]
assert expected == generated
# test parameters mod with 2d snow_adj
params_out = os.path.join(self.temp_dir, 'modify_params_out')
mod_d = {'snow_adj': lambda x: 1.1*x}
modify_params(self.test_param, params_out, mod_d)
generated = open(params_out).readlines()[2:]
expected = open(
os.path.join(
self.test_data_dir, 'expected_snow_adj_1.1'
)
).readlines()[2:]
for a, b in zip(expected, generated):
try:
a = float(a)
b = float(b)
self.assertAlmostEqual(a, b, places=5)
except ValueError:
assert a == b
def colored_string_diff(s1, s2):
""" Writes differences between strings s1 and s2 """
d = Differ()
diff = d.compare(s1.splitlines(), s2.splitlines())
diffList = [el for el in diff
if el[0] != ' ' and el[0] != '?']
for l in diffList:
if l[0] == '+':
print(bcolors.GREEN + '+' + l[1:] + bcolors.ENDC)
elif l[0] == '-':
print(bcolors.RED + '-' + l[1:] + bcolors.ENDC)
else:
assert False, 'Error, diffList entry must start with + or -'
class bcolors:
HEADER = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'