Skip to content

Commit

Permalink
merge changes to main scripts from nodegui
Browse files Browse the repository at this point in the history
  • Loading branch information
zeptofine committed Nov 19, 2023
1 parent 6f3b502 commit a3d0fdb
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 82 deletions.
75 changes: 28 additions & 47 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ target-version = "py311"

[tool.isort]
profile = "black"

[tool.pylint]
disable = [
"C0114", # missing-module-docstring
"C0115", # missing-class-docstring
"C0116", # missing-function-docstring
]
max-line-length = 120
4 changes: 1 addition & 3 deletions src/imdataset_creator/configs/keyworded.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,5 @@ def _obj_to_comment(obj) -> str:
return ""

def __repr__(self) -> str:
attrlist: list[str] = [
f"{key}={val!r}" for key, val in vars(self).items() if all(k not in key for k in ("__",))
]
attrlist = [f"{key}={val!r}" for key, val in vars(self).items() if all(k not in key for k in ("__",))]
return f"{self.__class__.__name__}({', '.join(attrlist)})"
4 changes: 2 additions & 2 deletions src/imdataset_creator/datarules/base_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class Input(Keyworded):

@classmethod
def from_cfg(cls, cfg: InputData):
return cls(Path(cfg["folder"]).expanduser(), cfg["expressions"])
return cls(Path(cfg.get("folder", ".")).expanduser(), cfg["expressions"])

