explore.py 1.4 KB

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