MCPcopy
hub / github.com/dmlc/dgl / remove_nodes

Method remove_nodes

python/dgl/heterograph.py:763–946  ·  view source on GitHub ↗

r"""Remove multiple nodes with the specified node type Edges that connect to the nodes will be removed as well. After removing nodes and edges, the rest nodes and edges will be re-indexed using consecutive integers from 0, with their relative order preserved. The fe

(self, nids, ntype=None, store_ids=False)

Source from the content-addressed store, hash-verified

761 self._edge_frames = sub_g._edge_frames
762
763 def remove_nodes(self, nids, ntype=None, store_ids=False):
764 r"""Remove multiple nodes with the specified node type
765
766 Edges that connect to the nodes will be removed as well. After removing
767 nodes and edges, the rest nodes and edges will be re-indexed using
768 consecutive integers from 0, with their relative order preserved.
769
770 The features for the removed nodes/edges will be removed accordingly.
771
772 Parameters
773 ----------
774 nids : int, tensor, numpy.ndarray, list
775 Nodes to remove.
776 ntype : str, optional
777 The type of the nodes to remove. Can be omitted if there is
778 only one node type in the graph.
779 store_ids : bool, optional
780 If True, it will store the raw IDs of the extracted nodes and edges in the ``ndata``
781 and ``edata`` of the resulting graph under name ``dgl.NID`` and ``dgl.EID``,
782 respectively.
783
784 Notes
785 -----
786 This function preserves the batch information.
787
788 Examples
789 --------
790
791 >>> import dgl
792 >>> import torch
793
794 **Homogeneous Graphs or Heterogeneous Graphs with A Single Node Type**
795
796 >>> g = dgl.graph((torch.tensor([0, 0, 2]), torch.tensor([0, 1, 2])))
797 >>> g.ndata['hv'] = torch.arange(3).float().reshape(-1, 1)
798 >>> g.edata['he'] = torch.arange(3).float().reshape(-1, 1)
799 >>> g.remove_nodes(torch.tensor([0, 1]))
800 >>> g
801 Graph(num_nodes=1, num_edges=1,
802 ndata_schemes={'hv': Scheme(shape=(1,), dtype=torch.float32)}
803 edata_schemes={'he': Scheme(shape=(1,), dtype=torch.float32)})
804 >>> g.ndata['hv']
805 tensor([[2.]])
806 >>> g.edata['he']
807 tensor([[2.]])
808
809 Removing nodes from a batched graph preserves batch information.
810
811 >>> g = dgl.graph((torch.tensor([0, 0, 2]), torch.tensor([0, 1, 2])))
812 >>> g2 = dgl.graph((torch.tensor([1, 2, 3]), torch.tensor([1, 3, 4])))
813 >>> bg = dgl.batch([g, g2])
814 >>> bg.batch_num_nodes()
815 tensor([3, 5])
816 >>> bg.remove_nodes([1, 4])
817 >>> bg.batch_num_nodes()
818 tensor([2, 4])
819 >>> bg.batch_num_edges()
820 tensor([2, 2])

Callers 11

__call__Method · 0.80
remove_nodesFunction · 0.80
test_empty_queryFunction · 0.80
test_remove_nodesFunction · 0.80
test_frameFunction · 0.80
test_node_removalFunction · 0.80
test_node_frameFunction · 0.80
test_issue1287Function · 0.80
test_remove_nodesFunction · 0.80

Calls 9

num_nodesMethod · 0.95
get_ntype_idMethod · 0.95
nodesMethod · 0.95
get_etype_idMethod · 0.95
DGLErrorClass · 0.85
number_of_ntypesMethod · 0.80
formatMethod · 0.80
astypeMethod · 0.45
num_edgesMethod · 0.45

Tested by 9

test_empty_queryFunction · 0.64
test_remove_nodesFunction · 0.64
test_frameFunction · 0.64
test_node_removalFunction · 0.64
test_node_frameFunction · 0.64
test_issue1287Function · 0.64
test_remove_nodesFunction · 0.64