diff --git a/Code/Exec.py b/Code/Exec.py index 8bf6c5769aafc5fb9a13fb4cb60c19c075fabdb1..72b121adc212294cf140c873f5adefbf3f278561 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 b5f75f49cb1194e52b364edfa889643bc633b7e4..1fa0b546c8af374e91b0badd0b42115aed9bbb8c 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 3f1a1d3e7fa1952ecfbbcdb7da47057e05e1fcdf..2f08d4c626a5f86381ca7217f6320107a4f9d53c 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 75866388d0b1f2210a8396b64fb52385bbdbb435..9b58de26f74565a432540792476283d69c2d3d2b 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 0000000000000000000000000000000000000000..318166e2d0d4599c263796eca66eeaca1e283a52 --- /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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/Code/Tests/Test_MonoviewClassifiers/test_Adaboost.py b/Code/Tests/Test_MonoviewClassifiers/test_Adaboost.py new file mode 100644 index 0000000000000000000000000000000000000000..0241cc3d830ac2a116f56c0bce9070e21b0ccdf2 --- /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 39984b898abdaf9149b9c886258c917319b04b18..c26700e2b0444db26835492e63e7e7b03f259e91 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 8b4cd14b2e09d3794305979c0fe4b24000447aea..fdec1b59507927fc3ecb6f9fdcc30dd6e5034d28 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 27925d1c65ac8c0c097fe0a5e4efdaa4cee6b060..4ed6671084a85435bcfbf14f6adde84ea506e7b0 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