Skip to content

Commit

Permalink
Merge pull request #567 from stweil/PEP8
Browse files Browse the repository at this point in the history
Clean import statements
  • Loading branch information
mittagessen authored Jan 30, 2024
2 parents b71cee0 + 56ed05d commit 2caaa97
Show file tree
Hide file tree
Showing 80 changed files with 456 additions and 456 deletions.
5 changes: 1 addition & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os
import shlex
from subprocess import PIPE, Popen

from subprocess import Popen, PIPE
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
Expand Down
13 changes: 6 additions & 7 deletions kraken/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
A character alignment module using a network output lattice and ground truth to
accuractely determine grapheme locations in input data.
"""
import torch
import logging
import dataclasses
import logging
from dataclasses import dataclass
from typing import TYPE_CHECKING, Literal, Optional

from PIL import Image
import torch
from bidi.algorithm import get_display

from dataclasses import dataclass
from typing import Optional, Literal, TYPE_CHECKING
from PIL import Image

from kraken import rpred
from kraken.containers import Segmentation, BaselineOCRRecord
from kraken.containers import BaselineOCRRecord, Segmentation

if TYPE_CHECKING:
from kraken.lib.models import TorchSeqRecognizer
Expand Down
12 changes: 6 additions & 6 deletions kraken/binarization.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
An adaptive binarization algorithm.
"""
import warnings
import logging
import numpy as np
import warnings
from typing import TYPE_CHECKING

from kraken.lib.util import pil2array, array2pil, is_bitonal, get_im_str
from scipy.ndimage import affine_transform, percentile_filter, gaussian_filter, binary_dilation
import numpy as np
from scipy.ndimage import (affine_transform, binary_dilation, gaussian_filter,
percentile_filter)
from scipy.ndimage import zoom as _zoom

from typing import TYPE_CHECKING

from kraken.lib.exceptions import KrakenInputException
from kraken.lib.util import array2pil, get_im_str, is_bitonal, pil2array

if TYPE_CHECKING:
from PIL import Image
Expand Down
34 changes: 16 additions & 18 deletions kraken/blla.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,30 @@
line recognizer uses the baseline paradigm.
"""

import PIL
import uuid
import torch
import logging
import numpy as np
import uuid
from typing import Any, Callable, Dict, List, Literal, Optional, Union

import importlib_resources
import numpy as np
import PIL
import shapely.geometry as geom
import torch
import torch.nn.functional as F
import torchvision.transforms as tf

from typing import Optional, Dict, Callable, Union, List, Any, Literal

from scipy.ndimage import gaussian_filter
from skimage.filters import sobel

from kraken.lib import vgsl, dataset
from kraken.containers import Region, Segmentation, BaselineLine
from kraken.lib.util import is_bitonal, get_im_str
from kraken.lib.exceptions import KrakenInputException, KrakenInvalidModelException
from kraken.lib.segmentation import (polygonal_reading_order,
neural_reading_order,
vectorize_lines, vectorize_regions,
scale_polygonal_lines,
calculate_polygonal_environment,
is_in_region,
scale_regions)
from kraken.containers import BaselineLine, Region, Segmentation
from kraken.lib import dataset, vgsl
from kraken.lib.exceptions import (KrakenInputException,
KrakenInvalidModelException)
from kraken.lib.segmentation import (calculate_polygonal_environment,
is_in_region, neural_reading_order,
polygonal_reading_order,
scale_polygonal_lines, scale_regions,
vectorize_lines, vectorize_regions)
from kraken.lib.util import get_im_str, is_bitonal

__all__ = ['segment']

Expand Down
11 changes: 6 additions & 5 deletions kraken/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
Container classes replacing the old dictionaries returned by kraken's
functional blocks.
"""
import numpy as np
import bidi.algorithm as bd

from typing import Literal, List, Dict, Union, Optional, Tuple, Any, TYPE_CHECKING
from dataclasses import dataclass, asdict
from abc import ABC, abstractmethod
from dataclasses import asdict, dataclass
from typing import (TYPE_CHECKING, Any, Dict, List, Literal, Optional, Tuple,
Union)

