Repository navigation
Expand file tree
/
Copy pathrunpart4.py
More file actions
191 lines (178 loc) · 10.3 KB
/
Copy pathrunpart4.py
File metadata and controls
191 lines (178 loc) · 10.3 KB
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
"""AstroSimBench Part 4 entry point at the project root.
python runpart4.py Run 4A-4E in course order.
python runpart4.py --only 4A Validate and measure the corrector pilot.
python runpart4.py --only 4B 4C Run periapsis and external-hybrid studies.
python runpart4.py --only 4D Read existing 3F tables, without integration.
python runpart4.py --only 4E Run ZLK and its reference-sampling validation.
python runpart4.py --show Show figures; close windows to continue.
python runpart4.py --list List lessons without integrating.
Each lesson uses a separate process, the current interpreter, and the project
root. Progress appears in the terminal and in each lesson's log. One batch
contains the selected lesson folders and an execution summary. Completion is
separate from accuracy; 4E also requires its additional sampling check.
"""
from datetime import datetime, timezone
from pathlib import Path
import argparse
import importlib.util
import json
import os
import subprocess
import sys
ROOT = Path(__file__).resolve().parent
EXPERIMENTS = (
("4A", "Correct the mapping coordinates", "exp4a_corrector.py", "4A_corrector"),
("4B", "Resolve the changing motion", "exp4b_periapsis.py", "4B_periapsis"),
("4C", "Pay for an encounter", "exp4c_hybrid.py", "4C_hybrid"),
("4D", "Read the cost of a prediction", "exp4d_cost_to_target.py", "4D_cost_to_target"),
("4E", "Follow an evolving spatial system", "exp4e_zlk.py", "4E_zlk"),
)
def _save_status(batch, report, status=None):
"""Persist execution progress without interpreting scientific target status."""
report["status"] = status or ("incomplete" if report["failed"] or report["skipped"] else "completed")
(batch/"batch_summary.json").write_text(json.dumps(report, indent=2)+"\n", encoding="utf-8")
def _read_completion(batch, folder):
"""Read one well-formed completion marker or return a diagnostic reason."""
markers = list((batch/folder).glob("*/completion.json"))
if len(markers) != 1:
return None, None, f"expected one completion marker, found {len(markers)}"
try:
record = json.loads(markers[0].read_text(encoding="utf-8"))
if not isinstance(record, dict):
raise ValueError("completion marker must be an object")
except (OSError, ValueError) as exc:
return markers[0], None, f"could not read completion marker: {exc}"
if record.get("status") != "completed" or record.get("failed_runs", record.get("numerical_failures", 0)):
return markers[0], record, "completion marker records an incomplete run or numerical failures"
return markers[0], record, None
def _run_logged(command, *, log_path, env, append=False):
"""Stream child output to the terminal and retain the same text in a log."""
with Path(log_path).open("a" if append else "w", encoding="utf-8") as log:
with subprocess.Popen(command, cwd=ROOT, env=env, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True, encoding="utf-8",
errors="replace", bufsize=1) as process:
try:
for line in process.stdout:
log.write(line)
log.flush()
print(line, end="", flush=True)
return subprocess.CompletedProcess(command, process.wait())
except BaseException:
process.terminate()
try:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
process.wait()
raise
def main(argv=None):
"""Run selected lessons in teaching order and return the batch exit status."""
for stream in (sys.stdout, sys.stderr):
if hasattr(stream, "reconfigure"):
stream.reconfigure(errors="replace")
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--only", nargs="+", type=str.upper, choices=[r[0] for r in EXPERIMENTS], metavar="ID",
help="Run selected lessons in course order, for example --only 4A 4D")
parser.add_argument("--output", type=Path, help="Parent directory for this batch, default results/part4")
parser.add_argument("--source-3f", type=Path, help="Existing 3F run used by 4D; other lessons do not read it")
parser.add_argument("--show", action="store_true", help="Show figures; close windows to continue")
parser.add_argument("--list", action="store_true", help="List experiments and exit")
args = parser.parse_args(argv)
if args.list:
for code, title, script, _ in EXPERIMENTS:
print(f"{code} {title}\n experiments/part4/{script}")
return 0
chosen = [r for r in EXPERIMENTS if args.only is None or r[0] in args.only]
codes = [r[0] for r in chosen]
folder = ROOT/"experiments"/"part4"
required = [folder/r[2] for r in chosen]
if "4E" in codes:
required.append(folder/"validate_zlk_sampling.py")
if codes==[r[0] for r in EXPERIMENTS]:
required.append(folder/"postprocess_part4.py")
missing_files = [str(p) for p in required if not p.is_file()]
if missing_files:
print("Experiment files missing. Extract the complete project into one root folder:\n"+"\n".join(missing_files), file=sys.stderr)
return 2
needs = ["numpy", "matplotlib", "astrosimbench"] + (["rebound"] if any(r != "4D" for r in codes) else [])
missing = [name for name in needs if importlib.util.find_spec(name) is None]
if missing:
print("Missing dependencies: " + ", ".join(missing) + '\nAt the project root run: python -m pip install -e ".[plots,reference]"', file=sys.stderr)
return 2
if args.source_3f:
if "4D" not in codes:
parser.error("--source-3f requires 4D in the selected lessons")
if not args.source_3f.is_dir():
parser.error("--source-3f must identify an existing 3F run directory")
base = args.output.resolve() if args.output else ROOT/"results"/"part4"
batch = base/datetime.now(timezone.utc).strftime("batch_%Y%m%d_%H%M%S_%f")
batch.mkdir(parents=True, exist_ok=False)
env = os.environ.copy()
env["PYTHONPATH"] = str(ROOT) + (os.pathsep+env["PYTHONPATH"] if env.get("PYTHONPATH") else "")
env["PYTHONUTF8"] = "1"
env["PYTHONIOENCODING"] = "utf-8"
env.setdefault("OPENBLAS_NUM_THREADS", "1")
env.setdefault("OMP_NUM_THREADS", "1")
if not args.show:
env.setdefault("MPLBACKEND", "Agg")
report = dict(schema="part4-v1", selected=codes, completed=[], failed=[], skipped=[], commands={}, failure_reasons={},
note="execution completion is separate from accuracy; 4E requires reference-sampling validation")
print("Run order: " + " -> ".join(codes) + f"\nBatch output: {batch}", flush=True)
_save_status(batch, report, "running")
for index, (code, title, script, lesson_folder) in enumerate(chosen, 1):
command = [sys.executable, "-u", str(folder/script), "--output", str(batch)]
command.append("--show" if args.show else "--no-show")
if code == "4D" and args.source_3f:
command.extend(["--source-3f", str(args.source_3f.resolve())])
report["commands"][code] = command
print(f"\n[{index}/{len(chosen)}] {code}: {title}\nLog: {batch / (code+'.log')}", flush=True)
reason = None
try:
process = _run_logged(command, log_path=batch/(code+".log"), env=env)
marker, record, reason = _read_completion(batch, lesson_folder)
if process.returncode:
reason = f"experiment exited with code {process.returncode}"
if code == "4E" and reason is None:
validation = [sys.executable, "-u", str(folder/"validate_zlk_sampling.py"), "--run", str(marker.parent)]
report["commands"]["4E_sampling"] = validation
process = _run_logged(validation, log_path=batch/(code+".log"), env=env, append=True)
_, record, reason = _read_completion(batch, lesson_folder)
if process.returncode:
reason = f"sampling validation exited with code {process.returncode}"
elif reason is None and not record.get("sampling_validation_passed"):
reason = "sampling validation flag is absent or false"
except KeyboardInterrupt:
report["failed"].append(code)
report["failure_reasons"][code] = "interrupted"
report["skipped"].extend(codes[index:])
_save_status(batch, report, "interrupted")
print("\nStopped; saved results retained.", file=sys.stderr, flush=True)
return 130
except OSError as exc:
reason = f"could not start or record this lesson: {exc}"
if reason is None:
report["completed"].append(code)
print(code+" completed.", flush=True)
else:
report["failed"].append(code)
report["failure_reasons"][code] = reason
print(f"{code} incomplete: {reason}; continuing to the next independent lesson.", file=sys.stderr, flush=True)
_save_status(batch, report, "running" if index < len(chosen) else None)
report["postprocess_failed"]=False
if codes==[r[0] for r in EXPERIMENTS] and not report["failed"]:
command=[sys.executable,"-u",str(folder/"postprocess_part4.py"),"--batch",str(batch),"--output",str(batch)]
report["commands"]["analysis"]=command
try:
process=_run_logged(command,log_path=batch/"analysis.log",env=env)
report["postprocess_failed"]=process.returncode!=0
except OSError as exc:
report["postprocess_failed"]=True;report["analysis_error"]=str(exc)
_save_status(batch,report,"incomplete" if report["postprocess_failed"] else None)
print("\nCompleted: " + (", ".join(report["completed"]) or "None"), flush=True)
if report["failed"]:
print("Incomplete: " + ", ".join(report["failed"]), flush=True)
print("After fixing the issue, rerun: python runpart4.py --only " + " ".join(report["failed"]), flush=True)
print(f"Batch: {batch}\nRead validation and reference reports before interpreting accuracy.", flush=True)
return 1 if report["failed"] or report.get("postprocess_failed") else 0
if __name__ == "__main__":
raise SystemExit(main())