From b5ffcec41a0a06c535024d06d2ea901a11d8b87a Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Sun, 21 Apr 2024 11:53:56 -0400 Subject: Delete old code --- configs/colors_64_v0.json | 1 - configs/corners_hollow4x4_v0.json | 1 - configs/gen_colors.py | 9 -- configs/gen_corners.py | 36 -------- display_animation.py | 167 -------------------------------------- display_animation_tkinter.py | 151 ---------------------------------- utils.py | 6 -- 7 files changed, 371 deletions(-) delete mode 100644 configs/colors_64_v0.json delete mode 100644 configs/corners_hollow4x4_v0.json delete mode 100644 configs/gen_colors.py delete mode 100644 configs/gen_corners.py delete mode 100644 display_animation.py delete mode 100644 display_animation_tkinter.py delete mode 100644 utils.py diff --git a/configs/colors_64_v0.json b/configs/colors_64_v0.json deleted file mode 100644 index d3ce3ff..0000000 --- a/configs/colors_64_v0.json +++ /dev/null @@ -1 +0,0 @@ -{"0": [0, 0, 0], "1": [0, 0, 85], "2": [0, 0, 170], "3": [0, 0, 255], "4": [0, 85, 0], "5": [0, 85, 85], "6": [0, 85, 170], "7": [0, 85, 255], "8": [0, 170, 0], "9": [0, 170, 85], "10": [0, 170, 170], "11": [0, 170, 255], "12": [0, 255, 0], "13": [0, 255, 85], "14": [0, 255, 170], "15": [0, 255, 255], "16": [85, 0, 0], "17": [85, 0, 85], "18": [85, 0, 170], "19": [85, 0, 255], "20": [85, 85, 0], "21": [85, 85, 85], "22": [85, 85, 170], "23": [85, 85, 255], "24": [85, 170, 0], "25": [85, 170, 85], "26": [85, 170, 170], "27": [85, 170, 255], "28": [85, 255, 0], "29": [85, 255, 85], "30": [85, 255, 170], "31": [85, 255, 255], "32": [170, 0, 0], "33": [170, 0, 85], "34": [170, 0, 170], "35": [170, 0, 255], "36": [170, 85, 0], "37": [170, 85, 85], "38": [170, 85, 170], "39": [170, 85, 255], "40": [170, 170, 0], "41": [170, 170, 85], "42": [170, 170, 170], "43": [170, 170, 255], "44": [170, 255, 0], "45": [170, 255, 85], "46": [170, 255, 170], "47": [170, 255, 255], "48": [255, 0, 0], "49": [255, 0, 85], "50": [255, 0, 170], "51": [255, 0, 255], "52": [255, 85, 0], "53": [255, 85, 85], "54": [255, 85, 170], "55": [255, 85, 255], "56": [255, 170, 0], "57": [255, 170, 85], "58": [255, 170, 170], "59": [255, 170, 255], "60": [255, 255, 0], "61": [255, 255, 85], "62": [255, 255, 170], "63": [255, 255, 255]} \ No newline at end of file diff --git a/configs/corners_hollow4x4_v0.json b/configs/corners_hollow4x4_v0.json deleted file mode 100644 index a01e166..0000000 --- a/configs/corners_hollow4x4_v0.json +++ /dev/null @@ -1 +0,0 @@ -{"corner_colors": {"0": [["#000000", "#000000", "#000000", "#000000"], ["#000000", "#ff0000", "#ff0000", "#000000"], ["#000000", "#ff0000", "#ff0000", "#000000"], ["#000000", "#000000", "#000000", "#000000"]], "1": [["#000000", "#000000", "#000000", "#000000"], ["#000000", "#00ff00", "#00ff00", "#000000"], ["#000000", "#00ff00", "#00ff00", "#000000"], ["#000000", "#000000", "#000000", "#000000"]], "2": [["#000000", "#000000", "#000000", "#000000"], ["#000000", "#0000ff", "#0000ff", "#000000"], ["#000000", "#0000ff", "#0000ff", "#000000"], ["#000000", "#000000", "#000000", "#000000"]], "3": [["#000000", "#000000", "#000000", "#000000"], ["#000000", "#ffffff", "#ffffff", "#000000"], ["#000000", "#ffffff", "#ffffff", "#000000"], ["#000000", "#000000", "#000000", "#000000"]]}, "corner_width": 4, "corner_height": 4} \ No newline at end of file diff --git a/configs/gen_colors.py b/configs/gen_colors.py deleted file mode 100644 index 2ab36a6..0000000 --- a/configs/gen_colors.py +++ /dev/null @@ -1,9 +0,0 @@ -import itertools -import json - - -color_vals = [0, 85, 170, 255] # giving all color channels the same equally-spaced values -color_combs = {i: comb for i, comb in enumerate(itertools.product(color_vals, repeat=3))} - -with open("colors_64_v0.json", "w") as f: - json.dump(color_combs, f) diff --git a/configs/gen_corners.py b/configs/gen_corners.py deleted file mode 100644 index e1f5701..0000000 --- a/configs/gen_corners.py +++ /dev/null @@ -1,36 +0,0 @@ -import json - -import sys # hacky way to import utils for now -sys.path.append("..") -from utils import rgb_to_hex - - -corner_shape = [ # giving all 4 corners have the same shape - [0, 0, 0, 0], - [0, 1, 1, 0], - [0, 1, 1, 0], - [0, 0, 0, 0], -] - -center_color = [0, 0, 0] -corner_colors = { # assuming each corner will only have two colors (center_color and a corner-specific color) - 0: [255, 0, 0], - 1: [0, 255, 0], - 2: [0, 0, 255], - 3: [255, 255, 255,] -} - -center_color = rgb_to_hex(center_color) -corner_colors = {key: rgb_to_hex(val) for key, val in corner_colors.items()} - -corner_colored_shapes = { - corner_ind: [[corner_color if cell else center_color for cell in row] for row in corner_shape] - for corner_ind, corner_color in corner_colors.items() -} - -with open("corners_hollow4x4_v0.json", "w") as f: - json.dump({ - "corner_colors": corner_colored_shapes, - "corner_width": len(corner_shape[0]), - "corner_height": len(corner_shape), - }, f) diff --git a/display_animation.py b/display_animation.py deleted file mode 100644 index b9050d5..0000000 --- a/display_animation.py +++ /dev/null @@ -1,167 +0,0 @@ -import argparse -import json -import math -import time -import tkinter as tk - -import numpy as np -import pygame - -from utils import rgb_to_hex - - -def parse_args(): - return { - "cell_size": 8, # in pixels - "num_cells_w": 100, # TODO: w/h or r/c? - "num_cells_h": 100, - "colors_path": "configs/colors_64_v0.json", - "corners_path": "configs/corners_hollow4x4_v0.json", - "frame_delay": 1000 // 20, # time b/w frames, in millis - } - - -def seq_to_frames(bin_seq: np.ndarray, args: dict) -> np.ndarray: # TODO: Namespace - """ - Converts a binary sequence to an array of frames. - TODO: doc, expecting size of exactly one frame in the future? - - np.packbits seems relevant but limited to 8 bits - - Args: - bin_seq: a 1D array of binary values. - args: config parameters - - Returns: - array of shape (num_frames, args["num_cells_h"], args["num_cells_w"]), where each element is - a hex string for the color of that cell - """ - bin_seq = bin_seq.copy() # so that we don't mutate bin_seq - - with open(args["colors_path"], "r") as f: - colors = json.load(f) - - with open(args["corners_path"], "r") as f: - corners = json.load(f) - - assert len(corners["corner_colors"]) == 4, "Hardcoded for 4 corners" - corner_width = corners["corner_width"] - corner_height = corners["corner_height"] - - num_colors = len(colors) - bits_per_cell = int(math.log2(num_colors)) - assert 2**bits_per_cell == num_colors, "Assumed the number of colors is a power of 2." - - # bits_per_frame = bits_per_cell * args["num_cells_w"] * args["num_cells_h"] - # num_frames = int(math.ceil(len(bin_seq) / bits_per_frame)) - # bin_seq.resize(bits_per_frame * num_frames) - # # frames_bits = bin_seq.reshape(num_frames, args["num_cells_h"], args["num_cells_w"], bits_per_cell) - # - # pows = 2 ** np.arange(bits_per_cell) - # frames_vals = (frames_bits * pows).sum(axis=-1) # low to high bit order - - cells_per_frame = args["num_cells_w"] * args["num_cells_h"] - 4 * corner_width * corner_height - bits_per_frame = bits_per_cell * cells_per_frame - num_frames = int(math.ceil(len(bin_seq) / bits_per_frame)) - bin_seq.resize(bits_per_frame * num_frames) - frames_bits = bin_seq.reshape(num_frames, cells_per_frame, bits_per_cell) - pows = 2 ** np.arange(bits_per_cell) - frames_vals = (frames_bits * pows).sum(axis=-1) # low to high bit order - - # Efficiently map frame_vals to the corresponding hex colors (https://stackoverflow.com/a/55950051) - color_mapping_arr = np.empty(num_colors, dtype=" 1, adjusted_delay # o/w we lagged too far behind, assuming 1 ms for this instruction - self.canvas.after(adjusted_delay, self.display_frame) # TODO: check delay is exact - # TODO: set self time and just sleep until then - - def animate(self): - epilepsy_warning_id = self.canvas.create_text(self.width_pixels / 2, self.height_pixels / 2, - text="Warning: Epilepsy") - - def delete_and_animate(): - self.canvas.delete(epilepsy_warning_id) - self.start_time = time.time_ns() - self.display_frame() - - self.canvas.after(5000, delete_and_animate) - # self.display_frame() - self.root.mainloop() - - -if __name__ == "__main__": - args = parse_args() - - rand_bin_seq = np.random.randint(2, size=1000000) - frames = seq_to_frames(rand_bin_seq, args) - - width_pixels = args["cell_size"] * args["num_cells_w"] - height_pixels = args["cell_size"] * args["num_cells_h"] - - pygame.init() - clock = pygame.time.Clock() - screen = pygame.display.set_mode((width_pixels, height_pixels)) - font = pygame.font.SysFont(None, 25) - pygame.event.get() - - text = font.render("", True, "white") - text_rect = text.get_rect(center=(width_pixels / 2, height_pixels / 2)) - screen.blit(text, text_rect) - pygame.display.update() - clock.tick(0.2) - frame_ind = 0 - running = True - while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - for (i, j), color_hex in np.ndenumerate(frames[frame_ind % len(frames)]): - rect = pygame.Rect(i * args["cell_size"], j * args["cell_size"], args["cell_size"], args["cell_size"]) - pygame.draw.rect(screen, color_hex, rect) - - text = font.render(str(frame_ind), True, "black") - text_rect = text.get_rect(center=((args["num_cells_w"] - 2) * args["cell_size"], (args["num_cells_h"] - 2) * args["cell_size"])) - screen.blit(text, text_rect) - frame_ind += 1 - pygame.display.update() - # clock.tick(15) - clock.tick_busy_loop(10) # https://gamedev.stackexchange.com/a/102831 - if frame_ind % 30 == 0: - print(f"{clock.get_fps()=}") - - # https://stackoverflow.com/questions/28405222/syncing-image-display-with-screen-refresh-rate? - pygame.quit() diff --git a/display_animation_tkinter.py b/display_animation_tkinter.py deleted file mode 100644 index f26202c..0000000 --- a/display_animation_tkinter.py +++ /dev/null @@ -1,151 +0,0 @@ -import argparse -import json -import math -import time -import tkinter as tk - -import numpy as np - -from utils import rgb_to_hex - - -def parse_args(): - return { - "cell_size": 8, # in pixels - "num_cells_w": 100, # TODO: w/h or r/c? - "num_cells_h": 100, - "colors_path": "configs/colors_64_v0.json", - "corners_path": "configs/corners_hollow4x4_v0.json", - "frame_delay": 1000 // 15, # time b/w frames, in millis - } - - -def seq_to_frames(bin_seq: np.ndarray, args: dict) -> np.ndarray: # TODO: Namespace - """ - Converts a binary sequence to an array of frames. - TODO: doc, expecting size of exactly one frame in the future? - - np.packbits seems relevant but limited to 8 bits - - Args: - bin_seq: a 1D array of binary values. - args: config parameters - - Returns: - array of shape (num_frames, args["num_cells_h"], args["num_cells_w"]), where each element is - a hex string for the color of that cell - """ - bin_seq = bin_seq.copy() # so that we don't mutate bin_seq - - with open(args["colors_path"], "r") as f: - colors = json.load(f) - - with open(args["corners_path"], "r") as f: - corners = json.load(f) - - assert len(corners["corner_colors"]) == 4, "Hardcoded for 4 corners" - corner_width = corners["corner_width"] - corner_height = corners["corner_height"] - - num_colors = len(colors) - bits_per_cell = int(math.log2(num_colors)) - assert 2**bits_per_cell == num_colors, "Assumed the number of colors is a power of 2." - - # bits_per_frame = bits_per_cell * args["num_cells_w"] * args["num_cells_h"] - # num_frames = int(math.ceil(len(bin_seq) / bits_per_frame)) - # bin_seq.resize(bits_per_frame * num_frames) - # # frames_bits = bin_seq.reshape(num_frames, args["num_cells_h"], args["num_cells_w"], bits_per_cell) - # - # pows = 2 ** np.arange(bits_per_cell) - # frames_vals = (frames_bits * pows).sum(axis=-1) # low to high bit order - - cells_per_frame = args["num_cells_w"] * args["num_cells_h"] - 4 * corner_width * corner_height - bits_per_frame = bits_per_cell * cells_per_frame - num_frames = int(math.ceil(len(bin_seq) / bits_per_frame)) - bin_seq.resize(bits_per_frame * num_frames) - frames_bits = bin_seq.reshape(num_frames, cells_per_frame, bits_per_cell) - pows = 2 ** np.arange(bits_per_cell) - frames_vals = (frames_bits * pows).sum(axis=-1) # low to high bit order - - # Efficiently map frame_vals to the corresponding hex colors (https://stackoverflow.com/a/55950051) - color_mapping_arr = np.empty(num_colors, dtype=" 1, adjusted_delay # o/w we lagged too far behind, assuming 1 ms for this instruction - self.canvas.after(adjusted_delay, self.display_frame) # TODO: check delay is exact - # TODO: set self time and just sleep until then - - def animate(self): - epilepsy_warning_id = self.canvas.create_text(self.width_pixels / 2, self.height_pixels / 2, - text="Warning: Epilepsy") - - def delete_and_animate(): - self.canvas.delete(epilepsy_warning_id) - self.start_time = time.time_ns() - self.display_frame() - - self.canvas.after(5000, delete_and_animate) - # self.display_frame() - self.root.mainloop() - - -if __name__ == "__main__": - args = parse_args() - - rand_bin_seq = np.random.randint(2, size=1000000) - frames = seq_to_frames(rand_bin_seq, args) - - AnimatedFrames(frames, args).animate() diff --git a/utils.py b/utils.py deleted file mode 100644 index ec402a4..0000000 --- a/utils.py +++ /dev/null @@ -1,6 +0,0 @@ -def rgb_to_hex(rgb_list: list[int]) -> str: - """ - Converts a list of RGB values to a hex string. - """ - r, g, b = rgb_list - return f"#{r:02x}{g:02x}{b:02x}" -- cgit v1.2.3-70-g09d2