Skip to content

Commit

Permalink
style: speed up linting and autoformatting. fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
brycedrennan committed Sep 30, 2023
1 parent 460add1 commit e0852be
Show file tree
Hide file tree
Showing 73 changed files with 402 additions and 504 deletions.
59 changes: 33 additions & 26 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,42 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.9
cache: pip
cache-dependency-path: requirements-dev.txt
- name: Install dependencies
run: |
python -m pip install --disable-pip-version-check wheel pip-tools
pip-sync requirements-dev.txt
python -m pip install --disable-pip-version-check --no-deps .
- name: Lint
run: |
echo "::add-matcher::.github/pylama_matcher.json"
pylama --options tox.ini
- uses: actions/checkout@v3
- uses: actions/[email protected]
with:
python-version: 3.9
- name: Cache dependencies
uses: actions/[email protected]
id: cache
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('requirements-dev.txt') }}-lint
- name: Install Ruff
if: steps.cache.outputs.cache-hit != 'true'
run: grep -E 'ruff==' requirements-dev.txt | xargs pip install
- name: Lint
run: |
echo "::add-matcher::.github/pylama_matcher.json"
ruff --config tests/ruff.toml .
autoformat:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --disable-pip-version-check black==23.1.0 isort==5.12.0
- name: Autoformatter
run: |
black --diff .
isort --atomic --profile black --check-only .
- uses: actions/checkout@v3
- uses: actions/[email protected]
with:
python-version: 3.9
- name: Cache dependencies
uses: actions/[email protected]
id: cache
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('requirements-dev.txt') }}-autoformat
- name: Install Black
if: steps.cache.outputs.cache-hit != 'true'
run: grep -E 'black==' requirements-dev.txt | xargs pip install
- name: Lint
run: |
black --diff --fast .
test:
runs-on: ubuntu-latest
strategy:
Expand Down
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,16 @@ init: require_pyenv ## Setup a dev environment for local development.

