From 0376223c9d292916f08853f16c2c6378f61dd61f Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Sun, 3 Apr 2022 04:00:15 +0200 Subject: [PATCH] coordinate transformers refac --- .../coordinate/coordinate_transformer.py | 16 ++++++++++++++-- .../transformer/transformers/coordinate/fitz.py | 4 ++-- .../transformer/transformers/coordinate/fpdf.py | 4 ++-- .../transformers/coordinate/pdfnet.py | 4 ++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/image_prediction/transformer/transformers/coordinate/coordinate_transformer.py b/image_prediction/transformer/transformers/coordinate/coordinate_transformer.py index df886e6..c033e09 100644 --- a/image_prediction/transformer/transformers/coordinate/coordinate_transformer.py +++ b/image_prediction/transformer/transformers/coordinate/coordinate_transformer.py @@ -4,9 +4,21 @@ import abc class CoordinateTransformer: @abc.abstractmethod - def forward(self, metadata): + def _forward(self, metadata): raise NotImplementedError @abc.abstractmethod - def backward(self, metadata): + def _backward(self, metadata): raise NotImplementedError + + def forward(self, metadata): + try: + return self._forward(metadata) + except TypeError: + return map(self._forward, metadata) + + def backward(self, metadata): + try: + return self._backward(metadata) + except TypeError: + return map(self._backward, metadata) diff --git a/image_prediction/transformer/transformers/coordinate/fitz.py b/image_prediction/transformer/transformers/coordinate/fitz.py index 0d2301e..50c7a36 100644 --- a/image_prediction/transformer/transformers/coordinate/fitz.py +++ b/image_prediction/transformer/transformers/coordinate/fitz.py @@ -3,9 +3,9 @@ from image_prediction.transformer.transformers.coordinate.coordinate_transformer class FitzCoordinateTransformer(CoordinateTransformer): - def forward(self, metadata: dict): + def _forward(self, metadata: dict): """Fitz uses top left corner as origin; we take this as the reference coordinate system.""" return metadata - def backward(self, metadata: dict): + def _backward(self, metadata: dict): return self.forward(metadata) diff --git a/image_prediction/transformer/transformers/coordinate/fpdf.py b/image_prediction/transformer/transformers/coordinate/fpdf.py index d153fc0..ea98ea2 100644 --- a/image_prediction/transformer/transformers/coordinate/fpdf.py +++ b/image_prediction/transformer/transformers/coordinate/fpdf.py @@ -3,9 +3,9 @@ from image_prediction.transformer.transformers.coordinate.coordinate_transformer class FPDFCoordinateTransformer(CoordinateTransformer): - def forward(self, metadata: dict): + def _forward(self, metadata: dict): """FPDF uses top left corner as origin; we take this as the reference coordinate system.""" return metadata - def backward(self, metadata: dict): + def _backward(self, metadata: dict): return self.forward(metadata) diff --git a/image_prediction/transformer/transformers/coordinate/pdfnet.py b/image_prediction/transformer/transformers/coordinate/pdfnet.py index 0ca22da..03568c1 100644 --- a/image_prediction/transformer/transformers/coordinate/pdfnet.py +++ b/image_prediction/transformer/transformers/coordinate/pdfnet.py @@ -8,12 +8,12 @@ from image_prediction.transformer.transformers.coordinate.coordinate_transformer class PDFNetCoordinateTransformer(CoordinateTransformer): - def forward(self, metadata: dict): + def _forward(self, metadata: dict): """PDFNet coordinate system origin is in the bottom left corner.""" y1, y2, page_height = itemgetter(Info.Y1, Info.Y2, Info.PAGE_HEIGHT)(metadata) y1_t = page_height - y2 y2_t = page_height - y1 return {**omit(metadata, [Info.Y1, Info.Y2]), **{Info.Y1: y1_t, Info.Y2: y2_t}} - def backward(self, metadata: dict): + def _backward(self, metadata: dict): return self.forward(metadata)