File size: 8,250 Bytes
79edee4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import glob
import pickle
from pathlib import Path

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from pymatgen.core import Element

from mlip_arena.models import REGISTRY

DATA_DIR = Path(__file__).parent

mlip_models = ["MACE-MP(M)", "MatterSim", "ORBv2", "M3GNet", "CHGNet", "SevenNet"]

fcc_pbe = pd.read_csv(DATA_DIR / "Table-A1-fcc.csv")
hcp_pbe = pd.read_csv(DATA_DIR / "Table-A2-hcp.csv")

# fcc

# Initialize an empty DataFrame
results_df = pd.DataFrame(columns=["symbol", "model", "fit_path", "fit_energies"])

for model in mlip_models:
    out_dir = Path(REGISTRY[model]["family"])

    for index, row in fcc_pbe.iterrows():
        symbol = row["symbol"]

        if Element(symbol.split("_")[0]).is_noble_gas:
            continue

        files = glob.glob(str(out_dir / f"{model}-fcc-{symbol.split('_')[0]}108.pkl"))
        if len(files) == 0:
            print("skip", model, symbol)
            # Add missing data to the DataFrame
            # if symbol not in results_df['symbol'].values:
            # Create a new row if the symbol is not yet in the DataFrame
            new_row = {
                "symbol": symbol,
                "model": model,
                "pbe_e_vacmig": row["e_vacmig"],
                "fit_path": [],
                "fit_energies": [],
            }
            results_df = pd.concat(
                [results_df, pd.DataFrame([new_row])], ignore_index=True
            )
            continue
        file = files[0]
        with open(file, "rb") as f:
            result = pickle.load(f)

        # Add data to the DataFrame
        # if symbol not in results_df['symbol'].values:
        # Create a new row if the symbol is not yet in the DataFrame
        forcefit = result["neb"]["forcefit"]
        new_row = {
            "symbol": symbol,
            "model": model,
            "pbe_e_vacmig": row["e_vacmig"],
            "fit_path": forcefit.fit_path,
            "fit_energies": forcefit.fit_energies,
        }
        results_df = pd.concat([results_df, pd.DataFrame([new_row])], ignore_index=True)


nrows = 2
ncols = len(mlip_models) // nrows

fig, axes = plt.subplots(
    nrows=nrows,
    ncols=ncols,
    figsize=(6, 4),
    sharex=True,
    sharey=True,
    constrained_layout=True,
    dpi=300,
)

for i, (ax, model) in enumerate(zip(axes.ravel(), mlip_models, strict=False)):
    filtered_df = results_df[results_df["model"] == model]

    asymmetries = []
    middle_deviations = []

    for index, row in filtered_df.iterrows():
        if len(row["fit_path"]) == 0 or pd.isna(row["pbe_e_vacmig"]):
            continue

        x = row["fit_path"] / max(row["fit_path"])
        y = row["fit_energies"] / row["pbe_e_vacmig"]

        # middle_idx = np.argmin(np.abs(x - 0.5))

        left_side = y[x <= 0.5]
        right_side = y[x >= 0.5][::-1]
        min_len = min(len(left_side), len(right_side))
        left_side = left_side[:min_len]
        right_side = right_side[:min_len]

        asymmetry = np.abs(left_side - right_side).mean()
        # middle = (left_side[-1] + right_side[-1]) / 2
        middle = max(y)

        if np.abs(np.array(y)).max() > 10:
            continue

        asymmetries.append(asymmetry)
        middle_deviations.append(middle - 1)

        ax.plot(
            x,
            y,
            alpha=0.5,
            color=method_color_mapping[model],
            label=model,
        )

    asymmetries = np.array(asymmetries)
    middle_deviations = np.array(middle_deviations)

    ax.text(
        0.05,
        0.95,
        "\n".join(
            [
                f"Miss: {len(filtered_df) - len(asymmetries) - filtered_df['pbe_e_vacmig'].isna().sum()}",
                f"Asym: {asymmetries.mean():.3f}",
                f"MAPE@max: {np.abs(middle_deviations).mean() * 100:.1f}",
            ]
        ),
        transform=ax.transAxes,
        ha="left",
        va="top",
        fontsize="small",
        # fontsize=6,
    )

    ax.set(
        title=model,
        xlabel="Normalized path" if i >= len(models) - ncols else None,
        ylabel="Normalized energy" if i % ncols == 0 else None,
        ylim=(-0.1, 2),
    )

