Skip to content

delta

A component for generating Features that represent differences in vision

Delta

Bases: FeatureExtractor[DeltaFeature]

A component for detecting changes in vision.

Source code in roc/feature_extractors/delta.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
@register_component("delta", "perception")
class Delta(FeatureExtractor[DeltaFeature]):
    """A component for detecting changes in vision."""

    def __init__(self) -> None:
        super().__init__()
        self.prev_viz: IntGrid | None = None

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

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

        prev = self.prev_viz
        # XXX: NLE reuses numpy arrays rather than creating new ones
        self.prev_viz = curr = IntGrid(data.glyphs.copy())

        if prev is None:
            # can't get difference when there was nothing before this
            self.settled()
            return None

        # roughly make sure that things are the same size
        assert prev.height == curr.height
        assert prev.width == curr.width

        for new_point in curr.points():
            old_point = prev.get_point(new_point.x, new_point.y)
            if old_point.val != new_point.val:
                self.pb_conn.send(
                    DeltaFeature(
                        origin_id=self.id,
                        point=(new_point.x, new_point.y),
                        old_val=old_point.val,
                        new_val=new_point.val,
                    )
                )

        self.settled()

prev_viz = None instance-attribute

__init__()

Source code in roc/feature_extractors/delta.py
76
77
78
def __init__(self) -> None:
    super().__init__()
    self.prev_viz: IntGrid | None = None

event_filter(e)

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

get_feature(e)

Source code in roc/feature_extractors/delta.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def get_feature(self, e: PerceptionEvent) -> None:
    data = e.data
    assert isinstance(data, VisionData)

    prev = self.prev_viz
    # XXX: NLE reuses numpy arrays rather than creating new ones
    self.prev_viz = curr = IntGrid(data.glyphs.copy())

    if prev is None:
        # can't get difference when there was nothing before this
        self.settled()
        return None

    # roughly make sure that things are the same size
    assert prev.height == curr.height
    assert prev.width == curr.width

    for new_point in curr.points():
        old_point = prev.get_point(new_point.x, new_point.y)
        if old_point.val != new_point.val:
            self.pb_conn.send(
                DeltaFeature(
                    origin_id=self.id,
                    point=(new_point.x, new_point.y),
                    old_val=old_point.val,
                    new_val=new_point.val,
                )
            )

    self.settled()

DeltaFeature dataclass

Bases: Feature[DeltaNode]

A Feature that describes changes in vision

Source code in roc/feature_extractors/delta.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@dataclass(kw_only=True)
class DeltaFeature(Feature[DeltaNode]):
    """A Feature that describes changes in vision"""

    feature_name: str = "Delta"
    old_val: int
    new_val: int
    point: tuple[XLoc, YLoc]

    def get_points(self) -> set[tuple[XLoc, YLoc]]:
        return {self.point}

    def __str__(self) -> str:
        return f"({self.point[0]}, {self.point[1]}): {self.old_val} -> {self.new_val}\n"

    def node_hash(self) -> int:
        return hash((self.old_val, self.new_val))

    def _create_nodes(self) -> DeltaNode:
        return DeltaNode(old_val=self.old_val, new_val=self.new_val)

    def _dbfetch_nodes(self) -> DeltaNode | None:
        return DeltaNode.find_one(
            "src.old_val = $old_val AND src.new_val = $new_val",
            params={"old_val": self.old_val, "new_val": self.new_val},
        )

feature_name = 'Delta' class-attribute instance-attribute

new_val instance-attribute

old_val instance-attribute

point instance-attribute

__init__(*, feature_name='Delta', old_val, new_val, point)

__str__()

Source code in roc/feature_extractors/delta.py
39
40
def __str__(self) -> str:
    return f"({self.point[0]}, {self.point[1]}): {self.old_val} -> {self.new_val}\n"

get_points()

Source code in roc/feature_extractors/delta.py
36
37
def get_points(self) -> set[tuple[XLoc, YLoc]]:
    return {self.point}

node_hash()

Source code in roc/feature_extractors/delta.py
42
43
def node_hash(self) -> int:
    return hash((self.old_val, self.new_val))

DeltaNode

Bases: FeatureNode

Source code in roc/feature_extractors/delta.py
18
19
20
21
22
23
24
class DeltaNode(FeatureNode):
    old_val: int
    new_val: int

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

attr_strs property

new_val instance-attribute

old_val instance-attribute