import bidi.algorithm as bd
import numpy as np

from kraken.lib.segmentation import compute_polygon_section

Expand Down
6 changes: 4 additions & 2 deletions kraken/contrib/baselineset_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
@click.argument('files', nargs=-1)
def cli(files):

import torch
from PIL import Image
from os.path import splitext

import torch
import torchvision.transforms as tf
from PIL import Image

from kraken.lib import dataset

batch, channels, height, width = 1, 3, 1200, 0
Expand Down
10 changes: 6 additions & 4 deletions kraken/contrib/extract_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ def cli(format_type, model, files):
click.echo(ctx.get_help())
ctx.exit()

from PIL import Image
from os.path import splitext
from kraken import blla
from kraken.lib import segmentation, vgsl, xml
import io
import json
from os.path import splitext

import pyarrow as pa
from PIL import Image

from kraken import blla
from kraken.lib import segmentation, vgsl, xml

if model is None:
for doc in files:
Expand Down
11 changes: 6 additions & 5 deletions kraken/contrib/forced_alignment_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
Draws a transparent overlay of the forced alignment output over the input
image.
"""
import re
import os
import click
import re
import unicodedata
from lxml import etree
from itertools import cycle
from unicodedata import normalize

import click
from lxml import etree

cmap = cycle([(230, 25, 75, 127),
(60, 180, 75, 127),
(255, 225, 25, 127),
Expand Down Expand Up @@ -100,9 +101,9 @@ def cli(format_type, model, normalization, output, files):

from PIL import Image, ImageDraw

from kraken.lib.xml import XMLPage
from kraken.lib import models
from kraken import align
from kraken.lib import models
from kraken.lib.xml import XMLPage

if format_type == 'alto':
repl_fn = _repl_alto
Expand Down
3 changes: 2 additions & 1 deletion kraken/contrib/generate_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"""
Script fetching the latest unicode Scripts.txt and dumping it as json.
"""
from urllib import request
import json
from urllib import request

import regex

uri = 'http://www.unicode.org/Public/UNIDATA/Scripts.txt'
Expand Down
8 changes: 5 additions & 3 deletions kraken/contrib/heatmap_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ def cli(model, files):
Applies a BLLA baseline segmentation model and outputs the raw heatmaps of
the first baseline class.
"""
from os.path import splitext

import torch
from PIL import Image
from kraken.lib import vgsl, dataset
import torch.nn.functional as F
from os.path import splitext
import torchvision.transforms as tf
from PIL import Image

from kraken.lib import dataset, vgsl

model = vgsl.TorchVGSLModel.load_model(model)
model.eval()
Expand Down
14 changes: 7 additions & 7 deletions kraken/contrib/hyperparameters/tune_pretraining.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
"""
A script for a grid search over pretraining hyperparameters.
"""
import click
from functools import partial

import click
import pytorch_lightning as pl
from pytorch_lightning import seed_everything
from ray import tune

from ray.tune.integration.pytorch_lightning import TuneReportCallback

from kraken.lib.default_specs import RECOGNITION_PRETRAIN_HYPER_PARAMS, RECOGNITION_SPEC
from kraken.lib.pretrain.model import PretrainDataModule, RecognitionPretrainModel
from kraken.ketos.util import _validate_manifests

import pytorch_lightning as pl
from pytorch_lightning import seed_everything
from kraken.lib.default_specs import (RECOGNITION_PRETRAIN_HYPER_PARAMS,
RECOGNITION_SPEC)
from kraken.lib.pretrain.model import (PretrainDataModule,
RecognitionPretrainModel)

