refactoring

This commit is contained in:
Matthias Bisping 2022-04-07 19:05:13 +02:00
parent 803cc57155
commit 160973e2be

View File

@ -131,8 +131,11 @@ class Stitcher:
groups = map(merge_group, groups) groups = map(merge_group, groups)
def merge_group(group, direction): def make_merger_aggregator(direction):
def merge_with(aggregation_pair, pairs): """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): 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.""" """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) aggr, non_aggr = juxt(first, rest)(pairs_aggr)
@ -142,15 +145,22 @@ def merge_group(group, direction):
else: else:
return aggr, pair, *non_aggr 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])) return list(reduce(merge_on_head_and_aggregate_in_tail, pairs, [aggregation_pair]))
c1_getter = make_coord_getter(f"{direction}1") c1_getter = make_coord_getter(f"{direction}1")
c2_getter = make_coord_getter(f"{direction}2") c2_getter = make_coord_getter(f"{direction}2")
pair_merger = make_pair_merger(direction) pair_merger = make_pair_merger(direction)
def reduce_group(group): return merger_aggregator
group_reduced = merge_with(*juxt(first, rest)(group))
return group_reduced
def merge_group(group, direction):
reduce_group = make_merger_aggregator(direction)
for g1, g2 in chunk_iterable(iterate(reduce_group, group), chunk_size=2): for g1, g2 in chunk_iterable(iterate(reduce_group, group), chunk_size=2):
if len(g1) == len(g2): if len(g1) == len(g2):