def run(self) -> PathGenerator:
"""
Expand Down Expand Up @@ -300,7 +300,7 @@ def combine_expr_conds(exprs: Iterable[Expr]) -> Expr:
Parameters
----------
exprs : list[Expr]
exprs : Iterable[Expr]
A list of `Expr` objects to be combined.
Returns
Expand Down
8 changes: 4 additions & 4 deletions src/imdataset_creator/datarules/dataset_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import textwrap
import warnings
from collections.abc import Collection, Generator, Iterable
from collections.abc import Collection, Container, Generator, Iterable, Mapping
from datetime import datetime
from io import BytesIO
from pathlib import Path
Expand Down Expand Up @@ -80,7 +80,7 @@ def combine_matchers(
yield combination


def blacklist_schema(schema: ProducerSchema, blacklist: Collection) -> ProducerSchema:
def blacklist_schema(schema: ProducerSchema, blacklist: Container[str]) -> ProducerSchema:
return [out for dct in schema if (out := {k: v for k, v in dct.items() if k not in blacklist})]


Expand Down Expand Up @@ -129,7 +129,7 @@ def add_producers(self, *producers: Producer):
for producer in producers:
self.add_producer(producer)

def fill_from_config(self, cfg: dict[str, dict], no_warn=False):
def fill_from_config(self, cfg: Mapping[str, dict], no_warn=False):
if not len(self.unready_rules):
return

Expand Down Expand Up @@ -277,7 +277,7 @@ def get_unfinished(self) -> LazyFrame:
def get_unfinished_existing(self) -> LazyFrame:
return self.get_unfinished().filter(pl.col("path").apply(os.path.exists))

def filter(self, lst) -> DataFrame: # noqa: A003
def filter(self, lst: Collection[str]) -> DataFrame: # noqa: A003
if len(self.unready_rules):
warnings.warn(
f"{len(self.unready_rules)} filters are not initialized and will not be populated",
Expand Down
13 changes: 11 additions & 2 deletions src/imdataset_creator/datarules/image_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@ def whash_db4(img) -> imagehash.ImageHash:
return imagehash.whash(img, mode="db4")


def get_hwc(pth):
def _get_hwc(pth):
img = Image.open(pth)
return {"width": img.width, "height": img.height, "channels": len(img.getbands())}
return img.height, img.width, len(img.getbands())


def get_hwc(pth):
h, w, c = _get_hwc(pth)
return {
"height": h,
"width": w,
"channels": c,
}


class ImShapeProducer(Producer):
Expand Down
2 changes: 0 additions & 2 deletions src/imdataset_creator/gui/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# this is one of those words that look weird when you hear it a lot

import logging
import os
import sys
Expand Down
12 changes: 8 additions & 4 deletions src/imdataset_creator/gui/config_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import contextlib
import functools
from copy import deepcopy as objcopy
from typing import Generic, Iterable, TypeVar

from PySide6.QtCore import QRect, Qt, Signal, Slot
from PySide6.QtGui import QAction, QMouseEvent
Expand Down Expand Up @@ -403,7 +404,7 @@ def get_cfg(self) -> list[ItemConfig]:
for item in self.items
]

def add_from_cfg(self, lst: list[ItemConfig]):
def add_from_cfg(self, lst: Iterable[ItemConfig]):
for new_item in lst:
item: ProceduralConfigItem = ProceduralConfigItem(self.registered_items[new_item["name"]], self)
item.from_cfg(new_item["data"])
Expand Down Expand Up @@ -446,11 +447,14 @@ def get_settings(self):
return ProceduralFlowListSettings(*self.items, parent=self.parent)


class ItemDeclaration:
T = TypeVar("T", bound=Keyworded, covariant=True)


class ItemDeclaration(Generic[T]):
def __init__(
self,
title: str,
bound_item: type[Keyworded],
bound_item: type[T],
desc: str | None = None,
settings: ItemSettings | None = None,
duplicable=True,
Expand All @@ -468,4 +472,4 @@ def create_settings_widget(self, parent=None):
def get(self, box: SettingsBox | None = None):
if box is None:
return self.bound_item()
return self.bound_item.from_cfg(box)
return self.bound_item.from_cfg(box.get_cfg())
2 changes: 1 addition & 1 deletion src/imdataset_creator/gui/input_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
".tif",
)

InputView_ = ItemDeclaration(
InputView_ = ItemDeclaration[Input](
"Input",
Input,
settings=ItemSettings(
Expand Down
18 changes: 11 additions & 7 deletions src/imdataset_creator/gui/output_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from PySide6.QtCore import Qt

from ..datarules import Output
from ..datarules.base_rules import Filter
from ..image_filters import destroyers, resizer
from .config_inputs import ItemDeclaration, ProceduralConfigList, ProceduralFlowListInput
from .settings_inputs import (
Expand Down Expand Up @@ -47,7 +48,10 @@ def div_100(val):
return val / 100


ResizeFilterView_ = ItemDeclaration(
FilterDeclaration = ItemDeclaration[Filter]


ResizeFilterView_ = FilterDeclaration(
"Resize",
resizer.Resize,
settings=ItemSettings(
Expand All @@ -65,7 +69,7 @@ def div_100(val):
),
)

CropFilterView_ = ItemDeclaration(
CropFilterView_ = FilterDeclaration(
"Crop",
resizer.Crop,
desc="Crop the image to the specified size. If the item is 0, it will not be considered",
Expand All @@ -80,7 +84,7 @@ def div_100(val):
)


BlurFilterView_ = ItemDeclaration(
BlurFilterView_ = FilterDeclaration(
"Blur",
destroyers.Blur,
settings=ItemSettings(
Expand All @@ -92,7 +96,7 @@ def div_100(val):
),
)

NoiseFilterView_ = ItemDeclaration(
NoiseFilterView_ = FilterDeclaration(
"Noise",
destroyers.Noise,
settings=ItemSettings(
Expand All @@ -110,7 +114,7 @@ def div_100(val):
)


CompressionFilterView_ = ItemDeclaration(
CompressionFilterView_ = FilterDeclaration(
"Compression",
destroyers.Compression,
settings=ItemSettings(
Expand All @@ -126,7 +130,7 @@ def div_100(val):
),
)

RandomFlipFilterView_ = ItemDeclaration(
RandomFlipFilterView_ = FilterDeclaration(
"Random Flip",
bound_item=resizer.RandomFlip,
settings=ItemSettings(
Expand All @@ -147,7 +151,7 @@ def div_100(val):
),
)

RandomRotateFilterView_ = ItemDeclaration(
RandomRotateFilterView_ = FilterDeclaration(
"Random Rotate",
bound_item=resizer.RandomRotate,
settings=ItemSettings(
Expand Down
Loading

0 comments on commit a3d0fdb

Please sign in to comment.