From 160973e2be2acd3640b4ca399ab0320617324e7e Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Thu, 7 Apr 2022 19:05:13 +0200 Subject: [PATCH] refactoring --- test/unit_tests/image_stitcher_test.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/unit_tests/image_stitcher_test.py b/test/unit_tests/image_stitcher_test.py index 5a47ccb..e6c546e 100644 --- a/test/unit_tests/image_stitcher_test.py +++ b/test/unit_tests/image_stitcher_test.py @@ -131,8 +131,11 @@ class Stitcher: groups = map(merge_group, groups) -def merge_group(group, direction): - def merge_with(aggregation_pair, pairs): +def make_merger_aggregator(direction): + """Produces a function f : [H, T1, ... Tn] -> [HTi...Tj, Tk ... Tl] that merges adjacent image-metadata pairs on the + head H and aggregates non-adjacent in the tail T. + """ + def merger_aggregator(pairs): def merge_on_head_and_aggregate_in_tail(pairs_aggr: Iterable[ImageMetadataPair], pair: ImageMetadataPair): """Keeps the image that is being merged with as the head and aggregates non-mergables in the tail.""" aggr, non_aggr = juxt(first, rest)(pairs_aggr) @@ -142,15 +145,22 @@ def merge_group(group, direction): else: return aggr, pair, *non_aggr + # Requires H to be the least element in image-concatenation direction by c1, since the concatenation happens + # only in c1 -> c2 direction. + pairs = sorted(pairs, key=c1_getter) + aggregation_pair, pairs = juxt(first, rest)(pairs) return list(reduce(merge_on_head_and_aggregate_in_tail, pairs, [aggregation_pair])) c1_getter = make_coord_getter(f"{direction}1") c2_getter = make_coord_getter(f"{direction}2") pair_merger = make_pair_merger(direction) - def reduce_group(group): - group_reduced = merge_with(*juxt(first, rest)(group)) - return group_reduced + return merger_aggregator + + +def merge_group(group, direction): + + reduce_group = make_merger_aggregator(direction) for g1, g2 in chunk_iterable(iterate(reduce_group, group), chunk_size=2): if len(g1) == len(g2):