#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 Radim Rehurek <radimrehurek@seznam.cz>
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html

"""
Automated tests for checking transformation algorithms (the models package).
"""


import logging
import unittest

import numpy as np

from gensim.corpora.mmcorpus import MmCorpus
from gensim.models import rpmodel
from gensim import matutils
from gensim.test.utils import datapath, get_tmpfile


class TestRpModel(unittest.TestCase):
    def setUp(self):
        self.corpus = MmCorpus(datapath('testcorpus.mm'))

    def test_transform(self):
        # create the transformation model
        # HACK; set fixed seed so that we always get the same random matrix (and can compare against expected results)
        np.random.seed(13)
        model = rpmodel.RpModel(self.corpus, num_topics=2)

        # transform one document
        doc = list(self.corpus)[0]
        transformed = model[doc]
        vec = matutils.sparse2full(transformed, 2)  # convert to dense vector, for easier equality tests

        expected = np.array([-0.70710677, 0.70710677])
        self.assertTrue(np.allclose(vec, expected))  # transformed entries must be equal up to sign

    def test_persistence(self):
        fname = get_tmpfile('gensim_models.tst')
        model = rpmodel.RpModel(self.corpus, num_topics=2)
        model.save(fname)
        model2 = rpmodel.RpModel.load(fname)
        self.assertEqual(model.num_topics, model2.num_topics)
        self.assertTrue(np.allclose(model.projection, model2.projection))
        tstvec = []
        self.assertTrue(np.allclose(model[tstvec], model2[tstvec]))  # try projecting an empty vector

    def test_persistence_compressed(self):
        fname = get_tmpfile('gensim_models.tst.gz')
        model = rpmodel.RpModel(self.corpus, num_topics=2)
        model.save(fname)
        model2 = rpmodel.RpModel.load(fname, mmap=None)
        self.assertEqual(model.num_topics, model2.num_topics)
        self.assertTrue(np.allclose(model.projection, model2.projection))
        tstvec = []
        self.assertTrue(np.allclose(model[tstvec], model2[tstvec]))  # try projecting an empty vector


if __name__ == '__main__':
    logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)
    unittest.main()

[(63, 'logging.basicConfig', 'logging.basicConfig', False, 'import logging\n'), (64, 'unittest.main', 'unittest.main', False, 'import unittest\n'), (30, 'numpy.random.seed', 'np.random.seed', True, 'import numpy as np\n'), (31, 'gensim.models.rpmodel.RpModel', 'rpmodel.RpModel', False, 'from gensim.models import rpmodel\n'), (36, 'gensim.matutils.sparse2full', 'matutils.sparse2full', False, 'from gensim import matutils\n'), (38, 'numpy.array', 'np.array', True, 'import numpy as np\n'), (42, 'gensim.test.utils.get_tmpfile', 'get_tmpfile', False, 'from gensim.test.utils import datapath, get_tmpfile\n'), (43, 'gensim.models.rpmodel.RpModel', 'rpmodel.RpModel', False, 'from gensim.models import rpmodel\n'), (45, 'gensim.models.rpmodel.RpModel.load', 'rpmodel.RpModel.load', False, 'from gensim.models import rpmodel\n'), (52, 'gensim.test.utils.get_tmpfile', 'get_tmpfile', False, 'from gensim.test.utils import datapath, get_tmpfile\n'), (53, 'gensim.models.rpmodel.RpModel', 'rpmodel.RpModel', False, 'from gensim.models import rpmodel\n'), (55, 'gensim.models.rpmodel.RpModel.load', 'rpmodel.RpModel.load', False, 'from gensim.models import rpmodel\n'), (25, 'gensim.test.utils.datapath', 'datapath', False, 'from gensim.test.utils import datapath, get_tmpfile\n'), (39, 'numpy.allclose', 'np.allclose', True, 'import numpy as np\n'), (47, 'numpy.allclose', 'np.allclose', True, 'import numpy as np\n'), (49, 'numpy.allclose', 'np.allclose', True, 'import numpy as np\n'), (57, 'numpy.allclose', 'np.allclose', True, 'import numpy as np\n'), (59, 'numpy.allclose', 'np.allclose', True, 'import numpy as np\n')]