From 4fca06fcdfb34abe54e8b063b870f8573b48449d Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Thu, 12 Aug 2021 20:03:48 +0200 Subject: [PATCH] Initial version --- ddresc_visu/__init__.py | 0 ddresc_visu/draw.py | 56 +++++++++++++++++++++++++++++++ ddresc_visu/mapfile.py | 73 +++++++++++++++++++++++++++++++++++++++++ test.py | 9 +++++ 4 files changed, 138 insertions(+) create mode 100644 ddresc_visu/__init__.py create mode 100644 ddresc_visu/draw.py create mode 100644 ddresc_visu/mapfile.py create mode 100755 test.py diff --git a/ddresc_visu/__init__.py b/ddresc_visu/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ddresc_visu/draw.py b/ddresc_visu/draw.py new file mode 100644 index 0000000..c38922e --- /dev/null +++ b/ddresc_visu/draw.py @@ -0,0 +1,56 @@ +from .mapfile import MapFile + +import pygame + +colors = { + '?' : (192, 192, 192), # Non-tried + '*' : (0 , 0 , 255), # Non-trimmed + '/' : (255, 255, 0) , # Non-scraped + '-' : (255, 0 , 0) , # Bad + '+' : (0 , 128, 0) , # Recovered + None: (0 , 0 , 0) , # Not on disk +} + +BORDERCOLOR = (64,64,64) + +W = 1900 +H = 1000 +CELLSIZE = 7 +CELLBORDER = 1 +CELLMARGIN = 1 + +CELLOFFSET = CELLSIZE + 2*CELLBORDER + CELLMARGIN +COLUMNS = W // CELLOFFSET +ROWS = H // CELLOFFSET +CELLS = COLUMNS * ROWS + +class Visualisation: + def __init__(self, fn): + pygame.init() + self.disp = pygame.display.set_mode((W, H)) + + self.mapfile = MapFile(fn) + self.mapfile.load() + + def draw(self): + self.disp.fill((0,0,0)) # Dark theme :-) + + sz = self.mapfile.size + sqsz = sz / CELLS + for cid in range(CELLS): + start = cid * sqsz + end = (cid + 1) * sqsz + hist = self.mapfile.get_hist(start, end).items() + # Majority + symb, _count = max(hist, key=lambda x: x[1]) + color = colors[symb] + + line = cid // COLUMNS + col = cid % COLUMNS + pos = (line, col) + + rect = pygame.Rect(col * CELLOFFSET + CELLBORDER, line * CELLOFFSET + CELLBORDER, CELLSIZE+2*CELLBORDER, CELLSIZE+2*CELLBORDER) + pygame.draw.rect(self.disp, color, rect) + pygame.draw.rect(self.disp, BORDERCOLOR, rect, width=CELLBORDER) + + pygame.display.flip() diff --git a/ddresc_visu/mapfile.py b/ddresc_visu/mapfile.py new file mode 100644 index 0000000..80e9475 --- /dev/null +++ b/ddresc_visu/mapfile.py @@ -0,0 +1,73 @@ +def read_mapfile(fn): + comments = [] + status = None + blockstrings = [] + with open(fn, 'r') as f: + for line in f: + if line.startswith("#"): + comments.append(line) + elif status is None: + status = line + else: + blockstrings.append(line) + + # Post-processing + status = status.split() + status[0] = int(status[0], base=0) + status[2] = int(status[2], base=0) + status = tuple(status) + + blocks = [] + for blockstring in blockstrings: + spl = blockstring.split() + start = int(spl[0], base=0) + length = int(spl[1], base=0) + char = spl[2] + blocks.append((start, length, char)) + + return (status, blocks, comments) + + +class MapFile: + def __init__(self, filename): + self.mapfilename = filename + + def load(self): + self.status, self.blocks, self.comments = read_mapfile(self.mapfilename) + self.size = max(map(lambda blk: blk[0]+blk[1], self.blocks)) + + def get_state(self, pos): + # TODO: this is lame + for blk in self.blocks: + if pos >= blk[0] and pos < blk[0] + blk[1]: + return blk[2] + + def get_hist(self, start, end): + relevant_blocks = [] + for blk in self.blocks: + if start < blk[0] + blk[1] and end >= blk[0]: + relevant_blocks.append(blk) + + # Trimming: + trmblks = [] + for blk in relevant_blocks: + bg, lt, symb = blk + if bg < start: + lt -= start - bg + bg = start + if bg + lt > end: + lt -= bg+lt - end + trmblks.append((bg, lt, symb)) + + # Grouping by symbols: + grp = {} + for blk in trmblks: + if blk[2] not in grp: + grp[blk[2]] = 0 + grp[blk[2]] += blk[1] + + return grp + + + + diff --git a/test.py b/test.py new file mode 100755 index 0000000..5064ea2 --- /dev/null +++ b/test.py @@ -0,0 +1,9 @@ +#!/bin/python3 +import ddresc_visu.draw as v + +visu = v.Visualisation('/mnt/TAP9701/2021-08-11_Pm_ddresc_mapfile') + +visu.draw() + +import time +time.sleep(10)