1 #!/usr/bin/env python3 2 import random 3 from .about import __doc__, the # the one doc (and config) is about.py 4 from .lib import * 5 from .data import * 6 from .dist import * 7 from .tree import * 8 from .stats import * 9 10 ALL = """misc/auto93 config/SS-A config/SS-M systems/HSQLDB 11 systems/LLVM systems/storm systems/redis sales_data/socks 12 process/coc1000 process/pom3d""" 13 14 FAST = """misc/auto93 config/SS-B process/nasa93dem 15 sales_data/wallpaper process/pom3d""" # 5 small, quick sets 16 17 def files() -> list: 18 "Paths named by --dir and --file (--file=all|fast, or a,b,c list)" 19 named = {"all": ALL, "fast": FAST}.get(the.file) 20 names = named.split() if named else the.file.split(",") 21 return ["%s/%s.csv" % (the.dir, name) for name in names] 22 23 #-- tests ------------------------------------------------------- 24 def test_the(): 25 "Show the settings" 26 print(the) 27 28 def test_tab(): 29 "Load a csv; show shape and the centroid" 30 tbl = TAB(csv(files()[0])) 31 print(len(tbl.rows), "rows;", len(tbl.x), "x;", len(tbl.y), "y") 32 print("mids:", [m if isa(m,str) else round(m,2) for m in mids(tbl)]) 33 34 def test_dist(): 35 "Best and worst five rows by disty" 36 tbl = TAB(csv(files()[0])) 37 rows = sorted(tbl.rows, key=lambda r: disty(tbl, r)) 38 for r in rows[:5] + ["..."] + rows[-5:]: 39 print(r if r == "..." else (r, round(disty(tbl, r), 2))) 40 41 def test_tree(): 42 "Regression tree predicting disty from the x columns" 43 tbl = TAB(csv(files()[0])) 44 t = tree(tbl, tbl.rows, lambda r: disty(tbl, r), 45 leaf=the.leaf, maxd=the.maxd) 46 show(tbl, t) 47 48 def test_stats(): 49 "same() should see tiny shifts as same, big ones as different" 50 random.seed(the.seed) 51 xs = [random.gauss(10, 1) for _ in range(50)] 52 for d in [0, .1, .5, 1, 2]: 53 print(d, same(xs, [x + d for x in xs])) 54 55 def test_rank(): 56 "Scott-Knott ranks of four treatments (two pairs)" 57 random.seed(the.seed) 58 g = lambda mu: [random.gauss(mu, 1) for _ in range(30)] 59 print(rank(dict(a=g(10), b=g(10.1), c=g(14), d=g(14.2)))) 60 61 def test_confuse(): 62 "Score a deliberately flawed classifier" 63 got = "a a a b b b b c".split() 64 want = "a a b b b b c c".split() 65 c = confuse(list(zip(want, got))) 66 print("acc %.0f" % c.acc) 67 for x in c.classes: 68 print("%4s prec %3.0f recall %3.0f" % (x.label, x.prec, x.recall)) 69 70 def main() -> None: 71 cli(the) 72 funs = {k[5:]: f for k,f in globals().items() if k[:5]=="test_"} 73 for arg in sys.argv[1:]: 74 for k,f in funs.items(): 75 if arg == k or arg == "all": 76 print("\n--", k); random.seed(the.seed); f() 77 78 if __name__ == "__main__": main() 79