code: def test_ii_large_models():
      "Several tests of large trained I-I models"
      _log.info("training limited model")
      MODEL_SIZE = 100
      algo_lim = knn.ItemItem(30, save_nbrs=MODEL_SIZE)
      algo_lim.fit(ml_ratings)
  
      _log.info("training unbounded model")
      algo_ub = knn.ItemItem(30)
      algo_ub.fit(ml_ratings)
  
      _log.info("testing models")
      assert all(np.logical_not(np.isnan(algo_lim.sim_matrix_.values)))
      assert all(algo_lim.sim_matrix_.values > 0)
      # a little tolerance
      assert all(algo_lim.sim_matrix_.values < 1 + 1.0e-6)
  
      means = ml_ratings.groupby("item").rating.mean()
      assert means[algo_lim.item_index_].values == approx(
          algo_lim.item_means_
      )
  
      assert all(np.logical_not(np.isnan(algo_ub.sim_matrix_.values)))
      assert all(algo_ub.sim_matrix_.values > 0)
      # a little tolerance
      assert all(algo_ub.sim_matrix_.values < 1 + 1.0e-6)
  
      means = ml_ratings.groupby("item").rating.mean()
      assert means[algo_ub.item_index_].values == approx(
          algo_ub.item_means_
      )
  
      mc_rates = (
          ml_ratings.set_index("item")
          .join(pd.DataFrame({"item_mean": means}))
          .assign(rating=lambda df: df.rating - df.item_mean)
      )
  
      mat_lim = algo_lim.sim_matrix_.to_scipy()
      mat_ub = algo_ub.sim_matrix_.to_scipy()
  
      _log.info("checking a sample of neighborhoods")
      items = pd.Series(algo_ub.item_index_)
      items = items[algo_ub.item_counts_ > 0]
      for i in items.sample(50):
          ipos = algo_ub.item_index_.get_loc(i)
          _log.debug("checking item %d at position %d", i, ipos)
          assert ipos == algo_lim.item_index_.get_loc(i)
          irates = mc_rates.loc[[i], :].set_index("user").rating
  
          ub_row = mat_ub.getrow(ipos)
          b_row = mat_lim.getrow(ipos)
          assert b_row.nnz <= MODEL_SIZE
          assert all(pd.Series(b_row.indices).isin(ub_row.indices))
  
          # it should be sorted !
          # check this by diffing the row values, and make sure they're negative
          assert all(np.diff(b_row.data) < 1.0e-6)
          assert all(np.diff(ub_row.data) < 1.0e-6)
  
          # spot-check some similarities
          for n in pd.Series(ub_row.indices).sample(
              min(10, len(ub_row.indices))
          ):
              n_id = algo_ub.item_index_[n]
              n_rates = mc_rates.loc[n_id, :].set_index("user").rating
              ir, nr = irates.align(n_rates, fill_value=0)
              cor = ir.corr(nr)
              assert mat_ub[ipos, n] == approx(cor)
  
          # short rows are equal
          if b_row.nnz < MODEL_SIZE:
              _log.debug("short row of length %d", b_row.nnz)
              assert b_row.nnz == ub_row.nnz
              ub_row.sort_indices()
              b_row.sort_indices()
              assert b_row.data == approx(ub_row.data)
              continue
  
          # row is truncated - check that truncation is correct
          ub_nbrs = pd.Series(
              ub_row.data, algo_ub.item_index_[ub_row.indices]
          )
          b_nbrs = pd.Series(
              b_row.data, algo_lim.item_index_[b_row.indices]
          )
  
          assert len(ub_nbrs) >= len(b_nbrs)
          assert len(b_nbrs) <= MODEL_SIZE
          assert all(b_nbrs.index.isin(ub_nbrs.index))
          # the similarities should be equal!
          b_match, ub_match = b_nbrs.align(ub_nbrs, join="inner")
          assert all(b_match == b_nbrs)
          assert b_match.values == approx(ub_match.values)
          assert b_nbrs.max() == approx(ub_nbrs.max())
          if len(ub_nbrs) > MODEL_SIZE:
              assert len(b_nbrs) == MODEL_SIZE
              ub_shrink = ub_nbrs.nlargest(MODEL_SIZE)
              # the minimums should be equal
              assert ub_shrink.min() == approx(b_nbrs.min())
              # everything above minimum value should be the same set of items
              ubs_except_min = ub_shrink[ub_shrink > b_nbrs.min()]
              assert all(ubs_except_min.index.isin(b_nbrs.index))
  
imports: from lenskit import ConfigWarning, DataWarning
  from lenskit.algorithms import Recommender
  from lenskit.algorithms.basic import Fallback
  from lenskit.algorithms.bias import Bias
  from lenskit import batch
  import lenskit.algorithms.item_knn
  from lenskit.sharing import persist
  from lenskit.util.parallel import run_sp
  from lenskit.util import clone
  import gc
  from pathlib import Path
  import logging
  import pickle
  import pandas
  import numpy
  from scipy import linalg
  import csr.kernel
  import pytest
  from pytest import approx, mark, fixture
  import lenskit.util.test
  from lenskit.util.parallel import invoker
  from lenskit.util import Stopwatch
label_desc: Use image node_id 17 for calling a global function with example usage: test_ii_large_models() without return types