Skip to content

Commit

Permalink
Fix rename labels (#164)
Browse files Browse the repository at this point in the history
* Fix transform to rename labels
* Fix openslide backend vendor call
  • Loading branch information
jonasteuwen authored Sep 21, 2023
1 parent c6bbf1c commit c418cad
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
include CONTRIBUTING.rst
include LICENSE
include README.md
include dlup/py.typed

recursive-include tests *
recursive-exclude * __pycache__
recursive-exclude * *.py[co]

recursive-include docs *.rst *.md conf.py Makefile make.bat *.jpg *.png *.gif *.typed
recursive-include docs *.rst *.md conf.py Makefile make.bat *.jpg *.png *.gif
18 changes: 11 additions & 7 deletions dlup/data/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import dlup.annotations
from dlup._exceptions import AnnotationError
from dlup.annotations import AnnotationType
from dlup.annotations import AnnotationClass, AnnotationType

_AnnotationsTypes = dlup.annotations.Point | dlup.annotations.Polygon

Expand Down Expand Up @@ -171,13 +171,17 @@ def __call__(self, sample):
output_annotations.append(annotation)
continue

if isinstance(annotation, dlup.annotations.Polygon):
output_annotations.append(dlup.annotations.Polygon(annotation, label=self._remap_labels[label]))

elif isinstance(annotation, dlup.annotations.Point):
output_annotations.append(dlup.annotations.Point(annotation, label=self._remap_labels[label]))
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 {type(annotation)}")
raise AnnotationError(f"Unsupported annotation type {annotation.a_cls.a_cls}")

sample["annotations"] = output_annotations
return sample
Expand Down
2 changes: 1 addition & 1 deletion dlup/experimental_backends/openslide_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def magnification(self) -> int | None:
@property
def vendor(self) -> str:
"""Returns the scanner vendor."""
return self.properties.properties[openslide.PROPERTY_NAME_VENDOR]
return self.properties.get(openslide.PROPERTY_NAME_VENDOR, None)

@property
def slide_bounds(self) -> tuple[tuple[int, int], tuple[int, int]]:
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ replace = {new_version}

[aliases]
test = pytest

[options.package_data]
dlup = py.typed
1 change: 0 additions & 1 deletion tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from dlup.annotations import AnnotationClass, AnnotationType, Polygon, WsiAnnotations
from dlup.utils.imports import DARWIN_SDK_AVAILABLE


ASAP_XML_EXAMPLE = b"""<?xml version="1.0"?>
<ASAP_Annotations>
<Annotations>
Expand Down
29 changes: 29 additions & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# coding=utf-8
# Copyright (c) dlup contributors
import pytest

from dlup.data.transforms import AnnotationClass, AnnotationType, RenameLabels


class Polygon:
def __init__(self, coordinates, a_cls):
self.coordinates = coordinates
self.label = a_cls.label
self.a_cls = a_cls


class Point:
def __init__(self, coordinates, a_cls):
self.coordinates = coordinates
self.label = a_cls.label
self.a_cls = a_cls


@pytest.fixture
def remap_labels():
return {"old_label1": "new_label1", "old_label2": "new_label2"}


@pytest.fixture
def renamer(remap_labels):
return RenameLabels(remap_labels)

0 comments on commit c418cad

Please sign in to comment.