File size: 3,277 Bytes
6fa9282 | 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | """Run the documented example from measured counts through nomination and plots."""
from pathlib import Path
import argparse, subprocess, sys
import numpy as np
from pivot.data.perturb_data import PerturbData
ROOT = Path(__file__).resolve().parents[1]
def run(args):
subprocess.run([sys.executable, "-m", "pivot.cli", *map(str, args)], check=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--output", default="runs/example")
a = parser.parse_args()
out = Path(a.output).resolve()
if out.exists():
raise FileExistsError("Choose a new example output directory")
cache = out / "cache"
model = out / "model"
run(
[
"prepare",
"--raw",
ROOT / "fixtures/norman_small.h5ad",
"--dataset",
"norman",
"--split",
"perturbation",
"--n-hvg",
200,
"--n-pca",
10,
"--output",
cache,
]
)
run(
[
"train",
"--cache",
cache,
"--config",
ROOT / "configs/small.json",
"--output",
model,
]
)
run(
[
"evaluate",
"--cache",
cache,
"--checkpoint",
model / "best.pt",
"--catalog",
"all",
"--n-cells",
16,
"--guidance-steps",
3,
"--output",
out / "pivot.json",
]
)
run(
[
"evaluate",
"--cache",
cache,
"--baseline",
"ridge",
"--catalog",
"all",
"--n-cells",
16,
"--output",
out / "ridge.json",
]
)
data = PerturbData(str(cache))
label = data.labels("test")[0]
ids = np.intersect1d(data.indices("test", False), data.pert_to_idx[label])
np.save(out / "target.npy", data.emb[ids])
run(
[
"predict",
"--cache",
cache,
"--checkpoint",
model / "best.pt",
"--label",
label,
"--n-cells",
16,
"--output",
out / "prediction.npz",
]
)
for search in ["exhaustive", "guidance", "greedy"]:
run(
[
"nominate",
"--cache",
cache,
"--checkpoint",
model / "best.pt",
"--target",
out / "target.npy",
"--catalog",
"all",
"--search",
search,
"--steps",
3,
"--n-cells",
16,
"--output",
out / (search + ".json"),
]
)
subprocess.run(
[
sys.executable,
str(ROOT / "scripts/plot_results.py"),
str(out / "pivot.json"),
str(out / "ridge.json"),
"--output",
str(out / "plots"),
],
check=True,
)
print("Example complete:", out)
|