Skip to content

Commit 68a370a

Browse files
committed
add ml_stability/pre-vs-post-m3gnet-relaxation.py
1 parent a16b46a commit 68a370a

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# %%
2+
from datetime import datetime
3+
4+
import pandas as pd
5+
import plotly.express as px
6+
import plotly.io as pio
7+
from pymatviz.utils import add_identity_line
8+
9+
from ml_stability import ROOT
10+
11+
12+
__author__ = "Janosh Riebesell"
13+
__date__ = "2022-06-18"
14+
15+
16+
pio.templates.default = "plotly_white"
17+
18+
today = f"{datetime.now():%Y-%m-%d}"
19+
20+
21+
# %%
22+
df_wbm = pd.read_json(
23+
f"{ROOT}/data/2022-06-26-wbm-cses-and-initial-structures.json.gz"
24+
).set_index("material_id")
25+
26+
27+
# %% spread M3GNet post-pseudo-relaxation lattice params into separate columns
28+
df_m3gnet = pd.read_json(
29+
f"{ROOT}/data/[email protected]"
30+
).set_index("material_id")
31+
32+
print("Number of WBM crystals for which we have M3GNet results:")
33+
print(f"{len(df_m3gnet):,} / {len(df_wbm):,} = {len(df_m3gnet)/len(df_wbm):.1%}")
34+
35+
36+
# %%
37+
df_m3gnet["final_energy"] = df_m3gnet.trajectory.map(lambda x: x["energies"][-1][0])
38+
39+
df_m3gnet_lattice = pd.json_normalize(
40+
df_m3gnet.final_structure.map(lambda x: x["lattice"])
41+
).add_prefix("m3gnet_")
42+
df_m3gnet[df_m3gnet_lattice.columns] = df_m3gnet_lattice.to_numpy()
43+
df_m3gnet
44+
45+
46+
# %% spread WBM initial and final lattice params into separate columns
47+
df_m3gnet["cse"] = df_wbm.cse
48+
df_wbm_final_lattice = pd.json_normalize(
49+
df_m3gnet.cse.map(lambda x: x["structure"]["lattice"])
50+
).add_prefix("final_wbm_")
51+
df_m3gnet[df_wbm_final_lattice.columns] = df_wbm_final_lattice.to_numpy()
52+
53+
54+
df_m3gnet["initial_structure"] = df_wbm.initial_structure
55+
df_wbm_initial_lattice = pd.json_normalize(
56+
df_m3gnet.initial_structure.map(lambda x: x["lattice"])
57+
).add_prefix("initial_wbm_")
58+
df_m3gnet[df_wbm_initial_lattice.columns] = df_wbm_initial_lattice.to_numpy()
59+
60+
61+
# %%
62+
df_wbm_final_lattice = pd.json_normalize(
63+
df_wbm.cse.map(lambda x: x["structure"]["lattice"])
64+
).add_prefix("final_wbm_")
65+
df_wbm = df_wbm.join(df_wbm_final_lattice)
66+
67+
df_wbm_initial_lattice = pd.json_normalize(
68+
df_wbm.initial_structure.map(lambda x: x["lattice"])
69+
).add_prefix("initial_wbm_")
70+
df_wbm[df_wbm_initial_lattice.columns] = df_wbm_initial_lattice
71+
72+
print(f"{df_wbm.isna().sum()=}")
73+
74+
df_wbm.query("initial_wbm_matrix.isna()")
75+
76+
77+
# %%
78+
px.histogram(
79+
df_m3gnet.filter(like="volume"),
80+
nbins=500,
81+
barmode="overlay",
82+
opacity=0.5,
83+
range_x=[0, 500],
84+
)
85+
86+
87+
# %%
88+
fig = px.scatter(
89+
df_m3gnet.round(1),
90+
x="final_wbm_volume",
91+
y=["initial_wbm_volume", "m3gnet_volume"],
92+
hover_data=[df_m3gnet.index],
93+
)
94+
add_identity_line(fig)
95+
fig.update_layout(
96+
title="Slightly tighter spread of M3GNet-relaxed vs initial WBM volumes"
97+
)
98+
fig.show()
99+
100+
101+
# %% histogram of alpha lattice angles (similar results for beta and gamma)
102+
fig = px.histogram(
103+
df_m3gnet.filter(like="alpha"), nbins=1000, barmode="overlay", log_y=True
104+
)
105+
# fig.write_image("plots/alpha-wbm-angles.png", scale=2)
106+
fig.show()
107+
108+
109+
# %%
110+
px.histogram(
111+
df_m3gnet.filter(regex="_c$"),
112+
nbins=1000,
113+
log_y=True,
114+
barmode="overlay",
115+
opacity=0.5,
116+
)

0 commit comments

Comments
 (0)