File size: 2,369 Bytes
39ea985
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Write a flat per-calculation index for a store: one row per calculation with its shard, row
number and the handful of columns most queries filter on. Lets a consumer find a calculation
without opening every shard.

    python build_index.py --store $PSCRATCH/omol_100k
writes <store>/index.tsv
"""
from __future__ import annotations
import argparse, glob, os
import numpy as np
import zarr


def p2_shards(store):
    out = []
    for depth in ("*", "*/*/*"):
        out += glob.glob(os.path.join(store, "p2", depth, "*.zarr"))
    return sorted(set(out))


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--store", required=True)
    args = ap.parse_args()
    shards = p2_shards(args.store)
    out = os.path.join(args.store, "index.tsv")
    n = 0
    cols = ["calc_id", "dataset", "shard", "row", "n_atoms", "nbas", "nelec", "charge", "mult",
            "is_uhf", "has_fock", "scf_converged", "mo_ok", "e_total", "conv_diiserr", "rel_path"]
    with open(out, "w") as fh:
        fh.write("\t".join(cols) + "\n")
        for sp in shards:
            g = zarr.open_group(sp, mode="r")
            rel = os.path.relpath(sp, os.path.join(args.store, "p2"))
            ids, rels, ds = g.attrs["calc_id"], g.attrs["rel_path"], g.attrs["dataset"]
            si = np.asarray(g["scalar_i"]); ic = {k: j for j, k in enumerate(g.attrs["scalar_i_cols"])}
            sf = np.asarray(g["scalar_f8"]); fc_ = {k: j for j, k in enumerate(g.attrs["scalar_f8_cols"])}
            fl = np.asarray(g["flags"]); fc = {k: j for j, k in enumerate(g.attrs["flag_cols"])}
            if g.attrs.get("has_mo"):
                cf = np.asarray(g["mo_flags"])[:, g.attrs["mo_flag_cols"].index("mo_ok")]
            else:
                cf = np.full(len(ids), -1)
            for i in range(len(ids)):
                fh.write("\t".join(map(str, [
                    ids[i], ds, rel, i, si[i, ic["n_atoms"]], si[i, ic["nbas"]], si[i, ic["nelec"]],
                    si[i, ic["charge"]], si[i, ic["mult"]], int(fl[i, fc["is_uhf"]]),
                    int(fl[i, fc["has_fock"]]), int(fl[i, fc["scf_converged"]]), int(cf[i]),
                    f"{sf[i, fc_['e_total']]:.10f}", f"{sf[i, fc_['conv_diiserr']]:.3e}", rels[i]])) + "\n")
                n += 1
    print(f"wrote {out}: {n:,} rows from {len(shards)} shards")


if __name__ == "__main__":
    main()