From ae3269f7da15d5854c8ad5334f0b180d963d6d9e Mon Sep 17 00:00:00 2001
From: bbauvin <baptiste.bauvin@centrale-marseille.fr>
Date: Wed, 25 Oct 2017 19:02:47 -0400
Subject: [PATCH] Added tests and refactored

---
 Code/Exec.py                                  | 11 ++++----
 .../MonoviewClassifiers/Adaboost.py           | 10 +++++--
 .../MultiviewClassifiers/__init__.py          |  1 -
 .../MonoMultiViewClassifiers/utils/Dataset.py |  2 +-
 Code/Tests.py                                 |  6 ++++
 .../Test_MonoviewClassifiers/__init__.py      |  0
 .../Test_MonoviewClassifiers/test_Adaboost.py | 28 +++++++++++++++++++
 Code/Tests/Test_utils/test_execution.py       | 14 +++++-----
 Code/Tests/test_ExecClassif.py                | 13 +++++++--
 Code/__init__.py                              |  2 +-
 Code/Versions.py => Versions.py               |  0
 11 files changed, 67 insertions(+), 20 deletions(-)
 create mode 100644 Code/Tests.py
 create mode 100644 Code/Tests/Test_MonoviewClassifiers/__init__.py
 create mode 100644 Code/Tests/Test_MonoviewClassifiers/test_Adaboost.py
 rename Code/Versions.py => Versions.py (100%)

diff --git a/Code/Exec.py b/Code/Exec.py
index 8bf6c576..72b121ad 100644
--- a/Code/Exec.py
+++ b/Code/Exec.py
@@ -1,8 +1,9 @@
-from Versions import testVersions
-testVersions()
-import sys
+if __name__=="__main__":
 
-from MonoMultiViewClassifiers import ExecClassif
 
-ExecClassif.execClassif(sys.argv[1:])
+    import sys
+
+    from MonoMultiViewClassifiers import ExecClassif
+    ExecClassif.execClassif(sys.argv[1:])
+
 
diff --git a/Code/MonoMultiViewClassifiers/MonoviewClassifiers/Adaboost.py b/Code/MonoMultiViewClassifiers/MonoviewClassifiers/Adaboost.py
index b5f75f49..1fa0b546 100644
--- a/Code/MonoMultiViewClassifiers/MonoviewClassifiers/Adaboost.py
+++ b/Code/MonoMultiViewClassifiers/MonoviewClassifiers/Adaboost.py
@@ -22,8 +22,9 @@ def canProbas():
 
 
 def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
+    """Used to fit the monoview classifier with the args stored in kwargs"""
     num_estimators = int(kwargs['0'])
-    base_estimators = DecisionTreeClassifier()  # kwargs['1']
+    base_estimators = DecisionTreeClassifier()
     classifier = AdaBoostClassifier(n_estimators=num_estimators, base_estimator=base_estimators,
                                     random_state=randomState)
     classifier.fit(DATASET, CLASS_LABELS)
@@ -31,6 +32,7 @@ def fit(DATASET, CLASS_LABELS, randomState, NB_CORES=1, **kwargs):
 
 
 def paramsToSet(nIter, randomState):
+    """Used for weighted linear early fusion to generate random search sets"""
     paramsSet = []
     for _ in range(nIter):
         paramsSet.append([randomState.randint(1, 15), DecisionTreeClassifier()])
@@ -38,12 +40,15 @@ def paramsToSet(nIter, randomState):
 
 
 def getKWARGS(kwargsList):
+    """Used to format kwargs for the parsed args"""
     kwargsDict = {}
     for (kwargName, kwargValue) in kwargsList:
         if kwargName == "CL_Adaboost_n_est":
             kwargsDict['0'] = int(kwargValue)
         elif kwargName == "CL_Adaboost_b_est":
             kwargsDict['1'] = kwargValue
+        else:
+            raise(ValueError, "Wrong arguments served to Adaboost")
     return kwargsDict
 
 
