MaterialsSaddles / scripts /dedup /verify_integrity.py
sunghjung3's picture
Merge v2-dedup (77d73af) into main, part 12
46709a8 verified
Raw History Blame Contribute Delete
2.68 kB
"""Audit 1: assumptions pass 1/2 relied on, checked on EVERY row of the released shards (read-only).
* the whole dataset's ms_ids are exactly 0..3N-1, each once
* within each triplet (3k, 3k+1, 3k+2): identical atomic numbers in identical order, identical cell, identical pbc
* the stored side (dimer rows) equals ms % 3 - 1
Also counts per-shard contiguity (whether each shard's ms_ids form one consecutive run, which SaddleFlow's loader assumes).
usage: verify_integrity.py [workers]
"""
import glob, sys, os
import numpy as np
from multiprocessing import Pool
ROOT = "/scratch/08405/ilgar/MaterialsSaddles"; SUBS = ["lemat", "mp20bat", "oc20", "oc22"]
def work(path):
from ase.db import connect
rows = {}
for row in connect(path).select():
ms = int(row.key_value_pairs["ms_id"]); side = row.key_value_pairs.get("side")
rows[ms] = (row.numbers.tobytes(), np.array(row.cell, float), tuple(bool(x) for x in row.pbc), side)
bad_z = bad_cell = bad_pbc = bad_side = incomplete = 0
for ms, (z, c, p, side) in rows.items():
if side is not None and int(side) != ms % 3 - 1: bad_side += 1
if ms % 3 != 1: continue
if ms - 1 not in rows or ms + 1 not in rows: incomplete += 1; continue
for o in (ms - 1, ms + 1):
zo, co, po, _ = rows[o]
if zo != z: bad_z += 1
if np.abs(co - c).max() > 1e-6: bad_cell += 1
if po != p: bad_pbc += 1
ids = np.sort(np.fromiter(rows.keys(), np.int64))
contiguous = bool(len(ids) == 0 or ids[-1] - ids[0] + 1 == len(ids))
return path, ids, dict(bad_z=bad_z, bad_cell=bad_cell, bad_pbc=bad_pbc, bad_side=bad_side, incomplete=incomplete), contiguous
if __name__ == "__main__":
nw = int(sys.argv[1]) if len(sys.argv) > 1 else 64
paths = sorted(p for s in SUBS for p in glob.glob(f"{ROOT}/{s}/*.aselmdb"))
paths.sort(key=lambda p: -os.path.getsize(p))
tot = dict(bad_z=0, bad_cell=0, bad_pbc=0, bad_side=0, incomplete=0); allids = []; noncontig = []
with Pool(nw) as pool:
for k, (p, ids, c, contig) in enumerate(pool.imap_unordered(work, paths)):
for key in tot: tot[key] += c[key]
allids.append(ids)
if not contig: noncontig.append(os.path.basename(p))
if (k + 1) % 64 == 0: print(f" {k+1}/{len(paths)} shards", flush=True)
ids = np.sort(np.concatenate(allids))
print("rows:", len(ids), " ms_id range:", ids[0], ids[-1], " exactly 0..3N-1 once each:", bool(np.array_equal(ids, np.arange(len(ids)))))
print("per-triplet mismatches:", tot)
print("shards whose ms_ids are NOT one consecutive run:", len(noncontig), noncontig[:5])