-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplt_ood.py
173 lines (151 loc) · 6.76 KB
/
plt_ood.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
from functools import lru_cache
import os
from typing import Optional
import matplotlib.pyplot as plt
import numpy as np
import pandas
import yaml
from matplotlib.axes import Axes
from matplotlib.lines import Line2D
from pandas import DataFrame
from lamstare.experiments.plt_test import COLOR, fetch_dptest_res, parse_record_dict_to_df
from lamstare.infra.ood_database import OODRecord
from lamstare.utils.plot import sendimg
with open(os.path.dirname(__file__) + "/../release/ood_test/OOD_DATASET_v2.yml", "r") as f:
OOD_DATASET = yaml.load(f, Loader=yaml.FullLoader)
OOD_DATASET = (
DataFrame(OOD_DATASET["OOD_TO_HEAD_MAP"]).T.rename_axis("Dataset").infer_objects()
)
# normalize weights
for index in OOD_DATASET.keys():
if "weight" in index:
efv_weight = OOD_DATASET[index]
efv_weight /= efv_weight.mean()
print(OOD_DATASET)
OOD_DATASET_STD = pandas.read_csv(
"/mnt/workspace/cc/LAMstare_new/lamstare/release/ood_test/ood_data_std.csv"
).infer_objects()
OOD_DATASET_STD.set_index("Dataset", inplace=True)
print(OOD_DATASET_STD)
@lru_cache
def get_weighted_result(exp_path: str) -> DataFrame:
run_id = exp_path.split("/")[-1] # Get basename as id
all_records = fetch_dptest_res(run_id, OODRecord)
all_records_df = parse_record_dict_to_df(all_records)
all_records_df_raw = all_records_df.copy()
# print(all_records_df)
# Remove records with zero weights
all_records_df.mask(all_records_df.isna(), inplace=True)
weighted_avg = all_records_df.groupby(
"Training Steps"
).mean().map(lambda x: np.nan) # provide a df with same shape
# mask.inplace and update() won't work; need to assign to a new variable
for efv in ["energy", "force", "virial"]:
data = all_records_df.loc[
:, [key for key in all_records_df.keys() if efv in key]
]
weights = OOD_DATASET[efv + "_weight"]
weighted_avg_efv = (
data.apply(np.log)
.mul(weights, axis="index")
.groupby("Training Steps")
.mean()
.apply(np.exp)
)
# mask out the results where NAN exists in the original data
weighted_avg_efv.mask(all_records_df.isna().any(axis=1).groupby("Training Steps").any(), inplace=True)
weighted_avg.update(weighted_avg_efv)
weighted_avg["Dataset"] = "Weighted"
weighted_avg.reset_index(inplace=True)
weighted_avg.set_index(["Dataset", "Training Steps"], inplace=True)
all_records_df = pandas.concat(
[all_records_df_raw, weighted_avg]
) # Preserve masked values
print(all_records_df)
return all_records_df
def plotting(
dataset_to_subplot: dict[str, list[Axes]],
all_records_df: DataFrame,
color: str,
legend_handles: list[Line2D],
metric_key: str="rmse"
):
for dataset, records in all_records_df.groupby("Dataset"):
assert dataset in dataset_to_subplot.keys(), f"Dataset {dataset} not presented"
subplot = dataset_to_subplot[dataset] # type: ignore
# print(dataset)
records = records.droplevel("Dataset")
# print(records)
subplot[0].set_ylabel(f"{dataset}")
for efv, suffix, subsubplot in zip(
["energy", "force", "virial"], ["_natoms", "", "_natoms"], subplot
):
if dataset in OOD_DATASET_STD.index:
std: numpy.float = OOD_DATASET_STD.loc[dataset, f"{efv}_std"] # type: ignore
if efv == "virial" and dataset != "Weighted" and np.isnan(std):
break # Careful! virial should be the last element in for loop
subsubplot.axhline(std, color="purple", linestyle="-.")
# note: this will draw duplicated lines
metric_name = efv + f"_{metric_key}" + suffix
line = subsubplot.loglog(
records.index, # step
records[metric_name],
"o-",
label=dataset,
color=color,
alpha=0.8,
)
legend_handles.extend(line) # type: ignore
def main(exps: list[str], metric_key: str="rmse"):
# Get dataset list from yaml file to preserve the order
datasets: list[str] = OOD_DATASET.index.tolist()
datasets.append("Weighted")
print(datasets)
fig, ax = plt.subplots(
len(datasets), 3, figsize=(12, 3 * len(datasets)), sharex=True
)
ax: list[list[Axes]]
legend_handles: list[Line2D] = []
# get axis by dataset name to prevent plotting on wrong axis
dataset_to_subplot = dict(zip(datasets, ax))
# add energy/force/virial to the beginning of plots
for axis, efv in zip(ax[0], ["energy", "force", "virial"]):
axis.set_title(efv)
for exp_path, color in zip(exps, COLOR):
all_records_df = get_weighted_result(exp_path)
plotting(dataset_to_subplot, all_records_df, color, legend_handles, metric_key)
## to set finer tick
from matplotlib.ticker import FixedLocator
ax[-1][0].yaxis.set_major_locator(FixedLocator(np.arange(0.02, 0.04, 0.002)))
ax[-1][1].yaxis.set_major_locator(FixedLocator(np.arange(0.2, 0.5, 0.04)))
## to handle hpt explosion
# for ax in dataset_to_subplot["HPt_NC_2022"]:
# ax.set_ylim(0.05,0.2)
fig.tight_layout()
fig.subplots_adjust(top=0.975)
title = f"Compare OOD-{metric_key}"
# fig.suptitle(title) # Poor placement
fig.legend(
handles=legend_handles,
labels=[exp_path.split("/")[-1] for exp_path in exps],
loc="upper left",
frameon=False,
)
filename = title + ".jpg"
fig.savefig(filename)
print(f"Saved to {filename}")
sendimg([filename], title)
if __name__ == "__main__":
exps = [
# "/mnt/data_nas/public/multitask/training_exps/1122_shareft_lr1e-3_1e-5_pref0021_1000100_24GUP_240by3_single_384_96_24",
"/mnt/data_nas/public/multitask/training_exps/1126_prod_shareft_120GUP_240by3_single_384_96_24",
# "/mnt/data_nas/public/multitask/training_exps/1223_prod_shareft_40GPU_finetune_pref0210_10010",
# "/mnt/data_nas/public/multitask/training_exps/1226_prod_shareft_40GPU_finetune_pref0210_10010_lr1e-5",
"/mnt/data_nas/public/multitask/training_exps/1225_dpa3a_shareft_rc6_120_arc_4_30_l6_120GPU_240by3_384_96_32_comp1",
"/mnt/data_nas/public/multitask/training_exps/0105_dpa3a_shareft_384_96_32_scp1_e1a_tanh_rc6_120_arc_4_30_l6_120GPU_240by3",
# "/mnt/workspace/public/multitask/training_exps/N0130_dpa3a_shareft_128_64_32_scp1_e1a_cdsilu10_rc6_120_arc_4_30_l6_64GPU_240by3_float32",
"/mnt/workspace/public/multitask/training_exps/0202_dpa3a_shareft_256_128_32_scp1_e1a_csilu10_rc6_120_arc_4_30_l9_104GPU_240by3"
# "/mnt/data_nas/public/multitask/training_exps/0115_dpa3a_shareft_128_64_32_scp1_e1a_tanh_rc6_120_arc_4_30_l6_64GPU_240by3_float32"
]
main(exps)
main(exps, "mae")