code: def graphsage_sample(graph, nodes, samples, ignore_edges=[]):
      """Implement of graphsage sample.
  
      Reference paper: https://cs.stanford.edu/people/jure/pubs/graphsage-nips17.pdf.
  
      Args:
          graph: A pgl graph instance
          nodes: Sample starting from nodes
          samples: A list, number of neighbors in each layer
          ignore_edges: list of edge(src, dst) will be ignored.
  
      Return:
          A list of subgraphs
      """
      start = time.time()
      num_layers = len(samples)
      start_nodes = nodes
      nodes = list(start_nodes)
      eids, edges = [], []
      nodes_set = set(nodes)
      layer_nodes, layer_eids, layer_edges = [], [], []
      ignore_edge_set = set(
          [edge_hash(src, dst) for src, dst in ignore_edges]
      )
  
      for layer_idx in reversed(range(num_layers)):
          if len(start_nodes) == 0:
              layer_nodes = [nodes] + layer_nodes
              layer_eids = [eids] + layer_eids
              layer_edges = [edges] + layer_edges
              continue
          batch_pred_nodes, batch_pred_eids = graph.sample_predecessor(
              start_nodes, samples[layer_idx], return_eids=True
          )
          start = time.time()
          last_nodes_set = nodes_set
  
          nodes, eids = copy.copy(nodes), copy.copy(eids)
          edges = copy.copy(edges)
          nodes_set, eids_set = set(nodes), set(eids)
          for srcs, dst, pred_eids in zip(
              batch_pred_nodes, start_nodes, batch_pred_eids
          ):
              for src, eid in zip(srcs, pred_eids):
                  if edge_hash(src, dst) in ignore_edge_set:
                      continue
                  if eid not in eids_set:
                      eids.append(eid)
                      edges.append([src, dst])
                      eids_set.add(eid)
                  if src not in nodes_set:
                      nodes.append(src)
                      nodes_set.add(src)
          layer_edges = [edges] + layer_edges
          start_nodes = list(nodes_set - last_nodes_set)
          layer_nodes = [nodes] + layer_nodes
          layer_eids = [eids] + layer_eids
          start = time.time()
          # Find new nodes
  
      subgraphs = []
      for i in range(num_layers):
          subgraphs.append(
              graph.subgraph(
                  nodes=layer_nodes[0],
                  eid=layer_eids[i],
                  edges=layer_edges[i],
              )
          )
          # only for this task
          subgraphs[i].node_feat["index"] = np.array(
              layer_nodes[0], dtype="int64"
          )
  
      return subgraphs
  
file_path: PGL/legacy/pgl/sample.py
imports: import time
  import copy
  import numpy
  import pgl
  from pgl.utils.logger import log
  from pgl import graph_kernel
label_desc: Use image node_id 4 for calling a global function with example usage: graphsage_sample(graph, nodes, samples, ignore_edges) and returns: subgraphs