Skip to content

Commit

Permalink
Merge pull request #13 from chinmayapancholi13/add_unittests
Browse files Browse the repository at this point in the history
[WIP] Adding unittests
  • Loading branch information
stephenhky committed Jul 17, 2017
2 parents e1a105b + e5c62ee commit 953bb6b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
9 changes: 8 additions & 1 deletion .travis.yml
@@ -1,3 +1,6 @@
sudo: false
dist: trusty
sudo: required
language: python
python:
- "2.7"
Expand All @@ -7,10 +10,14 @@ before_install:
- ./miniconda.sh -b
- export PATH=/home/travis/miniconda2/bin:$PATH
- conda update --yes conda
- sudo apt-get update
- sudo apt-get install libc6
install:
- conda create --yes -n shorttext-test python=$TRAVIS_PYTHON_VERSION pip numpy scipy
- source activate shorttext-test
- pip install unittest2
- pip install pytest
- pip install google-compute-engine
- pip install tensorflow==1.1.0
- pip install -U .
script: python shorttext_tests.py
script: python shorttext_tests.py
91 changes: 86 additions & 5 deletions shorttext_tests.py
@@ -1,11 +1,92 @@
import os
import unittest

class SampleTest(unittest.TestCase):
import shorttext


class TestVarNNEmbeddedVecClassifier(unittest.TestCase):
def setUp(self):
self.sample_var = True
if not os.path.isfile("test_w2v_model"):
os.system("wget https://raw.githubusercontent.com/chinmayapancholi13/shorttext_test_data/master/test_w2v_model") # download w2v model

self.w2v_model = shorttext.utils.load_word2vec_model("test_w2v_model", binary=False) # load word2vec model
self.trainclass_dict = shorttext.data.subjectkeywords() # load training data

def tearDown(self):
if os.path.isfile("test_w2v_model"):
os.remove("test_w2v_model") # delete downloaded w2v model

def testCNNWordEmbedWithoutGensim(self):
# create keras model using `CNNWordEmbed` class
keras_model = shorttext.classifiers.frameworks.CNNWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=False)

# create and train classifier using keras model constructed above
main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=False, vecsize=100)
main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2)

# compute classification score
score_vals = main_classifier.score('artificial intelligence')
self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0)

def testDoubleCNNWordEmbedWithGensim(self):
# create keras model using `DoubleCNNWordEmbed` class
keras_model = shorttext.classifiers.frameworks.DoubleCNNWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=True)

# create and train classifier using keras model constructed above
main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=True, vecsize=100)
main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2)

# compute classification score
score_vals = main_classifier.score('artificial intelligence')
self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0)

def testDoubleCNNWordEmbedWithoutGensim(self):
# create keras model using `DoubleCNNWordEmbed` class
keras_model = shorttext.classifiers.frameworks.DoubleCNNWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=False)

# create and train classifier using keras model constructed above
main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=False, vecsize=100)
main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2)

# compute classification score
score_vals = main_classifier.score('artificial intelligence')
self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0)

def testCNNWordEmbedWithGensim(self):
# create keras model using `CNNWordEmbed` class
keras_model = shorttext.classifiers.frameworks.CNNWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=True)

# create and train classifier using keras model constructed above
main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=True, vecsize=100)
main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2)

# compute classification score
score_vals = main_classifier.score('artificial intelligence')
self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0)

def testCLSTMWordEmbedWithoutGensim(self):
# create keras model using `CLSTMWordEmbed` class
keras_model = shorttext.classifiers.frameworks.CLSTMWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=False)

# create and train classifier using keras model constructed above
main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=False, vecsize=100)
main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2)

# compute classification score
score_vals = main_classifier.score('artificial intelligence')
self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0)

def testCLSTMWordEmbedWithGensim(self):
# create keras model using `CLSTMWordEmbed` class
keras_model = shorttext.classifiers.frameworks.CLSTMWordEmbed(wvmodel=self.w2v_model, nb_labels=len(self.trainclass_dict.keys()), vecsize=100, with_gensim=True)

# create and train classifier using keras model constructed above
main_classifier = shorttext.classifiers.VarNNEmbeddedVecClassifier(self.w2v_model, with_gensim=True, vecsize=100)
main_classifier.train(self.trainclass_dict, keras_model, nb_epoch=2)

def testSampleTestCase(self):
self.assertEqual(True, self.sample_var)
# compute classification score
score_vals = main_classifier.score('artificial intelligence')
self.assertTrue(score_vals['mathematics']>0.0 and score_vals['physics']>0.0 and score_vals['theology']>0.0)

if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 953bb6b

Please sign in to comment.