Skip to content

Commit

Permalink
[misc & build] replace isort pydocstyle and black with ruff (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdittrich92 authored Nov 15, 2023
1 parent e00cc79 commit 6d92df5
Show file tree
Hide file tree
Showing 153 changed files with 969 additions and 478 deletions.
3 changes: 1 addition & 2 deletions .github/verify_pr_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.

"""
Borrowed & adapted from https://github.com/pytorch/vision/blob/main/.github/process_commit.py
"""Borrowed & adapted from https://github.com/pytorch/vision/blob/main/.github/process_commit.py
This script finds the merger responsible for labeling a PR by a commit SHA. It is used by the workflow in
'.github/workflows/pr-labels.yml'. If there exists no PR associated with the commit or the PR is properly labeled,
this script is a no-op.
Expand Down
58 changes: 0 additions & 58 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,6 @@ jobs:
ruff --version
ruff check --diff .
black:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
python: ["3.8"]
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
architecture: x64
- name: Run black
run: |
pip install black
black --version
black --check --diff .
isort:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
python: ["3.8"]
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
architecture: x64
- name: Run isort
run: |
pip install isort
isort --version
isort .
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then exit 1; else echo "All clear"; fi
mypy:
runs-on: ${{ matrix.os }}
strategy:
Expand Down Expand Up @@ -92,22 +53,3 @@ jobs:
run: |
mypy --version
mypy
pydocstyle:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
python: ["3.8"]
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
architecture: x64
- name: Run pydocstyle
run: |
pip install pydocstyle[toml]
pydocstyle --version
pydocstyle
16 changes: 5 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-ast
- id: check-yaml
Expand All @@ -15,15 +15,9 @@ repos:
- id: check-merge-conflict
- id: no-commit-to-branch
args: ['--branch', 'main']
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.260'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.5
hooks:
- id: ruff
args: [ --fix ]
- id: ruff-format
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
.PHONY: quality style test test-common test-tf test-torch docs-single-version docs
# this target runs checks on all files
quality:
isort . -c
ruff check .
black --check .
mypy doctr/
pydocstyle doctr/

# this target runs checks on all files and potentially modifies some of them
style:
isort .
black .
ruff --fix .
ruff format .

# Run tests for the library
test:
Expand Down
3 changes: 0 additions & 3 deletions demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

if is_tf_available():
import tensorflow as tf

from backend.tensorflow import DET_ARCHS, RECO_ARCHS, forward_image, load_predictor

if any(tf.config.experimental.list_physical_devices("gpu")):
Expand All @@ -24,15 +23,13 @@

else:
import torch

from backend.pytorch import DET_ARCHS, RECO_ARCHS, forward_image, load_predictor

forward_device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")


def main(det_archs, reco_archs):
"""Build a streamlit layout"""

# Wide mode
st.set_page_config(layout="wide")

Expand Down
28 changes: 22 additions & 6 deletions demo/backend/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,37 @@
]


def load_predictor(det_arch: str, reco_arch: str, device) -> OCRPredictor:
"""
def load_predictor(det_arch: str, reco_arch: str, device: torch.device) -> OCRPredictor:
"""Load a predictor from doctr.models
Args:
device is torch.device
----
det_arch: detection architecture
reco_arch: recognition architecture
device: torch.device, the device to load the predictor on
Returns:
-------
instance of OCRPredictor
"""
predictor = ocr_predictor(
det_arch, reco_arch, pretrained=True, assume_straight_pages=("rotation" not in det_arch)
).to(device)
return predictor


def forward_image(predictor: OCRPredictor, image: np.ndarray, device) -> np.ndarray:
"""
def forward_image(predictor: OCRPredictor, image: np.ndarray, device: torch.device) -> np.ndarray:
"""Forward an image through the predictor
Args:
device is torch.device
----
predictor: instance of OCRPredictor
image: image to process
device: torch.device, the device to process the image on
Returns:
-------
segmentation map
"""
with torch.no_grad():
processed_batches = predictor.det_predictor.pre_processor([image])
Expand Down
28 changes: 22 additions & 6 deletions demo/backend/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@
]


def load_predictor(det_arch: str, reco_arch: str, device) -> OCRPredictor:
"""
def load_predictor(det_arch: str, reco_arch: str, device: tf.device) -> OCRPredictor:
"""Load a predictor from doctr.models
Args:
device is tf.device
----
det_arch: detection architecture
reco_arch: recognition architecture
device: tf.device, the device to load the predictor on
Returns:
-------
instance of OCRPredictor
"""
with device:
predictor = ocr_predictor(
Expand All @@ -41,10 +49,18 @@ def load_predictor(det_arch: str, reco_arch: str, device) -> OCRPredictor:
return predictor


def forward_image(predictor: OCRPredictor, image: np.ndarray, device) -> np.ndarray:
"""
def forward_image(predictor: OCRPredictor, image: np.ndarray, device: tf.device) -> np.ndarray:
"""Forward an image through the predictor
Args:
device is tf.device
----
predictor: instance of OCRPredictor
image: image to process as numpy array
device: tf.device, the device to process the image on
Returns:
-------
segmentation map
"""
with device:
processed_batches = predictor.det_predictor.pre_processor([image])
Expand Down
4 changes: 1 addition & 3 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ def add_ga_javascript(app, pagename, templatename, context, doctree):
gtag('js', new Date());
gtag('config', '{0}');
</script>
""".format(
app.config.googleanalytics_id
)
""".format(app.config.googleanalytics_id)
context["metatags"] = metatags


