|
| 1 | +""" |
| 2 | +Example: log metrics + system metrics + media + table + report + files locally. |
| 3 | +
|
| 4 | +Usage: |
| 5 | + python examples/kitchen-sink-local-only.py |
| 6 | +""" |
| 7 | + |
| 8 | +import math |
| 9 | +import random |
| 10 | +import time |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +import pandas as pd |
| 15 | +from PIL import Image as PILImage |
| 16 | +from PIL import ImageDraw |
| 17 | + |
| 18 | +import trackio |
| 19 | + |
| 20 | +PROJECT = f"local-full-demo-{random.randint(100000, 999999)}" |
| 21 | +STEPS = 8 |
| 22 | +N_RUNS = 3 |
| 23 | + |
| 24 | + |
| 25 | +def make_image(step: int, size: int = 128) -> PILImage.Image: |
| 26 | + img = PILImage.new("RGB", (size, size), "black") |
| 27 | + draw = ImageDraw.Draw(img) |
| 28 | + x = int((size / 2) + (size / 3) * math.sin(step * 0.8)) |
| 29 | + y = int((size / 2) + (size / 3) * math.cos(step * 0.6)) |
| 30 | + draw.ellipse((x - 10, y - 10, x + 10, y + 10), fill=(255, 120, 80)) |
| 31 | + draw.rectangle((8, 8, size - 8, size - 8), outline=(80, 160, 255), width=2) |
| 32 | + return img |
| 33 | + |
| 34 | + |
| 35 | +def make_audio(step: int, sr: int = 16000, duration_s: float = 0.3) -> np.ndarray: |
| 36 | + t = np.linspace(0, duration_s, int(sr * duration_s), endpoint=False) |
| 37 | + freq = 220 + step * 20 |
| 38 | + wave = 0.4 * np.sin(2 * np.pi * freq * t) |
| 39 | + return (wave * 32767).astype(np.int16) |
| 40 | + |
| 41 | + |
| 42 | +def main() -> None: |
| 43 | + examples_dir = Path(__file__).parent |
| 44 | + files_dir = examples_dir / "files" |
| 45 | + |
| 46 | + run_names = [f"run-{run_idx}" for run_idx in range(N_RUNS)] |
| 47 | + |
| 48 | + for run_idx in range(N_RUNS): |
| 49 | + trackio.init( |
| 50 | + project=PROJECT, |
| 51 | + name=f"run-{run_idx}", |
| 52 | + config={"steps": STEPS, "run_idx": run_idx, "mode": "local-only"}, |
| 53 | + auto_log_gpu=False, |
| 54 | + ) |
| 55 | + |
| 56 | + trackio.alert( |
| 57 | + title="Run started", |
| 58 | + text=f"Project: {PROJECT} | Run: run-{run_idx}", |
| 59 | + level=trackio.AlertLevel.INFO, |
| 60 | + ) |
| 61 | + |
| 62 | + if run_idx == 0: |
| 63 | + trackio.save(files_dir / "config1.yml", project=PROJECT) |
| 64 | + trackio.save(files_dir / "config2.yml", project=PROJECT) |
| 65 | + |
| 66 | + for step in range(STEPS): |
| 67 | + train_loss = round( |
| 68 | + max(0.05, 1.8 * math.exp(-0.35 * step) + random.gauss(0, 0.03)), |
| 69 | + 4, |
| 70 | + ) |
| 71 | + train_acc = round(min(0.99, 0.45 + 0.07 * step + random.gauss(0, 0.015)), 4) |
| 72 | + |
| 73 | + img = make_image(step + run_idx) |
| 74 | + audio = make_audio(step + run_idx) |
| 75 | + |
| 76 | + table_df = pd.DataFrame( |
| 77 | + { |
| 78 | + "sample_id": [f"s-{step}-a", f"s-{step}-b"], |
| 79 | + "prediction": ["cat", "dog"], |
| 80 | + "confidence": [ |
| 81 | + round(0.65 + 0.03 * step + 0.01 * run_idx, 3), |
| 82 | + round(0.58 + 0.025 * step + 0.01 * run_idx, 3), |
| 83 | + ], |
| 84 | + } |
| 85 | + ) |
| 86 | + |
| 87 | + trackio.log( |
| 88 | + { |
| 89 | + "train/loss": train_loss, |
| 90 | + "train/accuracy": train_acc, |
| 91 | + "media/preview_image": trackio.Image( |
| 92 | + img, caption=f"step {step}, run {run_idx}" |
| 93 | + ), |
| 94 | + "media/preview_audio": trackio.Audio( |
| 95 | + audio, sample_rate=16000, format="wav" |
| 96 | + ), |
| 97 | + "tables/predictions": trackio.Table(dataframe=table_df), |
| 98 | + }, |
| 99 | + step=step, |
| 100 | + ) |
| 101 | + |
| 102 | + trackio.log_system( |
| 103 | + { |
| 104 | + "cpu_percent": round(25 + step * 2 + random.uniform(-1.0, 1.0), 2), |
| 105 | + "memory_gb": round( |
| 106 | + 3.2 + step * 0.05 + random.uniform(-0.03, 0.03), 3 |
| 107 | + ), |
| 108 | + } |
| 109 | + ) |
| 110 | + |
| 111 | + if step == STEPS - 2: |
| 112 | + trackio.alert( |
| 113 | + title="Loss is drifting", |
| 114 | + text=f"Run run-{run_idx} step={step} loss={train_loss}", |
| 115 | + level=trackio.AlertLevel.WARN, |
| 116 | + ) |
| 117 | + |
| 118 | + report_md = f"""# Local Full Sync Report |
| 119 | +
|
| 120 | +Project: `{PROJECT}` |
| 121 | +Run: `run-{run_idx}` |
| 122 | +
|
| 123 | +This run logs: |
| 124 | +- scalar metrics |
| 125 | +- system metrics |
| 126 | +- media (image + audio) |
| 127 | +- a table artifact |
| 128 | +""" |
| 129 | + trackio.log({"reports/summary": trackio.Markdown(report_md)}) |
| 130 | + |
| 131 | + if run_idx == N_RUNS - 1: |
| 132 | + final_report_md = f"""# Final Local Kitchen Sink Report |
| 133 | +
|
| 134 | +Project: `{PROJECT}` |
| 135 | +Runs: {", ".join(f"`{name}`" for name in run_names)} |
| 136 | +
|
| 137 | +What to look for: |
| 138 | +- Alerts on this page and in the alert panel (run started + one drift warning). |
| 139 | +- Reports entries in the Reports tab (summary per run + this final report). |
| 140 | +""" |
| 141 | + trackio.log( |
| 142 | + {"reports/final_report": trackio.Markdown(final_report_md)}, |
| 143 | + step=STEPS - 1, |
| 144 | + ) |
| 145 | + |
| 146 | + trackio.alert( |
| 147 | + title="Run finished", |
| 148 | + text=f"Completed run-{run_idx} ({STEPS} steps).", |
| 149 | + level=trackio.AlertLevel.INFO, |
| 150 | + ) |
| 151 | + trackio.finish() |
| 152 | + |
| 153 | + result = trackio.show( |
| 154 | + project=PROJECT, |
| 155 | + open_browser=False, |
| 156 | + block_thread=False, |
| 157 | + ) |
| 158 | + full_url = result[3] |
| 159 | + print(f"Dashboard: {full_url}") |
| 160 | + time.sleep(3600) |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + main() |
0 commit comments