snnlab.sim.runlog
Complete declared API of the runlog module, with signatures, data fields, validation and source.
Logging and progress utilities for legacy execution commands. Several helpers write to a supplied logger or a process-global event stream; they are operational support rather than neuron-model APIs.
The signatures, defaults, fields, docstrings and implementation excerpts below are generated from the Python source. Annotations are shown as declared; unannotated means the source supplies no type annotation. These pages document callable surfaces, including legacy support utilities, without promising backend support for every declaration.
| Symbol | Kind |
|---|---|
| c | function |
| bold | function |
| dim | function |
| cyan | function |
| green | function |
| yellow | function |
| red | function |
| EventLog | class |
| init_events | function |
| event | function |
| close_events | function |
| MetricsJsonl | class |
| write_test_predictions | function |
| run_id | function |
| provenance | function |
| format_eta | function |
| format_bytes | function |
| banner | function |
| config_block | function |
| warn | function |
| phase | function |
| epoch_header | function |
| print_progress_header | function |
| epoch_row | function |
| print_epoch | function |
| epoch_progress | function |
| metrics_line | function |
| list_output_files | function |
| summary | function |
| done | function |
| Heartbeat | class |
| WarningTracker | class |
| print_intro | function |
| print_summary | function |
c
def c(code: str, text: str) -> strSource docstring:
Wrap text in an ANSI SGR code — only when stdout is a TTY.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
code | str | required | Defined by the source contract and implementation below. |
text | str | required | Defined by the source contract and implementation below. |
Return annotation: str.
Return expressions (branch-dependent; names refer to the linked implementation):
textf'\x1b[{code}m{text}\x1b[0m'Implementation
def c(code: str, text: str) -> str:
"""Wrap text in an ANSI SGR code — only when stdout is a TTY."""
if not _IS_TTY:
return text
return f"\x1b[{code}m{text}\x1b[0m"bold
def bold(text)Wrap text with the bold terminal style when terminal styling is active.
| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
text | unannotated | required | Defined by the source contract and implementation below. |
Return expressions (branch-dependent; names refer to the linked implementation):
c('1', text)Implementation
def bold(text):
return c("1", text)dim
def dim(text)Wrap text with the dim terminal style when terminal styling is active.
| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
text | unannotated | required | Defined by the source contract and implementation below. |
Return expressions (branch-dependent; names refer to the linked implementation):
c('2', text)Implementation
def dim(text):
return c("2", text)cyan
def cyan(text)Apply the cyan terminal color through c().
| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
text | unannotated | required | Defined by the source contract and implementation below. |
Return expressions (branch-dependent; names refer to the linked implementation):
c('36', text)Implementation
def cyan(text):
return c("36", text)green
def green(text)Apply the green terminal color through c().
| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
text | unannotated | required | Defined by the source contract and implementation below. |
Return expressions (branch-dependent; names refer to the linked implementation):
c('32', text)Implementation
def green(text):
return c("32", text)yellow
def yellow(text)Apply the yellow terminal color through c().
| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
text | unannotated | required | Defined by the source contract and implementation below. |
Return expressions (branch-dependent; names refer to the linked implementation):
c('33', text)Implementation
def yellow(text):
return c("33", text)red
def red(text)Apply the red terminal color through c().
| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
text | unannotated | required | Defined by the source contract and implementation below. |
Return expressions (branch-dependent; names refer to the linked implementation):
c('31', text)Implementation
def red(text):
return c("31", text)EventLog
Source docstring:
Append-only JSONL writer: one typed object per semantic event.
This is the canonical machine record of a run. Each line is
{"ts": <iso>, "event": <type>, ...fields}. Types, in order of a run:
run_start, config, phase, epoch, event, warning, summary. A reader can
replay the whole run from this file alone.Constructor:
EventLog(self, path: Path)| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
path | Path | required | Filesystem source or destination path, as described below. |
Instance members assigned by the constructor (expressions are evaluated when constructed):
| Member | Annotation | Initial expression |
|---|---|---|
path | unannotated | Path(path) |
EventLog.emit
def EventLog.emit(self, event: str, **fields)| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
event | str | required | Defined by the source contract and implementation below. |
**fields | unannotated | variadic | Defined by the source contract and implementation below. |
Implementation
def emit(self, event: str, **fields):
rec = {
"ts": datetime.datetime.now().isoformat(timespec="seconds"),
"event": event,
**fields,
}
self._f.write(json.dumps(rec, default=float) + "\n")
self._f.flush()EventLog.close
def EventLog.close(self)Implementation
def close(self):
try:
self._f.close()
except Exception:
passComplete class implementation
class EventLog:
"""Append-only JSONL writer: one typed object per semantic event.
This is the canonical machine record of a run. Each line is
{"ts": <iso>, "event": <type>, ...fields}. Types, in order of a run:
run_start, config, phase, epoch, event, warning, summary. A reader can
replay the whole run from this file alone.
"""
def __init__(self, path: Path):
self.path = Path(path)
self.path.parent.mkdir(parents=True, exist_ok=True)
self._f = open(self.path, "w")
def emit(self, event: str, **fields):
rec = {
"ts": datetime.datetime.now().isoformat(timespec="seconds"),
"event": event,
**fields,
}
self._f.write(json.dumps(rec, default=float) + "\n")
self._f.flush()
def close(self):
try:
self._f.close()
except Exception:
passinit_events
def init_events(out_dir) -> NoneSource docstring:
Open run.jsonl under out_dir and make event() live.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
out_dir | unannotated | required | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def init_events(out_dir) -> None:
"""Open run.jsonl under out_dir and make event() live."""
global _EVENTS
if _EVENTS is not None: # close a prior run's file rather than leak its handle
_EVENTS.close()
_EVENTS = EventLog(Path(out_dir) / "run.jsonl")event
def event(event_type: str, **fields) -> NoneSource docstring:
Emit a structured event to run.jsonl (no-op if not initialised).| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
event_type | str | required | Defined by the source contract and implementation below. |
**fields | unannotated | variadic | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def event(event_type: str, **fields) -> None:
"""Emit a structured event to run.jsonl (no-op if not initialised)."""
if _EVENTS is not None:
_EVENTS.emit(event_type, **fields)close_events
def close_events() -> NoneClose and clear the active process-global event log if one exists.
Return annotation: None.
Implementation
def close_events() -> None:
global _EVENTS
if _EVENTS is not None:
_EVENTS.close()
_EVENTS = NoneMetricsJsonl
Source docstring:
Append-only JSONL writer for per-epoch (or per-step) metrics.Constructor:
MetricsJsonl(self, path: Path)| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
path | Path | required | Filesystem source or destination path, as described below. |
Instance members assigned by the constructor (expressions are evaluated when constructed):
| Member | Annotation | Initial expression |
|---|---|---|
path | unannotated | Path(path) |
MetricsJsonl.write
def MetricsJsonl.write(self, **fields)| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
**fields | unannotated | variadic | Defined by the source contract and implementation below. |
Implementation
def write(self, **fields):
fields.setdefault(
"timestamp", datetime.datetime.now().isoformat(timespec="seconds")
)
self._f.write(json.dumps(fields) + "\n")
self._f.flush()MetricsJsonl.close
def MetricsJsonl.close(self)Implementation
def close(self):
try:
self._f.close()
except Exception:
passComplete class implementation
class MetricsJsonl:
"""Append-only JSONL writer for per-epoch (or per-step) metrics."""
def __init__(self, path: Path):
self.path = Path(path)
self.path.parent.mkdir(parents=True, exist_ok=True)
self._f = open(self.path, "w")
def write(self, **fields):
fields.setdefault(
"timestamp", datetime.datetime.now().isoformat(timespec="seconds")
)
self._f.write(json.dumps(fields) + "\n")
self._f.flush()
def close(self):
try:
self._f.close()
except Exception:
passwrite_test_predictions
def write_test_predictions(path: Path, predictions: list)Source docstring:
Save list of {idx, true, pred, correct, logits} records to JSON.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
path | Path | required | Filesystem source or destination path, as described below. |
predictions | list | required | Defined by the source contract and implementation below. |
Implementation
def write_test_predictions(path: Path, predictions: list):
"""Save list of {idx, true, pred, correct, logits} records to JSON."""
with open(path, "w") as f:
json.dump(predictions, f, indent=2, default=float)run_id
def run_id() -> strSource docstring:
Compact run ID: r-YYYYMMDD-HHMMSS.Return annotation: str.
Return expressions (branch-dependent; names refer to the linked implementation):
f"r-{now.strftime('%Y%m%d-%H%M%S')}"Implementation
def run_id() -> str:
"""Compact run ID: r-YYYYMMDD-HHMMSS."""
now = datetime.datetime.now()
return f"r-{now.strftime('%Y%m%d-%H%M%S')}"provenance
def provenance() -> dictSource docstring:
Return a provenance dict to embed in config.json.
`device` here is the best accelerator *available* on the host; the human
log reports the device a run actually *used* at its done/summary line, so
the two can differ (e.g. a CPU-only sim on an MPS Mac) without either
being wrong.Return annotation: dict.
Return expressions (branch-dependent; names refer to the linked implementation):
{'git_sha': _git_sha(), 'torch_version': torch.__version__, 'device': device, 'python_env_hash': _env_hash(), 'run_id': run_id(), 'started_at': datetime.datetime.now().isoformat(timespec='seconds')}Implementation
def provenance() -> dict:
"""Return a provenance dict to embed in config.json.
`device` here is the best accelerator *available* on the host; the human
log reports the device a run actually *used* at its done/summary line, so
the two can differ (e.g. a CPU-only sim on an MPS Mac) without either
being wrong.
"""
import torch
device = (
"cuda"
if torch.cuda.is_available()
else "mps"
if torch.backends.mps.is_available()
else "cpu"
)
return {
"git_sha": _git_sha(),
"torch_version": torch.__version__,
"device": device,
"python_env_hash": _env_hash(),
"run_id": run_id(),
"started_at": datetime.datetime.now().isoformat(timespec="seconds"),
}format_eta
def format_eta(seconds: float) -> strSource docstring:
Compact ETA string: 8m30s, 45s, 1h12m.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
seconds | float | required | Defined by the source contract and implementation below. |
Return annotation: str.
Return expressions (branch-dependent; names refer to the linked implementation):
f'{int(seconds)}s'f'{int(seconds // 60)}m{int(seconds % 60):02d}s'f'{int(seconds // 3600)}h{int(seconds % 3600 // 60):02d}m'Implementation
def format_eta(seconds: float) -> str:
"""Compact ETA string: 8m30s, 45s, 1h12m."""
if seconds < 60:
return f"{int(seconds)}s"
if seconds < 3600:
return f"{int(seconds // 60)}m{int(seconds % 60):02d}s"
return f"{int(seconds // 3600)}h{int((seconds % 3600) // 60):02d}m"format_bytes
def format_bytes(n: int) -> strFormat a byte count using binary B, KiB, MiB or GiB units.
| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
n | int | required | Defined by the source contract and implementation below. |
Return annotation: str.
Return expressions (branch-dependent; names refer to the linked implementation):
f'{size:.1f} {unit}' if unit != 'B' else f'{int(size)} B'f'{size:.1f} TB'Implementation
def format_bytes(n: int) -> str:
size: float = n
for unit in ["B", "KB", "MB", "GB"]:
if size < 1024:
return f"{size:.1f} {unit}" if unit != "B" else f"{int(size)} B"
size /= 1024
return f"{size:.1f} TB"banner
def banner(log, mode: str, model: str, subtitle: str) -> NoneSource docstring:
Top-of-run identity line: ◆ pinglab · <mode> · <model> <subtitle>.
subtitle is the drive descriptor ("on mnist" when a dataset feeds the net,
"synthetic-spikes" when it is driven synthetically) — the header never
claims a dataset the run does not read.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
mode | str | required | Defined by the source contract and implementation below. |
model | str | required | Defined by the source contract and implementation below. |
subtitle | str | required | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def banner(log, mode: str, model: str, subtitle: str) -> None:
"""Top-of-run identity line: ◆ pinglab · <mode> · <model> <subtitle>.
subtitle is the drive descriptor ("on mnist" when a dataset feeds the net,
"synthetic-spikes" when it is driven synthetically) — the header never
claims a dataset the run does not read.
"""
left = f"{cyan(_BANNER)} {bold('pinglab')} {dim(_DOT)} {mode} {dim(_DOT)} {cyan(model)}"
pad = max(1, WIDTH - _vis_len(left) - _vis_len(subtitle))
log.info(left + " " * pad + dim(subtitle))
log.info(_rule(heavy=True))
event("run_start", mode=mode, model=model, subtitle=subtitle)config_block
def config_block(log, groups: list) -> NoneSource docstring:
Print curated config as aligned label/value rows.
groups: list of (label, value) — value is a pre-composed one-liner, e.g.
("network", "784 → 1024 exc · 256 inh → 10 · 1.3M params").
The exhaustive config lives in config.json / run.jsonl; this is the
human-legible headline, not a dump.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
groups | list | required | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def config_block(log, groups: list) -> None:
"""Print curated config as aligned label/value rows.
groups: list of (label, value) — value is a pre-composed one-liner, e.g.
("network", "784 → 1024 exc · 256 inh → 10 · 1.3M params").
The exhaustive config lives in config.json / run.jsonl; this is the
human-legible headline, not a dump.
"""
fields = {}
for label, value in groups:
if value in (None, ""):
continue
log.info(f" {dim(f'{label:<9}')} {value}")
fields[label] = _strip_ansi(str(value))
log.info(_rule())
event("config", fields=fields)warn
def warn(log, msg: str) -> NoneSource docstring:
A standalone caution line: ⚠ <msg> (yellow), mirrored to run.jsonl.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
msg | str | required | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def warn(log, msg: str) -> None:
"""A standalone caution line: ⚠ <msg> (yellow), mirrored to run.jsonl."""
log.info(f" {yellow(_WARN)} {yellow(msg)}")
event("warning", kind="notice", message=_strip_ansi(msg))phase
def phase(log, name: str, detail: str='', elapsed_s: float | None=None) -> NoneSource docstring:
One setup step: ▸ <name> ............... <detail | time>.
Right-hand column is the cost (a time, a param count) so the eye can scan
the setup ledger. Announced *before* the slow work when detail is unknown;
call again after with the elapsed time to close it out if desired.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
name | str | required | Name used to identify the authored or rendered object. |
detail | str | '' | Defined by the source contract and implementation below. |
elapsed_s | float | None | None | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def phase(log, name: str, detail: str = "", elapsed_s: float | None = None) -> None:
"""One setup step: ▸ <name> ............... <detail | time>.
Right-hand column is the cost (a time, a param count) so the eye can scan
the setup ledger. Announced *before* the slow work when detail is unknown;
call again after with the elapsed time to close it out if desired.
"""
right = detail
if elapsed_s is not None:
t = format_eta(elapsed_s) if elapsed_s >= 1 else f"{elapsed_s:.1f}s"
right = f"{detail} {t}" if detail else t
left = f" {cyan(_PHASE)} {name}"
pad = max(1, WIDTH - _vis_len(left) - _vis_len(right))
log.info(left + " " * pad + dim(right))
event("phase", name=name, detail=detail, elapsed_s=elapsed_s)epoch_header
def epoch_header(log) -> NoneSource docstring:
Column titles + a rule spanning exactly the header's visible width.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def epoch_header(log) -> None:
"""Column titles + a rule spanning exactly the header's visible width."""
labels = {key: hdr for col in _EPOCH_COLS if col is not _SEP
for (key, hdr, _) in [col]}
head = _epoch_line(labels, header=True)
log.info(head)
log.info(dim(" " + _RULE * (len(_strip_ansi(head)) - 2)))print_progress_header
def print_progress_header(log) -> NoneCompatibility wrapper around epoch_header(log).
| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def print_progress_header(log) -> None:
epoch_header(log)epoch_row
def epoch_row(log, ep: int, total: int, acc: float, loss: float, e_rate: float, i_rate: float | None, cv: float, activity: float, elapsed_s: float, eta_s: float, new_best: bool=False, warnings: list | None=None) -> NoneSource docstring:
One epoch row + its run.jsonl event. Cells are bare (units in header).| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
ep | int | required | Defined by the source contract and implementation below. |
total | int | required | Defined by the source contract and implementation below. |
acc | float | required | Defined by the source contract and implementation below. |
loss | float | required | Defined by the source contract and implementation below. |
e_rate | float | required | Defined by the source contract and implementation below. |
i_rate | float | None | required | Defined by the source contract and implementation below. |
cv | float | required | Defined by the source contract and implementation below. |
activity | float | required | Defined by the source contract and implementation below. |
elapsed_s | float | required | Defined by the source contract and implementation below. |
eta_s | float | required | Defined by the source contract and implementation below. |
new_best | bool | False | Defined by the source contract and implementation below. |
warnings | list | None | None | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def epoch_row(
log,
ep: int,
total: int,
acc: float,
loss: float,
e_rate: float,
i_rate: float | None,
cv: float,
activity: float,
elapsed_s: float,
eta_s: float,
new_best: bool = False,
warnings: list | None = None,
) -> None:
"""One epoch row + its run.jsonl event. Cells are bare (units in header)."""
vals = {
"ep": f"{ep}/{total}",
"acc": f"{acc:.0f}",
"loss": f"{loss:.3f}",
"E": f"{e_rate:.0f}",
"I": f"{i_rate:.0f}" if i_rate is not None else "-",
"cv": f"{cv:.2f}",
"act": f"{activity:.0f}",
"dt": f"{int(elapsed_s)}s",
"eta": format_eta(eta_s),
}
line = _epoch_line(vals, best=new_best)
if new_best:
line += " " + green(_BEST)
if warnings:
line += " " + " ".join(warnings)
log.info(line)
event(
"epoch",
ep=ep,
total=total,
acc=acc,
loss=loss,
rate_e=e_rate,
rate_i=i_rate,
cv=cv,
act=activity,
elapsed_s=elapsed_s,
eta_s=eta_s,
new_best=new_best,
warnings=[_strip_ansi(w) for w in (warnings or [])],
)print_epoch
def print_epoch(*args, **kwargs) -> NoneCompatibility wrapper forwarding positional and keyword arguments to epoch_row.
| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
*args | unannotated | variadic | Defined by the source contract and implementation below. |
**kwargs | unannotated | variadic | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def print_epoch(*args, **kwargs) -> None:
epoch_row(*args, **kwargs)epoch_progress
def epoch_progress(ep: int, total: int, note: str, elapsed_s: float, loss: float | None=None) -> strSource docstring:
A partial epoch-stream row for the epoch still in progress.
Same column grid as a finished row, so everything lines up: the running
`loss` sits under the loss column, the `note` (batch/eval counter) fills the
as-yet-unknown dynamics columns between the bars, and elapsed sits under dt.
Returned plain (bars uncoloured); Heartbeat dims the whole line.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
ep | int | required | Defined by the source contract and implementation below. |
total | int | required | Defined by the source contract and implementation below. |
note | str | required | Defined by the source contract and implementation below. |
elapsed_s | float | required | Defined by the source contract and implementation below. |
loss | float | None | None | Defined by the source contract and implementation below. |
Return annotation: str.
Return expressions (branch-dependent; names refer to the linked implementation):
' ' + ' '.join(parts)Implementation
def epoch_progress(ep: int, total: int, note: str, elapsed_s: float,
loss: float | None = None) -> str:
"""A partial epoch-stream row for the epoch still in progress.
Same column grid as a finished row, so everything lines up: the running
`loss` sits under the loss column, the `note` (batch/eval counter) fills the
as-yet-unknown dynamics columns between the bars, and elapsed sits under dt.
Returned plain (bars uncoloured); Heartbeat dims the whole line.
"""
w = {c[0]: c[2] for c in _EPOCH_COLS if c is not _SEP}
ep_c = f"{f'{ep}/{total}':>{w['ep']}}"
acc_c = " " * w["acc"]
loss_c = f"{loss:>{w['loss']}.3f}" if loss is not None else " " * w["loss"]
dt_c = f"{f'{int(elapsed_s)}s':>{w['dt']}}"
eta_c = " " * w["eta"]
mid_w = w["E"] + 2 + w["I"] + 2 + w["cv"] + 2 + w["act"] # dynamics-group span
parts = [ep_c, acc_c, loss_c, _BAR, f"{note:<{mid_w}}", _BAR, dt_c, eta_c]
return " " + " ".join(parts)metrics_line
def metrics_line(log, m: dict, label: str='result') -> NoneSource docstring:
Render a firing-rate metrics dict as one dotted, glyph-led line.
Human: ◈ result E 16Hz · I 1Hz · CV 0.65 · act 84% · f₀ 10Hz
Machine: an event with the raw numeric fields.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
m | dict | required | Defined by the source contract and implementation below. |
label | str | 'result' | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def metrics_line(log, m: dict, label: str = "result") -> None:
"""Render a firing-rate metrics dict as one dotted, glyph-led line.
Human: ◈ result E 16Hz · I 1Hz · CV 0.65 · act 84% · f₀ 10Hz
Machine: an event with the raw numeric fields.
"""
parts = [f"E {m.get('rate_e', 0):.0f}Hz", f"I {m.get('rate_i', 0):.0f}Hz",
f"CV {m.get('cv', 0):.2f}", f"act {m.get('act', 0):.0%}"]
if m.get("f0"):
parts.append(f"f0 {m['f0']:.0f}Hz")
body = f" {dim(_DOT)} ".join(parts)
log.info(f" {cyan(_STATE)} {dim(f'{label:<9}')} {body}")
event("metrics", label=label, **{k: m.get(k) for k in
("rate_e", "rate_i", "cv", "act", "f0")})list_output_files
def list_output_files(out_dir: Path) -> listSource docstring:
List all files in out_dir (recursively) with sizes.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
out_dir | Path | required | Defined by the source contract and implementation below. |
Return annotation: list.
Return expressions (branch-dependent; names refer to the linked implementation):
filesImplementation
def list_output_files(out_dir: Path) -> list:
"""List all files in out_dir (recursively) with sizes."""
out_dir = Path(out_dir)
files = []
if not out_dir.exists():
return files
for p in sorted(out_dir.rglob("*")):
if p.is_file():
rel = p.relative_to(out_dir)
files.append((str(rel), p.stat().st_size))
return filessummary
def summary(log, *, best_acc: float | None=None, final_acc: float | None=None, best_epoch: int | None=None, total_epochs: int | None=None, runtime_s: float | None=None, perf: dict | None=None, device: str | None=None, dynamics: dict | None=None, out_dir: Path | None=None, warnings: list | None=None) -> NoneSource docstring:
The closing block: result, timing/cost, dynamics, warnings, artifacts.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
best_acc | float | None | None | Defined by the source contract and implementation below. |
final_acc | float | None | None | Defined by the source contract and implementation below. |
best_epoch | int | None | None | Defined by the source contract and implementation below. |
total_epochs | int | None | None | Defined by the source contract and implementation below. |
runtime_s | float | None | None | Defined by the source contract and implementation below. |
perf | dict | None | None | Defined by the source contract and implementation below. |
device | str | None | None | Requested or resolved tensor execution device. |
dynamics | dict | None | None | Defined by the source contract and implementation below. |
out_dir | Path | None | None | Defined by the source contract and implementation below. |
warnings | list | None | None | Defined by the source contract and implementation below. |
Return annotation: None.
Implementation
def summary(
log,
*,
best_acc: float | None = None,
final_acc: float | None = None,
best_epoch: int | None = None,
total_epochs: int | None = None,
runtime_s: float | None = None,
perf: dict | None = None,
device: str | None = None,
dynamics: dict | None = None,
out_dir: Path | None = None,
warnings: list | None = None,
) -> None:
"""The closing block: result, timing/cost, dynamics, warnings, artifacts."""
log.info(_rule())
if best_acc is not None:
ep_str = ""
if best_epoch:
ep_str = dim(f"epoch {best_epoch}" + (f"/{total_epochs}" if total_epochs else ""))
val = bold(green(f"{best_acc:.1f}%"))
if final_acc is not None and final_acc != best_acc:
val += f" {ep_str} {dim('final')} {final_acc:.1f}%"
elif ep_str:
val += f" {ep_str}"
_summary_row(log, _OK, green, "best", val)
# Timing + cost on one line — the numbers the repo cares about (throughput,
# peak memory) that were previously buried in metrics.json.
if runtime_s is not None:
bits = [format_eta(runtime_s)]
if perf:
if perf.get("epoch_warm_mean_s"):
bits.append(f"{perf['epoch_warm_mean_s']:.1f}s/epoch")
if perf.get("samples_per_sec_warm"):
bits.append(f"{perf['samples_per_sec_warm']:.0f} samp/s")
if perf.get("peak_memory_bytes"):
bits.append(f"{format_bytes(perf['peak_memory_bytes'])} peak")
if device:
bits.append(device)
_summary_row(log, _CLOCK, dim, "runtime",
f" {dim(_DOT)} ".join(bits))
if warnings:
_summary_row(log, _WARN, yellow,
"dynamics", yellow(" · ".join(_strip_ansi(w).strip("⚠ ")
for w in warnings)))
if dynamics:
body = f" {dim(_DOT)} ".join(
f"{k} {v}" for k, v in dynamics.items() if v is not None
)
_summary_row(log, _STATE, cyan, "end", body)
files = list_output_files(out_dir) if out_dir is not None else []
if files:
assert out_dir is not None # files is non-empty only when out_dir is set
_summary_row(log, _ARROW, cyan, "output",
cyan(str(Path(out_dir).resolve()) + "/"))
# Collapse subdirectories to one line; list top-level files with sizes.
dir_groups: dict = {}
standalone = []
for rel, sz in files:
parts = rel.split(os.sep)
if len(parts) > 1:
dir_groups.setdefault(parts[0], []).append(sz)
else:
standalone.append((rel, sz))
chips = [f"{rel} {dim(format_bytes(sz))}" for rel, sz in standalone]
for d, sizes in dir_groups.items():
chips.append(f"{d}/ {dim(f'({len(sizes)} files)')}")
# wrap chips under the path, indented
line = " "
for chip in chips:
add = ("" if line.strip() == "" else f" {dim(_DOT)} ") + chip
if _vis_len(line) + _vis_len(add) > WIDTH:
log.info(line)
line = " " + chip
else:
line += add
if line.strip():
log.info(line)
log.info(_rule())
event(
"summary",
best_acc=best_acc,
final_acc=final_acc,
best_epoch=best_epoch,
runtime_s=runtime_s,
perf=perf,
device=device,
dynamics=dynamics,
warnings=[_strip_ansi(w) for w in (warnings or [])],
)done
def done(log, elapsed_s: float, device: str | None=None) -> NoneSource docstring:
Final line: ✓ done <elapsed> · <device>.| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
elapsed_s | float | required | Defined by the source contract and implementation below. |
device | str | None | None | Requested or resolved tensor execution device. |
Return annotation: None.
Implementation
def done(log, elapsed_s: float, device: str | None = None) -> None:
"""Final line: ✓ done <elapsed> · <device>."""
_show_cursor() # progress is over — hand the cursor back
bits = [format_eta(elapsed_s)]
if device:
bits.append(device)
_summary_row(log, _OK, green, "done", f" {dim(_DOT)} ".join(bits))
event("done", elapsed_s=elapsed_s, device=device)Heartbeat
Source docstring:
Within-epoch progress for a long batch/eval loop.
A full-MNIST epoch is ~875 batches over minutes; without feedback the
stream looks hung. Two renderings, chosen by whether stdout is a TTY:
• terminal (TTY) — rewrites ONE line in place (carriage return), so a
live counter climbs where the finished row will land, instead of a
wall of lines. Call clear() before the finished row prints so the row
overwrites the live line cleanly.
• non-TTY (output.log, remote pod) — emits a plain line every `log_interval`
s. A periodic record with no control characters; clear() is a no-op.
`beat()` takes an already-formatted line (see epoch_progress) and dims it.Constructor:
Heartbeat(self, tty_interval: float=0.25, log_interval: float=5.0)| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
tty_interval | float | 0.25 | Defined by the constructor implementation below. |
log_interval | float | 5.0 | Defined by the constructor implementation below. |
Heartbeat.beat
def Heartbeat.beat(self, log, line: str)| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
line | str | required | Defined by the source contract and implementation below. |
Return expressions (branch-dependent; names refer to the linked implementation):
NoneImplementation
def beat(self, log, line: str):
now = time.perf_counter()
if now - self._last < self._interval:
return
self._last = now
line = line.rstrip()
if self._tty:
_hide_cursor() # keep the cursor out of the live table
sys.stdout.write("\r\x1b[2K" + dim(line)) # \r + clear-to-EOL
sys.stdout.flush()
self._pending = True
else:
log.info(dim(line))Heartbeat.clear
def Heartbeat.clear(self)Source docstring:
Erase the pending live line (TTY only) so the next full log line
prints cleanly in its place. No-op in non-TTY or if nothing pending.Implementation
def clear(self):
"""Erase the pending live line (TTY only) so the next full log line
prints cleanly in its place. No-op in non-TTY or if nothing pending."""
if self._tty and self._pending:
sys.stdout.write("\r\x1b[2K")
sys.stdout.flush()
self._pending = FalseComplete class implementation
class Heartbeat:
"""Within-epoch progress for a long batch/eval loop.
A full-MNIST epoch is ~875 batches over minutes; without feedback the
stream looks hung. Two renderings, chosen by whether stdout is a TTY:
• terminal (TTY) — rewrites ONE line in place (carriage return), so a
live counter climbs where the finished row will land, instead of a
wall of lines. Call clear() before the finished row prints so the row
overwrites the live line cleanly.
• non-TTY (output.log, remote pod) — emits a plain line every `log_interval`
s. A periodic record with no control characters; clear() is a no-op.
`beat()` takes an already-formatted line (see epoch_progress) and dims it.
"""
def __init__(self, tty_interval: float = 0.25, log_interval: float = 5.0):
self._tty = _IS_TTY
self._interval = tty_interval if self._tty else log_interval
self._last = time.perf_counter()
self._pending = False # a live line is on screen, awaiting clear()
def beat(self, log, line: str):
now = time.perf_counter()
if now - self._last < self._interval:
return
self._last = now
line = line.rstrip()
if self._tty:
_hide_cursor() # keep the cursor out of the live table
sys.stdout.write("\r\x1b[2K" + dim(line)) # \r + clear-to-EOL
sys.stdout.flush()
self._pending = True
else:
log.info(dim(line))
def clear(self):
"""Erase the pending live line (TTY only) so the next full log line
prints cleanly in its place. No-op in non-TTY or if nothing pending."""
if self._tty and self._pending:
sys.stdout.write("\r\x1b[2K")
sys.stdout.flush()
self._pending = FalseWarningTracker
Source docstring:
Tracks rolling dynamics state to flag dead / saturated / no-progress.Constructor:
WarningTracker(self)Instance members assigned by the constructor (expressions are evaluated when constructed):
| Member | Annotation | Initial expression |
|---|---|---|
dead_streak | unannotated | 0 |
saturated_streak | unannotated | 0 |
best_acc | unannotated | 0.0 |
best_epoch | unannotated | 0 |
no_progress_since | unannotated | 0 |
observed_warnings | unannotated | [] |
WarningTracker.tick
def WarningTracker.tick(self, ep: int, acc: float, activity: float, loss: float | None=None, grad_clip_frac: float=0.0)| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
ep | int | required | Defined by the source contract and implementation below. |
acc | float | required | Defined by the source contract and implementation below. |
activity | float | required | Defined by the source contract and implementation below. |
loss | float | None | None | Defined by the source contract and implementation below. |
grad_clip_frac | float | 0.0 | Defined by the source contract and implementation below. |
Return expressions (branch-dependent; names refer to the linked implementation):
flagsImplementation
def tick(
self,
ep: int,
acc: float,
activity: float,
loss: float | None = None,
grad_clip_frac: float = 0.0,
):
flags = []
# Activity flags only trigger when paired with no improvement —
# extreme firing rates alone aren't pathological if the network
# is still learning. Sparse-coding nets can run at ~1% activity;
# gamma-locked PING can sustain >80% — both fine if accuracy climbs.
stuck = self.no_progress_since >= 5
# dead: silent for 3+ epochs AND not improving
if activity < 1.0:
self.dead_streak += 1
if self.dead_streak >= 3 and stuck:
flags.append(yellow(f"{_WARN} dead"))
self._record(ep, "dead")
else:
self.dead_streak = 0
# saturated: nearly-all-firing for 3+ epochs AND not improving
if activity > 95.0:
self.saturated_streak += 1
if self.saturated_streak >= 3 and stuck:
flags.append(yellow(f"{_WARN} saturated"))
self._record(ep, "saturated")
else:
self.saturated_streak = 0
# NaN
if loss is not None and (loss != loss): # NaN check
flags.append(red(f"{_WARN} NaN"))
self._record(ep, "NaN")
# grad explosion
if grad_clip_frac > 0.5:
flags.append(yellow(f"{_WARN} grad-clip"))
self._record(ep, "grad-clip")
# new best tracking
if acc > self.best_acc:
self.best_acc = acc
self.best_epoch = ep
self.no_progress_since = 0
else:
self.no_progress_since += 1
if self.no_progress_since >= 10:
flags.append(yellow(f"{_WARN} stuck"))
self._record(ep, "stuck")
return flagsWarningTracker.summary_lines
def WarningTracker.summary_lines(self) -> listSource docstring:
Human-readable aggregated warnings for the summary block.Return annotation: list.
Return expressions (branch-dependent; names refer to the linked implementation):
[]linesImplementation
def summary_lines(self) -> list:
"""Human-readable aggregated warnings for the summary block."""
if not self.observed_warnings:
return []
lines = []
for start, end, kind in self.observed_warnings:
span = f"ep {start}" if start == end else f"ep {start}–{end}"
lines.append(f"{kind} {span}")
return linesComplete class implementation
class WarningTracker:
"""Tracks rolling dynamics state to flag dead / saturated / no-progress."""
def __init__(self):
self.dead_streak = 0
self.saturated_streak = 0
self.best_acc = 0.0
self.best_epoch = 0
self.no_progress_since = 0
self.observed_warnings = [] # (ep_start, ep_end, kind) aggregated
def tick(
self,
ep: int,
acc: float,
activity: float,
loss: float | None = None,
grad_clip_frac: float = 0.0,
):
flags = []
# Activity flags only trigger when paired with no improvement —
# extreme firing rates alone aren't pathological if the network
# is still learning. Sparse-coding nets can run at ~1% activity;
# gamma-locked PING can sustain >80% — both fine if accuracy climbs.
stuck = self.no_progress_since >= 5
# dead: silent for 3+ epochs AND not improving
if activity < 1.0:
self.dead_streak += 1
if self.dead_streak >= 3 and stuck:
flags.append(yellow(f"{_WARN} dead"))
self._record(ep, "dead")
else:
self.dead_streak = 0
# saturated: nearly-all-firing for 3+ epochs AND not improving
if activity > 95.0:
self.saturated_streak += 1
if self.saturated_streak >= 3 and stuck:
flags.append(yellow(f"{_WARN} saturated"))
self._record(ep, "saturated")
else:
self.saturated_streak = 0
# NaN
if loss is not None and (loss != loss): # NaN check
flags.append(red(f"{_WARN} NaN"))
self._record(ep, "NaN")
# grad explosion
if grad_clip_frac > 0.5:
flags.append(yellow(f"{_WARN} grad-clip"))
self._record(ep, "grad-clip")
# new best tracking
if acc > self.best_acc:
self.best_acc = acc
self.best_epoch = ep
self.no_progress_since = 0
else:
self.no_progress_since += 1
if self.no_progress_since >= 10:
flags.append(yellow(f"{_WARN} stuck"))
self._record(ep, "stuck")
return flags
def _record(self, ep: int, kind: str):
# Extend existing run of same kind or start new
if (
self.observed_warnings
and self.observed_warnings[-1][2] == kind
and self.observed_warnings[-1][1] == ep - 1
):
s, _, k = self.observed_warnings[-1]
self.observed_warnings[-1] = (s, ep, k)
else:
self.observed_warnings.append((ep, ep, kind))
def summary_lines(self) -> list:
"""Human-readable aggregated warnings for the summary block."""
if not self.observed_warnings:
return []
lines = []
for start, end, kind in self.observed_warnings:
span = f"ep {start}" if start == end else f"ep {start}–{end}"
lines.append(f"{kind} {span}")
return linesprint_intro
def print_intro(log, mode: str, model: str, subtitle: str, sections: dict)Render the execution banner and supplied configuration sections through the logger.
| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
mode | str | required | Defined by the source contract and implementation below. |
model | str | required | Defined by the source contract and implementation below. |
subtitle | str | required | Defined by the source contract and implementation below. |
sections | dict | required | Defined by the source contract and implementation below. |
Implementation
def print_intro(log, mode: str, model: str, subtitle: str, sections: dict):
banner(log, mode, model, subtitle)
groups = []
for section_name, items in sections.items():
if not items:
continue
vals = " · ".join(f"{k} {v}" for k, v in items.items() if v not in (None, ""))
groups.append((section_name.lower(), vals))
config_block(log, groups)print_summary
def print_summary(log, **kwargs)Compatibility wrapper forwarding summary keyword arguments to summary().
| Parameter | Annotation | Default | Meaning |
|---|---|---|---|
log | unannotated | required | Defined by the source contract and implementation below. |
**kwargs | unannotated | variadic | Defined by the source contract and implementation below. |
Implementation
def print_summary(log, **kwargs):
summary(log, **kwargs)Constants and type aliases
Initial source expressions are shown, not evaluated runtime values. Legacy configuration may mutate module defaults.
| Name | Annotation | Initial expression | Source |
|---|---|---|---|
WIDTH | unannotated | 70 | Source |