Skip to content

view

NodeAttributeView

Dynamic view for accessing node attributes with NetworkX-like syntax.

Source code in src/torch_structure/data/view.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class NodeAttributeView:
    """
    Dynamic view for accessing node attributes with NetworkX-like syntax.
    """

    def __init__(self, data, node_index, node_attr_list):
        self.data = data
        self.node_index = node_index
        self.node_attr_list = node_attr_list

    def __getitem__(self, attr):
        """Retrieves the attribute value for the node."""
        if attr not in self.node_attr_list:
            raise KeyError(f"Attribute '{attr}' does not exist.")
        return getattr(self.data, attr)[self.node_index]

    def __setitem__(self, attr, value):
        """Sets the attribute value for the node if the attribute exists."""
        if attr not in self.node_attr_list:
            raise KeyError(f"Attribute '{attr}' does not exist.")

        existing_attr = getattr(self.data, attr)
        existing_attr[self.node_index] = value

__getitem__(attr)

Retrieves the attribute value for the node.

Source code in src/torch_structure/data/view.py
46
47
48
49
50
def __getitem__(self, attr):
    """Retrieves the attribute value for the node."""
    if attr not in self.node_attr_list:
        raise KeyError(f"Attribute '{attr}' does not exist.")
    return getattr(self.data, attr)[self.node_index]

__setitem__(attr, value)

Sets the attribute value for the node if the attribute exists.

Source code in src/torch_structure/data/view.py
52
53
54
55
56
57
58
def __setitem__(self, attr, value):
    """Sets the attribute value for the node if the attribute exists."""
    if attr not in self.node_attr_list:
        raise KeyError(f"Attribute '{attr}' does not exist.")

    existing_attr = getattr(self.data, attr)
    existing_attr[self.node_index] = value

NodeView

Dynamic view for accessing nodes with NetworkX-like syntax.

Source code in src/torch_structure/data/view.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class NodeView:
    """
    Dynamic view for accessing nodes with NetworkX-like syntax.
    """

    def __init__(self, data, node_name_to_index, node_attr_list):
        self.data = data
        self.node_name_to_index = node_name_to_index
        self.node_attr_list = node_attr_list

    def __getitem__(self, node_name):
        if node_name not in self.node_name_to_index:
            raise KeyError(f"Node '{node_name}' does not exist.")

        node_index = self.node_name_to_index[node_name]
        return NodeAttributeView(self.data, node_index, self.node_attr_list)

    def __iter__(self):
        return iter(self.node_name_to_index)

    def keys(self):
        """Return the node names."""
        return self.node_name_to_index.keys()

    def items(self):
        """Return an iterator of ``(node_name, NodeAttributeView)`` pairs."""
        return ((name, self[name]) for name in self.node_name_to_index)

    def __len__(self):
        return len(self.node_name_to_index)

    def __repr__(self):
        return f"{dict(self.items())}"

items()

Return an iterator of (node_name, NodeAttributeView) pairs.

Source code in src/torch_structure/data/view.py
25
26
27
def items(self):
    """Return an iterator of ``(node_name, NodeAttributeView)`` pairs."""
    return ((name, self[name]) for name in self.node_name_to_index)

keys()

Return the node names.

Source code in src/torch_structure/data/view.py
21
22
23
def keys(self):
    """Return the node names."""
    return self.node_name_to_index.keys()