code: def test_precision(
      num_src_nodes, num_dst_nodes, num_edges, batch_size, k
  ):
      row = torch.randint(0, num_src_nodes, (num_edges,))
      col = torch.randint(0, num_dst_nodes, (num_edges,))
      edge_label_index = torch.stack([row, col], dim=0)
  
      pred = torch.rand(num_src_nodes, num_dst_nodes)
      pred[row, col] += 0.3  # Offset positive links by a little.
      top_k_pred_mat = pred.topk(k, dim=1)[1]
  
      metric = LinkPredPrecision(k)
      assert str(metric) == f"LinkPredPrecision(k={k})"
  
      for node_id in torch.split(
          torch.randperm(num_src_nodes), batch_size
      ):
          mask = torch.isin(edge_label_index[0], node_id)
  
          y_batch, y_index = edge_label_index[:, mask]
          # Remap `y_batch` back to `[0, batch_size - 1]` range:
          arange = torch.empty(num_src_nodes, dtype=node_id.dtype)
          arange[node_id] = torch.arange(node_id.numel())
          y_batch = arange[y_batch]
  
          metric.update(top_k_pred_mat[node_id], (y_batch, y_index))
  
      out = metric.compute()
      metric.reset()
  
      values: List[float] = []
      for i in range(num_src_nodes):  # Naive computation per node:
          y_index = col[row == i]
          if y_index.numel() > 0:
              mask = torch.isin(top_k_pred_mat[i], y_index)
              precision = float(mask.sum() / k)
              values.append(precision)
      expected = torch.tensor(values).mean()
      assert torch.allclose(out, expected)
  
file_path: pytorch_geometric/test/nn/test_metrics.py
imports: from typing import List
  import pytest
  import torch
  from torch_geometric.nn.metrics import LinkPredF1, LinkPredMAP, LinkPredNDCG, LinkPredPrecision, LinkPredRecall
label_desc: Use image node_id 1 for calling a global function with example usage: test_precision(num_src_nodes, num_dst_nodes, num_edges, batch_size, k) without return types