af: autoformat ## Alias for `autoformat`
autoformat: ## Run the autoformatter.
@pycln . --all --quiet --extend-exclude __init__\.py
@# ERA,T201
@-ruff --extend-ignore ANN,ARG001,C90,DTZ,D100,D101,D102,D103,D202,D203,D212,D415,E501,RET504,S101,UP006,UP007 --extend-select C,D400,I,W --unfixable T,ERA --fix-only .
@-ruff check --config tests/ruff.toml . --fix-only
@black .
@isort --atomic --profile black --skip downloads/** .


test: ## Run the tests.
@pytest
@echo -e "The tests pass! ✨ 🍰 ✨"

lint: ## Run the code linter.
@pylama
@ruff check --config tests/ruff.toml .
@echo -e "No linting errors - well done! ✨ 🍰 ✨"

deploy: ## Deploy the package to pypi.org
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/immortal_pearl_earring.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def generate_image_morph_video():
if os.path.exists(filename):
continue

result = list(imagine([prompt]))[0]
result = next(iter(imagine([prompt])))
generated_image = result.images["generated"]

draw = ImageDraw.Draw(generated_image)
Expand Down
2 changes: 1 addition & 1 deletion imaginairy/animations.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def make_bounce_animation(

middle_imgs = shrink_list(middle_imgs, max_frames)

frames = [first_img] + middle_imgs + [last_img] + list(reversed(middle_imgs))
frames = [first_img, *middle_imgs, last_img, *list(reversed(middle_imgs))]

# convert from latents
converted_frames = []
Expand Down
27 changes: 14 additions & 13 deletions imaginairy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ def _record_step(img, description, image_count, step_count, prompt):
os.makedirs(subpath, exist_ok=True)
filepath = os.path.join(subpath, f"{basefilename}.gif")

frames = result.progress_latents + [result.images["generated"]]
frames = [*result.progress_latents, result.images["generated"]]

if prompt.init_image:
resized_init_image = pillow_fit_image_within(
prompt.init_image, prompt.width, prompt.height
)
frames = [resized_init_image] + frames
frames = [resized_init_image, *frames]
frames.reverse()
make_bounce_animation(
imgs=frames,
Expand Down Expand Up @@ -170,7 +170,7 @@ def imagine(
logger.info(
f"🖼 Generating {i + 1}/{num_prompts}: {prompt.prompt_description()}"
)
for attempt in range(0, unsafe_retry_count + 1):
for attempt in range(unsafe_retry_count + 1):
if attempt > 0 and isinstance(prompt.seed, int):
prompt.seed += 100_000_000 + attempt
result = _generate_single_image(
Expand Down Expand Up @@ -238,7 +238,7 @@ def _generate_single_image(
latent_channels = 4
downsampling_factor = 8
batch_size = 1
global _most_recent_result # noqa
global _most_recent_result
# handle prompt pulling in previous values
# if isinstance(prompt.init_image, str) and prompt.init_image.startswith("*prev"):
# _, img_type = prompt.init_image.strip("*").split(".")
Expand Down Expand Up @@ -457,16 +457,17 @@ def latent_logger(latents):
if control_image_t.shape[1] != 3:
raise RuntimeError("Control image must have 3 channels")

if control_input.mode != "inpaint":
if control_image_t.min() < 0 or control_image_t.max() > 1:
raise RuntimeError(
f"Control image must be in [0, 1] but we received {control_image_t.min()} and {control_image_t.max()}"
)
if (
control_input.mode != "inpaint"
and control_image_t.min() < 0
or control_image_t.max() > 1
):
msg = f"Control image must be in [0, 1] but we received {control_image_t.min()} and {control_image_t.max()}"
raise RuntimeError(msg)

if control_image_t.max() == control_image_t.min():
raise RuntimeError(
f"No control signal found in control image {control_input.mode}."
)
msg = f"No control signal found in control image {control_input.mode}."
raise RuntimeError(msg)

c_cat.append(control_image_t)
control_strengths.append(control_input.strength)
Expand Down Expand Up @@ -517,7 +518,7 @@ def latent_logger(latents):
if (
prompt.allow_compose_phase
and not is_controlnet_model
and not model.cond_stage_key == "edit"
and model.cond_stage_key != "edit"
):
if prompt.init_image:
comp_image = _generate_composition_image(
Expand Down
17 changes: 7 additions & 10 deletions imaginairy/cli/clickshell_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shlex
import traceback
from functools import update_wrapper
from typing import ClassVar

import click
from click_help_colors import HelpColorsCommand, HelpColorsMixin
Expand Down Expand Up @@ -43,27 +44,23 @@ def invoke_(self, arg): # pylint: disable=unused-argument
# and that's not ideal when running in a shell.
pass
except Exception as e: # noqa
traceback.print_exception(e) # noqa
traceback.print_exception(e)
# logger.warning(traceback.format_exc())

# Always return False so the shell doesn't exit
return False

invoke_ = update_wrapper(invoke_, command.callback)
invoke_.__name__ = "do_%s" % command.name # noqa
invoke_.__name__ = "do_%s" % command.name
return invoke_


class ModClickShell(ClickShell):
def add_command(self, cmd, name):
# Use the MethodType to add these as bound methods to our current instance
setattr(
self, "do_%s" % name, get_method_type(mod_get_invoke(cmd), self) # noqa
)
setattr(self, "help_%s" % name, get_method_type(get_help(cmd), self)) # noqa
setattr(
self, "complete_%s" % name, get_method_type(get_complete(cmd), self) # noqa
)
setattr(self, "do_%s" % name, get_method_type(mod_get_invoke(cmd), self))
setattr(self, "help_%s" % name, get_method_type(get_help(cmd), self))
setattr(self, "complete_%s" % name, get_method_type(get_complete(cmd), self))


class ModShell(Shell):
Expand All @@ -85,7 +82,7 @@ class ColorShell(HelpColorsMixin, ModShell):


class ImagineColorsCommand(HelpColorsCommand):
_option_order = []
_option_order: ClassVar = []

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
6 changes: 3 additions & 3 deletions imaginairy/cli/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
)
@add_options(edit_options)
@click.pass_context
def edit_cmd( # noqa
def edit_cmd(
ctx,
image_paths,
image_strength,
Expand Down Expand Up @@ -77,7 +77,7 @@ def edit_cmd( # noqa
model_weights_path,
model_config_path,
prompt_library_path,
version, # noqa
version,
make_gif,
make_compare_gif,
arg_schedules,
Expand Down Expand Up @@ -130,7 +130,7 @@ def edit_cmd( # noqa
model_weights_path,
model_config_path,
prompt_library_path,
version, # noqa
version,
make_gif,
make_compare_gif,
arg_schedules,
Expand Down
8 changes: 4 additions & 4 deletions imaginairy/cli/imagine.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def imagine_cmd(
model_weights_path,
model_config_path,
prompt_library_path,
version, # noqa
version,
make_gif,
make_compare_gif,
arg_schedules,
Expand All @@ -110,7 +110,7 @@ def imagine_cmd(
# hacky method of getting order of control images (mixing raw and normal images)
control_images = [
(o, path)
for o, path in ImagineColorsCommand._option_order # noqa
for o, path in ImagineColorsCommand._option_order
if o.name in ("control_image", "control_image_raw")
]
control_inputs = []
Expand Down Expand Up @@ -176,7 +176,7 @@ def imagine_cmd(
model_weights_path,
model_config_path,
prompt_library_path,
version, # noqa
version,
make_gif,
make_compare_gif,
arg_schedules,
Expand All @@ -187,4 +187,4 @@ def imagine_cmd(


if __name__ == "__main__":
imagine_cmd() # noqa
imagine_cmd()
2 changes: 1 addition & 1 deletion imaginairy/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ def model_list_cmd():


if __name__ == "__main__":
aimg() # noqa
aimg()
18 changes: 8 additions & 10 deletions imaginairy/cli/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _imagine_cmd(
model_weights_path,
model_config_path,
prompt_library_path,
version=False, # noqa
version=False,
make_gif=False,
make_compare_gif=False,
arg_schedules=None,
Expand Down Expand Up @@ -78,20 +78,16 @@ def _imagine_cmd(

configure_logging(log_level)

if isinstance(init_image, str):
init_images = [init_image]
else:
init_images = init_image
init_images = [init_image] if isinstance(init_image, str) else init_image

from imaginairy.utils import glob_expand_paths

num_prexpaned_init_images = len(init_images)
init_images = glob_expand_paths(init_images)

if len(init_images) < num_prexpaned_init_images:
raise ValueError(
f"Could not find any images matching the glob pattern(s) {init_image}. Are you sure the file(s) exists?"
)
msg = f"Could not find any images matching the glob pattern(s) {init_image}. Are you sure the file(s) exists?"
raise ValueError(msg)

total_image_count = len(prompt_texts) * max(len(init_images), 1) * repeats
logger.info(
Expand Down Expand Up @@ -227,7 +223,8 @@ def replace_option(options, option_name, new_option):
if option.name == option_name:
options[i] = new_option
return
raise ValueError(f"Option {option_name} not found")
msg = f"Option {option_name} not found"
raise ValueError(msg)


def remove_option(options, option_name):
Expand All @@ -242,7 +239,8 @@ def temp_f():
if option.name == option_name:
del options[i]
return
raise ValueError(f"Option {option_name} not found")
msg = f"Option {option_name} not found"
raise ValueError(msg)


common_options = [
Expand Down
2 changes: 1 addition & 1 deletion imaginairy/colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def colorize_img(img, max_width=1024, max_height=1024, caption=None):
steps=30,
prompt_strength=12,
)
result = list(imagine(prompt))[0]
result = next(iter(imagine(prompt)))
colorized_img = replace_color(img, result.images["generated"])

# allows the algorithm some leeway for the overall brightness of the image
Expand Down
8 changes: 5 additions & 3 deletions imaginairy/enhancers/bool_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""
import operator
from abc import ABC
from typing import ClassVar

import pyparsing as pp
import torch
Expand Down Expand Up @@ -57,7 +58,7 @@ def apply_masks(self, mask_cache):


class ModifiedMask(Mask):
ops = {
ops: ClassVar = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
Expand All @@ -80,7 +81,7 @@ def from_modifier_parse(cls, instring, tokens_start, ret_tokens):
return cls(mask=ret_tokens[0][0], modifier=ret_tokens[0][1])

def __repr__(self):
return f"{repr(self.mask)}{self.modifier}"
return f"{self.mask!r}{self.modifier}"

def gather_text_descriptions(self):
return self.mask.gather_text_descriptions()
Expand Down Expand Up @@ -141,7 +142,8 @@ def apply_masks(self, mask_cache):
elif self.op == "NOT":
mask = 1 - mask
else:
raise ValueError(f"Invalid operand {self.op}")
msg = f"Invalid operand {self.op}"
raise ValueError(msg)
return torch.clamp(mask, 0, 1)


Expand Down
Loading

0 comments on commit e0852be

Please sign in to comment.