Skip to content

Commit

Permalink
black errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zeptofine committed Nov 3, 2023
1 parent f6f8b50 commit 8421b8a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 80 deletions.
32 changes: 9 additions & 23 deletions imdataset_creator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,21 @@
)

CPU_COUNT = int(cpu_count())
logging.basicConfig(
level=logging.INFO, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()]
)
logging.basicConfig(level=logging.INFO, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()])
app = typer.Typer(pretty_exceptions_show_locals=True, pretty_exceptions_short=True)
log = logging.getLogger()


@app.command()
def main(
config_path: Annotated[
Path, Option(help="Where the dataset config is placed")
] = Path("config.json"),
database_path: Annotated[Path, Option(help="Where the database is placed")] = Path(
"filedb.arrow"
config_path: Annotated[Path, Option(help="Where the dataset config is placed")] = Path(
"config.json"
),
threads: Annotated[int, Option(help="multiprocessing threads")] = CPU_COUNT
* 3
// 4,
database_path: Annotated[Path, Option(help="Where the database is placed")] = Path("filedb.arrow"),
threads: Annotated[int, Option(help="multiprocessing threads")] = CPU_COUNT * 3 // 4,
chunksize: Annotated[int, Option(help="imap chunksize")] = 5,
population_chunksize: Annotated[
int, Option(help="chunksize when populating the df")
] = 100,
population_interval: Annotated[
int, Option(help="save interval in secs when populating the df")
] = 60,
population_chunksize: Annotated[int, Option(help="chunksize when populating the df")] = 100,
population_interval: Annotated[int, Option(help="save interval in secs when populating the df")] = 60,
simulate: Annotated[bool, Option(help="stops before conversion")] = False,
verbose: Annotated[bool, Option(help="prints converted files")] = False,
sort_by: Annotated[str, Option(help="Which database column to sort by")] = "path",
Expand Down Expand Up @@ -159,16 +149,12 @@ def main(
files: list[File]
if db_cfg.rules:
filter_t = p.add_task("filtering", total=0)
files = [
resolved[file] for file in db.filter(set(resolved)).get_column("path")
]
files = [resolved[file] for file in db.filter(set(resolved)).get_column("path")]
p.update(filter_t, total=len(files), completed=len(files))
else:
files = list(resolved.values())

scenarios = list(
db_cfg.parse_files(p.track(files, description="parsing files"))
)
scenarios = list(db_cfg.parse_files(p.track(files, description="parsing files")))
if len(scenarios) != len(files):
p.log(f"{len(files) - len(scenarios)} files are completed")

Expand Down
39 changes: 8 additions & 31 deletions imdataset_creator/config_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,23 @@
class ConfigHandler:
def __init__(self, cfg: MainConfig):
# generate `Input`s
self.inputs: list[Input] = [
Input.from_cfg(folder["data"]) for folder in cfg["inputs"]
]
self.inputs: list[Input] = [Input.from_cfg(folder["data"]) for folder in cfg["inputs"]]
# generate `Output`s
self.outputs: list[Output] = [
Output.from_cfg(folder["data"]) for folder in cfg["output"]
]
self.outputs: list[Output] = [Output.from_cfg(folder["data"]) for folder in cfg["output"]]
# generate `Producer`s
self.producers: list[Producer] = [
Producer.all_producers[p["name"]].from_cfg(p["data"])
for p in cfg["producers"]
Producer.all_producers[p["name"]].from_cfg(p["data"]) for p in cfg["producers"]
]

# generate `Rule`s
self.rules: list[Rule] = [
Rule.all_rules[r["name"]].from_cfg(r["data"]) for r in cfg["rules"]
]
self.rules: list[Rule] = [Rule.all_rules[r["name"]].from_cfg(r["data"]) for r in cfg["rules"]]

@overload
def gather_images(
self, sort=True, reverse=False
) -> Generator[tuple[Path, list[Path]], None, None]:
def gather_images(self, sort=True, reverse=False) -> Generator[tuple[Path, list[Path]], None, None]:
...

@overload
def gather_images(
self, sort=False, reverse=False
) -> Generator[tuple[Path, PathGenerator], None, None]:
def gather_images(self, sort=False, reverse=False) -> Generator[tuple[Path, PathGenerator], None, None]:
...

def gather_images(
Expand All @@ -62,8 +51,7 @@ def get_outputs(self, file: File) -> list[OutputScenario]:
return [
OutputScenario(str(pth), output.filters)
for output in self.outputs
if not (pth := output.folder / Path(output.format_file(file))).exists()
or output.overwrite
if not (pth := output.folder / Path(output.format_file(file))).exists() or output.overwrite
]

def parse_files(self, files: Iterable[File]) -> Generator[FileScenario, None, None]:
Expand All @@ -72,16 +60,5 @@ def parse_files(self, files: Iterable[File]) -> Generator[FileScenario, None, No
yield FileScenario(file, out_s)

def __repr__(self):
i = ",\n".join(map(_repr_indent, map(repr, self.inputs)))
o = ",\n".join(map(_repr_indent, map(repr, self.outputs)))
p = ",\n".join(map(_repr_indent, map(repr, self.producers)))
r = ",\n".join(map(_repr_indent, map(repr, self.rules)))
attrs = ",\n".join(
[
_repr_indent(f"inputs=[\n{i}\n]"),
_repr_indent(f"outputs=[\n{o}\n]"),
_repr_indent(f"producers=[\n{p}\n]"),
_repr_indent(f"rules=[\n{r}\n]"),
]
)
attrs = ",".join(str(x) for x in (self.inputs, self.outputs, self.producers, self.rules))
return "\n".join([f"{self.__class__.__name__}(", attrs, ")"])
4 changes: 3 additions & 1 deletion imdataset_creator/datarules/dataset_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ def filter(self, lst) -> DataFrame: # noqa: A003
)

vdf: DataFrame = self.__df.filter(pl.col("path").is_in(lst))
for matcher in combine_matchers([rule.matcher for rule in self.rules]):
for matcher in combine_matchers(rule.matcher for rule in self.rules):
if not len(vdf):
break
vdf = matcher(vdf, self.__df) if isinstance(matcher, DataFrameMatcher) else vdf.filter(matcher)
return vdf

Expand Down
13 changes: 12 additions & 1 deletion imdataset_creator/gui/config_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import functools
from copy import deepcopy as objcopy

from PySide6.QtCore import QRect, QSize, Qt, Signal, Slot
from PySide6.QtCore import QMimeData, QRect, QSize, Qt, Signal, Slot
from PySide6.QtGui import QAction, QDrag, QDragEnterEvent, QDragLeaveEvent, QDragMoveEvent, QMouseEvent
from PySide6.QtWidgets import (
QCheckBox,
Expand Down Expand Up @@ -141,6 +141,17 @@ def mouseDoubleClickEvent(self, event: QMouseEvent) -> None:
self.toggle_group()
event.accept()

# def mousePressEvent(self, event: QMouseEvent):
# print(event)
# if event.button() == Qt.MouseButton.LeftButton:
# drag = QDrag(self)
# mimedata = QMimeData()
# mimedata.setText(self.checkbox.text())
# drag.setMimeData(mimedata)
# drop_action = drag.exec()

# event.accept()

@property
def n(self) -> int:
return self._n
Expand Down
2 changes: 0 additions & 2 deletions imdataset_creator/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,13 @@ def add_lock(self):
self.locks += 1
if self.locks == 1:
log.info("locked")
...

@Slot()
def remove_lock(self):
"""Unlocks the UI"""
self.locks -= 1
if self.locks == 0:
log.info("unlocked")
...

@catch_building
@Slot()
Expand Down
25 changes: 6 additions & 19 deletions imdataset_creator/image_filters/destroyers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,11 @@ def run(
ksize = ksize + (ksize % 2 == 0) # ensure ksize is odd
return cv2.GaussianBlur(img, (ksize, ksize), 0)

if (
algorithm == BlurAlgorithm.ISOTROPIC
or algorithm == BlurAlgorithm.ANISOTROPIC
):
if algorithm == BlurAlgorithm.ISOTROPIC or algorithm == BlurAlgorithm.ANISOTROPIC:
sigma1: float = ri * self.scale
ksize1: int = 2 * int(4 * sigma1 + 0.5) + 1
if algorithm == BlurAlgorithm.ANISOTROPIC:
return cv2.GaussianBlur(
img, (ksize1, ksize1), sigmaX=sigma1, sigmaY=sigma1
)
return cv2.GaussianBlur(img, (ksize1, ksize1), sigmaX=sigma1, sigmaY=sigma1)

sigma2: float = ri * self.scale
ksize2: int = 2 * int(4 * sigma2 + 0.5) + 1
Expand Down Expand Up @@ -179,16 +174,12 @@ def run(self, img: ndarray):
enc_img: ndarray
if algorithm == CompressionAlgorithms.JPEG:
quality = randint(*self.jpeg_quality_range)
enc_img = cv2.imencode(
".jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), quality]
)[1]
enc_img = cv2.imencode(".jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), quality])[1]
return cv2.imdecode(enc_img, 1)

if algorithm == CompressionAlgorithms.WEBP:
quality = randint(*self.webp_quality_range)
enc_img = cv2.imencode(
".webp", img, [int(cv2.IMWRITE_WEBP_QUALITY), quality]
)[1]
enc_img = cv2.imencode(".webp", img, [int(cv2.IMWRITE_WEBP_QUALITY), quality])[1]
return cv2.imdecode(enc_img, 1)

if algorithm in [
Expand Down Expand Up @@ -218,9 +209,7 @@ def run(self, img: ndarray):
codec = "mpeg2video"

compressor = (
ffmpeg.input(
"pipe:", format="rawvideo", pix_fmt="bgr24", s=f"{width}x{height}"
)
ffmpeg.input("pipe:", format="rawvideo", pix_fmt="bgr24", s=f"{width}x{height}")
.output("pipe:", format=container, vcodec=codec, **output_args)
.global_args("-loglevel", "error")
.global_args("-max_muxing_queue_size", "300000")
Expand All @@ -241,9 +230,7 @@ def run(self, img: ndarray):
newimg = np.frombuffer(out, np.uint8)
if len(newimg) != height * width * 3:
log.warning("New image size does not match")
newimg = newimg[
: height * width * 3
] # idrk why i need this sometimes
newimg = newimg[: height * width * 3] # idrk why i need this sometimes

return newimg.reshape((height, width, 3))
except subprocess.TimeoutExpired as e:
Expand Down
4 changes: 1 addition & 3 deletions imdataset_creator/image_filters/resizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,5 @@ def run(self, img: np.ndarray) -> np.ndarray:
def from_cfg(cls, cfg: RandomRotateData):
return cls(
rotate_chance=cfg["rotate_chance"],
rotate_directions=[
RandomRotateDirections[d] for d in cfg["rotate_directions"]
],
rotate_directions=[RandomRotateDirections[d] for d in cfg["rotate_directions"]],
)

0 comments on commit 8421b8a

Please sign in to comment.