Expand Down
2 changes: 1 addition & 1 deletion docs/source/using_doctr/using_models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ You can also export them as a nested dict, more appropriate for JSON format::

json_output = result.export()

For reference, here is the JSON export for the same `Document` as above::
For reference, here is the export for the same `Document` as above::

{
'pages': [
Expand Down
1 change: 1 addition & 0 deletions doctr/datasets/cord.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class CORD(VisionDataset):
>>> img, target = train_set[0]
Args:
----
train: whether the subset should be the training one
use_polygons: whether polygons should be considered as rotated bounding box (instead of straight ones)
recognition_task: whether the dataset should be used for recognition task
Expand Down
3 changes: 2 additions & 1 deletion doctr/datasets/datasets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __getitem__(self, index: int) -> Tuple[Any, Any]:
# Conditions to assess it is detection model with multiple classes and avoid confusion with other tasks.
if (
isinstance(target, dict)
and all([isinstance(item, np.ndarray) for item in target.values()])
and all(isinstance(item, np.ndarray) for item in target.values())
and set(target.keys()) != {"boxes", "labels"} # avoid confusion with obj detection target
):
img_transformed = _copy_tensor(img)
Expand All @@ -82,6 +82,7 @@ class _VisionDataset(_AbstractDataset):
"""Implements an abstract dataset
Args:
----
url: URL of the dataset
file_name: name of the file once downloaded
file_hash: expected SHA256 of the file
Expand Down
4 changes: 3 additions & 1 deletion doctr/datasets/datasets/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@


class AbstractDataset(_AbstractDataset):
"""Abstract class for all datasets"""

def _read_sample(self, index: int) -> Tuple[torch.Tensor, Any]:
img_name, target = self.data[index]

Expand Down Expand Up @@ -53,5 +55,5 @@ def collate_fn(samples: List[Tuple[torch.Tensor, Any]]) -> Tuple[torch.Tensor, L
return images, list(targets)


class VisionDataset(AbstractDataset, _VisionDataset):
class VisionDataset(AbstractDataset, _VisionDataset): # noqa: D101
pass
4 changes: 3 additions & 1 deletion doctr/datasets/datasets/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@


class AbstractDataset(_AbstractDataset):
"""Abstract class for all datasets"""

def _read_sample(self, index: int) -> Tuple[tf.Tensor, Any]:
img_name, target = self.data[index]

Expand Down Expand Up @@ -53,5 +55,5 @@ def collate_fn(samples: List[Tuple[tf.Tensor, Any]]) -> Tuple[tf.Tensor, List[An
return images, list(targets)


class VisionDataset(AbstractDataset, _VisionDataset):
class VisionDataset(AbstractDataset, _VisionDataset): # noqa: D101
pass
7 changes: 5 additions & 2 deletions doctr/datasets/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DetectionDataset(AbstractDataset):
>>> img, target = train_set[0]
Args:
----
img_folder: folder with all the images of the dataset
label_path: path to the annotations of each image
use_polygons: whether polygons should be considered as rotated bounding box (instead of straight ones)
Expand Down Expand Up @@ -66,14 +67,16 @@ def __init__(
def format_polygons(
self, polygons: Union[List, Dict], use_polygons: bool, np_dtype: Type
) -> Tuple[np.ndarray, List[str]]:
"""format polygons into an array
"""Format polygons into an array
Args:
----
polygons: the bounding boxes
use_polygons: whether polygons should be considered as rotated bounding box (instead of straight ones)
np_dtype: dtype of array
Returns:
-------
geoms: bounding boxes as np array
polygons_classes: list of classes for each bounding box
"""
Expand All @@ -92,4 +95,4 @@ def format_polygons(

@property
def class_names(self):
return sorted(list(set(self._class_names)))
return sorted(set(self._class_names))
1 change: 1 addition & 0 deletions doctr/datasets/doc_artefacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DocArtefacts(VisionDataset):
>>> img, target = train_set[0]
Args:
----
train: whether the subset should be the training one
use_polygons: whether polygons should be considered as rotated bounding box (instead of straight ones)
**kwargs: keyword arguments from `VisionDataset`.
Expand Down
3 changes: 2 additions & 1 deletion doctr/datasets/funsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class FUNSD(VisionDataset):
>>> img, target = train_set[0]
Args:
----
train: whether the subset should be the training one
use_polygons: whether polygons should be considered as rotated bounding box (instead of straight ones)
recognition_task: whether the dataset should be used for recognition task
Expand Down Expand Up @@ -81,7 +82,7 @@ def __init__(
text_targets, box_targets = zip(*_targets)
if use_polygons:
# xmin, ymin, xmax, ymax -> (x, y) coordinates of top left, top right, bottom right, bottom left corners
box_targets = [
box_targets = [ # type: ignore[assignment]
[
[box[0], box[1]],
[box[2], box[1]],
Expand Down
3 changes: 2 additions & 1 deletion doctr/datasets/generator/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ def synthesize_text_img(
"""Generate a synthetic text image
Args:
----
text: the text to render as an image
font_size: the size of the font
font_family: the font family (has to be installed on your system)
background_color: background color of the final image
text_color: text color on the final image
Returns:
-------
PIL image of the text
"""

background_color = (0, 0, 0) if background_color is None else background_color
text_color = (255, 255, 255) if text_color is None else text_color

Expand Down
Loading

0 comments on commit 6d92df5

Please sign in to comment.