Skip to content

Commit

Permalink
Migrate to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Dec 30, 2023
1 parent 9ae4df4 commit bfc46ab
Show file tree
Hide file tree
Showing 28 changed files with 239 additions and 214 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Flake8
- name: Install dependencies
run: |
pip install hacking==4.1.0
flake8 .
- name: Black
pip install -r requirements-dev.txt
- name: Lint
run: |
pip install black==22.3.0
black --check --diff .
ruff format --check
ruff check
- name: Mypy
run: |
pip install mypy types-PyYAML
Expand Down
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
all:
@echo '## Make commands ##'
@echo
@$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs

lint:
ruff format --check
ruff check

format:
ruff format
ruff check --fix

test:
pytest tests
8 changes: 2 additions & 6 deletions examples/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ def draw():
yxs = [(265, 265), (265, 330), (320, 315), (350, 270), (350, 320)]

# eye segment
viz = imgviz.draw.line(
viz, yx=[yxs[0], yxs[1]], fill=(255, 255, 255), width=5
)
viz = imgviz.draw.line(viz, yx=[yxs[0], yxs[1]], fill=(255, 255, 255), width=5)
# mouse segment
viz = imgviz.draw.line(
viz, yx=[yxs[3], yxs[4]], fill=(255, 255, 255), width=5
)
viz = imgviz.draw.line(viz, yx=[yxs[3], yxs[4]], fill=(255, 255, 255), width=5)

colors = imgviz.label_colormap(value=255)[1:]
shapes = ["star", "ellipse", "rectangle", "circle", "triangle"]
Expand Down
2 changes: 1 addition & 1 deletion examples/instances2rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def instances2rgb():
data = imgviz.data.voc()

captions = [data["class_names"][l] for l in data["labels"]]
captions = [data["class_names"][label_id] for label_id in data["labels"]]
insviz1 = imgviz.instances2rgb(
image=data["rgb"],
bboxes=data["bboxes"],
Expand Down
4 changes: 1 addition & 3 deletions examples/label2rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ def label2rgb():
rgb = data["rgb"]
label = data["class_label"]

label_names = [
"{}:{}".format(i, n) for i, n in enumerate(data["class_names"])
]
label_names = ["{}:{}".format(i, n) for i, n in enumerate(data["class_names"])]
labelviz_withname1 = imgviz.label2rgb(
label, label_names=label_names, font_size=25, loc="centroid"
)
Expand Down
8 changes: 6 additions & 2 deletions getting_started.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

# colorize label image
class_label = data["class_label"]
labelviz = imgviz.label2rgb(class_label, image=gray, label_names=data["class_names"], font_size=20)
labelviz = imgviz.label2rgb(
class_label, image=gray, label_names=data["class_names"], font_size=20
)

# instance bboxes
bboxes = data["bboxes"].astype(int)
Expand All @@ -33,7 +35,9 @@
maskviz = imgviz.instances2rgb(gray, masks=masks, labels=labels, captions=captions)

# tile instance masks
insviz = [(rgb * m[:, :, None])[b[0] : b[2], b[1] : b[3]] for b, m in zip(bboxes, masks)]
insviz = [
(rgb * m[:, :, None])[b[0] : b[2], b[1] : b[3]] for b, m in zip(bboxes, masks)
]
insviz = imgviz.tile(imgs=insviz, border=(255, 255, 255))
insviz = imgviz.resize(insviz, height=rgb.shape[0])

Expand Down
4 changes: 1 addition & 3 deletions imgviz/_io/_pyglet/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@

def check_pyglet_available():
if pyglet is None:
raise ImportError(
"pyglet is not installed, run following: pip install pyglet"
)
raise ImportError("pyglet is not installed, run following: pip install pyglet")
return pyglet
28 changes: 7 additions & 21 deletions imgviz/_io/_pyglet/pyglet_imshow.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def pyglet_imshow(image, caption=None, interval=0.5, keymap=None, hook=None):
hook=hook,
)
elif isinstance(image, np.ndarray):
_pyglet_imshow_ndarray(
image, caption=caption, keymap=keymap, hook=hook
)
_pyglet_imshow_ndarray(image, caption=caption, keymap=keymap, hook=hook)
else:
_pyglet_imshow_list(
image,
Expand All @@ -54,9 +52,7 @@ def pyglet_imshow(image, caption=None, interval=0.5, keymap=None, hook=None):
)


def _pyglet_imshow_list(
images, caption=None, interval=0.5, keymap=None, hook=None
):
def _pyglet_imshow_list(images, caption=None, interval=0.5, keymap=None, hook=None):
pyglet = check_pyglet_available()

def usage():
Expand Down Expand Up @@ -88,9 +84,7 @@ def usage():
else:
image = hook(images[0])
if not isinstance(image, (np.ndarray, PIL.Image.Image)):
raise TypeError(
"hook must return numpy.ndarray or PIL.Image.Image"
)
raise TypeError("hook must return numpy.ndarray or PIL.Image.Image")
max_image_height, max_image_width = image.shape[:2]
aspect_ratio = max_image_width / max_image_height

Expand All @@ -108,14 +102,10 @@ def usage():
def _post_image_update():
_centerize_sprite_in_window(sprite, window)
window.set_caption(
"{} {}/{}".format(
images[window.index], window.index + 1, len(images)
)
"{} {}/{}".format(images[window.index], window.index + 1, len(images))
)
print(
"{} {}/{}".format(
images[window.index], window.index + 1, len(images)
),
"{} {}/{}".format(images[window.index], window.index + 1, len(images)),
file=sys.stderr,
)

