class_docstr: Dataset for Graph Isomorphism Network (GIN)
  Adapted from https://github.com/weihua916/powerful-gnns/blob/master/dataset.zip.
class_docstr_tok: ["Dataset","for","Graph","Isomorphism","Network","(","GIN",")","Adapted","from","https",":","\/\/github.com\/weihua916\/powerful-gnns\/blob\/master\/dataset.zip","."]
docstr: Loads dataset
          
docstr_tok: ["Loads","dataset"]
code: def _load_data(self):
      """Loads dataset"""
      filename = os.path.join(
          self.data_path,
          self.dataset_name,
          "%s.txt" % self.dataset_name,
      )
      log.info("loading data from %s" % filename)
  
      with open(filename, "r") as reader:
          # first line --> N, means total number of graphs
          self.num_graph = int(reader.readline().strip())
  
          for i in range(self.num_graph):
              if (i + 1) % int(self.num_graph / 10) == 0:
                  log.info("processing graph %s" % (i + 1))
              graph = dict()
              # second line --> [num_node, label]
              # means [node number of a graph, class label of a graph]
              grow = reader.readline().strip().split()
              n_nodes, glabel = [int(w) for w in grow]
  
              # relabel graphs
              if glabel not in self.glabel_dict:
                  mapped = len(self.glabel_dict)
                  self.glabel_dict[glabel] = mapped
  
              graph["num_nodes"] = n_nodes
              self.glabel_list.append(self.glabel_dict[glabel])
  
              nlabels = []
              node_features = []
              num_edges = 0
              edges = []
  
              for j in range(graph["num_nodes"]):
                  slots = reader.readline().strip().split()
  
                  # handle edges and node feature(if has)
                  tmp = (
                      int(slots[1]) + 2
                  )  # tmp == 2 + num_edges of current node
                  if tmp == len(slots):
                      # no node feature
                      nrow = [int(w) for w in slots]
                      nfeat = None
                  elif tmp < len(slots):
                      nrow = [int(w) for w in slots[:tmp]]
                      nfeat = [float(w) for w in slots[tmp:]]
                      node_features.append(nfeat)
                  else:
                      raise Exception("edge number is not correct!")
  
                  # relabel nodes if is has labels
                  # if it doesn't have node labels, then every nrow[0] == 0
                  if not nrow[0] in self.nlabel_dict:
                      mapped = len(self.nlabel_dict)
                      self.nlabel_dict[nrow[0]] = mapped
  
                  nlabels.append(self.nlabel_dict[nrow[0]])
                  num_edges += nrow[1]
                  edges.extend([(j, u) for u in nrow[2:]])
  
                  if self.self_loop:
                      num_edges += 1
                      edges.append((j, j))
  
              if node_features != []:
                  node_features = np.stack(node_features)
                  graph["attr"] = node_features
                  self.nattrs_flag = True
              else:
                  node_features = None
                  graph["attr"] = node_features
  
              graph["nlabel"] = np.array(
                  nlabels, dtype="int64"
              ).reshape(-1, 1)
              if len(self.nlabel_dict) > 1:
                  self.nlabels_flag = True
  
              graph["edges"] = edges
              assert num_edges == len(edges)
  
              g = pgl.graph.Graph(
                  num_nodes=graph["num_nodes"],
                  edges=graph["edges"],
                  node_feat={
                      "nlabel": graph["nlabel"],
                      "attr": graph["attr"],
                  },
              )
  
              self.graph_list.append(g)
  
              # update statistics of graphs
              self.n += graph["num_nodes"]
              self.m += num_edges
  
      # if no attr
      if not self.nattrs_flag:
          log.info("there are no node features in this dataset!")
          label2idx = {}
          # generate node attr by node degree
          if self.degree_as_nlabel:
              log.info("generate node features by node degree...")
              nlabel_set = set([])
              for g in self.graph_list:
                  g.node_feat["nlabel"] = g.indegree()
                  # extracting unique node labels
                  nlabel_set = nlabel_set.union(
                      set(g.node_feat["nlabel"])
                  )
                  g.node_feat["nlabel"] = g.node_feat["nlabel"].reshape(
                      -1, 1
                  )
  
              nlabel_set = list(nlabel_set)
              # in case the labels/degrees are not continuous number
              self.ndegree_dict = {
                  nlabel_set[i]: i for i in range(len(nlabel_set))
              }
              label2idx = self.ndegree_dict
          # generate node attr by node label
          else:
              log.info("generate node features by node label...")
              label2idx = self.nlabel_dict
  
          for g in self.graph_list:
              attr = np.zeros((g.num_nodes, len(label2idx)))
              idx = [
                  label2idx[tag]
                  for tag in g.node_feat["nlabel"].reshape(
                      -1,
                  )
              ]
              attr[:, idx] = 1
              g.node_feat["attr"] = attr.astype("float32")
  
      # after load, get the #classes and #dim
      self.gclasses = len(self.glabel_dict)
      self.nclasses = len(self.nlabel_dict)
      self.eclasses = len(self.elabel_dict)
      self.dim_nfeats = len(self.graph_list[0].node_feat["attr"][0])
  
      message = "finished loading data\n"
      message += """
                      num_graph: %d
                      num_graph_class: %d
                      total_num_nodes: %d
                      node Classes: %d
                      node_features_dim: %d
                      num_edges: %d
                      edge_classes: %d
                      Avg. of #Nodes: %.2f
                      Avg. of #Edges: %.2f
                      Graph Relabeled: %s
                      Node Relabeled: %s
                      Degree Relabeled(If degree_as_nlabel=True): %s""" % (
          self.num_graph,
          self.gclasses,
          self.n,
          self.nclasses,
          self.dim_nfeats,
          self.m,
          self.eclasses,
          self.n / self.num_graph,
          self.m / self.num_graph,
          self.glabel_dict,
          self.nlabel_dict,
          self.ndegree_dict,
      )
      log.info(message)
  
imports: import os
  import sys
  import numpy
  from sklearn.model_selection import StratifiedKFold
  import pgl
  from pgl.utils.logger import log
label_desc: Use image node_id 4 for calling the GINDataset obj's underlying member method code with example usage: obj._load_data() without return types