1 #!/usr/bin/env python3 2 """ 3 egplot: chart a forest.py report. One line (small markers) per 4 treatment; x = data sets, split into forest.py's DATA groups, each 5 sorted by lgbm; y = 0..100, labelled every 10, dashed every 5. 6 Usage: python3 eg/forest.py > out.txt; python3 eg/plot.py out.txt 7 (writes out.png). 8 (c) 2026, Tim Menzies <timm@ieee.org>, MIT license 9 """ 10 import os, sys 11 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) 12 from forest import DATA 13 import matplotlib; matplotlib.use("Agg") 14 import matplotlib.pyplot as plt 15 16 def read(file: str): 17 "Treatment names; {dataset: {treatment: mean}} from a report" 18 ts, d = None, {} 19 for line in open(file): 20 cells = [c.strip(" !\n") for c in line.replace("!", "!,").split(",")] 21 cells = [c for c in cells if c] 22 if not cells or line.startswith("#") or cells[-1][0] == "#": continue 23 if "#x" in cells: ts = cells[:cells.index("#x")]; continue 24 if ts is None: continue # preamble 25 d[cells[-1]] = dict(zip(ts, map(float, cells))) 26 return ts, d 27 28 def main(file: str) -> None: 29 ts, d = read(file) 30 key = "lgbm" if "lgbm" in ts else ts[-1] 31 groups = [(g, sorted((f for f in fs.split() if f in d), 32 key=lambda f: d[f][key])) 33 for g, fs in DATA.items()] 34 xs = [f for _, fs in groups for f in fs] 35 fig, ax = plt.subplots(figsize=(max(8, len(xs) * .3), 5)) 36 for t, m in zip(ts, "osD^v"): 37 ax.plot(range(len(xs)), [d[f][t] for f in xs], marker=m, 38 markersize=3, linewidth=1, label=t) 39 i = 0 40 for g, fs in groups: 41 ax.axvline(i - .5, color="#999", linewidth=.6) 42 ax.text(i + len(fs)/2 - .5, 2, g, ha="center", fontsize=9) 43 i += len(fs) 44 ax.set_ylim(0, 100); ax.set_yticks(range(0, 101, 10)) 45 for y in range(5, 100, 10): 46 ax.axhline(y, color="#ccc", linewidth=.5, linestyle="--") 47 ax.grid(axis="y", color="#ddd", linewidth=.5) 48 ax.set_xticks(range(len(xs)), xs, rotation=90, fontsize=7) 49 ax.set_ylabel("auc"); ax.legend(loc="lower right", fontsize=8) 50 ax.set_title("sorted by %s within each group" % key, fontsize=9) 51 fig.tight_layout() 52 fig.savefig(out := os.path.splitext(file)[0] + ".png", dpi=150) 53 print(out) 54 55 if __name__ == "__main__": main(sys.argv[1]) 56