Skip to content

Commit 9fee132

Browse files
authored
Update example script (#80)
* Update example script * Update config file Change directory so actually works, delete an unnecessary variable, fix a comment * Update example script Include derivation of full global variability emulations and limit the number of trialed localisation radii to a smaller number since larger number of no added value with our incredibly coarse test data. * Tiny last updates
1 parent 61c9633 commit 9fee132

File tree

2 files changed

+25
-63
lines changed

2 files changed

+25
-63
lines changed

Diff for: configs/config_across_scen_T_cmip6ng_test.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# still work on windows (hard-coding "/" as file separators does not
1313
# work on windows)
1414
TEST_DATA_ROOT = os.path.join(
15-
MESMER_ROOT, "tests", "test-data", "first-run-test-coarse-grid"
15+
MESMER_ROOT, "tests", "test-data", "calibrate-coarse-grid"
1616
)
1717

1818
# ---------------------------------------------------------------------------------
@@ -53,15 +53,11 @@
5353
ref["start"] = "1850" # first included year
5454
ref["end"] = "1900" # last included year
5555

56-
time = {}
57-
time["start"] = "1850" # first included year
58-
time["end"] = "2100" # last included year #TODO: check if even used anywhere??
59-
6056
threshold_land = 1 / 3
6157

6258
wgt_scen_tr_eq = True # if True weigh each scenario equally (ie less weight to individ runs of scens with more ic members)
6359

64-
nr_emus_v = 5 # tmp made smaller for testing purposes. Normally 6000.
60+
nr_emus_v = 5 # tmp made smaller for testing purposes. Normally larger.
6561
scen_seed_offset_v = 0 # 0 meaning same emulations drawn for each scen, if put a number will have different ones for each scen
6662
max_iter_cv = 15 # max. nr of iterations in cross validation, small for testing purpose
6763

Diff for: examples/train_create_emus_automated.py

+23-57
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
# add pathway to folders 1 level higher (i.e., to mesmer and configs)
21
import sys
2+
import warnings
33

4+
# add pathway to configs folder
45
sys.path.append("../")
56

6-
import os.path
7-
import warnings
8-
97
# load in configurations used in this script
108
import configs.config_across_scen_T_cmip6ng_test as cfg
119

@@ -19,39 +17,28 @@
1917
create_emus_lt,
2018
create_emus_lv,
2119
)
22-
from mesmer.io import (
23-
load_cmipng,
24-
load_phi_gc,
25-
load_regs_ls_wgt_lon_lat,
26-
save_mesmer_bundle,
27-
)
20+
from mesmer.io import load_cmipng, load_phi_gc, load_regs_ls_wgt_lon_lat
2821
from mesmer.utils import convert_dict_to_arr, extract_land, separate_hist_future
2922

30-
# where to save the bundle
31-
bundle_out_file = os.path.join("tests", "test-data", "test-mesmer-bundle.pkl")
32-
os.makedirs(os.path.dirname(bundle_out_file), exist_ok=True)
33-
3423
# specify the target variable
3524
targ = cfg.targs[0]
3625
print(targ)
3726

3827
# load in the ESM runs
3928
esms = cfg.esms
29+
print(esms)
4030

4131
# load in tas with global coverage
4232
tas_g_dict = {} # tas with global coverage
4333
GSAT_dict = {} # global mean tas
44-
GHFDS_dict = {} # global mean hfds (needed as predictor)
4534
tas_g = {}
4635
GSAT = {}
47-
GHFDS = {}
4836
time = {}
4937

5038
for esm in esms:
5139
print(esm)
5240
tas_g_dict[esm] = {}
5341
GSAT_dict[esm] = {}
54-
GHFDS_dict[esm] = {}
5542
time[esm] = {}
5643

5744
for scen in cfg.scenarios:
@@ -62,19 +49,17 @@
6249

6350
if tas_g_tmp is None:
6451
warnings.warn(f"Scenario {scen} does not exist for tas for ESM {esm}")
65-
else: # if scen exists: save fields + load hfds fields for it too
52+
else: # if scen exists: save fields
6653
tas_g_dict[esm][scen], GSAT_dict[esm][scen], lon, lat, time[esm][scen] = (
6754
tas_g_tmp,
6855
GSAT_tmp,
6956
lon_tmp,
7057
lat_tmp,
7158
time_tmp,
7259
)
73-
_, GHFDS_dict[esm][scen], _, _, _ = load_cmipng("hfds", esm, scen, cfg)
7460

7561
tas_g[esm] = convert_dict_to_arr(tas_g_dict[esm])
7662
GSAT[esm] = convert_dict_to_arr(GSAT_dict[esm])
77-
GHFDS[esm] = convert_dict_to_arr(GHFDS_dict[esm])
7863

7964
# load in the constant files
8065
reg_dict, ls, wgt_g, lon, lat = load_regs_ls_wgt_lon_lat(cfg.reg_type, lon, lat)
@@ -89,62 +74,55 @@
8974
print(esm)
9075

