Skip to content

Commit

Permalink
Convert most enum definitions from the functional style to the class …
Browse files Browse the repository at this point in the history
…style (cvat-ai#290)
  • Loading branch information
Roman Donchenko authored Jun 11, 2021
1 parent 01e2551 commit 64b01b7
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 154 deletions.
9 changes: 4 additions & 5 deletions datumaro/cli/contexts/project/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,27 @@

from collections import Counter
from itertools import zip_longest
from enum import Enum
from enum import Enum, auto
import logging as log
import os
import os.path as osp

import cv2
import numpy as np

_formats = ['simple']

import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import tensorboardX as tb
_formats.append('tensorboard')

from datumaro.components.dataset import IDataset
from datumaro.components.extractor import AnnotationType, LabelCategories
from datumaro.util.image import save_image


OutputFormat = Enum('Formats', _formats)
class OutputFormat(Enum):
simple = auto()
tensorboard = auto()

class DatasetDiffVisualizer:
OutputFormat = OutputFormat
Expand Down
7 changes: 5 additions & 2 deletions datumaro/components/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#pylint: disable=redefined-builtin

from contextlib import contextmanager
from enum import Enum
from enum import Enum, auto
from typing import Iterable, Iterator, Optional, Tuple, Union, Dict, List
import logging as log
import os
Expand Down Expand Up @@ -144,7 +144,10 @@ def get(self, id, subset=None):
return self._parent.get(id, subset=subset)


ItemStatus = Enum('ItemStatus', ['added', 'modified', 'removed'])
class ItemStatus(Enum):
added = auto()
modified = auto()
removed = auto()

class DatasetPatch:
def __init__(self, data: DatasetItemStorage,
Expand Down
30 changes: 14 additions & 16 deletions datumaro/components/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# SPDX-License-Identifier: MIT

from enum import Enum
from enum import Enum, auto
from glob import iglob
from typing import Callable, Iterable, List, Dict, Optional
import numpy as np
Expand All @@ -17,16 +17,14 @@
from datumaro.util.attrs_util import not_empty, default_if_none


AnnotationType = Enum('AnnotationType',
[
'label',
'mask',
'points',
'polygon',
'polyline',
'bbox',
'caption',
])
class AnnotationType(Enum):
label = auto()
mask = auto()
points = auto()
polygon = auto()
polyline = auto()
bbox = auto()
caption = auto()

_COORDINATE_ROUNDING_DIGITS = 2

Expand Down Expand Up @@ -468,11 +466,11 @@ def add(self, label_id, labels=None, joints=None):

@attrs
class Points(_Shape):
Visibility = Enum('Visibility', [
('absent', 0),
('hidden', 1),
('visible', 2),
])
class Visibility(Enum):
absent = 0
hidden = 1
visible = 2

_type = AnnotationType.points

visibility = attrib(type=list, default=None)
Expand Down
11 changes: 8 additions & 3 deletions datumaro/components/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: MIT

from copy import deepcopy
from enum import Enum
from enum import Enum, auto
from typing import Union

import numpy as np
Expand All @@ -22,9 +22,14 @@
from datumaro.util import parse_str_enum_value


Severity = Enum('Severity', ['warning', 'error'])
class Severity(Enum):
warning = auto()
error = auto()

TaskType = Enum('TaskType', ['classification', 'detection', 'segmentation'])
class TaskType(Enum):
classification = auto()
detection = auto()
segmentation = auto()


class _Validator(CliPlugin):
Expand Down
8 changes: 5 additions & 3 deletions datumaro/plugins/camvid_format.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

# Copyright (C) 2020 Intel Corporation
# Copyright (C) 2020-2021 Intel Corporation
#
# SPDX-License-Identifier: MIT

import logging as log
import os
import os.path as osp
from collections import OrderedDict
from enum import Enum
from enum import Enum, auto

import numpy as np

Expand Down Expand Up @@ -213,7 +213,9 @@ def find_sources(cls, path):
file_filter=lambda p: osp.basename(p) != CamvidPath.LABELMAP_FILE)


LabelmapType = Enum('LabelmapType', ['camvid', 'source'])
class LabelmapType(Enum):
camvid = auto()
source = auto()

class CamvidConverter(Converter):
DEFAULT_IMAGE_EXT = CamvidPath.IMAGE_EXT
Expand Down
8 changes: 5 additions & 3 deletions datumaro/plugins/cityscapes_format.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

# Copyright (C) 2020 Intel Corporation
# Copyright (C) 2020-2021 Intel Corporation
#
# SPDX-License-Identifier: MIT

import logging as log
import os
import os.path as osp
from collections import OrderedDict
from enum import Enum
from enum import Enum, auto
from glob import iglob

import numpy as np
Expand Down Expand Up @@ -200,7 +200,9 @@ def find_sources(cls, path):
max_depth=1)


LabelmapType = Enum('LabelmapType', ['cityscapes', 'source'])
class LabelmapType(Enum):
cityscapes = auto()
source = auto()

class CityscapesConverter(Converter):
DEFAULT_IMAGE_EXT = '.png'
Expand Down
9 changes: 6 additions & 3 deletions datumaro/plugins/coco_format/converter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (C) 2020 Intel Corporation
# Copyright (C) 2020-2021 Intel Corporation
#
# SPDX-License-Identifier: MIT

