explore.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python3
  2. import csv
  3. from collections import defaultdict
  4. data = []
  5. with open("database.csv") as infile:
  6. for name, *nums in csv.reader(infile, delimiter=",", quotechar="'"):
  7. data.append((name, *[float(n) for n in nums]))
  8. counts = defaultdict(int)
  9. results = {}
  10. try:
  11. with open("best.csv") as infile:
  12. for r, g, b, name, score in csv.reader(infile, delimiter=",", quotechar="'"):
  13. results[(int(r), int(g), int(b))] = (name, float(score))
  14. except:
  15. pass # file not found, assume no prior results
  16. step = 8
  17. for r in range(0, 256, step):
  18. for g in range(0, 256, step):
  19. for b in range(0, 256, step):
  20. if (known := results.get((r, g, b), None)) is not None:
  21. counts[known[0]] += 1
  22. continue
  23. best_score, best_name = min((x - r * yr - g * yg - b * yb, name) for name, x, yr, yg, yb in data)
  24. results[(r, g, b)] = (best_name, best_score)
  25. counts[best_name] += 1
  26. with open("best.csv", "w") as outfile:
  27. csv.writer(outfile, delimiter=",", quotechar="'").writerows((*k, *v) for k, v in results.items())
  28. with open("counts.csv", "w") as outfile:
  29. csv.writer(outfile, delimiter=",", quotechar="'").writerows(counts.items())
  30. print(f"Top ten most hit:")
  31. for k in sorted(list(counts), key=counts.get, reverse=True)[:10]:
  32. print(f"{k} - {counts[k]}")