-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
249 lines (229 loc) · 8.4 KB
/
plot.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
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
"""
Script reading saved metrics for chosen models/ nowcasting methods from npy file
and plotting average metric values versus leadtime.
"""
import argparse
import os
from attrdict import AttrDict
import yaml
import shutil
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pincast_verif.io_tools import read_file
from pincast_verif.plot_tools import get_done_df_stats, plot_data_quality
from pincast_verif.metrics import *
def run(config, config_path=None):
plt.ioff()
# CONFIG
config_copy_path = config.config_copy_path
metric_exp_ids = config.metric_exp_ids
done_csv_path = config.done_csv_path
metrics_path_npy = config.metrics_npy_path
name_path = config.name_path
metrics = config.metrics
methods = config.methods
exp_id = config.exp_id
path_save = config.path_save
leadtimes = np.array(config.leadtimes) * config.timestep
dq_plot_params = config.dq_plot
cont_plot_params = config.cont_plot
cat_plot_params = config.cat_plot
fss_plot_params = config.fss_plot
rapsd_plot_params = config.rapsd_plot
is_plot_params = config.intensity_scale_plot
ssim_plot_params = config.ssim_plot
if config.stylefile is not None:
plt.style.use(config.stylefile)
# MAKE DIRS, COPY CONFIG FILE
for m in list(methods.keys()) + ["ALL", "DQ"]:
os.makedirs(
os.path.dirname(path_save.format(id=exp_id, method=m, metric="")),
exist_ok=True,
)
shutil.copyfile(src=config_path, dst=config_copy_path.format(id=exp_id))
# DATA QUALITY
done_dfs = {id: pd.read_csv(done_csv_path.format(id=id)) for id in metric_exp_ids}
df_stats = []
for i, df in done_dfs.items():
df_stats += get_done_df_stats(df, i)
plot_data_quality(df_stats, exp_id=exp_id, path_save=path_save, **dq_plot_params)
# FETCH SCORES
scores = dict()
for metric in metrics:
scores[metric] = {}
for method in methods.keys():
for id in metric_exp_ids:
try:
name_fn = name_path.format(id=id, metric=metric, method=method)
npy_fn = metrics_path_npy.format(
id=id, metric=metric, method=method
)
name_now = read_file(name_fn)
npy_now = np.load(npy_fn)
except:
continue
scores[metric].update({method: (npy_now, name_now)})
# Write to pd dataframe and csv file
methods_dfs = {}
for method_name, score_tuple in scores[metric].items():
methods_dfs[method_name] = pd.DataFrame(
score_tuple[0],
columns=np.arange(1, score_tuple[0].shape[1] + 1),
index=score_tuple[1],
)
score_df = pd.concat(methods_dfs.values(), keys=methods_dfs.keys())
csv_fn = (
Path(path_save.format(id=exp_id, method="ALL", metric=metric)).parents[1]
/ f"{metric}_values.csv"
)
# Sort to get nicer excels
score_df.index = score_df.index.swaplevel(0, 1)
score_df.sort_index(inplace=True)
score_df.to_csv(csv_fn)
# 1) compare all models for one metric at a time, save each as a figure
if metric == "CAT":
CategoricalMetric.plot(
scores=scores[metric],
method="ALL",
lt=leadtimes,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
cat_kwargs=cat_plot_params,
)
if metric == "CONT":
ContinuousMetric.plot(
scores=scores[metric],
method="ALL",
lt=leadtimes,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
cont_kwargs=cont_plot_params,
)
if metric == "FSS":
FssMetric.plot(
scores=scores[metric],
method="ALL",
lt=leadtimes,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
fss_kwargs=fss_plot_params,
)
if metric == "RAPSD":
RapsdMetric.plot(
data=scores[metric],
method="ALL",
leadtimes=rapsd_plot_params.lts,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
kwargs=rapsd_plot_params,
)
if metric == "CRPS":
Crps.plot(
scores=scores[metric],
method="ALL",
lt=leadtimes,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
crps_kwargs=cont_plot_params,
)
if metric == "SSIM":
SSIMMetric.plot(
scores=scores[metric],
method="ALL",
lt=leadtimes,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
ssim_kwargs=ssim_plot_params,
)
# 2) again for one model at a time
for method in methods.keys():
if metric == "CAT":
CategoricalMetric.plot(
scores=scores[metric],
method=method,
lt=leadtimes,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
cat_kwargs=cat_plot_params,
)
if metric == "CONT":
ContinuousMetric.plot(
scores=scores[metric],
method=method,
lt=leadtimes,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
cont_kwargs=cont_plot_params,
)
if metric == "FSS":
FssMetric.plot(
scores=scores[metric],
method=method,
lt=leadtimes,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
fss_kwargs=fss_plot_params,
)
if metric == "RAPSD":
RapsdMetric.plot(
data=scores[metric],
method=method,
leadtimes=rapsd_plot_params.lts,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
kwargs=rapsd_plot_params,
)
if metric == "INTENSITY_SCALE":
IntensityScaleMetric.plot(
exp_id=exp_id,
scores=scores[metric],
method=method,
path_save_fmt=path_save,
thresh=is_plot_params.thresh,
scales=is_plot_params.scales,
method_plot_params=methods,
kmperpixel=is_plot_params.kmperpixel,
vminmax=is_plot_params.vminmax,
)
if metric == "CRPS":
Crps.plot(
scores=scores[metric],
method=method,
lt=leadtimes,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
crps_kwargs=cont_plot_params,
)
if metric == "SSIM":
SSIMMetric.plot(
scores=scores[metric],
method=method,
lt=leadtimes,
exp_id=exp_id,
path_save=path_save,
method_plot_params=methods,
ssim_kwargs=ssim_plot_params,
)
del scores[metric]
if __name__ == "__main__":
argparser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
argparser.add_argument("config_path", type=str, help="Configuration file path")
args = argparser.parse_args()
with open(args.config_path, "r") as f:
config = AttrDict(yaml.safe_load(f))
run(config, config_path=args.config_path)