Skip to content

Shapes

siapy.entities.shapes

SHAPE_TYPE_RECTANGLE module-attribute

SHAPE_TYPE_RECTANGLE = 'rectangle'

SHAPE_TYPE_POINT module-attribute

SHAPE_TYPE_POINT = 'point'

SHAPE_TYPE_FREEDRAW module-attribute

SHAPE_TYPE_FREEDRAW = 'freedraw'

ShapeType module-attribute

Shape dataclass

Shape(shape_type: ShapeType, pixels: Pixels, label: str | None = None)

Bases: ABC

PARAMETER DESCRIPTION
shape_type

TYPE: ShapeType

pixels

TYPE: Pixels

label

TYPE: str | None DEFAULT: None

Source code in siapy/entities/shapes.py
27
28
29
30
31
32
33
34
35
def __init__(
    self,
    shape_type: ShapeType,
    pixels: Pixels,
    label: str | None = None,
):
    self._shape_type = shape_type
    self._pixels = pixels
    self._label = label

shape_type property

shape_type: str

pixels property

pixels: Pixels

label property

label: str | None

from_shape_type classmethod

from_shape_type(shape_type: ShapeType, pixels: Pixels, label: str | None = None) -> Shape
PARAMETER DESCRIPTION
shape_type

TYPE: ShapeType

pixels

TYPE: Pixels

label

TYPE: str | None DEFAULT: None

Source code in siapy/entities/shapes.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@classmethod
def from_shape_type(
    cls,
    shape_type: ShapeType,
    pixels: Pixels,
    label: str | None = None,
) -> "Shape":
    types_map: dict[ShapeType, type[Shape]] = {
        SHAPE_TYPE_RECTANGLE: Rectangle,
        SHAPE_TYPE_POINT: Point,
        SHAPE_TYPE_FREEDRAW: FreeDraw,
    }
    if shape_type in types_map:
        return types_map[shape_type](
            shape_type=shape_type,
            pixels=pixels,
            label=label,
        )
    else:
        raise InvalidInputError(
            {
                "shape_type": shape_type,
            },
            f"Unsupported shape type: {shape_type}",
        )

convex_hull abstractmethod

convex_hull()
Source code in siapy/entities/shapes.py
75
76
77
@abstractmethod
def convex_hull(self):
    raise MethodNotImplementedError(self.__class__.__name__, "convex_hull")

Rectangle

Rectangle(pixels: Pixels, label: str | None = None, **kwargs: Any)

Bases: Shape

PARAMETER DESCRIPTION
pixels

TYPE: Pixels

label

TYPE: str | None DEFAULT: None

**kwargs

TYPE: Any DEFAULT: {}

Source code in siapy/entities/shapes.py
81
82
def __init__(self, pixels: Pixels, label: str | None = None, **kwargs: Any):
    super().__init__(SHAPE_TYPE_RECTANGLE, pixels, label)

shape_type property

shape_type: str

pixels property

pixels: Pixels

label property

label: str | None

from_shape_type classmethod

from_shape_type(shape_type: ShapeType, pixels: Pixels, label: str | None = None) -> Shape
PARAMETER DESCRIPTION
shape_type

TYPE: ShapeType

pixels

TYPE: Pixels

label

TYPE: str | None DEFAULT: None

Source code in siapy/entities/shapes.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@classmethod
def from_shape_type(
    cls,
    shape_type: ShapeType,
    pixels: Pixels,
    label: str | None = None,
) -> "Shape":
    types_map: dict[ShapeType, type[Shape]] = {
        SHAPE_TYPE_RECTANGLE: Rectangle,
        SHAPE_TYPE_POINT: Point,
        SHAPE_TYPE_FREEDRAW: FreeDraw,
    }
    if shape_type in types_map:
        return types_map[shape_type](
            shape_type=shape_type,
            pixels=pixels,
            label=label,
        )
    else:
        raise InvalidInputError(
            {
                "shape_type": shape_type,
            },
            f"Unsupported shape type: {shape_type}",
        )

convex_hull

convex_hull() -> Pixels
Source code in siapy/entities/shapes.py
84
85
86
87
88
89
90
91
92
93
def convex_hull(self) -> Pixels:
    # Rectangle is defined by two opposite corners
    u1, u2 = self.pixels.u()
    v1, v2 = self.pixels.v()

    pixels_inside = []
    for u_coord in range(min(u1, u2), max(u1, u2) + 1):
        for v_coord in range(min(v1, v2), max(v1, v2) + 1):
            pixels_inside.append((u_coord, v_coord))
    return Pixels.from_iterable(pixels_inside)

