Skip to content

Commit

Permalink
Addressed typing in shape/dataset/annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertvanHouten committed May 12, 2022
1 parent 981dea2 commit 2286e21
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 40 deletions.
37 changes: 19 additions & 18 deletions ote_sdk/ote_sdk/entities/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ def __repr__(self):
return (
f"{self.__class__.__name__}("
f"shape={self.shape}, "
f"labels={self.get_labels(True)}"
f"labels={self.get_labels(include_empty=True)}"
)

@property
def shape(self):
def shape(self) -> ShapeEntity:
"""
Returns the shape that is in the annotation
"""
return self.__shape

@shape.setter
def shape(self, value):
def shape(self, value) -> None:
self.__shape = value

def get_labels(self, include_empty: bool = False) -> List[ScoredLabel]:
Expand All @@ -67,15 +67,15 @@ def get_label_ids(self, include_empty: bool = False) -> Set[ID]:
if include_empty or (not label.is_empty)
}

def append_label(self, label: ScoredLabel):
def append_label(self, label: ScoredLabel) -> None:
"""
Appends the scored label to the annotation.
:param label: the scored label to be appended to the annotation
"""
self.__labels.append(label)

def set_labels(self, labels: List[ScoredLabel]):
def set_labels(self, labels: List[ScoredLabel]) -> None:
"""
Sets the labels of the annotation to be the input of the function.
Expand All @@ -86,7 +86,8 @@ def set_labels(self, labels: List[ScoredLabel]):
def __eq__(self, other):
if isinstance(other, Annotation):
return (
self.get_labels(True) == other.get_labels(True)
self.get_labels(include_empty=True)
== other.get_labels(include_empty=True)
and self.shape == other.shape
)
return False
Expand Down Expand Up @@ -160,14 +161,14 @@ def __repr__(self):
)

@property
def id_(self):
def id_(self) -> ID:
"""
Returns the ID of the AnnotationSceneEntity.
"""
return self.__id_

@id_.setter
def id_(self, value):
def id_(self, value) -> None:
self.__id_ = value

@property
Expand All @@ -181,36 +182,36 @@ def id(self, value):
self.__id_ = value

@property
def kind(self):
def kind(self) -> AnnotationSceneKind:
"""
Returns the AnnotationSceneKind of the AnnotationSceneEntity.
"""
return self.__kind

@kind.setter
def kind(self, value):
def kind(self, value) -> None:
self.__kind = value

@property
def editor_name(self):
def editor_name(self) -> str:
"""
Returns the editor's name that made the AnnotationSceneEntity object.
"""
return self.__editor

@editor_name.setter
def editor_name(self, value):
def editor_name(self, value) -> None:
self.__editor = value

@property
def creation_date(self):
def creation_date(self) -> datetime.datetime:
"""
Returns the creation date of the AnnotationSceneEntity object.
"""
return self.__creation_date

@creation_date.setter
def creation_date(self, value):
def creation_date(self, value) -> None:
self.__creation_date = value

@property
Expand All @@ -231,7 +232,7 @@ def shapes(self) -> List[ShapeEntity]:
"""
return [annotation.shape for annotation in self.annotations]

def contains_any(self, labels: List[LabelEntity]):
def contains_any(self, labels: List[LabelEntity]) -> bool:
"""
Checks whether the annotation contains any labels in the input parameter.
Expand All @@ -249,13 +250,13 @@ def contains_any(self, labels: List[LabelEntity]):
!= 0
)

def append_annotation(self, annotation: Annotation):
def append_annotation(self, annotation: Annotation) -> None:
"""
Appends the passed annotation to the list of annotations present in the AnnotationSceneEntity object.
"""
self.annotations.append(annotation)

def append_annotations(self, annotations: List[Annotation]):
def append_annotations(self, annotations: List[Annotation]) -> None:
"""
Adds a list of annotations to the annotation scene.
"""
Expand All @@ -272,7 +273,7 @@ def get_labels(self, include_empty: bool = False) -> List[LabelEntity]:

labels: Dict[str, LabelEntity] = {}
for annotation in self.annotations:
for label in annotation.get_labels(include_empty):
for label in annotation.get_labels(include_empty=include_empty):
id_ = label.id_
if id_ not in labels:
labels[id_] = label.get_label()
Expand Down
8 changes: 4 additions & 4 deletions ote_sdk/ote_sdk/entities/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def purpose(self) -> DatasetPurpose:
return self._purpose

@purpose.setter
def purpose(self, value: DatasetPurpose):
def purpose(self, value: DatasetPurpose) -> None:
self._purpose = value

def _fetch(self, key):
Expand Down Expand Up @@ -306,7 +306,7 @@ def get_subset(self, subset: Subset) -> "DatasetEntity":
)
return dataset

def remove(self, item: DatasetItemEntity):
def remove(self, item: DatasetItemEntity) -> None:
"""
Remove an item from the items.
This function calls remove_at_indices function.
Expand Down Expand Up @@ -339,14 +339,14 @@ def append(self, item: DatasetItemEntity) -> None:
raise ValueError("Media in dataset item cannot be None")
self._items.append(item)

def sort_items(self):
def sort_items(self) -> None:
"""
Order the dataset items. Does nothing here, but may be overrided in child classes.
:return: None
"""

def remove_at_indices(self, indices: List[int]):
def remove_at_indices(self, indices: List[int]) -> None:
"""
Delete items based on the `indices`.
Expand Down
8 changes: 4 additions & 4 deletions ote_sdk/ote_sdk/entities/shapes/ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __hash__(self):
return hash(str(self))

@property
def width(self):
def width(self) -> float:
"""
Returns the width of the ellipse. (x-axis)
Expand All @@ -96,7 +96,7 @@ def width(self):
return self.x2 - self.x1

