import time, re, sys, csv, os from subprocess import call from os import listdir, system from os.path import isfile, join, getctime from dataclasses import dataclass, field ski_position_to_rgb_map = {1: "230, 138, 0", 2: "255,153,0", 3: "255, 184, 77", 4: "255, 194, 100", 5: "255, 194, 120", 6: "255, 210, 130", 7: "255, 210, 140", 8: "230, 204, 255", 9: "204, 153, 255", 10: "255, 102, 179", 11: "255, 51, 153", 12: "255, 0, 255", 13: "179, 0, 179", 14: "102, 0, 102"} def importance_color(ranking_value): low = 0x00ff00 high = 0xff0000 result = int(ranking_value * high + (1-ranking_value) * low) return f"#{result:06x}" def model_to_actual(ski_position): if ski_position == 1: return 1 elif ski_position in [2,3]: return 2 elif ski_position in [4,5]: return 3 elif ski_position in [6,7]: return 4 elif ski_position in [8,9]: return 5 elif ski_position in [10,11]: return 6 elif ski_position in [12,13]: return 7 elif ski_position == 14: return 8 def convert(tuples): return dict(tuples) @dataclass(frozen=True) class State: x: int y: int ski_position: int def default_value(): return {'action' : None, 'choiceValue' : None} @dataclass(frozen=True) class StateValue: ranking: float choices: dict = field(default_factory=default_value) def exec(command, verbose=True): if verbose: print(f"Executing {command}") system(f"echo {command} >> list_of_exec") return system(command) def fillStateRanking(file_name, match=""): state_ranking = dict() try: with open(file_name, "r") as f: file_content = f.readlines() for line in file_content: if match and skip_line.match(line): continue stateMapping = convert(re.findall(r"([a-zA-Z_]*[a-zA-Z])=(\d+)?", line)) #print("stateMapping", stateMapping) choices = convert(re.findall(r"[a-zA-Z_]*(left|right|noop)[a-zA-Z_]*:(-?\d+\.?\d*)", line)) #print("choices", choices) ranking_value = float(re.search(r"Value:([+-]?(\d*\.\d+)|\d+)", line)[0].replace("Value:","")) #print("ranking_value", ranking_value) state = State(int(stateMapping["x"]), int(stateMapping["y"]), int(stateMapping["ski_position"])) value = StateValue(ranking_value, choices) state_ranking[state] = value return state_ranking except EnvironmentError: print("TODO file not available. Exiting.") sys.exit(1) ranking = fillStateRanking("action_ranking_simplified") sorted_ranking = sorted(ranking.items(), key=lambda x: x[1].ranking) draw_commands = {1: list(), 2:list(), 3:list(), 4:list(), 5:list(), 6:list(), 7:list(), 8:list()} exec(f"cp images/empty_scaled_down.png full_first_try_safety.png") markerSize = 1 for state in sorted_ranking[-500000:-1]: if state[1].ranking < 0.09: continue x = state[0].x y = state[0].y ski_position = state[0].ski_position draw_commands[ski_position].append(f"-fill '{importance_color(state[1].ranking)}' -draw 'rectangle {x-markerSize},{y-markerSize} {x+markerSize},{y+markerSize}'") # {state[1].ranking} for pos, marker in draw_commands.items(): destination = f"first_try_{pos:02}.png" exec(f"cp images/empty_scaled_down.png {destination}") i=0 while i < len(marker): batch = marker[i:i+1000] command = f"convert {destination} {' '.join(batch)} {destination}" exec(command, verbose=False) command = f"convert full_first_try_safety.png {' '.join(batch)} full_first_try_safety.png" exec(command, verbose=False) i = i+1000 exec(f"montage first_try*.png -geometry +0+0 -tile x1 tiled_first_try.png")