Datasets:
Download scripts/dedup/dedup_pass2.py from SciLM/MaterialsSaddles: direct link, hf CLI and curl.
- Browser
- Download file 5.15 kB
-
https://huggingface.co/datasets/SciLM/MaterialsSaddles/resolve/main/scripts/dedup/dedup_pass2.py
- Command line
-
hf download hf://datasets/SciLM/MaterialsSaddles/scripts/dedup/dedup_pass2.py
-
curl -L -o dedup_pass2.py https://huggingface.co/datasets/SciLM/MaterialsSaddles/resolve/main/scripts/dedup/dedup_pass2.py
5.15 kB
| """Pass 2: duplicate clusters, one bucket per worker. | |
| Within each exact-composition group (all subsets together), triplets are taken in ms_id order and leader-clustered: | |
| a triplet joins the first cluster whose representative has | |
| * a cell within 2 % on every length and 1 degree on every angle, | |
| * a saddle within 0.2 A max-atom distance, and | |
| * both endpoints within 0.2 A max-atom, in the same or the swapped (R<->P) orientation; | |
| otherwise it founds a new cluster. Distances: species-wise Hungarian assignment (min summed squared distance) under the | |
| minimum-image convention, fractional coordinates mapped into the mean of the two cells. The representative (kept | |
| triplet) of every cluster is its lowest ms_id. | |
| Writes clusters/b<NN>.npz: ms, rep (representative ms of its cluster), and per-bucket counters. | |
| usage: dedup_pass2.py [workers] [threshold_A] | |
| """ | |
| import glob, os, sys, time | |
| import numpy as np | |
| from collections import defaultdict | |
| from multiprocessing import Pool | |
| from scipy.optimize import linear_sum_assignment | |
| OUT = "/scratch/08405/ilgar/saddleflow/data/dedup_2026-09-28"; NB = 64 | |
| THR = float(sys.argv[2]) if len(sys.argv) > 2 else 0.2 | |
| def cellpar(c): | |
| a, b, g = c | |
| L = np.linalg.norm(c, axis=1) | |
| ang = lambda u, v: np.degrees(np.arccos(np.clip(u @ v / np.linalg.norm(u) / np.linalg.norm(v), -1, 1))) | |
| return np.array([L[0], L[1], L[2], ang(b, g), ang(a, g), ang(a, b)]) | |
| def maxd(p1, c1, p2, c2, z, species, stop): | |
| """max-atom distance; returns early (> stop) as soon as one species exceeds `stop`.""" | |
| periodic = abs(np.linalg.det(c1)) > 1e-6 and abs(np.linalg.det(c2)) > 1e-6 | |
| if periodic: | |
| cm = 0.5 * (c1 + c2); inv = np.linalg.inv(cm) | |
| q1 = (p1 @ np.linalg.inv(c1)) @ cm; q2 = (p2 @ np.linalg.inv(c2)) @ cm | |
| else: # singular cell: plain Cartesian distances, no minimum image | |
| q1, q2 = p1.astype(float), p2.astype(float) | |
| mx = 0.0 | |
| for idx in species: | |
| dv = (q1[idx][:, None, :] - q2[idx][None, :, :]).reshape(-1, 3) | |
| if periodic: | |
| f = dv @ inv; f -= np.round(f); dv = f @ cm | |
| d2 = (dv.reshape(len(idx), len(idx), 3) ** 2).sum(2) | |
| r, k = linear_sum_assignment(d2); m = float(np.sqrt(d2[r, k].max())) | |
| if m > mx: | |
| mx = m | |
| if mx >= stop: return mx | |
| return mx | |
| def work(b): | |
| fs = sorted(glob.glob(f"{OUT}/buckets/b{b:02d}/*.npz")) | |
| if not fs: return b, 0, 0, 0, 0, 0 | |
| parts = [np.load(f) for f in fs] | |
| ms = np.concatenate([p["ms"] for p in parts]); nat = np.concatenate([p["nat"] for p in parts]); comp = np.concatenate([p["comp"] for p in parts]) | |
| cells = np.concatenate([p["cell"] for p in parts]) | |
| def cat(k): return np.concatenate([p[k] for p in parts]) | |
| Z, FX, R, S, P = cat("z"), cat("fx"), cat("R"), cat("S"), cat("P") | |
| off = np.concatenate([[0], np.cumsum(nat)]) | |
| groups = defaultdict(list) | |
| for i in np.argsort(ms, kind="stable"): groups[comp[i]].append(i) | |
| rep = np.empty(len(ms), np.int64); n_cmp = n_cell = 0; n_sing = int(sum(abs(np.linalg.det(c)) <= 1e-6 for c in cells)) | |
| for g in groups.values(): | |
| reps = [] # (index, cellpar) | |
| z = Z[off[g[0]]:off[g[0] + 1]]; species = [np.where(z == v)[0] for v in np.unique(z)] | |
| for i in g: | |
| ci = cells[i]; cpi = cellpar(ci); sl_i = slice(off[i], off[i + 1]); hit = -1 | |
| for j, cpj in reps: | |
| if np.any(np.abs(cpi[:3] - cpj[:3]) > 0.02 * cpj[:3]) or np.any(np.abs(cpi[3:] - cpj[3:]) > 1.0): continue | |
| cj = cells[j] | |
| # same lattice in the same orientation: every cell-matrix entry within 2 % of the longest cell vector | |
| if np.abs(ci - cj).max() > 0.02 * cpj[:3].max(): continue | |
| n_cell += 1; sl_j = slice(off[j], off[j + 1]) | |
| if maxd(S[sl_i], ci, S[sl_j], cj, z, species, THR) >= THR: continue | |
| n_cmp += 1 | |
| same = max(maxd(R[sl_i], ci, R[sl_j], cj, z, species, THR), maxd(P[sl_i], ci, P[sl_j], cj, z, species, THR)) | |
| if same < THR: hit = j; break | |
| swap = max(maxd(R[sl_i], ci, P[sl_j], cj, z, species, THR), maxd(P[sl_i], ci, R[sl_j], cj, z, species, THR)) | |
| if swap < THR: hit = j; break | |
| if hit < 0: reps.append((i, cpi)); rep[i] = ms[i] | |
| else: rep[i] = ms[hit] | |
| os.makedirs(f"{OUT}/clusters", exist_ok=True) | |
| np.savez(f"{OUT}/clusters/b{b:02d}.npz", ms=ms, rep=rep) | |
| return b, len(ms), int((rep != ms).sum()), n_cell, n_cmp, n_sing | |
| if __name__ == "__main__": | |
| nw = int(sys.argv[1]) if len(sys.argv) > 1 else 64; t0 = time.time(); tot = dup = 0 | |
| with Pool(nw) as pool: | |
| nsing = 0 | |
| for b, n, d, nc, nm, ns in pool.imap_unordered(work, range(NB)): | |
| tot += n; dup += d; nsing += ns | |
| print(f" bucket {b:02d}: {n} triplets, {d} duplicates, {nc} cell-compatible comparisons, {nm} saddle matches ({time.time()-t0:.0f}s)", flush=True) | |
| print(f"pass 2 done: {tot} triplets, {dup} duplicates ({100*dup/max(tot,1):.2f}%), {nsing} singular cells, {time.time()-t0:.0f}s") | |