From 97fb4b645d8ff37502d4acac3e00cd020be8b4a0 Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Wed, 4 Jan 2023 16:37:41 +0100 Subject: [PATCH] Refactoring Remove more code that is not adhering to separation of concerns from Rectangle class --- cv_analysis/utils/structures.py | 23 ++++------------------- test/unit_tests/server_pipeline_test.py | 3 +-- test/unit_tests/table_parsing_test.py | 2 +- 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/cv_analysis/utils/structures.py b/cv_analysis/utils/structures.py index b110862..f205eb7 100644 --- a/cv_analysis/utils/structures.py +++ b/cv_analysis/utils/structures.py @@ -2,8 +2,7 @@ from __future__ import annotations from json import dumps -from operator import itemgetter -from typing import Iterable, Union, Dict +from typing import Iterable, Union from funcy import identity @@ -50,13 +49,13 @@ class Rectangle: return self.__h def __str__(self): - return dumps(self.json()) + return dumps(self.to_dict()) def __repr__(self): return str(self) def __iter__(self): - return list(self.json().values()).__iter__() + return list(self.to_dict().values()).__iter__() def __eq__(self, other: Rectangle): return all([self.x1 == other.x1, self.y1 == other.y1, self.w == other.w, self.h == other.h]) @@ -64,11 +63,6 @@ class Rectangle: def __hash__(self): return hash((self.x1, self.y1, self.x2, self.y2)) - @classmethod - def from_xyxy(cls, xyxy: Iterable[Coord], discrete=True): - """Creates a rectangle from two points.""" - return cls(*xyxy, discrete=discrete) - @classmethod def from_xywh(cls, xywh: Iterable[Coord], discrete=True): """Creates a rectangle from a point and a width and height.""" @@ -83,16 +77,7 @@ class Rectangle: def xywh(self): return self.x1, self.y1, self.w, self.h - def json(self): - return self.json_xywh() - - def json_full(self): - return {**self.json_xyxy(), "width": self.w, "height": self.h} - - def json_xyxy(self): - return {"x1": self.x1, "y1": self.y1, "x2": self.x2, "y2": self.y2} - - def json_xywh(self): + def to_dict(self): return {"x": self.x1, "y": self.y1, "width": self.w, "height": self.h} def intersection(self, other): diff --git a/test/unit_tests/server_pipeline_test.py b/test/unit_tests/server_pipeline_test.py index b0f9a28..0abb98f 100644 --- a/test/unit_tests/server_pipeline_test.py +++ b/test/unit_tests/server_pipeline_test.py @@ -7,8 +7,7 @@ from cv_analysis.utils.structures import Rectangle def analysis_fn_mock(image: np.ndarray): - bbox = (0, 0, 42, 42) - return [Rectangle.from_xyxy(bbox)] + return [Rectangle(0, 0, 42, 42)] @pytest.fixture diff --git a/test/unit_tests/table_parsing_test.py b/test/unit_tests/table_parsing_test.py index 0c14725..e3ef8c6 100644 --- a/test/unit_tests/table_parsing_test.py +++ b/test/unit_tests/table_parsing_test.py @@ -12,7 +12,7 @@ from cv_analysis.utils.test_metrics import compute_document_score def test_table_parsing_on_client_pages( score_threshold, client_page_with_table, expected_table_annotation, test_file_index ): - result = [x.json_xywh() for x in parse_tables(client_page_with_table)] + result = [rect.to_dict() for rect in parse_tables(client_page_with_table)] formatted_result = {"pages": [{"page": str(test_file_index), "cells": result}]} score = compute_document_score(formatted_result, expected_table_annotation)