Skip to content

Commit

Permalink
refactor(label): remove useless "LabelType"
Browse files Browse the repository at this point in the history
PR Closed: #849
  • Loading branch information
linjiX committed Jul 22, 2021
1 parent cc0d4b8 commit 0a7e2bf
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 97 deletions.
10 changes: 5 additions & 5 deletions tensorbay/healthcheck/catalog_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from typing import Iterator, Optional, Tuple

from ..label import AttributeInfo, Catalog, CategoryInfo, LabelType
from ..label import AttributeInfo, Catalog, CategoryInfo
from ..utility import NameList
from .pipeline import PipelineForIterable
from .report import Error
Expand All @@ -38,7 +38,7 @@ def __init__(self, name: str) -> None:
] = PipelineForIterable()


def check_catalog(catalog: Catalog) -> Iterator[Tuple[LabelType, AttributeInfoError]]:
def check_catalog(catalog: Catalog) -> Iterator[Tuple[str, AttributeInfoError]]:
"""The health check method for :class:`~tensorbay.label.catalog.Catalog`.
Arguments:
Expand All @@ -50,16 +50,16 @@ def check_catalog(catalog: Catalog) -> Iterator[Tuple[LabelType, AttributeInfoEr
or 'parent categories' field.
"""
for label_type in LabelType:
subcatalog = getattr(catalog, label_type.value, None)
for key in catalog._attrs_fields: # pylint: disable=protected-access
subcatalog = getattr(catalog, key, None)
if not subcatalog:
continue
categories = getattr(subcatalog, "categories", None)
if hasattr(subcatalog, "attributes"):
attribute_info_pipeline = ATTRIBUTE_INFO_PIPELINE.copy()
attribute_info_pipeline.register(CheckParentCategories(categories))
for error in attribute_info_pipeline(subcatalog.attributes.values()):
yield label_type, error
yield key, error


class InvalidTypeError(AttributeInfoError): # pylint: disable=too-few-public-methods
Expand Down
5 changes: 2 additions & 3 deletions tensorbay/healthcheck/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from collections import OrderedDict
from typing import Any, Callable, List, TypeVar

from ..label import LabelType
from ..utility import UserMutableMapping, UserSequence


Expand Down Expand Up @@ -106,6 +105,6 @@ class HealthReport: # pylint: disable=too-few-public-methods

