diff --git a/src/polystar/filters/filter_abc.py b/src/polystar/filters/filter_abc.py index af7bf3dbf38acf6bb37391d446f8d2c453047eb5..794eee74a685d520cde95109137091e1048c61d0 100644 --- a/src/polystar/filters/filter_abc.py +++ b/src/polystar/filters/filter_abc.py @@ -66,11 +66,11 @@ class UnionFilter(FilterABC[T]): class NegationFilter(FilterABC[T]): - def __init__(self, filter_: FilterABC[T]): - self.filter = filter_ + def __init__(self, base_filter: FilterABC[T]): + self.base_filter = base_filter def validate_single(self, example: T) -> bool: - return not self.filter.validate_single(example) + return not self.base_filter.validate_single(example) def _filter_with_siblings_from_preds( diff --git a/src/polystar/models/roco_image_annotation.py b/src/polystar/models/roco_image_annotation.py deleted file mode 100644 index 6c3b272b01eab459f8cffe7de312aab7ebe79e13..0000000000000000000000000000000000000000 --- a/src/polystar/models/roco_image_annotation.py +++ /dev/null @@ -1,83 +0,0 @@ -import logging -from dataclasses import dataclass, field -from pathlib import Path -from typing import List -from xml.dom.minidom import parseString - -import xmltodict -from dicttoxml import dicttoxml - -from polystar.models.image import Image, load_image, save_image -from polystar.models.roco_object import ROCOObject, ROCOObjectFactory - - -@dataclass -class ROCOImageAnnotation: - - image_path: Path - xml_path: Path - - width: int - height: int - - objects: List[ROCOObject] - - has_rune: bool - - _image: Image = field(repr=False, default=None) - - @property - def image(self) -> Image: - if self._image is None: - self._image = load_image(self.image_path) - return self._image - - @staticmethod - def from_xml_file(xml_file: Path) -> "ROCOImageAnnotation": - try: - annotation = xmltodict.parse(xml_file.read_text())["annotation"] - - json_objects = annotation.get("object", []) or [] - json_objects = json_objects if isinstance(json_objects, list) else [json_objects] - roco_json_objects = [obj_json for obj_json in json_objects if not obj_json["name"].startswith("rune")] - objects = [ROCOObjectFactory.from_json(obj_json) for obj_json in roco_json_objects] - - return ROCOImageAnnotation( - width=int(annotation["size"]["width"]), - height=int(annotation["size"]["height"]), - objects=objects, - image_path=xml_file.parent.parent / "image" / f"{xml_file.stem}.jpg", - xml_path=xml_file, - has_rune=len(roco_json_objects) != len(json_objects), - ) - except Exception as e: - logging.error(f"Error parsing annotation file {xml_file}") - logging.exception(e) - raise e - - def to_xml(self) -> str: - return parseString( - dicttoxml( - { - "annotation": { - "size": {"width": self.width, "height": self.height}, - "object": [ROCOObjectFactory.to_json(obj) for obj in self.objects], - } - }, - attr_type=False, - root="annotation", - item_func=lambda x: x, - ) - .replace(b"<object><object>", b"<object>") - .replace(b"</object></object>", b"</object>") - ).toprettyxml() - - def save_to_dir(self, directory: Path, image_name: str): - self.image_path = (directory / "image" / image_name).with_suffix(".jpg") - self.xml_path = (directory / "image_annotation" / image_name).with_suffix(".xml") - - self.image_path.parent.mkdir(exist_ok=True, parents=True) - self.xml_path.parent.mkdir(exist_ok=True, parents=True) - - save_image(self.image, self.image_path) - self.xml_path.write_text(self.to_xml())