code: def test_sparse_matrix(rng):
      ratings = ml_test.ratings
      mat, uidx, iidx = sparse_ratings(ratings)
  
      assert mat.nrows == len(uidx)
      assert mat.nrows == ratings.user.nunique()
      assert mat.ncols == len(iidx)
      assert mat.ncols == ratings.item.nunique()
  
      # user indicators should correspond to user item counts
      ucounts = ratings.groupby("user").item.count()
      ucounts = ucounts.loc[uidx].cumsum()
      assert all(mat.rowptrs[1:] == ucounts.values)
  
      # verify rating values
      ratings = ratings.set_index(["user", "item"])
      for u in rng.choice(uidx, size=50):
          ui = uidx.get_loc(u)
          vs = mat.row_vs(ui)
          vs = pd.Series(vs, iidx[mat.row_cs(ui)])
          rates = ratings.loc[u]["rating"]
          vs, rates = vs.align(rates)
          assert not any(vs.isna())
          assert not any(rates.isna())
          assert all(vs == rates)
  
file_path: lkpy/tests/test_matrix.py
imports: import scipy.sparse
  import pandas
  from lenskit.data import sparse_ratings
  from lenskit.util.test import ml_test
  from pytest import mark
label_desc: Use image node_id 1 for calling a global function with example usage: test_sparse_matrix(rng) without return types