def __init__(self) -> None:
self.basic_reports = _ErrorList("Basic checking:")
self.subcatalog_reports: _ErrorDict[LabelType] = _ErrorDict(
"Subcatalog checking:", lambda key: key.name
self.subcatalog_reports: _ErrorDict[str] = _ErrorDict(
"Subcatalog checking:", lambda key: key
)
2 changes: 0 additions & 2 deletions tensorbay/label/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""Label related classes."""

from .attributes import AttributeInfo, Items
from .basic import LabelType
from .catalog import Catalog, Subcatalogs
from .label import Label
from .label_box import Box2DSubcatalog, Box3DSubcatalog, LabeledBox2D, LabeledBox3D
Expand Down Expand Up @@ -41,7 +40,6 @@
"Keypoints2DSubcatalog",
"KeypointsInfo",
"Label",
"LabelType",
"LabeledBox2D",
"LabeledBox3D",
"LabeledKeypoints2D",
Expand Down
38 changes: 4 additions & 34 deletions tensorbay/label/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
# Copyright 2021 Graviti. Licensed under MIT License.
#

"""LabelType, SubcatalogBase.
:class:`LabelType` is an enumeration type
which includes all the supported label types within :class:`Label`.
"""SubcatalogBase.
:class:`Subcatalogbase` is the base class for different types of subcatalogs,
which defines the basic concept of Subcatalog.
Expand All @@ -17,37 +14,10 @@

from typing import Any, Dict, List, Optional, Tuple, Type, TypeVar, Union

from ..utility import AttrsMixin, ReprMixin, ReprType, TypeEnum, TypeMixin, attr, common_loads


class LabelType(TypeEnum):
"""This class defines all the supported types within :class:`Label`.
Examples:
>>> LabelType.BOX3D
<LabelType.BOX3D: 'box3d'>
>>> LabelType["BOX3D"]
<LabelType.BOX3D: 'box3d'>
>>> LabelType.BOX3D.name
'BOX3D'
>>> LabelType.BOX3D.value
'box3d'
"""

CLASSIFICATION = "classification"
BOX2D = "box2d"
BOX3D = "box3d"
POLYGON = "polygon"
POLYLINE2D = "polyline2d"
MULTI_POLYLINE2D = "multi_polyline2d"
RLE = "rle"
KEYPOINTS2D = "keypoints2d"
MULTI_POLYGON = "multi_polygon"
SENTENCE = "sentence"
from ..utility import AttrsMixin, ReprMixin, ReprType, attr, common_loads


class SubcatalogBase(TypeMixin[LabelType], ReprMixin, AttrsMixin):
class SubcatalogBase(ReprMixin, AttrsMixin):
"""This is the base class for different types of subcatalogs.
It defines the basic concept of Subcatalog, which is the collection of the labels information.
Expand Down Expand Up @@ -104,7 +74,7 @@ def dumps(self) -> Dict[str, Any]:
return self._dumps()


class _LabelBase(AttrsMixin, TypeMixin[LabelType], ReprMixin):
class _LabelBase(AttrsMixin, ReprMixin): # pylint: disable=too-few-public-methods
"""This class defines the basic concept of label.
:class:`_LabelBase` is the most basic label level in the TensorBay dataset structure,
Expand Down
13 changes: 6 additions & 7 deletions tensorbay/label/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@
"""

from functools import partial
from typing import Any, Dict, Type, TypeVar, Union
from typing import Any, Dict, Iterator, Type, TypeVar, Union

from ..label import LabelType
from ..utility import AttrsMixin, ReprMixin, ReprType, attr, common_loads, upper
from .label_box import Box2DSubcatalog, Box3DSubcatalog
from .label_classification import ClassificationSubcatalog
Expand Down Expand Up @@ -95,7 +94,6 @@ class Catalog(ReprMixin, AttrsMixin):
_T = TypeVar("_T", bound="Catalog")

_repr_type = ReprType.INSTANCE
_repr_attrs = tuple(label_type.value for label_type in LabelType)
_repr_maxlevel = 2

classification: ClassificationSubcatalog = _attr()
Expand All @@ -110,10 +108,11 @@ class Catalog(ReprMixin, AttrsMixin):
sentence: SentenceSubcatalog = _attr()

def __bool__(self) -> bool:
for label_type in LabelType:
if hasattr(self, label_type.value):
return True
return False
return any(hasattr(self, key) for key in self._attrs_fields)

@property
def _repr_attrs(self) -> Iterator[str]: # type: ignore[override]
yield from self._attrs_fields

@classmethod
def loads(cls: Type[_T], contents: Dict[str, Any]) -> _T:
Expand Down
13 changes: 6 additions & 7 deletions tensorbay/label/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
"""

from functools import partial
from typing import Any, Dict, List, Type, TypeVar
from typing import Any, Dict, Iterator, List, Type, TypeVar

from ..utility import AttrsMixin, ReprMixin, ReprType, attr, common_loads, upper
from .basic import LabelType
from .label_box import LabeledBox2D, LabeledBox3D
from .label_classification import Classification
from .label_keypoints import LabeledKeypoints2D
Expand Down Expand Up @@ -68,7 +67,6 @@ class Label(ReprMixin, AttrsMixin):
_T = TypeVar("_T", bound="Label")

_repr_type = ReprType.INSTANCE
_repr_attrs = tuple(label_type.value for label_type in LabelType)
_repr_maxlevel = 2

classification: Classification = _attr()
Expand All @@ -83,10 +81,11 @@ class Label(ReprMixin, AttrsMixin):
sentence: List[LabeledSentence] = _attr()

def __bool__(self) -> bool:
for label_type in LabelType:
if hasattr(self, label_type.value):
return True
return False
return any(hasattr(self, key) for key in self._attrs_fields)

@property
def _repr_attrs(self) -> Iterator[str]: # type: ignore[override]
yield from self._attrs_fields

@classmethod
def loads(cls: Type[_T], contents: Dict[str, Any]) -> _T:
Expand Down
6 changes: 2 additions & 4 deletions tensorbay/label/label_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
from quaternion import quaternion

from ..geometry import Box2D, Box3D, Transform3D
from ..utility import MatrixType, ReprType, TypeRegister, attr_base, common_loads
from .basic import LabelType, SubcatalogBase, _LabelBase
from ..utility import MatrixType, ReprType, attr_base, common_loads
from .basic import SubcatalogBase, _LabelBase
from .supports import AttributesMixin, CategoriesMixin, IsTrackingMixin


Expand Down Expand Up @@ -99,7 +99,6 @@ def __init__(self, is_tracking: bool = False) -> None:
IsTrackingMixin.__init__(self, is_tracking)


@TypeRegister(LabelType.BOX2D)
class LabeledBox2D(_LabelBase, Box2D): # pylint: disable=too-many-ancestors
"""This class defines the concept of 2D bounding box label.
Expand Down Expand Up @@ -333,7 +332,6 @@ def __init__(self, is_tracking: bool = False) -> None:
IsTrackingMixin.__init__(self, is_tracking)


@TypeRegister(LabelType.BOX3D)
class LabeledBox3D(_LabelBase, Box3D):
"""This class defines the concept of 3D bounding box label.
Expand Down
5 changes: 2 additions & 3 deletions tensorbay/label/label_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

from typing import Any, Dict, Optional, Type, TypeVar

from ..utility import TypeRegister, common_loads
from .basic import LabelType, SubcatalogBase, _LabelBase
from ..utility import common_loads
from .basic import SubcatalogBase, _LabelBase
from .supports import AttributesMixin, CategoriesMixin


Expand Down Expand Up @@ -79,7 +79,6 @@ class ClassificationSubcatalog( # pylint: disable=too-many-ancestors
"""


@TypeRegister(LabelType.CLASSIFICATION)
class Classification(_LabelBase):
"""This class defines the concept of classification label.
Expand Down
5 changes: 2 additions & 3 deletions tensorbay/label/label_keypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from typing import Any, Dict, Iterable, List, Optional, Type, TypeVar, Union

from ..geometry import Keypoints2D
from ..utility import ReprType, TypeRegister, attr, attr_base, common_loads
from .basic import LabelType, SubcatalogBase, _LabelBase
from ..utility import ReprType, attr, attr_base, common_loads
from .basic import SubcatalogBase, _LabelBase
from .supports import AttributesMixin, CategoriesMixin, IsTrackingMixin, KeypointsInfo


Expand Down Expand Up @@ -202,7 +202,6 @@ def dumps(self) -> Dict[str, Any]:
return self._dumps()


@TypeRegister(LabelType.KEYPOINTS2D)
class LabeledKeypoints2D(_LabelBase, Keypoints2D): # pylint: disable=too-many-ancestors
"""This class defines the concept of 2D keypoints label.
Expand Down
7 changes: 2 additions & 5 deletions tensorbay/label/label_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from typing import Any, Dict, Iterable, Optional, Type, TypeVar

from ..geometry import RLE, MultiPolygon, Polygon
from ..utility import ReprType, TypeRegister, attr_base, common_loads
from .basic import LabelType, SubcatalogBase, _LabelBase
from ..utility import ReprType, attr_base, common_loads
from .basic import SubcatalogBase, _LabelBase
from .supports import AttributesMixin, CategoriesMixin, IsTrackingMixin


Expand Down Expand Up @@ -207,7 +207,6 @@ def __init__(self, is_tracking: bool = False) -> None:
IsTrackingMixin.__init__(self, is_tracking)


@TypeRegister(LabelType.POLYGON)
class LabeledPolygon(_LabelBase, Polygon): # pylint: disable=too-many-ancestors
"""This class defines the concept of polygon label.
Expand Down Expand Up @@ -321,7 +320,6 @@ def dumps(self) -> Dict[str, Any]: # type: ignore[override]
return self._dumps()


@TypeRegister(LabelType.MULTI_POLYGON)
class LabeledMultiPolygon( # type: ignore[misc]
_LabelBase, MultiPolygon
): # pylint: disable=too-many-ancestors
Expand Down Expand Up @@ -440,7 +438,6 @@ def dumps(self) -> Dict[str, Any]: # type: ignore[override]
return self._dumps()


@TypeRegister(LabelType.RLE)
class LabeledRLE(_LabelBase, RLE): # type: ignore[misc] # pylint: disable=too-many-ancestors
"""This class defines the concept of rle label.
Expand Down
6 changes: 2 additions & 4 deletions tensorbay/label/label_polyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from typing import Any, Dict, Iterable, Optional, Type, TypeVar

from ..geometry import MultiPolyline2D, Polyline2D
from ..utility import ReprType, TypeRegister, attr_base, common_loads
from .basic import LabelType, SubcatalogBase, _LabelBase
from ..utility import ReprType, attr_base, common_loads
from .basic import SubcatalogBase, _LabelBase
from .supports import AttributesMixin, CategoriesMixin, IsTrackingMixin


Expand Down Expand Up @@ -85,7 +85,6 @@ def __init__(self, is_tracking: bool = False) -> None:
IsTrackingMixin.__init__(self, is_tracking)


@TypeRegister(LabelType.POLYLINE2D)
class LabeledPolyline2D(_LabelBase, Polyline2D): # pylint: disable=too-many-ancestors
"""This class defines the concept of polyline2D label.
Expand Down Expand Up @@ -257,7 +256,6 @@ def __init__(self, is_tracking: bool = False) -> None:
IsTrackingMixin.__init__(self, is_tracking)


@TypeRegister(LabelType.MULTI_POLYLINE2D)
class LabeledMultiPolyline2D( # type: ignore[misc]
_LabelBase, MultiPolyline2D
): # pylint: disable=too-many-ancestors
Expand Down
5 changes: 2 additions & 3 deletions tensorbay/label/label_sentence.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

from typing import Any, Dict, Iterable, List, Optional, Type, TypeVar, Union

from ..utility import AttrsMixin, ReprMixin, TypeRegister, attr, camel, common_loads
from .basic import LabelType, SubcatalogBase, _LabelBase
from ..utility import AttrsMixin, ReprMixin, attr, camel, common_loads
from .basic import SubcatalogBase, _LabelBase
from .supports import AttributesMixin


Expand Down Expand Up @@ -209,7 +209,6 @@ def dumps(self) -> Dict[str, Union[str, float]]:
return self._dumps()


@TypeRegister(LabelType.SENTENCE) # pylint: disable=too-few-public-methods
class LabeledSentence(_LabelBase):
"""This class defines the concept of phonetic transcription lable.
Expand Down
17 changes: 0 additions & 17 deletions tensorbay/label/tests/test_basic.py

This file was deleted.

0 comments on commit 0a7e2bf

Please sign in to comment.