Return whether the graph contains the given nodes. Parameters ---------- vid : node ID(s) The nodes IDs. The allowed nodes ID formats are: * ``int``: The ID of a single node. * Int Tensor: Each element is a node ID. The tensor must have t
(self, vid, ntype=None)
| 2876 | return self._graph.dtype |
| 2877 | |
| 2878 | def has_nodes(self, vid, ntype=None): |
| 2879 | """Return whether the graph contains the given nodes. |
| 2880 | |
| 2881 | Parameters |
| 2882 | ---------- |
| 2883 | vid : node ID(s) |
| 2884 | The nodes IDs. The allowed nodes ID formats are: |
| 2885 | |
| 2886 | * ``int``: The ID of a single node. |
| 2887 | * Int Tensor: Each element is a node ID. The tensor must have the same device type |
| 2888 | and ID data type as the graph's. |
| 2889 | * iterable[int]: Each element is a node ID. |
| 2890 | |
| 2891 | ntype : str, optional |
| 2892 | The node type name. Can be omitted if there is |
| 2893 | only one type of nodes in the graph. |
| 2894 | |
| 2895 | Returns |
| 2896 | ------- |
| 2897 | bool or bool Tensor |
| 2898 | A tensor of bool flags where each element is True if the node is in the graph. |
| 2899 | If the input is a single node, return one bool value. |
| 2900 | |
| 2901 | Examples |
| 2902 | -------- |
| 2903 | |
| 2904 | The following example uses PyTorch backend. |
| 2905 | |
| 2906 | >>> import dgl |
| 2907 | >>> import torch |
| 2908 | |
| 2909 | Create a graph with two node types -- 'user' and 'game'. |
| 2910 | |
| 2911 | >>> g = dgl.heterograph({ |
| 2912 | ... ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])), |
| 2913 | ... ('user', 'plays', 'game'): (torch.tensor([3, 4]), torch.tensor([0, 1])) |
| 2914 | ... }) |
| 2915 | |
| 2916 | Query for the nodes. |
| 2917 | |
| 2918 | >>> g.has_nodes(0, 'user') |
| 2919 | True |
| 2920 | >>> g.has_nodes(3, 'game') |
| 2921 | False |
| 2922 | >>> g.has_nodes(torch.tensor([3, 0, 1]), 'game') |
| 2923 | tensor([False, True, True]) |
| 2924 | """ |
| 2925 | vid_tensor = utils.prepare_tensor(self, vid, "vid") |
| 2926 | if len(vid_tensor) > 0 and F.as_scalar(F.min(vid_tensor, 0)) < 0 < len( |
| 2927 | vid_tensor |
| 2928 | ): |
| 2929 | raise DGLError("All IDs must be non-negative integers.") |
| 2930 | ret = self._graph.has_nodes(self.get_ntype_id(ntype), vid_tensor) |
| 2931 | if isinstance(vid, numbers.Integral): |
| 2932 | return bool(F.as_scalar(ret)) |
| 2933 | else: |
| 2934 | return F.astype(ret, F.bool) |
| 2935 |