"""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 /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()