File size: 7,118 Bytes
5c3cfae | 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | """Collect trajectories with direct OpenEnv environment access."""
from __future__ import annotations
import argparse
import random
import uuid
from pathlib import Path
from typing import Dict, List, Sequence
from models import ActionType, ExperimentAction
from server.hackathon_environment import BioExperimentEnvironment
from training.evaluation import EvaluationSuite
from training.trajectory import Trajectory, TrajectoryDataset
HEURISTIC_SEQUENCE = [
ActionType.COLLECT_SAMPLE,
ActionType.PREPARE_LIBRARY,
ActionType.SEQUENCE_CELLS,
ActionType.RUN_QC,
ActionType.FILTER_DATA,
ActionType.NORMALIZE_DATA,
ActionType.CLUSTER_CELLS,
ActionType.TRAJECTORY_ANALYSIS,
ActionType.MARKER_SELECTION,
ActionType.SYNTHESIZE_CONCLUSION,
]
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Run rollout episodes and persist trajectories."
)
parser.add_argument("--episodes", type=int, default=10, help="Number of episodes.")
parser.add_argument(
"--policy",
choices=["random", "heuristic"],
default="heuristic",
help="Policy to use for rollouts.",
)
parser.add_argument(
"--max-steps",
type=int,
default=None,
help="Optional hard cutoff per episode (defaults to env limit).",
)
parser.add_argument(
"--output-dir",
default="training/rollouts",
help="Directory for JSON trajectory outputs.",
)
parser.add_argument("--seed", type=int, default=None, help="RNG seed.")
return parser.parse_args()
def heuristic_next_action(history: Sequence[ActionType], step_index: int) -> ActionType:
seen = set(history)
for action in HEURISTIC_SEQUENCE:
if action not in seen:
return action
if step_index >= 2 and ActionType.VALIDATE_MARKER not in seen:
return ActionType.VALIDATE_MARKER
if ActionType.SYNTHESIZE_CONCLUSION in seen:
return ActionType.SYNTHESIZE_CONCLUSION
return ActionType.SYNTHESIZE_CONCLUSION
def pick_action(policy: str, step_index: int, history: Sequence[ActionType]) -> ActionType:
if policy == "random":
return random.choice(list(ActionType))
return heuristic_next_action(history, step_index)
def default_comparison_name(conditions: Sequence[str]) -> str:
normalized = {condition.lower() for condition in conditions}
if {"healthy", "ipf"} <= normalized:
return "IPF_vs_healthy"
if any("treated" in condition for condition in normalized) and any(
"untreated" in condition for condition in normalized
):
return "treated_vs_untreated"
if any("healthy" in condition for condition in normalized):
return "disease_vs_healthy"
return "disease_vs_healthy"
def build_experiment_action(
action_type: ActionType,
discovered_markers: Sequence[str],
conditions: Sequence[str],
) -> ExperimentAction:
method = None
parameters: Dict[str, object] = {}
if action_type == ActionType.COLLECT_SAMPLE:
parameters = {"n_samples": 6}
elif action_type == ActionType.PREPARE_LIBRARY:
method = "10x_chromium"
elif action_type == ActionType.RUN_QC:
method = "scanpy.pp.calculate_qc_metrics"
elif action_type == ActionType.FILTER_DATA:
method = "scanpy.pp.filter_cells"
elif action_type == ActionType.NORMALIZE_DATA:
method = "scanpy.pp.normalize_total"
elif action_type == ActionType.CLUSTER_CELLS:
method = "scanpy.tl.leiden"
elif action_type == ActionType.DIFFERENTIAL_EXPRESSION:
method = "scanpy.tl.rank_genes_groups"
parameters = {"comparison": default_comparison_name(conditions)}
elif action_type == ActionType.TRAJECTORY_ANALYSIS:
method = "scanpy.tl.dpt"
elif action_type == ActionType.MARKER_SELECTION:
method = "scanpy.tl.rank_genes_groups"
elif action_type == ActionType.VALIDATE_MARKER:
method = "qPCR"
parameters = {"marker": discovered_markers[0] if discovered_markers else "SPP1"}
elif action_type == ActionType.SYNTHESIZE_CONCLUSION:
parameters = {"claims": []}
return ExperimentAction(
action_type=action_type,
method=method,
parameters=parameters,
confidence=0.75,
)
def run_episode(
env: BioExperimentEnvironment,
episode_id: str,
policy: str,
max_steps: int | None = None,
) -> Trajectory:
structured_obs = env.reset()
traj = Trajectory(
episode_id=episode_id,
task=structured_obs.task.model_dump(),
metadata={
"task_problem": structured_obs.task.problem_statement,
"policy": policy,
},
)
done = structured_obs.done
step_num = 0
while not done:
if max_steps is not None and step_num >= max_steps:
break
history = [rec.action_type for rec in structured_obs.pipeline_history]
action_type = pick_action(policy, step_num, history)
experiment_action = build_experiment_action(
action_type=action_type,
discovered_markers=structured_obs.discovered_markers,
conditions=structured_obs.task.conditions,
)
structured_obs = env.step(experiment_action)
reward = structured_obs.reward
done = structured_obs.done
step_num += 1
traj.add_step(
action=experiment_action,
observation=structured_obs,
reward=reward,
done=done,
reward_breakdown=structured_obs.step_reward_breakdown,
)
print(
f" step={structured_obs.step_index:02d} "
f"action={action_type.value:>28} "
f"reward={reward:+.3f}"
)
return traj
def main() -> None:
args = parse_args()
if args.seed is not None:
random.seed(args.seed)
out_dir = Path(args.output_dir)
out_dir.mkdir(parents=True, exist_ok=True)
env = BioExperimentEnvironment()
trajectories: List[Trajectory] = []
print(
f"Starting rollout collection: episodes={args.episodes}, policy={args.policy}"
)
for ep in range(args.episodes):
print(f"Episode {ep + 1}/{args.episodes}")
traj = run_episode(
env=env,
episode_id=str(uuid.uuid4()),
policy=args.policy,
max_steps=args.max_steps,
)
traj.save(out_dir / f"{traj.episode_id}.json")
trajectories.append(traj)
dataset = TrajectoryDataset(trajectories)
stats = EvaluationSuite.online_metrics(trajectories)
print("\nRun complete.")
print(f"Saved trajectories to: {out_dir}")
print("Online metrics:")
for metric in stats:
print(f" - {metric.name}: {metric.value:.4f}")
print(f"Summary: {dataset.summary()}")
if __name__ == "__main__":
main()
|