Download code/work/edit_parts.py from bryan7264/lorapop: direct link, hf CLI and curl.
- Browser
- Download file 7.1 kB
-
https://huggingface.co/datasets/bryan7264/lorapop/resolve/main/code/work/edit_parts.py
- Command line
-
hf download hf://datasets/bryan7264/lorapop/code/work/edit_parts.py
-
curl -L -o edit_parts.py https://huggingface.co/datasets/bryan7264/lorapop/resolve/main/code/work/edit_parts.py
7.1 kB
| # MIT License, Bryan Cheng, 2026. One-off source edit (D-036): per-trait resumable parts + lock claiming for scan_extras.py | |
| # and obs_boot.py (the 1,000-resample extras bootstrap takes ~14 min per trait, beyond a 6 h job for 53 traits). | |
| p = "src/analysis/scan_extras.py" | |
| s = open(p).read() | |
| s = s.replace(''' --scan results/scans/qwen05 --out results/scans/qwen05/extras.json | |
| """''', ''' --scan results/scans/qwen05 --out results/scans/qwen05/extras.json [--claim] [--shard i --nshards n] | |
| Per-trait results go to <out>.parts/<trait>.json as they finish (resumable; with --claim any number of processes share the | |
| traits through lock files, D-036); <out> is written once every primary trait has a part. The bootstrap RNG is seeded per | |
| trait by (seed, trait index). | |
| """''') | |
| s = s.replace("from src.run.scan_run import blocks_of, cosine # noqa: E402", | |
| "from src.run.intervene import HELD, claim, unlock # noqa: E402\nfrom src.run.scan_run import blocks_of, cosine # noqa: E402") | |
| s = s.replace(''' ap.add_argument("--seed", type=int, default=0) | |
| a = ap.parse_args() | |
| rng = np.random.default_rng(a.seed) | |
| P = build(''', ''' ap.add_argument("--seed", type=int, default=0) | |
| ap.add_argument("--claim", action="store_true") | |
| ap.add_argument("--stale", type=float, default=14400) | |
| ap.add_argument("--shard", type=int, default=0) | |
| ap.add_argument("--nshards", type=int, default=1) | |
| a = ap.parse_args() | |
| pd = a.out + ".parts" | |
| os.makedirs(pd, exist_ok=True) | |
| P = build(''') | |
| s = s.replace(''' out = {"traits": {}} | |
| for t in prim: | |
| y = P["Yt"][:, P["traits"].index(t)] | |
| ok = np.where(np.isfinite(y))[0] | |
| rec = {}''', ''' for ti, t in enumerate(prim): | |
| pf = os.path.join(pd, t + ".json") | |
| if ti % a.nshards != a.shard or os.path.exists(pf): | |
| continue | |
| if a.claim: | |
| if not claim(pf + ".lock", a.stale): | |
| continue | |
| HELD[:] = [pf + ".lock"] | |
| if os.path.exists(pf): | |
| unlock(HELD) | |
| continue | |
| rng = np.random.default_rng([a.seed, ti]) | |
| y = P["Yt"][:, P["traits"].index(t)] | |
| ok = np.where(np.isfinite(y))[0] | |
| rec = {}''') | |
| s = s.replace(''' rec["susie"] = fm | |
| out["traits"][t] = rec | |
| print(t, "lsr intercept", round(ic, 2), "sig modules", len(sig), "lambda CI", rec["lambda_gc_boot_ci95"], flush=True) | |
| json.dump(out, open(a.out, "w"), indent=1, default=float) | |
| print("EXTRASDONE", flush=True)''', ''' rec["susie"] = fm | |
| json.dump(rec, open(pf + ".tmp", "w"), indent=1, default=float) | |
| os.replace(pf + ".tmp", pf) | |
| unlock(HELD) | |
| print(t, "lsr intercept", round(ic, 2), "sig modules", len(sig), "lambda CI", rec["lambda_gc_boot_ci95"], flush=True) | |
| if all(os.path.exists(os.path.join(pd, t + ".json")) for t in prim): | |
| out = {"traits": {t: json.load(open(os.path.join(pd, t + ".json"))) for t in prim}, "seed": a.seed, | |
| "n_boot": a.n_boot, "rng": "per trait: default_rng([seed, trait index])"} | |
| json.dump(out, open(a.out, "w"), indent=1, default=float) | |
| print("EXTRASDONE", flush=True) | |
| else: | |
| print("EXTRAS_PARTIAL", sum(os.path.exists(os.path.join(pd, t + ".json")) for t in prim), "/", len(prim), flush=True)''') | |
| s = s.replace('''if __name__ == "__main__": | |
| main()''', '''if __name__ == "__main__": | |
| try: | |
| main() | |
| finally: | |
| unlock(HELD)''') | |
| open(p, "w").write(s) | |
| assert "EXTRAS_PARTIAL" in s and "default_rng([a.seed, ti])" in s and "unlock(HELD)\n print(t" in s and "rng = np.random.default_rng(a.seed)" not in s | |
| p = "src/analysis/obs_boot.py" | |
| s = open(p).read() | |
| s = s.replace(''' --scan results/scans/qwen05 --plan results/interventions/plan_qwen05.json --out results/scans/qwen05/obs_boot.npz | |
| """''', ''' --scan results/scans/qwen05 --plan results/interventions/plan_qwen05.json --out results/scans/qwen05/obs_boot.npz | |
| [--claim] [--shard i --nshards n] | |
| Per-trait variances go to <out>.parts/<trait>.npy as they finish (resumable; --claim shares traits through lock files, | |
| D-036); <out> is written once every primary trait has a part. RNG seeded per trait by (seed, trait index). | |
| """''') | |
| s = s.replace("from src.run.scan_run import EST, blocks_of, cosine, dedup # noqa: E402", | |
| "from src.run.intervene import HELD, claim, unlock # noqa: E402\nfrom src.run.scan_run import EST, blocks_of, cosine, dedup # noqa: E402") | |
| s = s.replace(''' ap.add_argument("--seed", type=int, default=0) | |
| a = ap.parse_args() | |
| rng = np.random.default_rng(a.seed) | |
| P = build(''', ''' ap.add_argument("--seed", type=int, default=0) | |
| ap.add_argument("--claim", action="store_true") | |
| ap.add_argument("--stale", type=float, default=14400) | |
| ap.add_argument("--shard", type=int, default=0) | |
| ap.add_argument("--nshards", type=int, default=1) | |
| a = ap.parse_args() | |
| pd = a.out + ".parts" | |
| os.makedirs(pd, exist_ok=True) | |
| P = build(''') | |
| s = s.replace(''' for ti, t in enumerate(prim): | |
| y = P["Yt"][:, P["traits"].index(t)] | |
| E = np.where((P["split"] == 1) & np.isfinite(y))[0]''', ''' for ti, t in enumerate(prim): | |
| pf = os.path.join(pd, t + ".npy") | |
| if ti % a.nshards != a.shard or os.path.exists(pf): | |
| continue | |
| if a.claim: | |
| if not claim(pf + ".lock", a.stale): | |
| continue | |
| HELD[:] = [pf + ".lock"] | |
| if os.path.exists(pf): | |
| unlock(HELD) | |
| continue | |
| rng = np.random.default_rng([a.seed, ti]) | |
| y = P["Yt"][:, P["traits"].index(t)] | |
| E = np.where((P["split"] == 1) & np.isfinite(y))[0]''') | |
| s = s.replace(''' var[:, ti, J] = np.nanvar(draws, 0) | |
| print(t, "median boot SD naive / mm_fd", np.nanmedian(np.sqrt(var[0, ti, J])), | |
| np.nanmedian(np.sqrt(var[3, ti, J])), flush=True) | |
| np.savez(a.out, var=var, estimators=np.array(EST), traits=np.array(prim), loci=np.array(J)) | |
| print("BOOTDONE", flush=True)''', ''' v = np.nanvar(draws, 0) | |
| np.save(pf + ".tmp.npy", v) | |
| os.replace(pf + ".tmp.npy", pf) | |
| unlock(HELD) | |
| print(t, "median boot SD naive / mm_fd", np.nanmedian(np.sqrt(v[0])), np.nanmedian(np.sqrt(v[3])), flush=True) | |
| if all(os.path.exists(os.path.join(pd, t + ".npy")) for t in prim): | |
| for ti, t in enumerate(prim): | |
| var[:, ti, J] = np.load(os.path.join(pd, t + ".npy")) | |
| np.savez(a.out, var=var, estimators=np.array(EST), traits=np.array(prim), loci=np.array(J)) | |
| print("BOOTDONE", flush=True) | |
| else: | |
| print("BOOT_PARTIAL", sum(os.path.exists(os.path.join(pd, t + ".npy")) for t in prim), "/", len(prim), flush=True)''') | |
| s = s.replace('''if __name__ == "__main__": | |
| main()''', '''if __name__ == "__main__": | |
| try: | |
| main() | |
| finally: | |
| unlock(HELD)''') | |
| open(p, "w").write(s) | |
| assert "BOOT_PARTIAL" in s and "default_rng([a.seed, ti])" in s and "rng = np.random.default_rng(a.seed)" not in s | |
| print("parts edits ok") | |