Skip to content

line

MIN_LINE_COUNT = 4 module-attribute

Line

Bases: FeatureExtractor[TypedPointCollection]

A component for identifying similar values located along a vertical or horizontal line

Source code in roc/feature_extractors/line.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
@register_component("line", "perception")
class Line(FeatureExtractor[TypedPointCollection]):
    """A component for identifying similar values located along a vertical or
    horizontal line
    """

    def event_filter(self, e: PerceptionEvent) -> bool:
        return isinstance(e.data, VisionData)

    def get_feature(self, e: PerceptionEvent) -> None:
        assert isinstance(e.data, VisionData)
        data = IntGrid(e.data.glyphs)

        points: PointList = []

        def try_emit(ln: Line) -> None:
            nonlocal points

            if len(points) >= MIN_LINE_COUNT:
                point_set = {(p.x, p.y) for p in points}
                ln.pb_conn.send(
                    LineFeature(
                        origin_id=self.id,
                        points=point_set,
                        type=points[0].val,
                        size=len(points),
                    ),
                )
            points = []

        ## iterate points by 'x' to identify horizontal lines
        for y in range(data.height):
            for x in range(data.width):
                val = data.get_val(x, y)
                points.append(Point(XLoc(x), YLoc(y), val))
                if val != points[0].val:
                    p = points.pop()
                    try_emit(self)
                    points = [p]
            try_emit(self)

        ## iterate points by 'y' to identify vertical lines
        for x in range(data.width):
            for y in range(data.height):
                val = data.get_val(x, y)
                points.append(Point(XLoc(x), YLoc(y), val))
                if val != points[0].val:
                    p = points.pop()
                    try_emit(self)
                    points = [p]
            try_emit(self)

        self.settled()

event_filter(e)

Source code in roc/feature_extractors/line.py
46
47
def event_filter(self, e: PerceptionEvent) -> bool:
    return isinstance(e.data, VisionData)

get_feature(e)

Source code in roc/feature_extractors/line.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def get_feature(self, e: PerceptionEvent) -> None:
    assert isinstance(e.data, VisionData)
    data = IntGrid(e.data.glyphs)

    points: PointList = []

    def try_emit(ln: Line) -> None:
        nonlocal points

        if len(points) >= MIN_LINE_COUNT:
            point_set = {(p.x, p.y) for p in points}
            ln.pb_conn.send(
                LineFeature(
                    origin_id=self.id,
                    points=point_set,
                    type=points[0].val,
                    size=len(points),
                ),
            )
        points = []

    ## iterate points by 'x' to identify horizontal lines
    for y in range(data.height):
        for x in range(data.width):
            val = data.get_val(x, y)
            points.append(Point(XLoc(x), YLoc(y), val))
            if val != points[0].val:
                p = points.pop()
                try_emit(self)
                points = [p]
        try_emit(self)

    ## iterate points by 'y' to identify vertical lines
    for x in range(data.width):
        for y in range(data.height):
            val = data.get_val(x, y)
            points.append(Point(XLoc(x), YLoc(y), val))
            if val != points[0].val:
                p = points.pop()
                try_emit(self)
                points = [p]
        try_emit(self)

    self.settled()

LineFeature dataclass

Bases: AreaFeature[LineNode]

A collection of points representing a line

Source code in roc/feature_extractors/line.py
25
26
27
28
29
30
31
32
33
34
35
36
37
@dataclass(kw_only=True)
class LineFeature(AreaFeature[LineNode]):
    """A collection of points representing a line"""

    feature_name: str = "Line"

    def _create_nodes(self) -> LineNode:
        return LineNode(type=self.type, size=self.size)

    def _dbfetch_nodes(self) -> LineNode | None:
        return LineNode.find_one(
            "src.type = $type AND src.size = $size", params={"type": self.type, "size": self.size}
        )

feature_name = 'Line' class-attribute instance-attribute

__init__(*, feature_name='Line')

LineNode

Bases: FeatureNode

Source code in roc/feature_extractors/line.py
16
17
18
19
20
21
22
class LineNode(FeatureNode):
    type: int
    size: int

    @property
    def attr_strs(self) -> list[str]:
        return [str(self.type), str(self.size)]

attr_strs property

size instance-attribute

type instance-attribute