code: def _trim_sim_block(nitems, bsp, bitems, block, min_sim, max_nbrs):
      # pass 1: compute the size of each row
      sizes = np.zeros(bitems, np.int32)
      for i in range(nitems):
          sp, ep = block.row_extent(i)
          for j in range(sp, ep):
              # we accept the neighbor if it passes threshold and isn't a self-similarity
              r = block.colinds[j]
              if i != bsp + r and block.values[j] >= min_sim:
                  sizes[r] += 1
  
      if max_nbrs > 0:
          for i in range(bitems):
              if sizes[i] > max_nbrs:
                  sizes[i] = max_nbrs
  
      # if bnc == 0:
      #     # empty resulting matrix, oops
      #     return _empty_csr(bitems, nitems, np.zeros(bitems, np.int32))
  
      # allocate a matrix
      block_csr = create_from_sizes(bitems, nitems, sizes)
  
      # pass 2: truncate each row into the matrix
      eps = block_csr.rowptrs[:-1].copy()
      for c in range(nitems):
          sp, ep = block.row_extent(c)
          for j in range(sp, ep):
              v = block.values[j]
              r = block.colinds[j]
              sp, lep = block_csr.row_extent(r)
              lim = lep - sp
              if c != bsp + r and v >= min_sim:
                  eps[r] = kvp_minheap_insert(
                      sp,
                      eps[r],
                      lim,
                      c,
                      v,
                      block_csr.colinds,
                      block_csr.values,
                  )
          # we're done!
      return block_csr
  
imports: from sys import intern
  import logging
  import warnings
  import pandas
  import numpy
  import scipy.sparse
  import scipy.sparse.linalg
  from csr import CSR, create_from_sizes, create_empty
  import csr.kernel
  from numba import njit, prange
  from numba.typed import List
  from lenskit import util, DataWarning, ConfigWarning
  from lenskit.data import sparse_ratings
  from lenskit.sharing import in_share_context
  from lenskit.util.parallel import is_mp_worker
  from lenskit.util.accum import kvp_minheap_insert, kvp_minheap_sort
  from .None import Predictor
label_desc: Use image node_id 3 for calling a global function with example usage: _trim_sim_block(nitems, bsp, bitems, block, min_sim, max_nbrs) and returns: block_csr