-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict_short.py
205 lines (175 loc) · 5.99 KB
/
predict_short.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
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
#!/bin/env python
# -*- coding: utf-8 -*-
#
# Created on 21.05.22
#
# Created for Paper SASIP screening
#
# @author: Tobias Sebastian Finn, [email protected]
#
# Copyright (C) {2022} {Tobias Sebastian Finn}
# System modules
import sys
sys.path.append("../../")
import logging
import argparse
import os
from typing import Tuple
# External modules
import xarray as xr
import torch
import dask
from tqdm import tqdm
from hydra.utils import instantiate
from hydra import initialize, compose
# Internal modules
from src_screening.utils import initialize_cluster_client
from src_screening.model import PropagateCorrect, FixedTimeIterator, \
available_combine_functions, NeuralNetworkPipeline
logger = logging.getLogger(__name__)
torch.set_num_threads(1)
os.environ["OMP_NUM_THREADS"] = "1"
dask.config.set({"distributed.comm.timeouts.tcp": "120s"})
dask.config.set({"distributed.comm.timeouts.connect": "120s"})
_namelist_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"meb_model", "config_coarse.cfg"
)
parser = argparse.ArgumentParser()
parser.add_argument("--processed_path", type=str, required=True)
parser.add_argument("--model_path", type=str, required=True)
parser.add_argument("--n_workers", type=int, default=64)
def load_model(
model_checkpoint: str,
) -> Tuple[torch.nn.Module, str, str]:
model_dir = os.path.dirname(model_checkpoint)
with initialize(config_path=os.path.join(model_dir, 'hydra')):
cfg = compose('config.yaml')
# To support old models
if "model" in cfg.keys():
cfg["model"]["_target_"] = cfg["model"]["_target_"].replace(
".model.", ".network."
)
cfg["model"]["backbone"]["_target_"] = cfg["model"]["backbone"]["_target_"].replace(
".model.", ".network."
)
model = instantiate(
cfg.model,
optimizer_config=cfg.optimizer,
_recursive_=False
)
else:
model = instantiate(
cfg.network,
optimizer_config=cfg.optimizer,
_recursive_=False
)
state_dict = torch.load(model_checkpoint, map_location=torch.device("cpu"))
model.load_state_dict(state_dict["state_dict"], strict=False)
model = model.eval().cpu()
input_type = "normal"
target_type = "normal"
if "input_type" in cfg["data"].keys():
input_type = cfg["data"]["input_type"]
target_type = cfg["data"]["target_type"]
return model, input_type, target_type
def generate_short_forecast(
zarr_store: str,
model_checkpoint: str,
):
ds_initial = xr.open_zarr("data/raw/test/forecast_data")
ds_initial = ds_initial.isel(lead_time=0)
ds_initial = ds_initial.set_index(samples=["ensemble", "time"])
ds_initial = ds_initial.unstack("samples")
ds_initial = ds_initial.transpose("ensemble", "time", ...)
ds_initial = ds_initial.compute()
ds_forcing_params = xr.open_dataset(
os.path.join("data/raw/test/forcing_params.nc")
)
logger.info("Data loaded")
network, input_type, target_type = load_model(model_checkpoint)
logger.info("Network loaded")
input_climatology = {
"mean": xr.open_dataset(
f"data/raw/train/climatology/input_{input_type:s}_mean.nc"
),
"std": xr.open_dataset(
f"data/raw/train/climatology/input_{input_type:s}_std.nc"
),
}
target_climatology = {
"mean": xr.open_dataset(
f"data/raw/train/climatology/target_{target_type:s}_mean.nc"
),
"std": xr.open_dataset(
f"data/raw/train/climatology/target_{target_type:s}_std.nc"
),
}
combine_function = available_combine_functions[input_type]
pipeline = NeuralNetworkPipeline(
network=network,
input_climatology=input_climatology,
target_climatology=target_climatology,
combine_function=combine_function
)
logger.info("Initialised neural network pipeline")
propagation_func = PropagateCorrect(
namelist_path=_namelist_path,
update_time="10min 8s",
integration_step="16s",
output_frequency=1,
pipeline=pipeline
)
try:
iterator = FixedTimeIterator(propagation_func=propagation_func)
iterator(
zarr_store=zarr_store,
ds_initial=ds_initial,
ds_forcing_params=ds_forcing_params,
lead_time="1 hour"
)
finally:
del pipeline
def iterate_through_dirs(
processed_path: str, path_to_search: str
):
list_of_files = {}
for (dirpath, dirnames, filenames) in os.walk(path_to_search):
for filename in filenames:
if filename == 'last.ckpt':
exp_path = dirpath.replace(path_to_search, "")
list_of_files[exp_path] = os.sep.join([dirpath, filename])
logger.info(f"Found {len(list_of_files):d} model runs")
for exp_name, ckpt_path in tqdm(list_of_files.items()):
logger.info(f"Model checkpoint: {ckpt_path:s}")
exp_processed_path = os.path.join(processed_path, exp_name)
zarr_store = os.path.abspath(
os.path.join(exp_processed_path, "traj_short")
)
logger.info(f"Zarr store path: {zarr_store:s}")
if os.path.isdir(zarr_store):
logger.info(
f"{zarr_store:s} already exists, skipping"
)
else:
generate_short_forecast(
zarr_store=zarr_store,
model_checkpoint=ckpt_path,
)
def main(args: argparse.Namespace):
model_path: str = args.model_path
if not model_path.endswith("/"):
model_path = model_path + "/"
iterate_through_dirs(
processed_path=args.processed_path,
path_to_search=model_path,
)
logger.info("Stored predictions for all model directories")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
args = parser.parse_args()
client = initialize_cluster_client(
n_workers=args.n_workers,
memory_limit="8GB",
)
main(args)