@@ -74,7 +79,7 @@ def randomizedSearch(X_train, y_train, randomState, outputFileName, KFolds=4, me
 
 
 def getConfig(config):
-    if type(config) not in [list, dict]:
+    if type(config) not in [list, dict]: # Used in late fusion when config is a classifier
         return "\n\t\t- Adaboost with num_esimators : " + str(config.n_estimators) + ", base_estimators : " + str(
             config.base_estimator)
     else:
@@ -84,6 +89,7 @@ def getConfig(config):
             return "\n\t\t- Adaboost with num_esimators : " + str(config["0"]) + ", base_estimators : " + str(
                 config["1"])
 
+
 def getInterpret(classifier, directory):
     interpretString = getFeatureImportance(classifier, directory)
     return interpretString
\ No newline at end of file
diff --git a/Code/MonoMultiViewClassifiers/MultiviewClassifiers/__init__.py b/Code/MonoMultiViewClassifiers/MultiviewClassifiers/__init__.py
index 3f1a1d3e..2f08d4c6 100644
--- a/Code/MonoMultiViewClassifiers/MultiviewClassifiers/__init__.py
+++ b/Code/MonoMultiViewClassifiers/MultiviewClassifiers/__init__.py
@@ -3,7 +3,6 @@ import os
 for module in os.listdir(os.path.dirname(os.path.realpath(__file__))):
     if module == '__init__.py' or module[-3:] == '.py' or module[-4:] == '.pyc' or module == '__pycache__' :
         continue
-    print(module)
     __import__(module, locals(), globals(), [], 1)
 del module
 del os
diff --git a/Code/MonoMultiViewClassifiers/utils/Dataset.py b/Code/MonoMultiViewClassifiers/utils/Dataset.py
index 75866388..9b58de26 100644
--- a/Code/MonoMultiViewClassifiers/utils/Dataset.py
+++ b/Code/MonoMultiViewClassifiers/utils/Dataset.py
@@ -107,7 +107,7 @@ def confirm(resp=True, timeout=15):
 
 def input_(timeout=15):
     """used as a UI to stop if too much HDD space will be used"""
-    print("You have " + str(timeout) + " seconds to stop the script by typing n")
+    print("You have " + str(timeout) + " seconds to stop the dataset copy by typing n")
     i, o, e = select.select([sys.stdin], [], [], timeout)
     if i:
         return sys.stdin.readline().strip()
diff --git a/Code/Tests.py b/Code/Tests.py
new file mode 100644
index 00000000..318166e2
--- /dev/null
+++ b/Code/Tests.py
@@ -0,0 +1,6 @@
+# if __name__=="__main__":
+#     import unittest
+#     from .Tests.test_ExecClassif import suite
+#
+#     runner = unittest.TextTestRunner()
+#     runner.run(suite())
\ No newline at end of file
diff --git a/Code/Tests/Test_MonoviewClassifiers/__init__.py b/Code/Tests/Test_MonoviewClassifiers/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/Code/Tests/Test_MonoviewClassifiers/test_Adaboost.py b/Code/Tests/Test_MonoviewClassifiers/test_Adaboost.py
new file mode 100644
index 00000000..0241cc3d
--- /dev/null
+++ b/Code/Tests/Test_MonoviewClassifiers/test_Adaboost.py
@@ -0,0 +1,28 @@
+import unittest
+import numpy as np
+
+from ...MonoMultiViewClassifiers.MonoviewClassifiers import Adaboost
+
+
+class Test_fit(unittest.TestCase):
+
+    def setUp(self):
+        self.random_state = np.random.RandomState(42)
+        self.dataset = self.random_state.randint(0,100,(10,5))
+        self.labels = self.random_state.randint(0,2,10)
+        self.kwargs = {"0":5}
+        self.classifier = Adaboost.fit(self.dataset, self.labels, 42, NB_CORES=1, **self.kwargs)
+
+    def test_fit_kwargs_string(self):
+        self.kwargs = {"0":"5"}
+        classifier = Adaboost.fit(self.dataset, self.labels, 42, NB_CORES=1, **self.kwargs)
+        self.assertEqual(classifier.n_estimators, 5)
+
+    def test_fit_kwargs_int(self):
+        self.kwargs = {"0":5}
+        classifier = Adaboost.fit(self.dataset, self.labels, 42, NB_CORES=1, **self.kwargs)
+        self.assertEqual(classifier.n_estimators, 5)
+
+    def test_fit_labels(self):
+        predicted_labels = self.classifier.predict(self.dataset)
+        np.testing.assert_array_equal(predicted_labels, self.labels)
\ No newline at end of file
diff --git a/Code/Tests/Test_utils/test_execution.py b/Code/Tests/Test_utils/test_execution.py
index 39984b89..c26700e2 100644
--- a/Code/Tests/Test_utils/test_execution.py
+++ b/Code/Tests/Test_utils/test_execution.py
@@ -6,7 +6,7 @@ import numpy as np
 
 from sklearn.model_selection import StratifiedShuffleSplit
 
-from MonoMultiViewClassifiers.utils import execution
+from ...MonoMultiViewClassifiers.utils import execution
 
 
 class Test_parseTheArgs(unittest.TestCase):
@@ -23,16 +23,16 @@ class Test_initRandomState(unittest.TestCase):
 
     def test_random_state_42(self):
         randomState_42 = np.random.RandomState(42)
-        randomState = execution.initRandomState("42", "Tests/temp_tests/")
-        os.remove("Tests/temp_tests/randomState.pickle")
+        randomState = execution.initRandomState("42", "Code/Tests/temp_tests/")
+        os.remove("Code/Tests/temp_tests/randomState.pickle")
         np.testing.assert_array_equal(randomState.beta(1,100,100),
                                       randomState_42.beta(1,100,100))
 
     def test_random_state_pickle(self):
-        randomState_to_pickle = execution.initRandomState(None, "Tests/temp_tests/")
-        pickled_randomState = execution.initRandomState("Tests/temp_tests/randomState.pickle",
-                                                        "Tests/temp_tests/")
-        os.remove("Tests/temp_tests/randomState.pickle")
+        randomState_to_pickle = execution.initRandomState(None, "Code/Tests/temp_tests/")
+        pickled_randomState = execution.initRandomState("Code/Tests/temp_tests/randomState.pickle",
+                                                        "Code/Tests/temp_tests/")
+        os.remove("Code/Tests/temp_tests/randomState.pickle")
         np.testing.assert_array_equal(randomState_to_pickle.beta(1,100,100),
                                       pickled_randomState.beta(1,100,100))
 
diff --git a/Code/Tests/test_ExecClassif.py b/Code/Tests/test_ExecClassif.py
index 8b4cd14b..fdec1b59 100644
--- a/Code/Tests/test_ExecClassif.py
+++ b/Code/Tests/test_ExecClassif.py
@@ -1,7 +1,7 @@
 import unittest
 import argparse
 
-from MonoMultiViewClassifiers import ExecClassif
+from ..MonoMultiViewClassifiers import ExecClassif
 
 
 class Test_initBenchmark(unittest.TestCase):
@@ -23,12 +23,12 @@ class Test_initMonoviewArguments(unittest.TestCase):
 
     def test_initMonoviewArguments_no_monoview(self):
         benchmark = {"Monoview":{}, "Multiview":{}}
-        arguments = ExecClassif.initMonoviewArguments(benchmark, {}, [], [], None, 0, {})
+        arguments = ExecClassif.initMonoviewExps(benchmark, {}, [], None, 0, {})
         self.assertEqual(arguments, {})
 
     def test_initMonoviewArguments_empty(self):
         benchmark = {"Monoview":{}, "Multiview":{}}
-        arguments = ExecClassif.initMonoviewArguments(benchmark, {}, [], [], None, 0, {})
+        arguments = ExecClassif.initMonoviewExps(benchmark, {}, [], None, 0, {})
 
 class Essai(unittest.TestCase):
 
@@ -203,3 +203,10 @@ class Essai(unittest.TestCase):
                                      help='Determine which method to use to select the monoview classifiers',
                                      default="intersect")
         self.args = parser.parse_args([])
+
+
+def suite():
+    suite = unittest.TestSuite()
+    suite.addTest(Test_initBenchmark('test_initKWARGSFunc_no_monoview'))
+    # suite.addTest(WidgetTestCase('test_widget_resize'))
+    return suite
\ No newline at end of file
diff --git a/Code/__init__.py b/Code/__init__.py
index 27925d1c..4ed66710 100644
--- a/Code/__init__.py
+++ b/Code/__init__.py
@@ -1,2 +1,2 @@
 from . import MonoMultiViewClassifiers, Tests, Exec
-import pdb;pdb.set_trace()
\ No newline at end of file
+# import pdb;pdb.set_trace()
\ No newline at end of file
diff --git a/Code/Versions.py b/Versions.py
similarity index 100%
rename from Code/Versions.py
rename to Versions.py
-- 
GitLab