text: import pickle as pkl
  
  import networkx as nx
  
  from pygcn.utils import *
  
  
  def generate_synthetic():
      import random
      max_nodes = 200
      min_nodes = 100
      community_num_nodes = 10
      graphs = []
      labels = []
      com_1 = nx.caveman_graph(1, community_num_nodes)
      com_2 = nx.star_graph(community_num_nodes)
  
      for i in range(500):
          num_nodes = random.randint(min_nodes, max_nodes)
          graph = nx.fast_gnp_random_graph(num_nodes, 0.1)
          graph = nx.disjoint_union(graph, com_1)
          for i in range(num_nodes, graph.number_of_nodes()):
              for j in range(num_nodes):
                  if random.random() > 0.9:
                      # graph.add_edge(graph.nodes()[i], graph.nodes()[j])
                      graph.add_edge(list(graph.nodes)[i], list(graph.nodes)[j])
          graphs.append(graph)
          labels.append(1)
          num_nodes = random.randint(min_nodes, max_nodes)
          graph = nx.fast_gnp_random_graph(num_nodes, 0.1)
          for i in range(num_nodes, graph.number_of_nodes()):
              for j in range(num_nodes):
                  if random.random() > 0.9:
                      graph.add_edge(list(graph.nodes)[i], list(graph.nodes)[j])
          graphs.append(graph)
          labels.append(0)
  
      return graphs, labels
  
  
  def load_cora(path="../data/cora/", dataset="cora"):
      """Load citation network dataset (cora only for now)"""
      print('Loading {} dataset...'.format(dataset))
  
      idx_features_labels = np.genfromtxt("{}{}.content".format(path, dataset),
                                          dtype=np.dtype(str))
      features = sp.csr_matrix(idx_features_labels[:, 1:-1], dtype=np.float32)
      labels = encode_onehot(idx_features_labels[:, -1])
  
      # build graph
      idx = np.array(idx_features_labels[:, 0], dtype=np.int32)
      idx_map = {j: i for i, j in enumerate(idx)}
      edges_unordered = np.genfromtxt("{}{}.cites".format(path, dataset),
                                      dtype=np.int32)
      edges = np.array(list(map(idx_map.get, edges_unordered.flatten())),
                       dtype=np.int32).reshape(edges_unordered.shape)
      adj = sp.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
                          shape=(labels.shape[0], labels.shape[0]),
                          dtype=np.float32)
  
      # build symmetric adjacency matrix
      adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
  
      features = normalize(features)
      adj = normalize(adj + sp.eye(adj.shape[0]))
  
      idx_train = range(140)
      idx_val = range(200, 500)
      idx_test = range(500, 1500)
  
      features = torch.FloatTensor(np.array(features.todense()))
      labels = torch.LongTensor(np.where(labels)[1])
      adj = sparse_mx_to_torch_sparse_tensor(adj)
  
      idx_train = torch.LongTensor(idx_train)
      idx_val = torch.LongTensor(idx_val)
      idx_test = torch.LongTensor(idx_test)
  
      return adj, features, labels, idx_train, idx_val, idx_test
  
  
  def load_planetoid(path="../data/Planetoid/", dataset="cora"):
      """
      ind.[:dataset].x     => the feature vectors of the training instances (scipy.sparse.csr.csr_matrix)
      ind.[:dataset].y     => the one-hot labels of the labeled training instances (numpy.ndarray)
      ind.[:dataset].allx  => the feature vectors of both labeled and unlabeled training instances (csr_matrix)
      ind.[:dataset].ally  => the labels for instances in ind.dataset_str.allx (numpy.ndarray)
      ind.[:dataset].graph => the dict in the format {index: [index of neighbor nodes]} (collections.defaultdict)
      ind.[:dataset].tx    => the feature vectors of the test instances (scipy.sparse.csr.csr_matrix)
      ind.[:dataset].ty    => the one-hot labels of the test instances (numpy.ndarray)
      ind.[:dataset].test.index => indices of test instances in graph, for the inductive setting
      """
      print("\n[STEP 1]: Upload {} dataset.".format(dataset))
  
      names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
      objects = []
  
      for i in range(len(names)):
          with open("{}/ind.{}.{}".format(path, dataset, names[i]), 'rb') as f:
              objects.append(pkl.load(f, encoding='latin1'))
  
      x, y, tx, ty, allx, ally, graph = tuple(objects)
  
      test_idx_reorder = parse_index_file("{}/ind.{}.test.index".format(path, dataset))
      test_idx_range = np.sort(test_idx_reorder)
  
      if dataset == 'citeseer':
          # Citeseer dataset contains some isolated nodes in the graph
          test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder) + 1)
          tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1]))
          tx_extended[test_idx_range - min(test_idx_range), :] = tx
          tx = tx_extended
  
          ty_extended = np.zeros((len(test_idx_range_full), y.shape[1]))
          ty_extended[test_idx_range - min(test_idx_range), :] = ty
          ty = ty_extended
  
      features = sp.vstack((allx, tx)).tolil()
      features[test_idx_reorder, :] = features[test_idx_range, :]
  
      adj = nx.adjacency_matrix(nx.from_dict_of_lists(graph))
      print("| # of nodes : {}".format(adj.shape[0]))
      print("| # of edges : {}".format(adj.sum().sum() / 2))
  
      features = normalize(features)
      adj = normalize_adj(adj + sp.eye(adj.shape[0]))
      print("| # of features : {}".format(features.shape[1]))
      print("| # of classes   : {}".format(ally.shape[1]))
  
      features = torch.FloatTensor(np.array(features.todense()))
      sparse_mx = adj.tocoo().astype(np.float32)
      adj = torch.FloatTensor(np.array(adj.todense()))
  
      labels = np.vstack((ally, ty))
      labels[test_idx_reorder, :] = labels[test_idx_range, :]
  
      if dataset == 'citeseer':
          save_label = np.where(labels)[1]
      labels = torch.LongTensor(np.where(labels)[1])
  
      idx_train = range(len(y))
      idx_val = range(len(y), len(y) + 500)
      idx_test = test_idx_range.tolist()
  
      print("| # of train set : {}".format(len(idx_train)))
      print("| # of val set   : {}".format(len(idx_val)))
      print("| # of test set  : {}".format(len(idx_test)))
  
      idx_train, idx_val, idx_test = list(map(lambda x: torch.LongTensor(x), [idx_train, idx_val, idx_test]))
  
      def missing_elements(L):
          start, end = L[0], L[-1]
          return sorted(set(range(start, end + 1)).difference(L))
  
      if dataset == 'citeseer':
          L = np.sort(idx_test)
          missing = missing_elements(L)
  
          for element in missing:
              save_label = np.insert(save_label, element, 0)
          labels = torch.LongTensor(save_label)
  
      return adj, features, labels, idx_train, idx_val, idx_test
  