from copy import deepcopy from dataclasses import field, dataclass from operator import attrgetter from image_prediction.info import Info @dataclass class SplitMapper: """Manages access into a coordinate encoding mapping by abstracting over x1, x2 and y1, y2 as c1, c2; as well as over width and height as 'dim'.""" __keymap: dict wrapped: dict __wrapped: dict = field(init=False) dim: float = field(init=False) c1: float = field(init=False) c2: float = field(init=False) def __post_init__(self): for k, v in self.__keymap.items(): setattr(self, k, self.__wrapped[v]) @property def wrapped(self): ret = deepcopy(self.__wrapped) ret.update(dict(zip(self.__keymap.values(), attrgetter(*self.__keymap.keys())(self)))) return ret @wrapped.setter def wrapped(self, wrapped): self.__wrapped = wrapped class HorizontalSplitMapper(SplitMapper): def __init__(self, wrapped: dict): super().__init__({"dim": Info.WIDTH, "c1": Info.X1, "c2": Info.X2}, wrapped) class VerticalSplitMapper(SplitMapper): def __init__(self, wrapped: dict): super().__init__({"dim": Info.HEIGHT, "c1": Info.Y1, "c2": Info.Y2}, wrapped)