code: def top_k_indices(
      top_k: int, scores: Any, sort: bool = False
  ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]:
      """Returns top_k indices into a list of scores.
  
      Note that the indices are returned in a form that is useful for assigning
      values to the array. If using to select values from an array you may need to
      reshape the output. Examples:
  
         # Assigning values to scores based on indices
         indices = top_k_indices(1, scores)
         scores[indices] = 0.0
  
         # Selecting top_k
         indices = top_k_indices(scores)
         scores[indices].reshape(scores.shape[:-1] + (top_k,))
  
      Args:
        top_k: Number of top k values to return.
        scores: Array or list of scores for computing the top_k indices.
        sort: True if the indices should be sorted (in descending order).
  
      Returns:
        An array of indices into scores that can be used with either 1D or 2D
        arrays. If sort was True the indices will be returned in descending order of
        score (i.e. top score first).
  
      Raises:
        ValueError: If top_k doesn't match scores or input has more than 2 dims.
      """
      scores = util.to_numpy(scores)
      if scores.shape[-1] < top_k:
          raise ValueError(
              "not enough values were provided to perform the requested "
              f"calcuations for top k. The requested value for k is {top_k}, but the "
              f"values are {scores}\n\nThis may be caused by a metric configuration "
              "error or an error in the pipeline."
          )
  
      if len(scores.shape) == 1:
          # 1D data
          indices = np.argpartition(scores, -top_k)[-top_k:]
          if sort:
              indices = indices[np.argsort(-scores[indices])]
          return indices
      elif len(scores.shape) == 2:
          # 2D data
          indices = np.argpartition(scores, -top_k, axis=-1)[:, -top_k:]
          # The above creates an n x top_k matrix where each row in indices matches
          # the corresponding row in scores. For example:
          #   [
          #      [<row1_top_k_index_1>, <row_1_top_k_index_2>, ...],
          #      [<row2_top_k_index_1>, <row_2_top_k_index_2>, ...],
          #      ...
          #   ]
          # However numpy indexing wants the index to be be a 2-tuple of where the
          # first tuple value contains the row indices (repeated top k times for each
          # row) and the second tuple value contains the column values.
          #   (row1, row1, ..., row2, ...), (row1_top_k_index1, row1_top_index_2,...)
          if sort:
              for i in range(indices.shape[0]):
                  indices[i] = indices[i][
                      np.argsort(-scores[i][indices[i]])
                  ]
          return (
              np.arange(indices.shape[0]).repeat(top_k),
              indices.flatten(),
          )
      else:
          raise NotImplementedError(
              "top_k not supported for shapes > 2: scores = {}".format(
                  scores
              )
          )
  
imports: import inspect
  import math
  from typing import Any, Callable, Dict, Iterable, Iterator, List, Mapping, Optional, Tuple, Union
  import numpy
  import tensorflow
  from tensorflow_model_analysis import constants
  from tensorflow_model_analysis.api import types
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.proto import config_pb2
  from tensorflow_model_analysis.utils import util
  from tensorflow_metadata.proto.v0 import schema_pb2
label_desc: Use image node_id 15 for calling a global function with example usage: top_k_indices(top_k, scores, sort) and returns: indices, np,  indices