From 303970db513185adeada062ceea13ae0dd2ecc89 Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Thu, 7 Apr 2022 21:44:04 +0200 Subject: [PATCH] refactoring --- image_prediction/stitcher/stitcher.py | 5 ++++- image_prediction/stitcher/utils.py | 6 +++++- image_prediction/utils/generic.py | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/image_prediction/stitcher/stitcher.py b/image_prediction/stitcher/stitcher.py index 1f7d8a2..a0d91d2 100644 --- a/image_prediction/stitcher/stitcher.py +++ b/image_prediction/stitcher/stitcher.py @@ -50,4 +50,7 @@ class Stitcher: return pairs def stitch(self, pairs: Iterable[ImageMetadataPair]) -> List[ImageMetadataPair]: - return until_convergence(self.merge_along_both_axes, pairs) + def break_condition(pairs1, pairs2): + return len(pairs1) == len(pairs2) + + return until_convergence(self.merge_along_both_axes, break_condition, pairs) diff --git a/image_prediction/stitcher/utils.py b/image_prediction/stitcher/utils.py index ddbd0a3..951a621 100644 --- a/image_prediction/stitcher/utils.py +++ b/image_prediction/stitcher/utils.py @@ -130,8 +130,12 @@ def make_merger_aggregator(direction): def merge_group(group, direction): + + def break_condition(pairs1, pairs2): + return len(pairs1) == len(pairs2) + reduce_group = make_merger_aggregator(direction) - return until_convergence(reduce_group, group) + return until_convergence(reduce_group, break_condition, group) def merge_group_horizontally(group): diff --git a/image_prediction/utils/generic.py b/image_prediction/utils/generic.py index aa7228a..1560a9e 100644 --- a/image_prediction/utils/generic.py +++ b/image_prediction/utils/generic.py @@ -8,7 +8,7 @@ def chunk_iterable(iterable, chunk_size): return takewhile(truth, map(tuple, starmap(islice, repeat((iter(iterable), chunk_size))))) -def until_convergence(func, *args, **kwargs): +def until_convergence(func, cond, *args, **kwargs): for a, b in chunk_iterable(iterate(func, *args, **kwargs), chunk_size=2): - if len(a) == len(b): + if cond(a, b): return a