Expand All @@ -8,7 +8,7 @@
import numpy as np
import os
import os.path as osp
from enum import Enum
from enum import Enum, auto
from itertools import chain, groupby

import pycocotools.mask as mask_utils
Expand All @@ -24,7 +24,10 @@

from .format import CocoPath, CocoTask

SegmentationMode = Enum('SegmentationMode', ['guess', 'polygons', 'mask'])
class SegmentationMode(Enum):
guess = auto()
polygons = auto()
mask = auto()

class _TaskConverter:
def __init__(self, context):
Expand Down
21 changes: 10 additions & 11 deletions datumaro/plugins/coco_format/format.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@

# Copyright (C) 2019-2020 Intel Corporation
# Copyright (C) 2019-2021 Intel Corporation
#
# SPDX-License-Identifier: MIT

from enum import Enum
from enum import Enum, auto


CocoTask = Enum('CocoTask', [
'instances',
'person_keypoints',
'captions',
'labels', # extension, does not exist in the original COCO format
'image_info',
'panoptic',
'stuff',
])
class CocoTask(Enum):
instances = auto()
person_keypoints = auto()
captions = auto()
labels = auto() # extension, does not exist in the original COCO format
image_info = auto()
panoptic = auto()
stuff = auto()

class CocoPath:
IMAGES_DIR = 'images'
Expand Down
13 changes: 6 additions & 7 deletions datumaro/plugins/icdar_format/format.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Copyright (C) 2020 Intel Corporation
# Copyright (C) 2020-2021 Intel Corporation
#
# SPDX-License-Identifier: MIT

from enum import Enum
from enum import Enum, auto


IcdarTask = Enum('IcdarTask', [
'word_recognition',
'text_localization',
'text_segmentation',
])
class IcdarTask(Enum):
word_recognition = auto()
text_localization = auto()
text_segmentation = auto()

class IcdarPath:
IMAGE_EXT = '.png'
Expand Down
6 changes: 4 additions & 2 deletions datumaro/plugins/kitti_format/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
import os.path as osp
from collections import OrderedDict
from enum import Enum
from enum import Enum, auto

import numpy as np

Expand All @@ -24,7 +24,9 @@
parse_label_map, write_label_map,
)

LabelmapType = Enum('LabelmapType', ['kitti', 'source'])
class LabelmapType(Enum):
kitti = auto()
source = auto()

class KittiConverter(Converter):
DEFAULT_IMAGE_EXT = KittiPath.IMAGE_EXT
Expand Down
9 changes: 4 additions & 5 deletions datumaro/plugins/kitti_format/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SPDX-License-Identifier: MIT

from collections import OrderedDict
from enum import Enum
from enum import Enum, auto

from datumaro.components.extractor import (AnnotationType,
LabelCategories, MaskCategories
Expand All @@ -13,10 +13,9 @@
from datumaro.util.mask_tools import generate_colormap


KittiTask = Enum('KittiTask', [
'segmentation',
'detection'
])
class KittiTask(Enum):
segmentation = auto()
detection = auto()

KittiLabelMap = OrderedDict([
('unlabeled', (0, 0, 0)),
Expand Down
13 changes: 6 additions & 7 deletions datumaro/plugins/mots_format.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2020 Intel Corporation
# Copyright (C) 2020-2021 Intel Corporation
#
# SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -26,12 +26,11 @@ class MotsPath:
LABELS_FILE = 'labels.txt'
MAX_INSTANCES = 1000

MotsLabels = Enum('MotsLabels', [
('background', 0),
('car', 1),
('pedestrian', 2),
('ignored', 10),
])
class MotsLabels(Enum):
background = 0
car = 1
pedestrian = 2
ignored = 10

class MotsPngExtractor(SourceExtractor):
@staticmethod
Expand Down
14 changes: 10 additions & 4 deletions datumaro/plugins/ndr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: MIT

from enum import Enum
from enum import Enum, auto
import logging as log

import cv2
Expand All @@ -14,11 +14,17 @@
from datumaro.util import parse_str_enum_value


Algorithm = Enum("Algorithm", ["gradient"]) # other algorithms will be added
class Algorithm(Enum):
gradient = auto()
# other algorithms will be added

OverSamplingMethod = Enum("OverSamplingMethod", ["random", "similarity"])
class OverSamplingMethod(Enum):
random = auto()
similarity = auto()

UnderSamplingMethod = Enum("UnderSamplingMethod", ["uniform", "inverse"])
class UnderSamplingMethod(Enum):
uniform = auto()
inverse = auto()

class NDR(Transform, CliPlugin):
"""
Expand Down
13 changes: 9 additions & 4 deletions datumaro/plugins/sampler/algorithm/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
#
# SPDX-License-Identifier: MIT

from enum import Enum
from enum import Enum, auto


SamplingMethod = Enum("SamplingMethod",
["topk", "lowk", "randk", "mixk", "randtopk"])
class SamplingMethod(Enum):
topk = auto()
lowk = auto()
randk = auto()
mixk = auto()
randtopk = auto()

Algorithm = Enum("Algorithm", ["entropy"])
class Algorithm(Enum):
entropy = auto()

class InferenceResultAnalyzer:
"""
Expand Down
Loading

0 comments on commit 64b01b7

Please sign in to comment.