Download code/work/edit_raw.py from bryan7264/lorapop: direct link, hf CLI and curl.
- Browser
- Download file 4.3 kB
-
https://huggingface.co/datasets/bryan7264/lorapop/resolve/main/code/work/edit_raw.py
- Command line
-
hf download hf://datasets/bryan7264/lorapop/code/work/edit_raw.py
-
curl -L -o edit_raw.py https://huggingface.co/datasets/bryan7264/lorapop/resolve/main/code/work/edit_raw.py
4.3 kB
| # MIT License, Bryan Cheng, 2026. One-off source edit (D-037): raw-coordinate interventions for the plan 14.7 ablation. | |
| p = "src/run/intervene.py" | |
| s = open(p).read() | |
| s = s.replace('''--claim: any number of processes''', '''Raw-coordinate plans (plan["coords"] == "raw", D-037): loci come from plan["loci_file"] (loci_raw.pt) and the allele is | |
| the raw weight-space direction sum_i u_il Delta_i / sqrt(lam_l) (V = identity), dosed in SDs of the raw genotype. | |
| --claim: any number of processes''') | |
| s = s.replace(''' loci = torch.load(os.path.join(G, "loci.pt"))''', ''' loci = torch.load(os.path.join(G, plan.get("loci_file", "loci.pt"))) | |
| raw = plan.get("coords", "expressed") == "raw"''') | |
| s = s.replace(''' V, _ = Vk(cov, key) | |
| D = allele(fcache[key], lo["U"][:, d["l"]], lo["lam"][d["l"]], V, device=dev)''', ''' if raw: | |
| V = torch.eye(table[key].lin.weight.shape[1]) | |
| else: | |
| V, _ = Vk(cov, key) | |
| D = allele(fcache[key], lo["U"][:, d["l"]], lo["lam"][d["l"]], V, device=dev)''') | |
| assert 'raw = plan.get("coords"' in s and "torch.eye(table[key].lin.weight.shape[1])" in s | |
| open(p, "w").write(s) | |
| p = "src/analysis/obs_boot.py" | |
| s = open(p).read() | |
| s = s.replace(''' ap.add_argument("--n_boot", type=int, default=200)''', ''' ap.add_argument("--n_boot", type=int, default=200) | |
| ap.add_argument("--loci_file", default="loci.pt", help="loci_raw.pt for the raw-coordinate ablation (D-037)")''') | |
| s = s.replace(''' P = build(a.geno, a.pheno, recipe_threshold=a.recipe_threshold)''', | |
| ''' P = build(a.geno, a.pheno, recipe_threshold=a.recipe_threshold, loci_file=a.loci_file)''') | |
| assert "loci_file=a.loci_file" in s | |
| open(p, "w").write(s) | |
| p = "src/analysis/plan_interventions.py" | |
| s = open(p).read() | |
| s = s.replace('''largest recipe cluster); 0 = all. | |
| ''', '''largest recipe cluster); 0 = all. | |
| --coords raw (D-037; plan 14.7 ablation): loci from <geno>/loci_raw.pt and a raw-coordinate scan; with --no_controls and | |
| --max_ko 0 only tested loci are planned; --test_bg k puts every direction on the first k backgrounds. | |
| ''') | |
| s = s.replace(''' ap.add_argument("--ctrl_bg", type=int, default=0)''', ''' ap.add_argument("--ctrl_bg", type=int, default=0) | |
| ap.add_argument("--test_bg", type=int, default=0, help="D-037: every direction on the first k backgrounds; 0 = all") | |
| ap.add_argument("--coords", default="expressed", choices=["expressed", "raw"]) | |
| ap.add_argument("--no_controls", action="store_true")''') | |
| s = s.replace(''' G = torch.load(os.path.join(a.geno, "loci.pt"))''', ''' loci_file = "loci_raw.pt" if a.coords == "raw" else "loci.pt" | |
| G = torch.load(os.path.join(a.geno, loci_file))''') | |
| s = s.replace(''' dirs.append({"id": f"rnd_{key}_{l}", "kind": "random",''', ''' if a.no_controls: | |
| continue | |
| dirs.append({"id": f"rnd_{key}_{l}", "kind": "random",''') | |
| s = s.replace(''' if a.ctrl_bg: | |
| for d in dirs:''', ''' if a.test_bg: | |
| for d in dirs: | |
| d["backgrounds"] = bgs[:a.test_bg] | |
| if a.ctrl_bg: | |
| for d in dirs:''') | |
| s = s.replace(''' plan = {"base": a.base, "adapters_dir": a.adapters_dir, "geno_dir": a.geno, "backgrounds": bgs,''', | |
| ''' plan = {"base": a.base, "adapters_dir": a.adapters_dir, "geno_dir": a.geno, "backgrounds": bgs, "coords": a.coords, | |
| "loci_file": loci_file,''') | |
| s = s.replace('''"ctrl_bg": a.ctrl_bg, "nonassoc_est": a.nonassoc_est,''', '''"ctrl_bg": a.ctrl_bg, "nonassoc_est": a.nonassoc_est, | |
| "test_bg": a.test_bg, "coords": a.coords, "controls": not a.no_controls,''') | |
| assert s.count("a.no_controls") == 2 and '"loci_file": loci_file' in s and "bgs[:a.test_bg]" in s | |
| open(p, "w").write(s) | |
| print("raw edits ok") | |
| p = "src/run/intervene.py" | |
| s = open(p).read() | |
| s = s.replace(''' zero = [("__base__", 0.0)] + [(b, 0.0) for b in plan["backgrounds"]] + [(f"KO::{n}", 0.0) for n in carriers]''', | |
| ''' used = {b for d in plan["directions"] for b in d.get("backgrounds", plan["backgrounds"])} | |
| zero = [("__base__", 0.0)] + [(b, 0.0) for b in plan["backgrounds"] if b in used] + [(f"KO::{n}", 0.0) for n in carriers]''') | |
| assert "if b in used]" in s | |
| open(p, "w").write(s) | |
| print("refs-used edit ok") | |