File size: 4,297 Bytes
a4054b7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | # 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")
|