9176
print(esm, "Start with global trend module")
77+
9278
params_gt_T = train_gt(GSAT[esm], targ, esm, time[esm], cfg, save_params=True)
93-
params_gt_hfds = train_gt(GHFDS[esm], "hfds", esm, time[esm], cfg, save_params=True)
9479

9580
preds_gt = {"time": time[esm]}
96-
emus_gt_T = create_emus_gt(
97-
params_gt_T, preds_gt, cfg, concat_h_f=True, save_emus=True
98-
)
9981
gt_T_s = create_emus_gt(
10082
params_gt_T, preds_gt, cfg, concat_h_f=False, save_emus=False
10183
)
84+
emus_gt_T = create_emus_gt(
85+
params_gt_T, preds_gt, cfg, concat_h_f=True, save_emus=True
86+
)
10287

10388
print(
10489
esm,
10590
"Start preparing predictors for global variability, local trends, and local variability",
10691
)
107-
gt_T2_s = {}
108-
for scen in gt_T_s.keys():
109-
gt_T2_s[scen] = gt_T_s[scen] ** 2
11092

111-
gt_hfds_s = create_emus_gt(
112-
params_gt_hfds, preds_gt, cfg, concat_h_f=False, save_emus=False
113-
)
114-
115-
gv_novolc_T = {}
116-
for scen in emus_gt_T.keys():
117-
gv_novolc_T[scen] = GSAT[esm][scen] - emus_gt_T[scen]
118-
gv_novolc_T_s, time_s = separate_hist_future(gv_novolc_T, time[esm], cfg)
93+
GSAT_s, time_s = separate_hist_future(GSAT[esm], time[esm], cfg)
94+
gv_novolc_T_s = {}
95+
for scen in gt_T_s.keys():
96+
gv_novolc_T_s[scen] = GSAT_s[scen] - gt_T_s[scen]
11997

12098
tas_s, time_s = separate_hist_future(tas[esm], time[esm], cfg)
12199

122100
print(esm, "Start with global variability module")
101+
123102
params_gv_T = train_gv(gv_novolc_T_s, targ, esm, cfg, save_params=True)
124103

125104
time_v = {}
105+
scen = list(emus_gt_T.keys())[0]
126106
time_v["all"] = time[esm][scen]
127-
# remember: scen comes from emus_gt_T.keys() here
128-
# (= necessary to derive compatible emus_gt & emus_gv)
129107
preds_gv = {"time": time_v}
130108
emus_gv_T = create_emus_gv(params_gv_T, preds_gv, cfg, save_emus=True)
131109

110+
# create full global emulations
132111
print(esm, "Merge the global trend and the global variability.")
133-
emus_g_T = create_emus_g(
112+
emus_g = create_emus_g(
134113
emus_gt_T, emus_gv_T, params_gt_T, params_gv_T, cfg, save_emus=True
135114
)
136115

137116
print(esm, "Start with local trends module")
117+
138118
preds = {
139119
"gttas": gt_T_s,
140-
"gttas2": gt_T2_s,
141-
"gthfds": gt_hfds_s,
142120
"gvtas": gv_novolc_T_s,
143-
} # predictors_list
144-
targs = {"tas": tas_s} # targets list
121+
}
122+
targs = {"tas": tas_s}
145123
params_lt, params_lv = train_lt(preds, targs, esm, cfg, save_params=True)
146124

147-
preds_lt = {"gttas": gt_T_s, "gttas2": gt_T2_s, "gthfds": gt_hfds_s}
125+
preds_lt = {"gttas": gt_T_s}
148126
lt_s = create_emus_lt(params_lt, preds_lt, cfg, concat_h_f=False, save_emus=True)
149127
emus_lt = create_emus_lt(params_lt, preds_lt, cfg, concat_h_f=True, save_emus=True)
150128

@@ -162,8 +140,8 @@
162140
# load in the auxiliary files
163141
aux = {}
164142
aux["phi_gc"] = load_phi_gc(
165-
lon, lat, ls, cfg, L_start=1750, L_end=2000, L_interval=250
166-
) # better results with default values L, but like this much faster + less space needed
143+
lon, lat, ls, cfg, L_start=1500, L_end=2000, L_interval=250
144+
)
167145

168146
# train lv AR1_sci on residual variability
169147
targs_res_lv = {"tas": res_lv_s}
@@ -178,15 +156,3 @@
178156
# create full emulations
179157
print(esm, "Merge the local trends and the local variability.")
180158
emus_l = create_emus_l(emus_lt, emus_lv, params_lt, params_lv, cfg, save_emus=True)
181-
182-
save_mesmer_bundle(
183-
bundle_out_file,
184-
params_lt,
185-
params_lv,
186-
params_gv_T,
187-
seeds=cfg.seed,
188-
land_fractions=ls["grid_l_m"],
189-
lat=lat["c"],
190-
lon=lon["c"],
191-
time=time_s,
192-
)

0 commit comments

Comments
 (0)