Expand Down Expand Up @@ -152,18 +142,14 @@ def on_key_press(symbol, modifiers):
if window.index + 1 <= len(images) - 1:
window.index += 1
sprite.image = _convert_to_imagedata(
hook(images[window.index])
if hook
else images[window.index]
hook(images[window.index]) if hook else images[window.index]
)
_post_image_update()
elif symbol == pyglet.window.key.P:
if window.index - 1 >= 0:
window.index -= 1
sprite.image = _convert_to_imagedata(
hook(images[window.index])
if hook
else images[window.index]
hook(images[window.index]) if hook else images[window.index]
)
_post_image_update()
elif symbol == pyglet.window.key.S:
Expand Down
15 changes: 9 additions & 6 deletions imgviz/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ def asgray(img):
gray = rgb2gray(img)
else:
raise ValueError(
"Unsupported image format to convert to gray:"
"shape={}, dtype={}".format(img.shape, img.dtype)
"Unsupported image format to convert to gray:" "shape={}, dtype={}".format(
img.shape, img.dtype
)
)
return gray

Expand Down Expand Up @@ -190,8 +191,9 @@ def asrgb(img):
rgb = img
else:
raise ValueError(
"Unsupported image format to convert to rgb:"
"shape={}, dtype={}".format(img.shape, img.dtype)
"Unsupported image format to convert to rgb:" "shape={}, dtype={}".format(
img.shape, img.dtype
)
)
return rgb

Expand Down Expand Up @@ -221,8 +223,9 @@ def asrgba(img):
rgba = rgb2rgba(img)
else:
raise ValueError(
"Unsupported image format to convert to rgba:"
"shape={}, dtype={}".format(img.shape, img.dtype)
"Unsupported image format to convert to rgba:" "shape={}, dtype={}".format(
img.shape, img.dtype
)
)
return rgba

Expand Down
4 changes: 1 addition & 3 deletions imgviz/data/arc2017/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ def arc2017():
class_names = [name.strip() for name in f]
data["class_names"] = class_names

data["res4"] = np.load(osp.join(here, "res4.npz"), allow_pickle=True)[
"res4"
]
data["res4"] = np.load(osp.join(here, "res4.npz"), allow_pickle=True)["res4"]

with open(osp.join(here, "camera_info.yaml")) as f:
data["camera_info"] = yaml.safe_load(f)
Expand Down
4 changes: 1 addition & 3 deletions imgviz/depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ def __call__(self, depth, dtype=np.uint8):
"""
assert depth.ndim == 2, "depth image must be 2 dimensional"
assert np.issubdtype(
depth.dtype, np.floating
), "depth dtype must be float"
assert np.issubdtype(depth.dtype, np.floating), "depth dtype must be float"

normalized, self._min_value, self._max_value = normalize(
depth,
Expand Down
4 changes: 1 addition & 3 deletions imgviz/draw/ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

def ellipse(src, yx1, yx2, fill=None, outline=None, width=0):
dst = utils.numpy_to_pillow(src)
ellipse_(
img=dst, yx1=yx1, yx2=yx2, fill=fill, outline=outline, width=width
)
ellipse_(img=dst, yx1=yx1, yx2=yx2, fill=fill, outline=outline, width=width)
return utils.pillow_to_numpy(dst)


Expand Down
4 changes: 1 addition & 3 deletions imgviz/draw/rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,4 @@ def rectangle_(img, aabb1, aabb2, fill=None, outline=None, width=0):

y1, x1 = aabb1
y2, x2 = aabb2
draw.rectangle(
xy=(x1, y1, x2, y2), fill=fill, outline=outline, width=width
)
draw.rectangle(xy=(x1, y1, x2, y2), fill=fill, outline=outline, width=width)
4 changes: 1 addition & 3 deletions imgviz/draw/star.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ def star_(img, center, size, fill=None, outline=None):

# 5 valleys
angles_v = angles_m + np.pi / 5
length = radius / (
np.sin(np.pi / 5) / np.tan(np.pi / 10) + np.cos(np.pi / 10)
)
length = radius / (np.sin(np.pi / 5) / np.tan(np.pi / 10) + np.cos(np.pi / 10))
x_v = cx + length * np.cos(angles_v)
y_v = cy - length * np.sin(angles_v)
xy_v = np.stack((x_v, y_v), axis=1)
Expand Down
8 changes: 2 additions & 6 deletions imgviz/draw/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

def _get_font(size, font_path=None):
if font_path is None:
fonts_path = osp.join(
osp.dirname(matplotlib.__file__), "mpl-data/fonts/ttf"
)
fonts_path = osp.join(osp.dirname(matplotlib.__file__), "mpl-data/fonts/ttf")
font_path = osp.join(fonts_path, "DejaVuSansMono.ttf")
font = PIL.ImageFont.truetype(font=font_path, size=size)
return font
Expand Down Expand Up @@ -80,9 +78,7 @@ def text(src, yx, text, size, color=(0, 0, 0), font_path=None):
"""
dst = utils.numpy_to_pillow(src)
text_(
img=dst, yx=yx, text=text, size=size, color=color, font_path=font_path
)
text_(img=dst, yx=yx, text=text, size=size, color=color, font_path=font_path)
return utils.pillow_to_numpy(dst)


Expand Down
4 changes: 1 addition & 3 deletions imgviz/draw/text_in_rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
from .text import text_size


def text_in_rectangle_aabb(
img_shape, loc, text, size, aabb1, aabb2, font_path=None
):
def text_in_rectangle_aabb(img_shape, loc, text, size, aabb1, aabb2, font_path=None):
height, width = img_shape[:2]

y1, x1 = (0, 0) if aabb1 is None else aabb1
Expand Down
Loading

0 comments on commit bfc46ab

Please sign in to comment.