# MIT License, Bryan Cheng, 2026. One-off source patches from the pre-training review. import sys def sub(p, a, b): s = open(p).read(); assert s.count(a) == 1, (p, a[:50]); open(p, "w").write(s.replace(a, b)) # C1: rank-safe covariate projection (SVD basis with tolerance) instead of QR on possibly rank-deficient X sub("src/analysis/scan.py", '''def resid(M, X): """Residuals of the columns of M after OLS on X.""" Q, _ = np.linalg.qr(X) return M - Q @ (Q.T @ M)''', '''def basis(X, tol=1e-10): """Orthonormal basis of the column space of X (drops dependent or all-zero columns).""" U, s, _ = np.linalg.svd(X, full_matrices=False) return U[:, s > tol * max(s.max(), 1e-300)] def resid(M, X): """Residuals of the columns of M after OLS on X (rank-safe).""" Q = basis(X) return M - Q @ (Q.T @ M)''') sub("src/analysis/scan.py", ''' n, p = X.shape yr = resid(y[:, None], X)[:, 0] Gr = resid(G, X) ss = (Gr ** 2).sum(0) ok = ss''', ''' n, p = len(y), basis(X).shape[1] yr = resid(y[:, None], X)[:, 0] Gr = resid(G, X) ss = (Gr ** 2).sum(0) ok = ss''') sub("src/analysis/scan.py", ''' n = len(y) mats = list(Ks) + [np.eye(n)]''', ''' n = len(y) X = basis(X) mats = list(Ks) + [np.eye(n)]''') sub("src/analysis/scan.py", ''' Xw = np.linalg.solve(L, X) Gw = np.linalg.solve(L, G) n, p = X.shape''', ''' X = basis(X) Xw = np.linalg.solve(L, X) Gw = np.linalg.solve(L, G) n, p = X.shape''') # C2: DoRA adapters are not converted yet -> excluded from analyses (recorded in DECISIONS) sub("src/analysis/population.py", ''' keep = np.array([n in recs for n in names_all])''', ''' keep = np.array([(n in recs) and not info[n]["flags"].get("dora") for n in names_all])''') sub("src/analysis/population.py", ''' names = [n for n in names_all if n in recs]''', ''' names = [x for x, k in zip(names_all, keep) if k]''') # W1: on OOM do not write a record, so a relaunch retries (smaller --group) sub("src/run/phenotype.py", ''' for rec in recs: rec.update({"status": "oom", "error": str(e)[:300]}) write(rec) continue''', ''' print("OOM in group starting", recs[0]["id"][:40], "- not recorded, rerun with smaller --group", flush=True) continue''') print("patched")