Skip to content

Commit 1c3cc92

Browse files
committed
add ml_stability/m3gnet/plots/2022-08-02-m3gnet-wbm-hull-dist-hist.pdf
and plots/from-rhys/2022-08-06-hist-wbm-batches-true-energy-all-elements.pdf
1 parent 1fb2149 commit 1c3cc92

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

ml_stability/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import os
22
from os.path import dirname
3+
from typing import Any, Generator, Sequence
34

45

56
PKG_DIR = dirname(__file__)
67
ROOT = dirname(PKG_DIR)
78

89
os.makedirs(f"{PKG_DIR}/plots", exist_ok=True)
10+
11+
12+
def chunks(xs: Sequence[Any], n: int) -> Generator[Sequence[Any], None, None]:
13+
return (xs[i : i + n] for i in range(0, len(xs), n))
14+
15+
16+
def as_dict_handler(obj: Any) -> dict[str, Any] | None:
17+
"""Use as default_handler kwarg to json.dump() or pandas.to_json()."""
18+
try:
19+
return obj.as_dict() # all MSONable objects implement as_dict()
20+
except AttributeError:
21+
return None # replace unhandled objects with None in serialized data

ml_stability/wbm_pre_vs_post_m3gnet_relaxation.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,34 @@
2424
).set_index("material_id")
2525

2626

27-
# %% spread M3GNet post-pseudo-relaxation lattice params into separate columns
27+
# %%
2828
df_m3gnet = pd.read_json(
29-
f"{ROOT}/data/2022-07-17@20-27-m3gnet-wbm-relax-results.json.gz"
29+
f"{ROOT}/data/2022-07-17-m3gnet-wbm-relax-results.json.gz"
3030
).set_index("material_id")
3131

3232
print("Number of WBM crystals for which we have M3GNet results:")
3333
print(f"{len(df_m3gnet):,} / {len(df_wbm):,} = {len(df_m3gnet)/len(df_wbm):.1%}")
3434

3535

36-
# %%
36+
# %% spread M3GNet post-pseudo-relaxation lattice params into separate columns
3737
df_m3gnet["final_energy"] = df_m3gnet.trajectory.map(lambda x: x["energies"][-1][0])
3838

3939
df_m3gnet_lattice = pd.json_normalize(
40-
df_m3gnet.final_structure.map(lambda x: x["lattice"])
40+
df_m3gnet.initial_structure.map(lambda x: x["lattice"])
4141
).add_prefix("m3gnet_")
4242
df_m3gnet[df_m3gnet_lattice.columns] = df_m3gnet_lattice.to_numpy()
4343
df_m3gnet
4444

4545

4646
# %% spread WBM initial and final lattice params into separate columns
47-
df_m3gnet["cse"] = df_wbm.cse
47+
df_m3gnet["final_wbm_structure"] = df_wbm.cse.map(lambda x: x["structure"])
4848
df_wbm_final_lattice = pd.json_normalize(
49-
df_m3gnet.cse.map(lambda x: x["structure"]["lattice"])
49+
df_m3gnet.final_wbm_structure.map(lambda x: x["lattice"])
5050
).add_prefix("final_wbm_")
5151
df_m3gnet[df_wbm_final_lattice.columns] = df_wbm_final_lattice.to_numpy()
5252

5353

54-
df_m3gnet["initial_structure"] = df_wbm.initial_structure
54+
df_m3gnet["initial_wbm_structure"] = df_wbm.initial_structure
5555
df_wbm_initial_lattice = pd.json_normalize(
5656
df_m3gnet.initial_structure.map(lambda x: x["lattice"])
5757
).add_prefix("initial_wbm_")
@@ -67,7 +67,7 @@
6767
df_wbm_initial_lattice = pd.json_normalize(
6868
df_wbm.initial_structure.map(lambda x: x["lattice"])
6969
).add_prefix("initial_wbm_")
70-
df_wbm[df_wbm_initial_lattice.columns] = df_wbm_initial_lattice
70+
df_wbm[df_wbm_initial_lattice.columns] = df_wbm_initial_lattice.to_numpy()
7171

7272
print(f"{df_wbm.isna().sum()=}")
7373

@@ -102,7 +102,6 @@
102102
fig = px.histogram(
103103
df_m3gnet.filter(like="alpha"), nbins=1000, barmode="overlay", log_y=True
104104
)
105-
# fig.write_image("plots/alpha-wbm-angles.png", scale=2)
106105
fig.show()
107106

108107

0 commit comments

Comments
 (0)