Point

Point(pixels: Pixels, label: str | None = None, **kwargs: Any)

Bases: Shape

PARAMETER DESCRIPTION
pixels

TYPE: Pixels

label

TYPE: str | None DEFAULT: None

**kwargs

TYPE: Any DEFAULT: {}

Source code in siapy/entities/shapes.py
97
98
def __init__(self, pixels: Pixels, label: str | None = None, **kwargs: Any):
    super().__init__(SHAPE_TYPE_POINT, pixels, label)

shape_type property

shape_type: str

pixels property

pixels: Pixels

label property

label: str | None

from_shape_type classmethod

from_shape_type(shape_type: ShapeType, pixels: Pixels, label: str | None = None) -> Shape
PARAMETER DESCRIPTION
shape_type

TYPE: ShapeType

pixels

TYPE: Pixels

label

TYPE: str | None DEFAULT: None

Source code in siapy/entities/shapes.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@classmethod
def from_shape_type(
    cls,
    shape_type: ShapeType,
    pixels: Pixels,
    label: str | None = None,
) -> "Shape":
    types_map: dict[ShapeType, type[Shape]] = {
        SHAPE_TYPE_RECTANGLE: Rectangle,
        SHAPE_TYPE_POINT: Point,
        SHAPE_TYPE_FREEDRAW: FreeDraw,
    }
    if shape_type in types_map:
        return types_map[shape_type](
            shape_type=shape_type,
            pixels=pixels,
            label=label,
        )
    else:
        raise InvalidInputError(
            {
                "shape_type": shape_type,
            },
            f"Unsupported shape type: {shape_type}",
        )

convex_hull

convex_hull() -> Pixels
Source code in siapy/entities/shapes.py
100
101
def convex_hull(self) -> Pixels:
    return self.pixels

FreeDraw

FreeDraw(pixels: Pixels, label: str | None = None, **kwargs: Any)

Bases: Shape

PARAMETER DESCRIPTION
pixels

TYPE: Pixels

label

TYPE: str | None DEFAULT: None

**kwargs

TYPE: Any DEFAULT: {}

Source code in siapy/entities/shapes.py
105
106
def __init__(self, pixels: Pixels, label: str | None = None, **kwargs: Any):
    super().__init__(SHAPE_TYPE_FREEDRAW, pixels, label)

shape_type property

shape_type: str

pixels property

pixels: Pixels

label property

label: str | None

from_shape_type classmethod

from_shape_type(shape_type: ShapeType, pixels: Pixels, label: str | None = None) -> Shape
PARAMETER DESCRIPTION
shape_type

TYPE: ShapeType

pixels

TYPE: Pixels

label

TYPE: str | None DEFAULT: None

Source code in siapy/entities/shapes.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@classmethod
def from_shape_type(
    cls,
    shape_type: ShapeType,
    pixels: Pixels,
    label: str | None = None,
) -> "Shape":
    types_map: dict[ShapeType, type[Shape]] = {
        SHAPE_TYPE_RECTANGLE: Rectangle,
        SHAPE_TYPE_POINT: Point,
        SHAPE_TYPE_FREEDRAW: FreeDraw,
    }
    if shape_type in types_map:
        return types_map[shape_type](
            shape_type=shape_type,
            pixels=pixels,
            label=label,
        )
    else:
        raise InvalidInputError(
            {
                "shape_type": shape_type,
            },
            f"Unsupported shape type: {shape_type}",
        )

convex_hull

convex_hull() -> Pixels
Source code in siapy/entities/shapes.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def convex_hull(self) -> Pixels:
    if len(self.pixels) < 3:
        return self.pixels

    points = self.pixels.to_numpy()
    points_path = Path(points)

    # Create a grid of points that covers the convex hull area
    u_min, v_min = points.min(axis=0)
    u_max, v_max = points.max(axis=0)
    u, v = np.meshgrid(np.arange(u_min, u_max + 1), np.arange(v_min, v_max + 1))
    grid_points = np.vstack((u.flatten(), v.flatten())).T

    # Filter points that are inside the convex hull
    inside_points = grid_points[points_path.contains_points(grid_points)]

    return Pixels.from_iterable(inside_points.tolist())