merging algorithm explanation adjusted

This commit is contained in:
Matthias Bisping 2022-04-11 09:28:00 +02:00
parent 887b8339a2
commit 710783a2f8

View File

@ -1,6 +1,6 @@
from copy import deepcopy
from functools import reduce
from typing import Iterable, Callable
from typing import Iterable, Callable, List
from PIL import Image
from funcy import juxt, first, rest, rcompose
@ -17,14 +17,29 @@ def no_new_merges(pairs1, pairs2):
return len(pairs1) == len(pairs2)
def merge_along_both_axes(pairs: Iterable[ImageMetadataPair]):
def merge_along_both_axes(pairs: Iterable[ImageMetadataPair]) -> List[ImageMetadataPair]:
pairs = merge_along_axis(pairs, "x")
pairs = list(merge_along_axis(pairs, "y"))
return pairs
def merge_along_axis(pairs: Iterable[ImageMetadataPair], axis):
def merge_along_axis(pairs: Iterable[ImageMetadataPair], axis) -> Iterable[ImageMetadataPair]:
"""Partially merges image-metadata pairs of adjacent images along a given axis. Needs to be iterated with
alternating axes until no more merges happen to merge all adjacent images.
Explanation:
Merging algorithm works as follows:
A dot represents a pair, a bracket a group and a colon a merged pair.
1) Start with pairs: (........)
2) Align on lesser: ([....] [....])
3) Align on greater: ([[..] [..]] [[....]])
4) Flatten once: ([..] [..] [....])
5) Merge orthogonally: ([:] [..] [:..])
6) Flatten once: (:..:..)
"""
def group_pairs_within_groups_by_greater_coordinate(groups):
return map(CoordGrouper(axis).group_pairs_by_greater_coordinate, groups)
@ -35,11 +50,11 @@ def merge_along_axis(pairs: Iterable[ImageMetadataPair], axis):
return CoordGrouper(axis).group_pairs_by_lesser_coordinate(pairs)
return rcompose(
group_pairs_by_lesser_coordinate, # pairs -> groups of pairs aligned on one edge
group_pairs_within_groups_by_greater_coordinate, # -> groups of pairs fully aligned on orthogonal axis
flatten_groups_once, # groups of groups of pairs -> groups of pairs
group_pairs_by_lesser_coordinate,
group_pairs_within_groups_by_greater_coordinate,
flatten_groups_once,
merge_groups_along_orthogonal_axis,
flatten_groups_once, # groups of pairs -> pairs
flatten_groups_once,
)(pairs)