code: def getKneighborsConnections(
      affinity_mat: torch.Tensor,
      p_value: int,
      mask_method: str = "binary",
  ) -> torch.Tensor:
      """
      Binarize top-p values for each row from the given affinity matrix.
  
      Args:
          affinity_mat (Tensor):
              A square matrix (tensor) containing normalized cosine similarity values
          p_value (int):
              The number of top values that are selected from each row.
          mask_method (str):
              The method that is used to manipulate the affinity matrix. The default method is 'binary'.
  
      Returns:
          binarized_affinity_mat (Tensor):
              A binarized affinity matrix based on the given mask method.
      """
      dim = affinity_mat.shape
      binarized_affinity_mat = torch.zeros_like(affinity_mat).half()
      sorted_matrix = torch.argsort(
          affinity_mat, dim=1, descending=True
      )[:, :p_value]
      binarized_affinity_mat[
          sorted_matrix.T, torch.arange(affinity_mat.shape[0])
      ] = (torch.ones(1).to(affinity_mat.device).half())
      indices_row = sorted_matrix[:, :p_value].flatten()
      indices_col = torch.arange(dim[1]).repeat(p_value, 1).T.flatten()
      if mask_method == "binary" or mask_method is None:
          binarized_affinity_mat[indices_row, indices_col] = (
              torch.ones(indices_row.shape[0])
              .to(affinity_mat.device)
              .half()
          )
      elif mask_method == "drop":
          binarized_affinity_mat[
              indices_row, indices_col
          ] = affinity_mat[indices_row, indices_col].half()
      elif mask_method == "sigmoid":
          binarized_affinity_mat[
              indices_row, indices_col
          ] = torch.sigmoid(
              affinity_mat[indices_row, indices_col]
          ).half()
      else:
          raise ValueError(f"Unknown mask method: {mask_method}")
      return binarized_affinity_mat
  
imports: from typing import Dict, List, Tuple
  import torch
  from torch.linalg import eigh, eigvalsh
label_desc: Use image node_id 8 for calling a global function with example usage: getKneighborsConnections(affinity_mat, p_value, mask_method) and returns: binarized_affinity_mat