class_docstr: Repurpose a dataset for link prediction task.
  
  The created dataset will include data needed for link prediction.
  Currently it only supports homogeneous graphs.
  It will keep only the first graph in the provided dataset and
  generate train/val/test edges according to the given split ratio,
  and the correspondent negative edges based on the neg_ratio. The generated
  edges will be cached to disk for fast re-loading. If the provided split ratio
  differs from the cached one, it will re-process the dataset properly.
  
  Parameters
  ----------
  dataset : DGLDataset
      The dataset to be converted.
  split_ratio : (float, float, float), optional
      Split ratios for training, validation and test sets. Must sum to one.
  neg_ratio : int, optional
      Indicate how much negative samples to be sampled
      The number of the negative samples will be equal or less than neg_ratio * num_positive_edges.
  
  Attributes
  -------
  feat_size: int
      The size of the feature dimension in the graph
  train_graph: DGLGraph
      The DGLGraph for training
  val_edges: Tuple[Tuple[Tensor, Tensor], Tuple[Tensor, Tensor]]
      The validation set edges, encoded as
      ((positive_edge_src, positive_edge_dst), (negative_edge_src, negative_edge_dst))
  test_edges: Tuple[Tuple[Tensor, Tensor], Tuple[Tensor, Tensor]]
      The test set edges, encoded as
      ((positive_edge_src, positive_edge_dst), (negative_edge_src, negative_edge_dst))
  
  Examples
  --------
  >>> ds = dgl.data.CoraGraphDataset()
  >>> print(ds)
  Dataset("cora_v2", num_graphs=1, save_path=...)
  >>> new_ds = dgl.data.AsLinkPredDataset(ds, [0.8, 0.1, 0.1])
  >>> print(new_ds)
  Dataset("cora_v2-as-linkpred", num_graphs=1, save_path=/home/ubuntu/.dgl/cora_v2-as-linkpred)
  >>> print(hasattr(new_ds, "test_edges"))
  True
