File size: 7,405 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
from pathlib import Path

import numpy as np
import pandas as pd
from ase.db import connect
from scipy import stats

from mlip_arena.models import REGISTRY, MLIPEnum

DATA_DIR = Path(__file__).parent.absolute()


def load_wbm_structures():
    """
    Load the WBM structures from a ASE DB file.
    """
    with connect(DATA_DIR.parent / "wbm_structures.db") as db:
        for row in db.select():
            yield row.toatoms(add_additional_information=True)

def gather_results():
    for model in MLIPEnum:
        if "wbm_ev" not in REGISTRY[model.name].get("gpu-tasks", []):
            continue

        if (DATA_DIR / f"{model.name}.parquet").exists():
            continue

        all_data = []

        for atoms in load_wbm_structures():
            fpath = Path(model.name) / f"{atoms.info['key_value_pairs']['wbm_id']}.json"
            if not fpath.exists():
                continue

            all_data.append(pd.read_json(fpath))

        df = pd.concat(all_data, ignore_index=True)
        df.to_parquet(DATA_DIR / f"{model.name}.parquet")


def summarize():
    summary_table = pd.DataFrame(
        columns=[
            "model",
            "energy-diff-flip-times",
            "tortuosity",
            "spearman-compression-energy",
            "spearman-compression-derivative",
            "spearman-tension-energy",
            "missing",
        ]
    )


    for model in MLIPEnum:
        fpath = DATA_DIR / f"{model.name}.parquet"
        if not fpath.exists():
            continue
        df_raw_results = pd.read_parquet(fpath)

        df_analyzed = pd.DataFrame(
            columns=[
                "model",
                "structure",
                "formula",
                "volume-ratio",
                "energy-delta-per-atom",
                "energy-diff-flip-times",
                "tortuosity",
                "spearman-compression-energy",
                "spearman-compression-derivative",
                "spearman-tension-energy",
                "missing",
            ]
        )

        for wbm_struct in load_wbm_structures():
            structure_id = wbm_struct.info["key_value_pairs"]["wbm_id"]

            try:
                results = df_raw_results.loc[df_raw_results["id"] == structure_id]
                results = results["eos"].values[0]
                es = np.array(results["energies"])
                vols = np.array(results["volumes"])
                vol0 = wbm_struct.get_volume()

                indices = np.argsort(vols)
                vols = vols[indices]
                es = es[indices]

                imine = len(es) // 2
                # min_center_val = np.min(es[imid - 1 : imid + 2])
                # imine = np.where(es == min_center_val)[0][0]
                emin = es[imine]

                interpolated_volumes = [
                    (vols[i] + vols[i + 1]) / 2 for i in range(len(vols) - 1)
                ]
                ediff = np.diff(es)
                ediff_sign = np.sign(ediff)
                mask = ediff_sign != 0
                ediff = ediff[mask]
                ediff_sign = ediff_sign[mask]
                ediff_flip = np.diff(ediff_sign) != 0

                etv = np.sum(np.abs(np.diff(es)))

                data = {
                    "model": model.name,
                    "structure": structure_id,
                    "formula": wbm_struct.get_chemical_formula(),
                    "missing": False,
                    "volume-ratio": vols / vol0,
                    "energy-delta-per-atom": (es - emin) / len(wbm_struct),
                    "energy-diff-flip-times": np.sum(ediff_flip).astype(int),
                    "tortuosity": etv / (abs(es[0] - emin) + abs(es[-1] - emin)),
                    "spearman-compression-energy": stats.spearmanr(
                        vols[:imine], es[:imine]
                    ).statistic,
                    "spearman-compression-derivative": stats.spearmanr(
                        interpolated_volumes[:imine], ediff[:imine]
                    ).statistic,
                    "spearman-tension-energy": stats.spearmanr(
                        vols[imine:], es[imine:]
                    ).statistic,
                }

            except Exception:
                data = {
                    "model": model.name,
                    "structure": structure_id,
                    "formula": wbm_struct.get_chemical_formula(),
                    "missing": True,
                    "volume-ratio": None,
                    "energy-delta-per-atom": None,
                    "energy-diff-flip-times": None,
                    "tortuosity": None,
                    "spearman-compression-energy": None,
                    "spearman-compression-derivative": None,
                    "spearman-tension-energy": None,
                }

            df_analyzed = pd.concat([df_analyzed, pd.DataFrame([data])], ignore_index=True)

        df_analyzed.to_parquet(DATA_DIR / f"{model.name}_processed.parquet")
        # json_fpath = DATA_DIR / f"EV_scan_analyzed_{model.name}.json"

        # df_analyzed.to_json(json_fpath, orient="records")

        valid_results = df_analyzed[df_analyzed["missing"] == False]

        analysis_summary = {
            "model": model.name,
            "energy-diff-flip-times": valid_results["energy-diff-flip-times"].mean(),
            "tortuosity": valid_results["tortuosity"].mean(),
            "spearman-compression-energy": valid_results[
                "spearman-compression-energy"
            ].mean(),
            "spearman-compression-derivative": valid_results[
                "spearman-compression-derivative"
            ].mean(),
            "spearman-tension-energy": valid_results["spearman-tension-energy"].mean(),
            "missing": len(df_analyzed[df_analyzed["missing"] == True]),
        }
        summary_table = pd.concat(
            [summary_table, pd.DataFrame([analysis_summary])], ignore_index=True
        )


    flip_rank = (
        (summary_table["energy-diff-flip-times"] - 1)
        .abs()
        .rank(ascending=True, method="min")
    )
    tortuosity_rank = summary_table["tortuosity"].rank(ascending=True, method="min")
    spearman_compression_energy_rank = summary_table["spearman-compression-energy"].rank(
        method="min"
    )
    spearman_compression_derivative_rank = summary_table[
        "spearman-compression-derivative"
    ].rank(ascending=False, method="min")
    spearman_tension_energy_rank = summary_table["spearman-tension-energy"].rank(
        ascending=False, method="min"
    )
    missing_rank = summary_table["missing"].rank(ascending=True, method="min")

    rank_aggr = (
        flip_rank
        + tortuosity_rank
        + spearman_compression_energy_rank
        + spearman_compression_derivative_rank
        + spearman_tension_energy_rank
        + missing_rank
    )
    rank = rank_aggr.rank(method="min")

    summary_table.insert(1, "rank", rank.astype(int))
    summary_table.insert(2, "rank-aggregation", rank_aggr.astype(int))
    summary_table = summary_table.sort_values(by="rank", ascending=True)
    summary_table = summary_table.reset_index(drop=True)

    summary_table.to_csv(DATA_DIR / "summary.csv", index=False)
    summary_table.to_latex(DATA_DIR / "summary.tex", index=False)

    return summary_table

if __name__ == "__main__":
    gather_results()
    summarize()