@property
def height(self):
def height(self) -> float:
"""
Returns the height of the ellipse. (y-axis)
Expand All @@ -111,14 +111,14 @@ def height(self):
return self.y2 - self.y1

@property
def x_center(self):
def x_center(self) -> float:
"""
Returns the x coordinate in the center of the ellipse.
"""
return self.x1 + self.width / 2

@property
def y_center(self):
def y_center(self) -> float:
"""
Returns the y coordinate in the center of the ellipse.
"""
Expand Down
2 changes: 1 addition & 1 deletion ote_sdk/ote_sdk/entities/shapes/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def normalize_wrt_roi(self, roi_shape: Rectangle) -> "Point":
y1 = roi_shape.y1
return Point(x=self.x * width + x1, y=self.y * height + y1)

def denormalize_wrt_roi_shape(self, roi_shape: Rectangle):
def denormalize_wrt_roi_shape(self, roi_shape: Rectangle) -> "Point":
"""
The inverse of normalize_wrt_roi_shape.
Transforming Polygon from the normalized coordinate system to the `roi` coordinate system.
Expand Down
12 changes: 7 additions & 5 deletions ote_sdk/ote_sdk/entities/shapes/rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def clip_to_visible_region(self) -> "Rectangle":
x2 = min(max(0.0, self.x2), 1.0)
y2 = min(max(0.0, self.y2), 1.0)

return Rectangle(x1, y1, x2, y2, self.modification_date)
return Rectangle(
x1=x1, y1=y1, x2=x2, y2=y2, modification_date=self.modification_date
)

def normalize_wrt_roi_shape(self, roi_shape: "Rectangle") -> "Rectangle":
"""
Expand Down Expand Up @@ -230,7 +232,7 @@ def is_full_box(rectangle: ShapeEntity) -> bool:
return True
return False

def crop_numpy_array(self, data: np.ndarray):
def crop_numpy_array(self, data: np.ndarray) -> np.ndarray:
"""
Crop the given Numpy array to the region of interest represented by this
rectangle.
Expand All @@ -252,7 +254,7 @@ def crop_numpy_array(self, data: np.ndarray):
return data[y1:y2, x1:x2, ::]

@property
def width(self):
def width(self) -> float:
"""
Returns the width of the rectangle. (x-axis)
Expand All @@ -267,7 +269,7 @@ def width(self):
return self.x2 - self.x1

@property
def height(self):
def height(self) -> float:
"""
Returns the height of the rectangle. (y-axis)
Expand All @@ -282,7 +284,7 @@ def height(self):
return self.y2 - self.y1

@property
def diagonal(self):
def diagonal(self) -> float:
"""
Returns the diagonal size/hypotenuse of the rectangle. (x-axis)
Expand Down
2 changes: 1 addition & 1 deletion ote_sdk/ote_sdk/entities/shapes/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, shape_type: ShapeType):
self._type = shape_type

@property
def type(self):
def type(self) -> ShapeType:
"""
Get the type of Shape that this Shape represents
"""
Expand Down
13 changes: 6 additions & 7 deletions ote_sdk/ote_sdk/tests/entities/shapes/test_rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ def test_rectangle_optional_parameters(self):
Instance of Rectangle class
<b>Expected results:</b>
Test passes if Rectangle instance has expected labels and modification attributes specified during Rectangle
Test passes if Rectangle instance has expected modification attributes specified during Rectangle
class object initiation with optional parameters
<b>Steps</b>
1. Compare default label Rectangle instance attribute with expected value
1. Compare default Rectangle instance attribute with expected value
2. Check type of default modification_date Rectangle instance attribute
3. Compare specified label Rectangle instance attribute with expected value
3. Compare specified Rectangle instance attribute with expected value
4. Compare specified modification_date Rectangle instance attribute with expected value
"""
# Checking default values of optional parameters
Expand Down Expand Up @@ -221,8 +221,7 @@ def test_rectangle_eq(self):
1. Check __eq__ method for instances of Rectangle class with equal parameters
2. Check __eq__ method for different instances of Rectangle class
3. Check __eq__ method for instances of different classes
4. Check __eq__ method for instances of Rectangle class with unequal labels attribute
5. Check __eq__ method for instances of Rectangle class with unequal x1, y1, x2, y2 and
4. Check __eq__ method for instances of Rectangle class with unequal x1, y1, x2, y2 and
modification_date attributes
"""
rectangle = self.vertical_rectangle()
Expand All @@ -233,7 +232,7 @@ def test_rectangle_eq(self):
assert rectangle != self.horizontal_rectangle()
# Check for different types branch
assert rectangle != str
# Check for unequal labels parameters. Expected that different labels are not affecting equality

assert rectangle == equal_rectangle
# Check for instances with unequal parameters combinations
# Generating all possible scenarios of parameter values submission
Expand Down Expand Up @@ -458,7 +457,7 @@ def test_rectangle_generate_full_box(self):
(x1=0.0, y1=0.0, x2=1.0, y2=1.0)
<b>Steps</b>
1. Check generate_full_box method for Rectangle instance with no labels specified
1. Check generate_full_box method for Rectangle instance
"""
full_box = Rectangle.generate_full_box()
assert full_box.type == ShapeType.RECTANGLE
Expand Down

0 comments on commit 2286e21

Please sign in to comment.