From 57440f5106c54ca15b4be7ff5d8db6b2a2d89fbc Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Mon, 11 Apr 2022 09:53:32 +0200 Subject: [PATCH] refactoring --- image_prediction/stitching/merging.py | 6 +++--- test/utils/stitching.py | 30 +++++++++++++-------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/image_prediction/stitching/merging.py b/image_prediction/stitching/merging.py index 33e3541..fc557ef 100644 --- a/image_prediction/stitching/merging.py +++ b/image_prediction/stitching/merging.py @@ -10,7 +10,7 @@ from image_prediction.info import Info from image_prediction.stitching.grouping import CoordGrouper from image_prediction.stitching.utils import make_coord_getter, flatten_groups_once from image_prediction.utils.generic import until -from test.utils.stitching import HorizontalKeyMapper, VerticalKeyMapper +from test.utils.stitching import HorizontalSplitMapper, VerticalMapper def no_new_merges(pairs1, pairs2): @@ -120,12 +120,12 @@ def merge_pair_horizontally(p1: ImageMetadataPair, p2: ImageMetadataPair): def merge_metadata_vertically(m1: dict, m2: dict): - m1, m2 = map(VerticalKeyMapper, [m1, m2]) + m1, m2 = map(VerticalMapper, [m1, m2]) return merge_metadata(m1, m2) def merge_metadata_horizontally(m1: dict, m2: dict): - m1, m2 = map(HorizontalKeyMapper, [m1, m2]) + m1, m2 = map(HorizontalSplitMapper, [m1, m2]) return merge_metadata(m1, m2) diff --git a/test/utils/stitching.py b/test/utils/stitching.py index 6bdadbc..9bb2df0 100644 --- a/test/utils/stitching.py +++ b/test/utils/stitching.py @@ -2,15 +2,13 @@ import abc import random from copy import deepcopy from itertools import chain -from operator import itemgetter from funcy import rpartial, juxt from image_prediction.info import Info -class SplitKeyMapper(abc.ABC): - +class SplitMapper(abc.ABC): def __init__(self, wrapped: dict, keymap: dict): self.wrapped = wrapped self.keymap = keymap @@ -23,35 +21,35 @@ class SplitKeyMapper(abc.ABC): @property def dim(self): - return self.__getitem__("dim") + return self["dim"] @dim.setter def dim(self, value): - self.__setitem__("dim", value) + self["dim"] = value @property def c1(self): - return self.__getitem__("c1") + return self["c1"] @c1.setter def c1(self, value): - self.__setitem__("c1", value) + self["c1"] = value @property def c2(self): - return self.__getitem__("c2") + return self["c2"] @c2.setter def c2(self, value): - self.__setitem__("c2", value) + self["c2"] = value -class HorizontalKeyMapper(SplitKeyMapper): +class HorizontalSplitMapper(SplitMapper): def __init__(self, wrapped: dict): super().__init__(wrapped, {"dim": Info.WIDTH, "c1": Info.X1, "c2": Info.X2}) -class VerticalKeyMapper(SplitKeyMapper): +class VerticalMapper(SplitMapper): def __init__(self, wrapped: dict): super().__init__(wrapped, {"dim": Info.HEIGHT, "c1": Info.Y1, "c2": Info.Y2}) @@ -88,12 +86,12 @@ class BoxSplitter: return map(rpartial(self.__split_recursively, step + 1), boxes) def __split_horizontal(self, box): - return self.__split_if_large_enough(HorizontalKeyMapper(box)) + return self.__split_if_large_enough(HorizontalSplitMapper(box)) def __split_vertical(self, box): - return self.__split_if_large_enough(VerticalKeyMapper(box)) + return self.__split_if_large_enough(VerticalMapper(box)) - def __split_if_large_enough(self, wrapped_box: SplitKeyMapper): + def __split_if_large_enough(self, wrapped_box: SplitMapper): return ( self.__get_child_boxes(wrapped_box) if self.__large_enough(wrapped_box) @@ -101,11 +99,11 @@ class BoxSplitter: ) @staticmethod - def __large_enough(wrapped_box: SplitKeyMapper): + def __large_enough(wrapped_box: SplitMapper): return wrapped_box.dim >= 10 @staticmethod - def __get_child_boxes(wrapped_box: SplitKeyMapper): + def __get_child_boxes(wrapped_box: SplitMapper): split_len = random.randint(5, wrapped_box.dim - 5) split_point = wrapped_box.c1 + split_len