config = {'lrate': tune.loguniform(1e-8, 1e-2),
'num_negatives': tune.qrandint(1, 4, 1),
Expand Down
11 changes: 5 additions & 6 deletions kraken/contrib/hyperparameters/tune_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import sys
from functools import partial

import pytorch_lightning as pl
from ray import tune

from ray.tune.integration.pytorch_lightning import TuneReportCallback

from kraken.lib.default_spec import RECOGNITION_PRETRAIN_HYPER_PARAMS, RECOGNITION_SPEC
from kraken.lib.pretrain.model import PretrainDataModule, RecognitionPretrainModel

import pytorch_lightning as pl

from kraken.lib.default_spec import (RECOGNITION_PRETRAIN_HYPER_PARAMS,
RECOGNITION_SPEC)
from kraken.lib.pretrain.model import (PretrainDataModule,
RecognitionPretrainModel)

config = {'lrate': tune.loguniform(1e-8, 1e-2),
'num_negatives': tune.qrandint(2, 100, 8),
Expand Down
7 changes: 6 additions & 1 deletion kraken/contrib/print_word_spreader.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#!/usr/bin/env python
#2020, Bruce Robertson
#Master file at https://github.com/brobertson/Lace2-tools/blob/master/normalize_hocr.py
import html, os, sys, argparse
import argparse
import html
import os
import sys
from statistics import mean

from lxml import etree
from PIL import Image


#a custom exception to indicate when a page or other element doesn't
#have a bounding box where we would expect it.
class BboxError(Exception):
Expand Down
6 changes: 3 additions & 3 deletions kraken/contrib/recognition_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

import os
import sys
from itertools import cycle

from PIL import Image, ImageDraw

from kraken.pageseg import segment
from kraken.binarization import nlbin
from kraken.rpred import rpred
from itertools import cycle
from kraken.lib import models
from kraken.pageseg import segment
from kraken.rpred import rpred

cmap = cycle([(230, 25, 75, 127),
(60, 180, 75, 127),
Expand Down
7 changes: 4 additions & 3 deletions kraken/contrib/repolygonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ def cli(format_type, topline, files):
click.echo(ctx.get_help())
ctx.exit()

from lxml import etree
from itertools import groupby
from os.path import splitext

from itertools import groupby
from kraken.lib import xml
from lxml import etree
from PIL import Image

from kraken.lib import xml
from kraken.lib.segmentation import calculate_polygonal_environment

def _repl_alto(fname, polygons):
Expand Down
10 changes: 5 additions & 5 deletions kraken/contrib/segmentation_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Draws a transparent overlay of baseline segmenter output over a list of image
files.
"""
import re
import dataclasses
import os
import click
import re
import unicodedata
import dataclasses
from itertools import cycle
from collections import defaultdict
from itertools import cycle

import click

cmap = cycle([(230, 25, 75, 127),
(60, 180, 75, 127)])
Expand Down Expand Up @@ -58,8 +58,8 @@ def cli(model, text_direction, repolygonize, files):

from PIL import Image, ImageDraw

from kraken.lib import vgsl, xml, segmentation
from kraken import blla
from kraken.lib import segmentation, vgsl, xml

if model is None:
for doc in files:
Expand Down
3 changes: 2 additions & 1 deletion kraken/contrib/set_seg_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"""
A script setting the metadata of segmentation models.
"""
import click
import shutil

import click


@click.command()
@click.option('-b', '--bounding-region', multiple=True, help='Sets region identifiers which bound line bounding polygons')
Expand Down
8 changes: 4 additions & 4 deletions kraken/ketos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
Command line drivers for training functionality.
"""

import click
import logging

import click
from PIL import Image
from rich.traceback import install

Expand All @@ -30,11 +30,11 @@
from .dataset import compile
from .linegen import line_generator
from .pretrain import pretrain
from .recognition import train, test
from .recognition import test, train
from .repo import publish
from .segmentation import segtrain, segtest
from .ro import roadd, rotrain
from .segmentation import segtest, segtrain
from .transcription import extract, transcription
from .ro import rotrain, roadd

APP_NAME = 'kraken'

Expand Down
Loading

0 comments on commit 2caaa97

Please sign in to comment.