def testModelSaveAndLoad(self):
    # Keywords model uses the base class implementations of save/load methods.
    self.modelDir = "poke_model"
    model = ClassificationModelKeywords(modelDir=self.modelDir, verbosity=0)
    
    samples = {0: (["Pickachu"], numpy.array([0, 2, 2])),
               1: (["Eevee"], numpy.array([2])),
               2: (["Charmander"], numpy.array([0, 1, 1])),
               3: (["Abra"], numpy.array([1])),
               4: (["Squirtle"], numpy.array([1, 0, 1]))}

    patterns = model.encodeSamples(samples)
    for i in xrange(len(samples)):
      model.trainModel(i)

    output = [model.testModel(i) for i in xrange(len(patterns))]

    model.saveModel()

    loadedModel = ClassificationModel(verbosity=0).loadModel(self.modelDir)
    loadedModelOutput = [loadedModel.testModel(i)
                         for i in xrange(len(patterns))]
    
    for mClasses, lClasses in zip(output, loadedModelOutput):
      self.assertSequenceEqual(mClasses.tolist(), lClasses.tolist(), "Output "
          "classifcations from loaded model don't match original model's.")
    def testModelSaveAndLoad(self):
        # Keywords model uses the base class implementations of save/load methods.
        self.modelDir = "poke_model"
        model = ClassificationModelKeywords(modelDir=self.modelDir,
                                            verbosity=0)

        samples = {
            0: (["Pickachu"], numpy.array([0, 2, 2])),
            1: (["Eevee"], numpy.array([2])),
            2: (["Charmander"], numpy.array([0, 1, 1])),
            3: (["Abra"], numpy.array([1])),
            4: (["Squirtle"], numpy.array([1, 0, 1]))
        }

        patterns = model.encodeSamples(samples)
        for i in xrange(len(samples)):
            model.trainModel(i)

        output = [model.testModel(i) for i in xrange(len(patterns))]

        model.saveModel()

        loadedModel = ClassificationModel(verbosity=0).loadModel(self.modelDir)
        loadedModelOutput = [
            loadedModel.testModel(i) for i in xrange(len(patterns))
        ]

        for mClasses, lClasses in zip(output, loadedModelOutput):
            self.assertSequenceEqual(
                mClasses.tolist(), lClasses.tolist(), "Output "
                "classifcations from loaded model don't match original model's."
            )
  def testClassifyKeywordsSingleAndMultiClass(self):
    """Tests simple classification with multiple labels for Keywords model."""
    model = ClassificationModelKeywords()

    samples = {0: (["Pickachu"], numpy.array([0, 2, 2])),
               1: (["Eevee"], numpy.array([2])),
               2: (["Charmander"], numpy.array([0, 1, 1])),
               3: (["Abra"], numpy.array([1])),
               4: (["Squirtle"], numpy.array([1, 0, 1]))}

    patterns = model.encodeSamples(samples)
    for i in xrange(len(samples)):
      model.trainModel(i)

    output = [model.testModel(i) for i in xrange(len(patterns))]

    self.assertSequenceEqual(output[0].tolist(), [2, 0],
                             "Incorrect output for first sample.")
    self.assertSequenceEqual(output[1].tolist(), [2],
                             "Incorrect output for second sample.")
    self.assertSequenceEqual(output[2].tolist(), [1, 0],
                             "Incorrect output for third sample.")
    self.assertSequenceEqual(output[3].tolist(), [1],
                             "Incorrect output for fourth sample.")

    # Test the order of class labels doesn't matter when training.
    self.assertTrue(numpy.allclose(output[2], output[4]),
                    "Outputs for samples 2 and 4 should be identical.")
    def testClassifyKeywordsSingleAndMultiClass(self):
        """Tests simple classification with multiple labels for Keywords model."""
        model = ClassificationModelKeywords()

        samples = {
            0: (["Pickachu"], numpy.array([0, 2, 2])),
            1: (["Eevee"], numpy.array([2])),
            2: (["Charmander"], numpy.array([0, 1, 1])),
            3: (["Abra"], numpy.array([1])),
            4: (["Squirtle"], numpy.array([1, 0, 1]))
        }

        patterns = model.encodeSamples(samples)
        for i in xrange(len(samples)):
            model.trainModel(i)

        output = [model.testModel(i) for i in xrange(len(patterns))]

        self.assertSequenceEqual(output[0].tolist(), [2, 0],
                                 "Incorrect output for first sample.")
        self.assertSequenceEqual(output[1].tolist(), [2],
                                 "Incorrect output for second sample.")
        self.assertSequenceEqual(output[2].tolist(), [1, 0],
                                 "Incorrect output for third sample.")
        self.assertSequenceEqual(output[3].tolist(), [1],
                                 "Incorrect output for fourth sample.")

        # Test the order of class labels doesn't matter when training.
        self.assertTrue(numpy.allclose(output[2], output[4]),
                        "Outputs for samples 2 and 4 should be identical.")