class_docstr: Find closest anchors using :mod:`scipy.sparse`.
class_docstr_tok: ["Find","closest","anchors","using",":","mod",":","`","scipy.sparse","`","."]
docstr: Create a sparse adjacency matrix from a given edge index.
  
  :param edge_index: shape: (2, m)
      the edge index
  :param num_entities:
      the number of entities. Can be inferred from `edge_index`
  
  :return: shape: (n, n)
      a square sparse adjacency matrix
docstr_tok: ["Create","a","sparse","adjacency","matrix","from","a","given","edge","index",".",":","param","edge_index",":","shape",":","(","2",",","m",")","the","edge","index",":","param","num_entities",":","the","number","of","entities",".","Can","be","inferred","from","`","edge_index","`",":","return",":","shape",":","(","n",",","n",")","a","square","sparse","adjacency","matrix"]
code: def create_adjacency(
      edge_index: numpy.ndarray, num_entities: Optional[int] = None
  ) -> scipy.sparse.spmatrix:
      """
      Create a sparse adjacency matrix from a given edge index.
  
      :param edge_index: shape: (2, m)
          the edge index
      :param num_entities:
          the number of entities. Can be inferred from `edge_index`
  
      :return: shape: (n, n)
          a square sparse adjacency matrix
      """
      # infer shape
      num_entities = ensure_num_entities(
          edge_index, num_entities=num_entities
      )
      # create adjacency matrix
      adjacency = scipy.sparse.coo_matrix(
          (
              numpy.ones_like(edge_index[0], dtype=bool),
              tuple(edge_index),
          ),
          shape=(num_entities, num_entities),
      )
      # symmetric + self-loops
      adjacency = (
          adjacency
          + adjacency.transpose()
          + scipy.sparse.eye(num_entities, dtype=bool, format="coo")
      )
      adjacency = adjacency.tocsr()
      logger.debug(
          f"Created sparse adjacency matrix of shape {adjacency.shape} where "
          f"{format_relative_comparison(part=adjacency.nnz, total=numpy.prod(adjacency.shape))} "
          f"are non-zero entries.",
      )
      return adjacency
  
imports: import logging
  from abc import ABC, abstractmethod
  from typing import Iterable, Optional
  import numpy
  import scipy.sparse
  import torch
  from class_resolver import ClassResolver, OptionalKwargs
  from torch_ppr import page_rank
  from torch_ppr.utils import edge_index_to_sparse_matrix, prepare_page_rank_adjacency, prepare_x0
  from tqdm.auto import tqdm
  from .utils import ensure_num_entities
  from ...typing import DeviceHint
  from ...utils import ExtraReprMixin, format_relative_comparison, resolve_device
label_desc: Use image node_id 3 for calling the ScipySparseAnchorSearcher obj's underlying member method code with example usage: obj.create_adjacency(edge_index, num_entities) and returns: adjacency