The data view class when G.ndata[ntype] is called.
| 56 | |
| 57 | |
| 58 | class HeteroNodeDataView(MutableMapping): |
| 59 | """The data view class when G.ndata[ntype] is called.""" |
| 60 | |
| 61 | __slots__ = ["_graph", "_ntype", "_ntid", "_nodes"] |
| 62 | |
| 63 | def __init__(self, graph, ntype, ntid, nodes): |
| 64 | self._graph = graph |
| 65 | self._ntype = ntype |
| 66 | self._ntid = ntid |
| 67 | self._nodes = nodes |
| 68 | |
| 69 | def __getitem__(self, key): |
| 70 | if isinstance(self._ntype, list): |
| 71 | ret = {} |
| 72 | for (i, ntype) in enumerate(self._ntype): |
| 73 | value = self._graph._get_n_repr(self._ntid[i], self._nodes).get( |
| 74 | key, None |
| 75 | ) |
| 76 | if value is not None: |
| 77 | ret[ntype] = value |
| 78 | return ret |
| 79 | else: |
| 80 | return self._graph._get_n_repr(self._ntid, self._nodes)[key] |
| 81 | |
| 82 | def __setitem__(self, key, val): |
| 83 | if isinstance(val, LazyFeature): |
| 84 | self._graph._node_frames[self._ntid][key] = val |
| 85 | elif isinstance(self._ntype, list): |
| 86 | assert isinstance(val, dict), ( |
| 87 | "Current HeteroNodeDataView has multiple node types, " |
| 88 | "please passing the node type and the corresponding data through a dict." |
| 89 | ) |
| 90 | |
| 91 | for (ntype, data) in val.items(): |
| 92 | ntid = self._graph.get_ntype_id(ntype) |
| 93 | self._graph._set_n_repr(ntid, self._nodes, {key: data}) |
| 94 | else: |
| 95 | assert isinstance(val, dict) is False, ( |
| 96 | "The HeteroNodeDataView has only one node type. " |
| 97 | "please pass a tensor directly" |
| 98 | ) |
| 99 | self._graph._set_n_repr(self._ntid, self._nodes, {key: val}) |
| 100 | |
| 101 | def __delitem__(self, key): |
| 102 | if isinstance(self._ntype, list): |
| 103 | for ntid in self._ntid: |
| 104 | if self._graph._get_n_repr(ntid, ALL).get(key, None) is None: |
| 105 | continue |
| 106 | self._graph._pop_n_repr(ntid, key) |
| 107 | else: |
| 108 | self._graph._pop_n_repr(self._ntid, key) |
| 109 | |
| 110 | def _transpose(self, as_dict=False): |
| 111 | if isinstance(self._ntype, list): |
| 112 | ret = defaultdict(dict) |
| 113 | for (i, ntype) in enumerate(self._ntype): |
| 114 | data = self._graph._get_n_repr(self._ntid[i], self._nodes) |
| 115 | for key in self._graph._node_frames[self._ntid[i]]: |
no outgoing calls
no test coverage detected