mvd001's picture
download
raw
2.1 kB
#!/usr/bin/env python3
"""Recompute MinHash signatures/bands (codecommon.MINHASH_VERSION) for staged sources whose code is stored in part-*.parquet
(publishable sources). Restricted sources keep no code, so they are re-ingested from raw instead.
Each part-X.parquet -> bands-X.parquet + sigs-X.npy (first occurrence of a sample_id within the source only).
Usage: resign.py <source> [<source> ...]"""
import sys, os, glob, numpy as np, pyarrow as pa, pyarrow.parquet as pq
from multiprocessing import Pool
sys.path.insert(0, os.path.dirname(__file__)); import codecommon as C
def sign(part):
t = pq.read_table(part, columns=["sample_id", "code", "language"])
out = {}
for sid, code, lang in zip(*(t.column(c).to_pylist() for c in ("sample_id", "code", "language"))):
if sid in out or code is None: continue
norm = C.normalize(code, lang)
if C.sha256(norm) != sid: raise SystemExit(f"hash mismatch in {part}: {sid}")
out[sid] = C.minhash(norm)
return part, out
for src in sys.argv[1:]:
d = f"{C.STAGE}/{src}"; parts = sorted(glob.glob(f"{d}/part-*.parquet"))
for f in glob.glob(f"{d}/bands-*.parquet") + glob.glob(f"{d}/sigs-*.npy"): os.remove(f)
seen, n = set(), 0
with Pool(6, maxtasksperchild=20) as pool:
for part, out in pool.imap(sign, parts): # ordered -> deterministic "first occurrence"
keep = [(s, g) for s, g in out.items() if s not in seen]; seen.update(s for s, _ in keep)
if not keep: continue
sfx = os.path.basename(part)[len("part-"):-len(".parquet")]
bl = [[s] + C.bands(g) for s, g in keep]; cols = list(zip(*bl))
pq.write_table(pa.table({nm: pa.array(c, type=t.type) for nm, c, t in zip(C.BAND_SCHEMA.names, cols, C.BAND_SCHEMA)}),
f"{d}/bands-{sfx}.parquet", compression="zstd")
np.save(f"{d}/sigs-{sfx}.npy", np.stack([g for _, g in keep])); n += len(keep)
open(f"{d}/MINHASH_VERSION", "w").write(str(C.MINHASH_VERSION))
print(f"[resign] {src}: {n} signatures from {len(parts)} parts", flush=True)

Xet Storage Details

Size:
2.1 kB
·
Xet hash:
b70e5a9f59996beb50280ff98b626b173389326e827f30f1d6d06a35d5804c24

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.