Skip to content

Commit

Permalink
TLDR-428 add flake8 tests and fix styles (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
dronperminov authored Jul 28, 2023
1 parent 71a9679 commit e2f5553
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 152 deletions.
20 changes: 20 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[flake8]

max-line-length = 160
max-complexity = 10
inline-quotes = "
application-import-names = dedocutils, tests
import-order-style = pycharm
exclude =
.git,
__pycache__,
.idea,
build,
dedocutils/text_detection/doctr_text_detector/doctr,
*__init__.py
# ANN101 - type annotations for self
ignore =
ANN101
5 changes: 4 additions & 1 deletion .github/workflows/test_on_push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ jobs:
run: |
python3 -m pip install --upgrade pip
pip3 install .[dev,torch]
- name: Run lint
run: |
pip3 install .[lint]
flake8 .
- name: Run tests
run: |
python3 -m unittest -v -f tests/test_style.py
python3 -m unittest -v -f tests/unit_tests/test*
12 changes: 6 additions & 6 deletions dedocutils/data_structures/bbox.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import OrderedDict
from dataclasses import dataclass
from typing import Tuple, Dict
from typing import Dict, Tuple


@dataclass
Expand Down Expand Up @@ -46,12 +46,12 @@ def from_two_points(top_left: Tuple[int, int], bottom_right: Tuple[int, int]) ->

def have_intersection_with_box(self, box: "BBox", threshold: float = 0.3) -> bool:
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(self.x_top_left, box.x_top_left)
yA = max(self.y_top_left, box.y_top_left)
xB = min(self.x_top_left + self.width, box.x_top_left + box.width)
yB = min(self.y_top_left + self.height, box.y_top_left + box.height)
x_min = max(self.x_top_left, box.x_top_left)
y_min = max(self.y_top_left, box.y_top_left)
x_max = min(self.x_top_left + self.width, box.x_top_left + box.width)
y_max = min(self.y_top_left + self.height, box.y_top_left + box.height)
# compute the area of intersection rectangle
inter_a_area = max(0, xB - xA) * max(0, yB - yA)
inter_a_area = max(0, x_max - x_min) * max(0, y_max - y_min)
# compute the area of both the prediction and ground-truth
# rectangles
box_b_area = float(box.width * box.height)
Expand Down
10 changes: 5 additions & 5 deletions dedocutils/line_segmentation/clustering_line_segmenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def segment(self, bboxes: List[BBox], parameters: Optional[dict] = None) -> List
for i, pred_item in enumerate(pred):
bbox = bboxes[i]
cluster = pred_item
sorted_bboxes[cluster]['bbox'].append(bbox)
sorted_bboxes[cluster]['avgY'] += bbox.y_top_left
sorted_bboxes[cluster]["bbox"].append(bbox)
sorted_bboxes[cluster]["avgY"] += bbox.y_top_left

for el in sorted_bboxes:
el['avgY'] /= len(el['bbox'])
sorted_bboxes = sorted(sorted_bboxes, key=lambda x: x['avgY'])
sorted_bboxes = [el['bbox'] for el in sorted_bboxes]
el["avgY"] /= len(el["bbox"])
sorted_bboxes = sorted(sorted_bboxes, key=lambda x: x["avgY"])
sorted_bboxes = [el["bbox"] for el in sorted_bboxes]
sorted_bboxes = [sorted(line, key=lambda x: x.x_top_left) for line in sorted_bboxes]
return sorted_bboxes
4 changes: 2 additions & 2 deletions dedocutils/line_segmentation/intersection_line_segmenter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Tuple, Optional
from typing import List, Optional, Tuple

import numpy as np

Expand All @@ -21,7 +21,7 @@ def segment(self, bboxes: List[BBox], parameters: Optional[dict] = None) -> List
box_group, lines = self.__segment_lines(np.array(box_group))

res_lines = []
for line_id, line in enumerate(lines):
for line in lines:
res_lines.append([BBox(box[0][0], box[0][1], box[1][0] - box[0][0], box[1][1] - box[0][1]) for box in line])

return res_lines
Expand Down
3 changes: 2 additions & 1 deletion dedocutils/preprocessing/adaptive_binarizer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Tuple, Optional
from typing import Optional, Tuple

import cv2
import numpy as np

Expand Down
2 changes: 1 addition & 1 deletion dedocutils/preprocessing/skew_corrector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple, Optional
from typing import Optional, Tuple

import cv2
import numpy as np
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ def _set_device(self, on_gpu: bool) -> None:
self.location = lambda storage, loc: storage.cuda()
else:
self.device = torch.device("cpu")
self.location = 'cpu'
self.location = "cpu"
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Optional, List
from typing import List, Optional

import numpy as np
import pytesseract
from dedocutils.data_structures import BBox

from dedocutils.data_structures import BBox
from dedocutils.data_structures.text_with_bbox import TextWithBBox
from dedocutils.text_detection_recognition.abstract_detector_recognizer import AbstractDetectorRecognizer

Expand Down
19 changes: 14 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@ torch = [
]
dev = [
"build==0.10.0",
"twine==4.0.2",
"pycodestyle==2.7.0",
"flake8==3.9.2",
"flake8-annotations==2.6.2",
"pyflakes==2.3.1"
"twine==4.0.2"
]
lint = [
"flake8==5.0.4",
"flake8-absolute-import==1.0.0.1",
"flake8-annotations==2.9.1",
"flake8-bugbear==23.3.12",
"flake8-builtins==2.1.0",
"flake8-import-order==0.18.2",
"flake8-print==5.0.0",
"flake8-quotes==3.3.2",
"flake8-use-fstring==1.4",
"pycodestyle==2.9.0",
"pep8-naming==0.13.3"
]

[tool.setuptools.packages.find]
Expand Down
128 changes: 0 additions & 128 deletions tests/test_style.py

This file was deleted.

1 change: 1 addition & 0 deletions tests/unit_tests/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest

import cv2

from dedocutils.text_detection import DoctrTextDetector


Expand Down

0 comments on commit e2f5553

Please sign in to comment.