Refactoring

Remove more code that is not adhering to separation of concerns from Rectangle class
This commit is contained in:
Matthias Bisping 2023-01-04 16:37:41 +01:00
parent 00e53fb54d
commit 97fb4b645d
3 changed files with 6 additions and 22 deletions

View File

@ -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):

View File

@ -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

View File

@ -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)