class_docstr_tok: ["Repurpose","a","dataset","for","link","prediction","task",".","The","created","dataset","will","include","data","needed","for","link","prediction",".","Currently","it","only","supports","homogeneous","graphs",".","It","will","keep","only","the","first","graph","in","the","provided","dataset","and","generate","train\/val\/test","edges","according","to","the","given","split","ratio",",","and","the","correspondent","negative","edges","based","on","the","neg_ratio",".","The","generated","edges","will","be","cached","to","disk","for","fast","re-loading",".","If","the","provided","split","ratio","differs","from","the","cached","one",",","it","will","re-process","the","dataset","properly",".","Parameters","--","--","--","--","--","dataset",":","DGLDataset","The","dataset","to","be","converted",".","split_ratio",":","(","float",",","float",",","float",")",",","optional","Split","ratios","for","training",",","validation","and","test","sets",".","Must","sum","to","one",".","neg_ratio",":","int",",","optional","Indicate","how","much","negative","samples","to","be","sampled","The","number","of","the","negative","samples","will","be","equal","or","less","than","neg_ratio","*","num_positive_edges",".","Attributes","--","--","--","-","feat_size",":","int","The","size","of","the","feature","dimension","in","the","graph","train_graph",":","DGLGraph","The","DGLGraph","for","training","val_edges",":","Tuple","[","Tuple","[","Tensor",",","Tensor","]",",","Tuple","[","Tensor",",","Tensor","]","]","The","validation","set","edges",",","encoded","as","(","(","positive_edge_src",",","positive_edge_dst",")",",","(","negative_edge_src",",","negative_edge_dst",")",")","test_edges",":","Tuple","[","Tuple","[","Tensor",",","Tensor","]",",","Tuple","[","Tensor",",","Tensor","]","]","The","test","set","edges",",","encoded","as","(","(","positive_edge_src",",","positive_edge_dst",")",",","(","negative_edge_src",",","negative_edge_dst",")",")","Examples","--","--","--","--",">",">",">","ds","=","dgl.data.CoraGraphDataset","(",")",">",">",">","print","(","ds",")","Dataset","(","``","cora_v2","''",",","num_graphs=1",",","save_path=","...",")",">",">",">","new_ds","=","dgl.data.AsLinkPredDataset","(","ds",",","[","0.8",",","0.1",",","0.1","]",")",">",">",">","print","(","new_ds",")","Dataset","(","``","cora_v2-as-linkpred","''",",","num_graphs=1",",","save_path=\/home\/ubuntu\/.dgl\/cora_v2-as-linkpred",")",">",">",">","print","(","hasattr","(","new_ds",",","``","test_edges","''",")",")","True"]
code: def process(self):
      if self.split_ratio is None:
          # Handle logics for OGB link prediction dataset
          assert hasattr(
              self.dataset, "get_edge_split"
          ), "dataset doesn't have get_edge_split method, please specify split_ratio and neg_ratio to generate the split"
          # This is likely to be an ogb dataset
          self.edge_split = self.dataset.get_edge_split()
          self._train_graph = self.g
          if "source_node" in self.edge_split["test"]:
              # Probably ogbl-citation2
              pos_e = (
                  self.edge_split["valid"]["source_node"],
                  self.edge_split["valid"]["target_node"],
              )
              neg_e_size = self.edge_split["valid"][
                  "target_node_neg"
              ].shape[-1]
              neg_e_src = np.repeat(
                  self.edge_split["valid"]["source_node"], neg_e_size
              )
              neg_e_dst = np.reshape(
                  self.edge_split["valid"]["target_node_neg"], -1
              )
              self._val_edges = pos_e, (neg_e_src, neg_e_dst)
              pos_e = (
                  self.edge_split["test"]["source_node"],
                  self.edge_split["test"]["target_node"],
              )
              neg_e_size = self.edge_split["test"][
                  "target_node_neg"
              ].shape[-1]
              neg_e_src = np.repeat(
                  self.edge_split["test"]["source_node"], neg_e_size
              )
              neg_e_dst = np.reshape(
                  self.edge_split["test"]["target_node_neg"], -1
              )
              self._test_edges = pos_e, (neg_e_src, neg_e_dst)
          elif "edge" in self.edge_split["test"]:
              # Probably ogbl-collab
              pos_e_tensor, neg_e_tensor = (
                  self.edge_split["valid"]["edge"],
                  self.edge_split["valid"]["edge_neg"],
              )
              pos_e = (pos_e_tensor[:, 0], pos_e_tensor[:, 1])
              neg_e = (neg_e_tensor[:, 0], neg_e_tensor[:, 1])
              self._val_edges = pos_e, neg_e
  
              pos_e_tensor, neg_e_tensor = (
                  self.edge_split["test"]["edge"],
                  self.edge_split["test"]["edge_neg"],
              )
              pos_e = (pos_e_tensor[:, 0], pos_e_tensor[:, 1])
              neg_e = (neg_e_tensor[:, 0], neg_e_tensor[:, 1])
              self._test_edges = pos_e, neg_e
          # delete edge split to save memory
          self.edge_split = None
      else:
          assert (
              self.split_ratio is not None
          ), "Need to specify split_ratio"
          assert self.neg_ratio is not None, "Need to specify neg_ratio"
          ratio = self.split_ratio
          graph = self.dataset[0]
          n = graph.num_edges()
          src, dst = graph.edges()
          src, dst = F.asnumpy(src), F.asnumpy(dst)
          n_train, n_val, n_test = (
              int(n * ratio[0]),
              int(n * ratio[1]),
              int(n * ratio[2]),
          )
  
          idx = np.random.permutation(n)
          train_pos_idx = idx[:n_train]
          val_pos_idx = idx[n_train : n_train + n_val]
          test_pos_idx = idx[n_train + n_val :]
          neg_src, neg_dst = negative_sample(
              graph, self.neg_ratio * (n_val + n_test)
          )
          neg_n_val, neg_n_test = (
              self.neg_ratio * n_val,
              self.neg_ratio * n_test,
          )
          neg_val_src, neg_val_dst = (
              neg_src[:neg_n_val],
              neg_dst[:neg_n_val],
          )
          neg_test_src, neg_test_dst = (
              neg_src[neg_n_val:],
              neg_dst[neg_n_val:],
          )
          self._val_edges = (
              F.tensor(src[val_pos_idx]),
              F.tensor(dst[val_pos_idx]),
          ), (F.tensor(neg_val_src), F.tensor(neg_val_dst))
          self._test_edges = (
              F.tensor(src[test_pos_idx]),
              F.tensor(dst[test_pos_idx]),
          ), (F.tensor(neg_test_src), F.tensor(neg_test_dst))
          self._train_graph = create_dgl_graph(
              (src[train_pos_idx], dst[train_pos_idx]),
              num_nodes=self.num_nodes,
          )
          self._train_graph.ndata["feat"] = graph.ndata["feat"]
  
imports: import json
  import os
  import numpy
  from ..None import backend
  from ..base import DGLError
  from ..convert import graph
  from ..sampling.negative import _calc_redundancy
  from .None import utils
  from .dgl_dataset import DGLDataset
label_desc: Use image node_id 2 for calling the AsLinkPredDataset obj's underlying member method code with example usage: obj.process() without return types