class_docstr: Iterator
code: def prepare_data(self):
      dataset = MAG240MDataset(self.data_dir)
  
      graph_file_list = []
      paper_edge_path = (
          f"{dataset.dir}/paper_to_paper_symmetric_pgl_split"
      )
      graph_file_list.append(paper_edge_path)
      t = time.perf_counter()
      if not osp.exists(paper_edge_path):
          log.info("Converting adjacency matrix...")
          edge_index = dataset.edge_index("paper", "cites", "paper")
          edge_index = edge_index.T
  
          edges_new = np.zeros((edge_index.shape[0], 2))
          edges_new[:, 0] = edge_index[:, 1]
          edges_new[:, 1] = edge_index[:, 0]
          edge_index = np.vstack((edge_index, edges_new))
          edge_types = np.full(
              [
                  edge_index.shape[0],
              ],
              0,
              dtype="int32",
          )
  
          graph = Graph(
              edge_index,
              num_nodes=dataset.num_papers,
              edge_feat={"edge_type": edge_types},
          )
          graph.adj_dst_index
          graph.dump(paper_edge_path)
          log.info(f"Done! [{time.perf_counter() - t:.2f}s]")
  
      author_edge_path = (
          f"{dataset.dir}/paper_to_author_symmetric_pgl_split_src"
      )
      graph_file_list.append(author_edge_path)
      t = time.perf_counter()
      if not osp.exists(author_edge_path):
          log.info("Converting author matrix...")
  
          # author
          log.info("adding author edges")
          edge_index = dataset.edge_index("author", "writes", "paper")
          edge_index = edge_index.T
          row, col = edge_index[:, 0], edge_index[:, 1]
          log.info(row[:10])
          row += dataset.num_papers
  
          edge_types = np.full(row.shape, 1, dtype="int32")
          edge_index = np.stack([row, col], axis=1)
  
          graph = Graph(edge_index, edge_feat={"edge_type": edge_types})
          graph.adj_dst_index
          graph.dump(author_edge_path)
          log.info(
              f"Done! finish author_edge [{time.perf_counter() - t:.2f}s]"
          )
  
      author_edge_path = (
          f"{dataset.dir}/paper_to_author_symmetric_pgl_split_dst"
      )
      graph_file_list.append(author_edge_path)
      t = time.perf_counter()
      if not osp.exists(author_edge_path):
          log.info("Converting author matrix...")
  
          # author
          log.info("adding author edges")
          edge_index = dataset.edge_index("author", "writes", "paper")
          edge_index = edge_index.T
          row, col = edge_index[:, 0], edge_index[:, 1]
          log.info(row[:10])
          row += dataset.num_papers
  
          edge_types = np.full(row.shape, 2, dtype="int32")
          edge_index = np.stack([col, row], axis=1)
  
          graph = Graph(edge_index, edge_feat={"edge_type": edge_types})
          graph.adj_dst_index
          graph.dump(author_edge_path)
          log.info(
              f"Done! finish author_edge [{time.perf_counter() - t:.2f}s]"
          )
  
      institution_edge_path = (
          f"{dataset.dir}/institution_edge_symmetric_pgl_split_src"
      )
      graph_file_list.append(institution_edge_path)
      t = time.perf_counter()
      if not osp.exists(institution_edge_path):
          log.info("Converting institution matrix...")
  
          # institution
          log.info("adding institution edges")
          edge_index = dataset.edge_index("author", "institution")
          edge_index = edge_index.T
          row, col = edge_index[:, 0], edge_index[:, 1]
          log.info(row[:10])
          row += dataset.num_papers
          col += dataset.num_papers + dataset.num_authors
  
          # edge_type
          log.info("building edge type")
          edge_types = np.full(row.shape, 3, dtype="int32")
          edge_index = np.stack([row, col], axis=1)
  
          graph = Graph(edge_index, edge_feat={"edge_type": edge_types})
          graph.adj_dst_index
          graph.dump(institution_edge_path)
          log.info(
              f"Done! finish institution_edge [{time.perf_counter() - t:.2f}s]"
          )
  
      institution_edge_path = (
          f"{dataset.dir}/institution_edge_symmetric_pgl_split_dst"
      )
      graph_file_list.append(institution_edge_path)
      t = time.perf_counter()
      if not osp.exists(institution_edge_path):
          log.info("Converting institution matrix...")
  
          # institution
          log.info("adding institution edges")
          edge_index = dataset.edge_index("author", "institution")
          edge_index = edge_index.T
          row, col = edge_index[:, 0], edge_index[:, 1]
          log.info(row[:10])
          row += dataset.num_papers
          col += dataset.num_papers + dataset.num_authors
  
          # edge_type
          log.info("building edge type")
          edge_types = np.full(row.shape, 4, dtype="int32")
          edge_index = np.stack([col, row], axis=1)
  
          graph = Graph(edge_index, edge_feat={"edge_type": edge_types})
          graph.adj_dst_index
          graph.dump(institution_edge_path)
          log.info(
              f"Done! finish institution_edge [{time.perf_counter() - t:.2f}s]"
          )
  
      path = f"{dataset.dir}/full_feat.npy"
  
      author_feat_path = f"{dataset.dir}/author_feat.npy"
  
      institution_feat_path = f"{dataset.dir}/institution_feat.npy"
  
      t = time.perf_counter()
      if not osp.exists(path):  # Will take ~3 hours...
          print("Generating full feature matrix...")
  
          node_chunk_size = 100000
          N = (
              dataset.num_papers
              + dataset.num_authors
              + dataset.num_institutions
          )
  
          paper_feat = dataset.paper_feat
  
          author_feat = np.memmap(
              author_feat_path,
              dtype=np.float16,
              shape=(dataset.num_authors, self.num_features),
              mode="r",
          )
  
          institution_feat = np.memmap(
              institution_feat_path,
              dtype=np.float16,
              shape=(dataset.num_institutions, self.num_features),
              mode="r",
          )
  
          x = np.memmap(
              path,
              dtype=np.float16,
              mode="w+",
              shape=(N, self.num_features),
          )
  
          print("Copying paper features...")
          start_idx = 0
          end_idx = dataset.num_papers
          for i in tqdm(range(start_idx, end_idx, node_chunk_size)):
              j = min(i + node_chunk_size, end_idx)
              x[i:j] = paper_feat[i:j]
          del paper_feat
  
          print("Copying author feature...")
          start_idx = dataset.num_papers
          end_idx = dataset.num_papers + dataset.num_authors
          for i in tqdm(range(start_idx, end_idx, node_chunk_size)):
              j = min(i + node_chunk_size, end_idx)
              x[i:j] = author_feat[i - start_idx : j - start_idx]
          del author_feat
  
          print("Copying institution feature...")
          start_idx = dataset.num_papers + dataset.num_authors
          end_idx = (
              dataset.num_papers
              + dataset.num_authors
              + dataset.num_institutions
          )
          for i in tqdm(range(start_idx, end_idx, node_chunk_size)):
              j = min(i + node_chunk_size, end_idx)
              x[i:j] = institution_feat[i - start_idx : j - start_idx]
          del institution_feat
  
          x.flush()
          del x
          print(f"Done! [{time.perf_counter() - t:.2f}s]")
  
      np.random.seed(self.seed)
      self.train_idx = dataset.get_idx_split("train")
      self.val_idx = dataset.get_idx_split("valid")
      valid_name = os.path.join(self.valid_path, self.valid_name)
      self.val_idx_cv = np.load(valid_name)
      log.info(self.train_idx.shape)
      log.info(self.val_idx.shape)
      log.info(self.val_idx_cv.shape)
      self.test_idx = dataset.get_idx_split("test")
  
      ##self.val_idx = np.load('valid_idx_eval.npy')
      def get_sinusoid_encoding_table(
          n_position, d_hid, padding_idx=None
      ):
          def cal_angle(position, hid_idx):
              return position / np.power(
                  10000, 2 * (hid_idx // 2) / d_hid
              )
  
          def get_posi_angle_vec(position):
              return [
                  cal_angle(position, hid_j) for hid_j in range(d_hid)
              ]
  
          sinusoid_table = np.array(
              [get_posi_angle_vec(pos_i) for pos_i in range(n_position)]
          )
          sinusoid_table[:, 0::2] = np.sin(
              sinusoid_table[:, 0::2]
          )  # dim 2i
          sinusoid_table[:, 1::2] = np.cos(
              sinusoid_table[:, 1::2]
          )  # dim 2i+1
          return sinusoid_table
  
      N = (
          dataset.num_papers
          + dataset.num_authors
          + dataset.num_institutions
      )
      self.x = np.memmap(
          f"{dataset.dir}/full_feat.npy",
          dtype=np.float16,
          mode="r",
          shape=(N, self.num_features),
      )
  
      self.id_x = np.memmap(
          f"{dataset.dir}/{self.m2v_file}",
          dtype=np.float16,
          mode="r",
          shape=(N, self.m2v_dim),
      )
  
      self.y = dataset.all_paper_label
  
      self.graph = [
          Graph.load(edge_path, mmap_mode="r+")
          for edge_path in graph_file_list
      ]
  
      self.pos = get_sinusoid_encoding_table(200, 768)
      # self.year = dataset.all_paper_year
      year_file = f"{dataset.dir}/all_feat_year.npy"
      self.year = np.memmap(
          year_file, dtype=np.int32, mode="r", shape=(N,)
      )
      self.num_papers = dataset.num_papers
      self.train_idx_label = None
      self.train_idx_data = None
      log.info(f"Done! [{time.perf_counter() - t:.2f}s]")
  
imports: import os
  import yaml
  import pgl
  import time
  import copy
  import numpy
  import os.path
  from pgl.utils.logger import log
  from pgl.graph import Graph
  from pgl import graph_kernel
  from pgl.sampling.custom import subgraph
  from ogb.lsc import MAG240MDataset, MAG240MEvaluator
  from dataset.base_dataset import BaseDataGenerator
  import time
  from tqdm import tqdm
label_desc: Use image node_id 2 for calling the MAG240M obj's underlying member method code with example usage: obj.prepare_data() and returns: sinusoid_table, unknown, unknown