Skip to content

Commit

Permalink
Implement functional interface for RenameLabels (#176)
Browse files Browse the repository at this point in the history
* Implement functional interface `rename_labels` for `RenameLabels`
  • Loading branch information
jonasteuwen authored Oct 13, 2023
1 parent 756b4eb commit 5bbef42
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
12 changes: 8 additions & 4 deletions .dev/bump_date.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import re
from datetime import datetime
from typing import Callable, Dict
import re

citation_regex = lambda new_date, content: re.sub(
r"date-released: \d{4}-\d{2}-\d{2}", f"date-released: {new_date}", content
)

def citation_regex(new_date, content):
"""
Replaces the date in the CITATION.cff file with the new date
"""
return re.sub(r"date-released: \d{4}-\d{2}-\d{2}", f"date-released: {new_date}", content)


readme_regexes = {
"bibtex_year": lambda year, content: re.sub(r"\{\d{4}\}", str(year), content), # {2023} in bibtex
Expand Down
58 changes: 38 additions & 20 deletions dlup/data/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,43 @@ def __call__(self, sample: TileSample) -> TileSampleWithAnnotationData:
return output


def rename_labels(annotations: Iterable[_AnnotationsTypes], remap_labels: dict[str, str]) -> list[_AnnotationsTypes]:
"""
Rename the labels in the annotations.
Parameters
----------
annotations: Iterable[_AnnotationsTypes]
The annotations
remap_labels: dict[str, str]
The renaming table
Returns
-------
list[_AnnotationsTypes]
"""
output_annotations = []
for annotation in annotations:
label = annotation.label
if label not in remap_labels:
output_annotations.append(annotation)
continue

if annotation.a_cls.a_cls == AnnotationType.BOX:
a_cls = AnnotationClass(label=remap_labels[label], a_cls=AnnotationType.BOX)
output_annotations.append(dlup.annotations.Polygon(annotation, a_cls=a_cls))
elif annotation.a_cls.a_cls == AnnotationType.POLYGON:
a_cls = AnnotationClass(label=remap_labels[label], a_cls=AnnotationType.POLYGON)
output_annotations.append(dlup.annotations.Polygon(annotation, a_cls=a_cls))
elif annotation.a_cls.a_cls == AnnotationType.POINT:
a_cls = AnnotationClass(label=remap_labels[label], a_cls=AnnotationType.POINT)
output_annotations.append(dlup.annotations.Point(annotation, a_cls=a_cls))
else:
raise AnnotationError(f"Unsupported annotation type {annotation.a_cls.a_cls}")

return output_annotations


class RenameLabels:
"""Remap the label names"""

Expand All @@ -175,26 +212,7 @@ def __call__(self, sample: TileSample) -> TileSample:
if not _annotations:
raise ValueError("No annotations found to rename.")

output_annotations = []
for annotation in _annotations:
label = annotation.label
if label not in self._remap_labels:
output_annotations.append(annotation)
continue

if annotation.a_cls.a_cls == AnnotationType.BOX:
a_cls = AnnotationClass(label=self._remap_labels[label], a_cls=AnnotationType.BOX)
output_annotations.append(dlup.annotations.Polygon(annotation, a_cls=a_cls))
elif annotation.a_cls.a_cls == AnnotationType.POLYGON:
a_cls = AnnotationClass(label=self._remap_labels[label], a_cls=AnnotationType.POLYGON)
output_annotations.append(dlup.annotations.Polygon(annotation, a_cls=a_cls))
elif annotation.a_cls.a_cls == AnnotationType.POINT:
a_cls = AnnotationClass(label=self._remap_labels[label], a_cls=AnnotationType.POINT)
output_annotations.append(dlup.annotations.Point(annotation, a_cls=a_cls))
else:
raise AnnotationError(f"Unsupported annotation type {annotation.a_cls.a_cls}")

sample["annotations"] = output_annotations
sample["annotations"] = rename_labels(_annotations, self._remap_labels)
return sample


Expand Down
Empty file removed log.txt
Empty file.

0 comments on commit 5bbef42

Please sign in to comment.