code: def _sparse_slice_rows(
      sparse_tensor_value: tf.compat.v1.SparseTensorValue,
  ) -> List[tf.compat.v1.SparseTensorValue]:
      """Returns a list of single rows of a SparseTensorValue.
  
      This is equivalent to:
      [tf.sparse_slice(sparse_tensor_value, [row, 0, 0, ...], [1, INF, INF, ...])
       for row in range(0, sparse_tensor_value.dense_shape[0])]
      except that this operates on NumPy arrays.
  
      Args:
        sparse_tensor_value: SparseTensorValue to slice.
  
      Returns:
        List of SparseTensorValue representing the sliced rows.
      """
  
      # Create empty indices and values arrays with the same shape as the
      # sparse_tensor_value, except the number of rows (batch dimension) is 0.
      #
      # We need this because we need to preserve the shape of these arrays,
      # even if their batch dimension is 0.
      empty_indices_with_shape = np.zeros(
          _copy_shape_zero_rows(sparse_tensor_value.indices.shape),
          dtype=sparse_tensor_value.indices.dtype,
      )
      empty_values_with_shape = np.zeros(
          _copy_shape_zero_rows(sparse_tensor_value.values.shape),
          dtype=sparse_tensor_value.values.dtype,
      )
  
      if sparse_tensor_value.indices.size > 0:
          indices = sparse_tensor_value.indices
          # Sort indices matrix by rows, treating each row as a coordinate
          argsort_indices = np.lexsort(np.transpose(indices)[::-1])
          sorted_indices = indices[argsort_indices]
          sorted_values = sparse_tensor_value.values[argsort_indices]
      else:
          sorted_indices = []
          sorted_values = []
      num_sorted_indices = len(sorted_indices)
  
      result = []
      original_dense_shape = list(sparse_tensor_value.dense_shape)
  
      # Offset into the indices/values arrays of the original SparseTensorValue
      offset = 0
      dense_shape = [1] + original_dense_shape[1:]
  
      for row in range(0, original_dense_shape[0]):
          # Process each output row one at a time.
          indices = []
          values = []
  
          # Collect all the elements for this output row.
          while (
              offset < num_sorted_indices
              and sorted_indices[offset][0] == row
          ):
              # Okay to not make a copy and mutate sorted_indices here.
              cur_index = sorted_indices[offset]
              cur_index[0] = 0  # Zero out the row number
              indices.append(cur_index)
              values.append(sorted_values[offset])
              offset += 1
  
          # We treat each split SparseTensorValue as having dense_shape equal to the
          # maximum index in each dimension (+1 for zero-index).
          if indices:
              dense_shape[1:] = [
                  max([index[i] for index in indices]) + 1
                  for i in range(1, len(indices[0]))
              ]
          # For empty examples, we should have 0 in all other dimensions for the
          # dense_shape.
          else:
              dense_shape[1:] = [0] * (len(original_dense_shape) - 1)
  
          # pylint: disable=g-long-ternary
          result.append(
              tf.compat.v1.SparseTensorValue(
                  indices=(
                      np.array(
                          indices, dtype=empty_indices_with_shape.dtype
                      )
                      if indices
                      else empty_indices_with_shape
                  ),
                  values=(
                      np.array(
                          values, dtype=empty_values_with_shape.dtype
                      )
                      if values
                      else empty_values_with_shape
                  ),
                  dense_shape=np.array(dense_shape),
              )
          )
          # pylint: enable=g-long-ternary
  
      return result
  
file_path: model-analysis/tensorflow_model_analysis/eval_saved_model/util.py
imports: from typing import Any, Callable, List, Optional, Tuple
  import numpy
  import tensorflow
  from tensorflow import estimator
  from tensorflow_model_analysis.api import types
  from tensorflow_model_analysis.utils import util
  from tensorflow.core.example import example_pb2
label_desc: Use image node_id 8 for calling a global function with example usage: _sparse_slice_rows(sparse_tensor_value) and returns: result