with open(DATA_DIR / "fcc.pkl", "wb") as f:
    pickle.dump(fig, f)

# hcp

# Initialize an empty DataFrame
results_df = pd.DataFrame(columns=["symbol", "model", "fit_path", "fit_energies"])

for model in mlip_models:
    out_dir = Path(REGISTRY[model]["family"])

    for index, row in hcp_pbe.iterrows():
        symbol = row["symbol"]

        if Element(symbol.split("_")[0]).is_noble_gas:
            continue

        files = glob.glob(str(out_dir / f"{model}-hcp-{symbol.split('_')[0]}36.pkl"))
        if len(files) == 0:
            print("skip", model, symbol)
            # Add missing data to the DataFrame
            # if symbol not in results_df['symbol'].values:
            # Create a new row if the symbol is not yet in the DataFrame
            new_row = {
                "symbol": symbol,
                "model": model,
                "pbe_e_vacmig": row["e_vacmig"],
                "fit_path": [],
                "fit_energies": [],
            }
            results_df = pd.concat(
                [results_df, pd.DataFrame([new_row])], ignore_index=True
            )
            # else:
            #     # Update the existing row with the model's prediction
            #     results_df.loc[results_df['symbol'] == symbol, model] = pd.NA
            continue
        file = files[0]
        with open(file, "rb") as f:
            result = pickle.load(f)

        # Add data to the DataFrame
        # if symbol not in results_df['symbol'].values:
        # Create a new row if the symbol is not yet in the DataFrame
        forcefit = result["neb"]["forcefit"]
        new_row = {
            "symbol": symbol,
            "model": model,
            "pbe_e_vacmig": row["e_vacmig"],
            "fit_path": forcefit.fit_path,
            "fit_energies": forcefit.fit_energies,
        }
        results_df = pd.concat([results_df, pd.DataFrame([new_row])], ignore_index=True)



nrows = 2
ncols = len(mlip_models) // nrows

threshold = 0.10

fig, axes = plt.subplots(
    nrows=nrows,
    ncols=ncols,
    figsize=(6, 4),
    sharex=True,
    sharey=True,
    constrained_layout=True,
    dpi=300,
)

for i, (ax, model) in enumerate(zip(axes.ravel(), mlip_models, strict=False)):
    filtered_df = results_df[results_df["model"] == model]

    asymmetries = []
    middle_deviations = []

    for index, row in filtered_df.iterrows():
        if len(row["fit_path"]) == 0 or pd.isna(row["pbe_e_vacmig"]):
            continue

        x = row["fit_path"] / max(row["fit_path"])
        y = row["fit_energies"] / row["pbe_e_vacmig"]

        # middle_idx = np.argmin(np.abs(x - 0.5))

        left_side = y[x <= 0.5]
        right_side = y[x >= 0.5][::-1]
        min_len = min(len(left_side), len(right_side))
        left_side = left_side[:min_len]
        right_side = right_side[:min_len]

        asymmetry = np.abs(left_side - right_side).mean()
        # middle = (left_side[-1] + right_side[-1]) / 2
        middle = max(y)

        if np.abs(np.array(y)).max() > 10:
            continue

        asymmetries.append(asymmetry)
        middle_deviations.append(middle - 1)

        ax.plot(
            x,
            y,
            alpha=0.5,
            color=method_color_mapping[model],
            label=model,
        )

    asymmetries = np.array(asymmetries)
    middle_deviations = np.array(middle_deviations)

    ax.text(
        0.05,
        0.95,
        "\n".join(
            [
                f"Miss: {len(filtered_df) - len(asymmetries) - filtered_df['pbe_e_vacmig'].isna().sum()}",
                f"Asym: {asymmetries.mean():.3f}",
                f"MAPE@max: {np.abs(middle_deviations).mean() * 100:.1f}",
            ]
        ),
        transform=ax.transAxes,
        ha="left",
        va="top",
        fontsize="small",
    )

    ax.set(
        title=model,
        xlabel="Normalized path" if i >= len(mlip_models) - ncols else None,
        ylabel="Normalized energy" if i % ncols == 0 else None,
        ylim=(-0.1, 2),
    )

with open(DATA_DIR / "hcp.pkl", "wb") as f:
    pickle.dump(fig, f)