forked from MESMER-group/mesmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_calibrate_mesmer.py
85 lines (73 loc) · 2.66 KB
/
test_calibrate_mesmer.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
import os.path
import shutil
import joblib
import pytest
from mesmer.calibrate_mesmer import _calibrate_and_draw_realisations
from mesmer.testing import _check_dict
@pytest.mark.filterwarnings("ignore:No local minimum found")
@pytest.mark.parametrize(
"scenarios, outname",
(
[["h-ssp126"], "one_scen_one_ens"],
[["h-ssp585"], "one_scen_multi_ens"],
[["h-ssp126", "h-ssp585"], "multi_scen_multi_ens"],
),
)
def test_calibrate_mesmer(
scenarios, outname, test_data_root_dir, tmpdir, update_expected_files
):
ouput_dir = os.path.join(test_data_root_dir, "output", outname)
expected_output_file = os.path.join(ouput_dir, "test-mesmer-bundle.pkl")
params_output_dir = os.path.join(ouput_dir, "params")
test_esms = ["IPSL-CM6A-LR"]
test_scenarios_to_train = scenarios
test_target_variable = "tas"
test_reg_type = "srex"
test_threshold_land = 1 / 3
test_output_file = os.path.join(tmpdir, "test_calibrate_mesmer_output.pkl")
test_scen_seed_offset_v = 0
test_cmip_generation = 6
test_cmip_data_root_dir = os.path.join(
test_data_root_dir,
"calibrate-coarse-grid",
f"cmip{test_cmip_generation}-ng",
)
test_observations_root_dir = os.path.join(
test_data_root_dir,
"calibrate-coarse-grid",
"observations",
)
test_auxiliary_data_dir = os.path.join(
test_data_root_dir,
"calibrate-coarse-grid",
"auxiliary",
)
_calibrate_and_draw_realisations(
esms=test_esms,
scenarios_to_train=test_scenarios_to_train,
target_variable=test_target_variable,
reg_type=test_reg_type,
threshold_land=test_threshold_land,
output_file=test_output_file,
scen_seed_offset_v=test_scen_seed_offset_v,
cmip_data_root_dir=test_cmip_data_root_dir,
cmip_generation=test_cmip_generation,
observations_root_dir=test_observations_root_dir,
auxiliary_data_dir=test_auxiliary_data_dir,
# save params as well - they are .gitignored
save_params=update_expected_files,
params_output_dir=params_output_dir,
)
res = joblib.load(test_output_file)
if update_expected_files:
shutil.copyfile(test_output_file, expected_output_file)
pytest.skip(f"Updated {expected_output_file}")
else:
exp = joblib.load(expected_output_file)
assert isinstance(res, dict)
assert type(res) == type(exp)
assert res.keys() == exp.keys()
# check all keys of res match exp
_check_dict(res, exp, "result", "expected")
# check all keys of exp match res
_check_dict(exp, res, "expected", "result")