# HG changeset patch # User Paul Boddie # Date 1441743765 -7200 # Node ID cc44040363e1de10d9ce0dd680a0e64ff5da81f0 # Parent c9dcc6167aae5212f6641679cfe09d5653f26632 Removed redundant tables and added the statistical fallback function. diff -r c9dcc6167aae -r cc44040363e1 optimiser.py --- a/optimiser.py Tue Sep 08 22:14:55 2015 +0200 +++ b/optimiser.py Tue Sep 08 22:22:45 2015 +0200 @@ -35,14 +35,11 @@ def match_darker(b, bases): return downward[b] in bases and downward[b] -def neutral(bases, light): - l = ["W", "C", "Y", "M", "G", "R", "B", "_"] - if not light: - l.reverse() - for b in l: - if b in bases: +def fallback(bases): + for b in by_frequency(bases): + if b not in ["_", "W"]: return b - return bases[randint(0, 3)] + return by_frequency(bases)[0] tones = [ "__", "_B", "BB", # 00x @@ -58,30 +55,6 @@ colours = ["_", "R", "G", "Y", "B", "M", "C", "W"] -upward = { - "_" : ["R", "G", "B"], - "R" : ["Y", "M"], - "G" : ["Y", "C"], - "B" : ["C", "M"], - "Y" : ["W"], - "C" : ["W"], - "M" : ["W"], - "W" : ["W"], - "*" : ["W", "Y", "C", "M"], - } - -downward = { - "_" : ["_"], - "R" : ["_"], - "G" : ["_"], - "B" : ["_"], - "Y" : ["R", "G"], - "C" : ["G", "B"], - "M" : ["R", "B"], - "W" : ["Y", "C", "M"], - "*" : ["R", "G", "B", "_"], - } - if __name__ == "__main__": width = 320 input_filename, output_filename = sys.argv[1:3] @@ -117,17 +90,17 @@ chosen = [] for row, (u, bu) in enumerate(zip(usage, base_usage)): - light = row % 2 best = 0 best_bases = None best_missing = None best_map = None for bases in combinations(bu, 4): + bases = dict([(base, bu[base]) for base in bases]) count = 0 missing = [] tone_map = {} for tone, freq in u.items(): - base = match(light and tone[1] or tone[0], bases) + base = match(tone[1], bases) or match(tone[0], bases) if base: tone_map[tone] = base count += freq @@ -143,11 +116,10 @@ output = [] for row, (tr, ch) in enumerate(zip(toned, chosen)): - light = row % 2 o = [] for column, t in enumerate(tr): best, bases, tone_map, missing = ch - base = tone_map.get(t) or neutral(bases, light) + base = tone_map.get(t) or fallback(bases) o.append(base) i = colours.index(base) im.putpixel((column, row), colour(i))