code: def test_persistence(self):
      # Test persistence without using `smartirs`
      fname = get_tmpfile("gensim_models.tst")
      model = tfidfmodel.TfidfModel(self.corpus, normalize=True)
      model.save(fname)
      model2 = tfidfmodel.TfidfModel.load(fname)
      self.assertTrue(model.idfs == model2.idfs)
      tstvec = [corpus[1], corpus[2]]
      self.assertTrue(np.allclose(model[tstvec[0]], model2[tstvec[0]]))
      self.assertTrue(np.allclose(model[tstvec[1]], model2[tstvec[1]]))
      self.assertTrue(
          np.allclose(model[[]], model2[[]])
      )  # try projecting an empty vector
  
      # Test persistence with using `smartirs`
      fname = get_tmpfile("gensim_models_smartirs.tst")
      model = tfidfmodel.TfidfModel(self.corpus, smartirs="nfc")
      model.save(fname)
      model2 = tfidfmodel.TfidfModel.load(fname)
      self.assertTrue(model.idfs == model2.idfs)
      tstvec = [corpus[1], corpus[2]]
      self.assertTrue(np.allclose(model[tstvec[0]], model2[tstvec[0]]))
      self.assertTrue(np.allclose(model[tstvec[1]], model2[tstvec[1]]))
      self.assertTrue(
          np.allclose(model[[]], model2[[]])
      )  # try projecting an empty vector
  
      # Test persistence between Gensim v3.2.0 and current model.
      model3 = tfidfmodel.TfidfModel(self.corpus, smartirs="nfc")
      model4 = tfidfmodel.TfidfModel.load(datapath("tfidf_model.tst"))
      idfs3 = [model3.idfs[key] for key in sorted(model3.idfs.keys())]
      idfs4 = [model4.idfs[key] for key in sorted(model4.idfs.keys())]
      self.assertTrue(np.allclose(idfs3, idfs4))
      tstvec = [corpus[1], corpus[2]]
      self.assertTrue(np.allclose(model3[tstvec[0]], model4[tstvec[0]]))
      self.assertTrue(np.allclose(model3[tstvec[1]], model4[tstvec[1]]))
      self.assertTrue(
          np.allclose(model3[[]], model4[[]])
      )  # try projecting an empty vector
  
      # Test persistence with using pivoted normalization
      fname = get_tmpfile("gensim_models_smartirs.tst")
      model = tfidfmodel.TfidfModel(self.corpus, pivot=0, slope=1)
      model.save(fname)
      model2 = tfidfmodel.TfidfModel.load(fname, mmap=None)
      self.assertTrue(model.idfs == model2.idfs)
      tstvec = [corpus[1], corpus[2]]
      self.assertTrue(np.allclose(model[tstvec[0]], model2[tstvec[0]]))
      self.assertTrue(np.allclose(model[tstvec[1]], model2[tstvec[1]]))
  
      # Test persistence between Gensim v3.2.0 and pivoted normalization compressed model.
      model3 = tfidfmodel.TfidfModel(self.corpus, pivot=0, slope=1)
      model4 = tfidfmodel.TfidfModel.load(datapath("tfidf_model.tst"))
      idfs3 = [model3.idfs[key] for key in sorted(model3.idfs.keys())]
      idfs4 = [model4.idfs[key] for key in sorted(model4.idfs.keys())]
      self.assertTrue(np.allclose(idfs3, idfs4))
      tstvec = [corpus[1], corpus[2]]
      self.assertTrue(np.allclose(model3[tstvec[0]], model4[tstvec[0]]))
      self.assertTrue(np.allclose(model3[tstvec[1]], model4[tstvec[1]]))
  
file_path: gensim/gensim/test/test_tfidfmodel.py
imports: import logging
  import unittest
  import numpy
  from gensim.corpora.mmcorpus import MmCorpus
  from gensim.models import tfidfmodel
  from gensim.test.utils import datapath, get_tmpfile, common_dictionary, common_corpus
  from gensim.corpora import Dictionary
label_desc: Use image node_id 4 for calling the TestTfidfModel obj's underlying member method code with example usage: obj.test_persistence() without return types