def setUp(self): super(RegistryTest, self).setUp() self.test_directory = os.path.join( os.path.dirname(os.path.abspath(__file__)).split("test")[0], "test") self.custom_folder = os.path.join(self.test_directory, "base_tests/custom_elements/") self.registry = PhotonRegistry(self.custom_folder)
def test_PhotonRegistry_load_json(): # too many args r = PhotonRegistry() assert (len( r.load_json( PhotonRegistry.PHOTON_REGISTRIES[PhotonRegistry.PhotonCoreID])) == T_PhotonCoreLen and len( r.load_json(PhotonRegistry.PHOTON_REGISTRIES[ PhotonRegistry.PhotonClusterID])) == T_PhotonClusterLen and len( r.load_json(PhotonRegistry.PHOTON_REGISTRIES[ PhotonRegistry.PhotonNeuroID])) == T_PhotonNeuroLen)
def test_PhotonRegistry_3arg(): # too many args name = "CustomElements" element = PhotonRegistry(name) assert ((element.custom_elements == {}) & (element.custom_elements_folder == name) & (element.custom_elements_file == name + "/" + name + ".json"))
def test_PhotonRegistry_0arg(): # too many args assert ((PhotonRegistry().custom_elements == None) & (PhotonRegistry().custom_elements_folder == None) & (PhotonRegistry().custom_elements_file == None))
def test_PhotonRegistry_pos_arg_erroe(): # too many args name = "myhype" with pytest.raises(TypeError): assert PhotonRegistry(name, 1, 2, 3)
import os from sklearn.datasets import load_breast_cancer from sklearn.model_selection import KFold from photonai.base import Hyperpipe, PipelineElement, OutputSettings, PhotonRegistry from photonai.optimization import IntegerRange # REGISTER ELEMENT base_folder = os.path.dirname(os.path.abspath(__file__)) custom_elements_folder = os.path.join(base_folder, "custom_elements") registry = PhotonRegistry(custom_elements_folder=custom_elements_folder) registry.register( photon_name="MyCustomEstimator", class_str="custom_estimator.CustomEstimator", element_type="Estimator", ) registry.register( photon_name="MyCustomTransformer", class_str="custom_transformer.CustomTransformer", element_type="Transformer", ) registry.activate() # WE USE THE BREAST CANCER SET FROM SKLEARN X, y = load_breast_cancer(True) settings = OutputSettings(project_folder="./tmp/")
import os from sklearn.datasets import load_breast_cancer from sklearn.model_selection import KFold from photonai.base import Hyperpipe, PipelineElement, OutputSettings, PhotonRegistry from photonai.optimization import IntegerRange # REGISTER ELEMENT base_folder = os.path.dirname(os.path.abspath(__file__)) custom_elements_folder = os.path.join(base_folder, 'custom_elements') registry = PhotonRegistry(custom_elements_folder=custom_elements_folder) registry.register(photon_name='MyCustomEstimator', class_str='custom_estimator.CustomEstimator', element_type='Estimator') registry.register(photon_name='MyCustomTransformer', class_str='custom_transformer.CustomTransformer', element_type='Transformer') registry.activate() # WE USE THE BREAST CANCER SET FROM SKLEARN X, y = load_breast_cancer(return_X_y=True) settings = OutputSettings(project_folder='./tmp/') # DESIGN YOUR PIPELINE my_pipe = Hyperpipe( 'custom_estimator_pipe',
def register_photonai_neuro(): current_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "photonai_neuro.json") reg = PhotonRegistry() reg.add_module(current_path)
def test_PhotonRegistry_get_package_info(): # too many args r = PhotonRegistry() assert (len(r.get_package_info()) == sum(PHOTON_pkgs_len))
def test_PhotonRegistry_get_element_metadata_SmoothImages(): # too many args r = PhotonRegistry() md = r.load_json( PhotonRegistry.PHOTON_REGISTRIES[PhotonRegistry.PhotonNeuroID]) assert r.get_element_metadata('SmoothImages', md)[3] == SM_MD[2]
def test_PhotonRegistry_get_element_metadata_PCA(): # too many args r = PhotonRegistry() md = r.load_json( PhotonRegistry.PHOTON_REGISTRIES[PhotonRegistry.PhotonCoreID]) assert r.get_element_metadata('PCA', md)[0:2] == PCA_MD[0:2]
def test_PhotonRegistry_get_element_metadata_KMEANS_ERROR(): # too many args r = PhotonRegistry() md = r.load_json( PhotonRegistry.PHOTON_REGISTRIES[PhotonRegistry.PhotonCoreID]) with pytest.raises(PhotonaiError): assert r.get_element_metadata('FRED', md)[0:2] == GM_MD[0:2]
def test_PhotonRegistry_get_element_metadata_KMEANS(): # too many args r = PhotonRegistry() md = r.load_json(PHOTON_pkgs[1]) assert r.get_element_metadata('KMeans', md)[0] == KMEANS_MD[0]
def test_PhotonRegistry_PHOTON_REGISTRIES(): # too many args PhotonRegistry().reset() assert PhotonRegistry.PHOTON_REGISTRIES[0:2] == [ "PhotonCore", "PhotonNeuro" ]
def test_PhotonRegistry_get_package_info_error(): # too many args r = PhotonRegistry() assert (len(r.get_package_info('fred')) == 0)
class RegistryTest(PhotonBaseTest): @classmethod def setUpClass(cls) -> None: cls.file = __file__ super(RegistryTest, cls).setUpClass() def setUp(self): super(RegistryTest, self).setUp() self.test_directory = os.path.join(os.path.dirname(os.path.abspath(__file__)).split('test')[0], 'test') self.custom_folder = os.path.join(self.test_directory, 'base_tests/custom_elements/') self.registry = PhotonRegistry(self.custom_folder) def tearDown(self): super(RegistryTest, self).tearDown() if os.path.isfile(os.path.join(self.custom_folder, 'CustomElements.json')): os.remove(os.path.join(self.custom_folder, 'CustomElements.json')) def test_register_without_custom_folder(self): registry = PhotonRegistry() with self.assertRaises(ValueError): registry.register('SomeName', 'not_existing_file.SomeName', 'Estimator') def test_python_file_not_in_custom_folder(self): with self.assertRaises(FileNotFoundError): self.registry.register('SomeName', 'not_existing_file.SomeName', 'Estimator') def test_list_available_elements(self): self.registry.list_available_elements() self.registry.info('PCA') self.registry.info('NotExistingEstimator') def test_register_element(self): with self.assertRaises(ValueError): self.registry.register('MyCustomEstimator', 'custom_estimator.CustomEstimator', 'WrongType') self.registry.register('MyCustomEstimator', 'custom_estimator.CustomEstimator', 'Estimator') self.registry.activate() settings = OutputSettings(save_output=False, project_folder='./tmp/') # DESIGN YOUR PIPELINE pipe = Hyperpipe('custom_estimator_pipe', optimizer='random_grid_search', optimizer_params={'n_configurations': 2}, metrics=['accuracy', 'precision', 'recall', 'balanced_accuracy'], best_config_metric='accuracy', outer_cv=KFold(n_splits=2), inner_cv=KFold(n_splits=2), verbosity=1, output_settings=settings) pipe += PipelineElement('MyCustomEstimator') pipe.fit(np.random.randn(30, 30), np.random.randint(0, 2, 30)) self.registry.delete('MyCustomEstimator') os.remove(os.path.join(self.custom_folder, 'CustomElements.json')) def test_estimator_check_during_register(self): with self.assertRaises(NotImplementedError): self.registry.register('MyCustomEstimatorNoFit', 'custom_estimator.CustomEstimatorNoFit', 'Estimator') with self.assertRaises(NotImplementedError): self.registry.register('MyCustomEstimatorNoPredict', 'custom_estimator.CustomEstimatorNoPredict', 'Estimator') with self.assertRaises(NotImplementedError): self.registry.register('MyCustomEstimatorNoEstimatorType', 'custom_estimator.CustomEstimatorNoEstimatorType', 'Estimator') with self.assertRaises(NotImplementedError): self.registry.register('MyCustomEstimatorNotReturningSelf', 'custom_estimator.CustomEstimatorNotReturningSelf', 'Estimator') e = self.registry.register('MyCustomEstimatorReturningFalsePredictions', 'custom_estimator.CustomEstimatorReturningFalsePredictions', 'Estimator') self.assertIsInstance(e, ValueError) e = self.registry.register('MyCustomEstimatorNotWorking', 'custom_estimator.CustomEstimatorNotWorking', 'Estimator') self.assertIsInstance(e, ValueError) os.remove(os.path.join(self.custom_folder, 'CustomElements.json')) def test_transformer_needs_covariates(self): self.registry.register('MyCustomTransformerNeedsCovariates', 'custom_transformer.CustomTransformerNeedsCovariates', 'Transformer') with self.assertRaises(ValueError): self.registry.register('MyCustomTransformerNeedsCovariatesWrongInterface', 'custom_transformer.CustomTransformerNeedsCovariatesWrongInterface', 'Transformer') def test_transformer_needs_y(self): self.registry.register('MyCustomTransformerNeedsY', 'custom_transformer.CustomTransformerNeedsY', 'Transformer') with self.assertRaises(ValueError): self.registry.register('MyCustomTransformerNeedsYWrongInterface', 'custom_transformer.CustomTransformerNeedsYWrongInterface', 'Transformer') def test_add_module(self): json_module_file = os.path.join(self.custom_folder, "fake_module.json") # first add module self.registry.add_module(json_module_file) self.assertTrue("fake_module" in self.registry.PHOTON_REGISTRIES) # check if registered items are available self.assertTrue("FakeElement1" in self.registry.ELEMENT_DICTIONARY) fe = PipelineElement("FakeElement1") # then delete the module self.registry.delete_module("fake_module") self.assertFalse(os.path.isfile(os.path.join(self.registry.module_path, "fake_module.json"))) self.assertFalse("FakeElement1" in self.registry.ELEMENT_DICTIONARY) with self.assertRaises(NameError): fe = PipelineElement("FakeElement1") def test_add_failing_module(self): json_module_file = os.path.join(self.custom_folder, "not_working_fake_module.json") # try to add module self.registry.add_module(json_module_file) # that should not have worked because we cant import first element self.assertFalse("FakeElement2001" in self.registry.ELEMENT_DICTIONARY) self.assertFalse(os.path.isfile(os.path.join(self.registry.module_path, "not_working_fake_module.json")))
def test_PhotonRegistry_PHOTON_REGISTRIES_reset(): # too many args PhotonRegistry().reset() assert PhotonRegistry.PHOTON_REGISTRIES[0:] == PHOTON_pkgs[:-1]
def delete_photonai_neuro(): reg = PhotonRegistry() if 'photonai_neuro' in reg.PHOTON_REGISTRIES: reg.delete_module('photonai_neuro')
def test_PhotonRegistry_PHOTON_REGISTRIES_folder(): f = '/photonai/base/registry' r = PhotonRegistry() r._load_custom_folder(f) assert r.custom_elements_folder == f
class RegistryTest(PhotonBaseTest): def setUp(self): super(RegistryTest, self).setUp() self.test_directory = os.path.join( os.path.dirname(os.path.abspath(__file__)).split("test")[0], "test") self.custom_folder = os.path.join(self.test_directory, "base_tests/custom_elements/") self.registry = PhotonRegistry(self.custom_folder) def tearDown(self): super(RegistryTest, self).tearDown() if os.path.isfile( os.path.join(self.custom_folder, "CustomElements.json")): os.remove(os.path.join(self.custom_folder, "CustomElements.json")) def test_register_without_custom_folder(self): registry = PhotonRegistry() with self.assertRaises(ValueError): registry.register("SomeName", "not_existing_file.SomeName", "Estimator") def test_python_file_not_in_custom_folder(self): with self.assertRaises(FileNotFoundError): self.registry.register("SomeName", "not_existing_file.SomeName", "Estimator") def test_list_available_elements(self): self.registry.list_available_elements() self.registry.list_available_elements("PhotonNeuro") self.registry.info("PCA") self.registry.info("NotExistingEstimator") def test_register_element(self): with self.assertRaises(ValueError): self.registry.register("MyCustomEstimator", "custom_estimator.CustomEstimator", "WrongType") self.registry.register("MyCustomEstimator", "custom_estimator.CustomEstimator", "Estimator") self.registry.activate() settings = OutputSettings(save_output=False, project_folder="./tmp/") # DESIGN YOUR PIPELINE pipe = Hyperpipe( "custom_estimator_pipe", optimizer="random_grid_search", optimizer_params={"n_configurations": 2}, metrics=["accuracy", "precision", "recall", "balanced_accuracy"], best_config_metric="accuracy", outer_cv=KFold(n_splits=2), inner_cv=KFold(n_splits=2), verbosity=1, output_settings=settings, ) pipe += PipelineElement("MyCustomEstimator") pipe.fit(np.random.randn(30, 30), np.random.randint(0, 2, 30)) self.registry.delete("MyCustomEstimator") os.remove(os.path.join(self.custom_folder, "CustomElements.json")) def test_estimator_check_during_register(self): with self.assertRaises(NotImplementedError): self.registry.register( "MyCustomEstimatorNoFit", "custom_estimator.CustomEstimatorNoFit", "Estimator", ) with self.assertRaises(NotImplementedError): self.registry.register( "MyCustomEstimatorNoPredict", "custom_estimator.CustomEstimatorNoPredict", "Estimator", ) with self.assertRaises(NotImplementedError): self.registry.register( "MyCustomEstimatorNoEstimatorType", "custom_estimator.CustomEstimatorNoEstimatorType", "Estimator", ) with self.assertRaises(NotImplementedError): self.registry.register( "MyCustomEstimatorNotReturningSelf", "custom_estimator.CustomEstimatorNotReturningSelf", "Estimator", ) e = self.registry.register( "MyCustomEstimatorReturningFalsePredictions", "custom_estimator.CustomEstimatorReturningFalsePredictions", "Estimator", ) self.assertIsInstance(e, ValueError) e = self.registry.register( "MyCustomEstimatorNotWorking", "custom_estimator.CustomEstimatorNotWorking", "Estimator", ) self.assertIsInstance(e, ValueError) os.remove(os.path.join(self.custom_folder, "CustomElements.json")) def test_transformer_needs_covariates(self): self.registry.register( "MyCustomTransformerNeedsCovariates", "custom_transformer.CustomTransformerNeedsCovariates", "Transformer", ) with self.assertRaises(ValueError): self.registry.register( "MyCustomTransformerNeedsCovariatesWrongInterface", "custom_transformer.CustomTransformerNeedsCovariatesWrongInterface", "Transformer", ) def test_transformer_needs_y(self): self.registry.register( "MyCustomTransformerNeedsY", "custom_transformer.CustomTransformerNeedsY", "Transformer", ) with self.assertRaises(ValueError): self.registry.register( "MyCustomTransformerNeedsYWrongInterface", "custom_transformer.CustomTransformerNeedsYWrongInterface", "Transformer", )
def test_PhotonRegistry_PHOTON_REGISTRIES_contents(): # too many args f = '/photonai/base/registry' r = PhotonRegistry() r._load_custom_folder(f) assert r.activate() == None
def test_register_without_custom_folder(self): registry = PhotonRegistry() with self.assertRaises(ValueError): registry.register("SomeName", "not_existing_file.SomeName", "Estimator")
class RegistryTest(PhotonBaseTest): def setUp(self): super(RegistryTest, self).setUp() self.test_directory = os.path.join( os.path.dirname(os.path.abspath(__file__)).split('test')[0], 'test') self.custom_folder = os.path.join(self.test_directory, 'base_tests/custom_elements/') self.registry = PhotonRegistry(self.custom_folder) def tearDown(self): super(RegistryTest, self).tearDown() if os.path.isfile( os.path.join(self.custom_folder, 'CustomElements.json')): os.remove(os.path.join(self.custom_folder, 'CustomElements.json')) def test_register_without_custom_folder(self): registry = PhotonRegistry() with self.assertRaises(ValueError): registry.register('SomeName', 'not_existing_file.SomeName', 'Estimator') def test_python_file_not_in_custom_folder(self): with self.assertRaises(FileNotFoundError): self.registry.register('SomeName', 'not_existing_file.SomeName', 'Estimator') def test_list_available_elements(self): self.registry.list_available_elements() self.registry.list_available_elements('PhotonNeuro') self.registry.info('PCA') self.registry.info('NotExistingEstimator') def test_register_element(self): with self.assertRaises(ValueError): self.registry.register('MyCustomEstimator', 'custom_estimator.CustomEstimator', 'WrongType') self.registry.register('MyCustomEstimator', 'custom_estimator.CustomEstimator', 'Estimator') self.registry.activate() settings = OutputSettings(save_output=False, project_folder='./tmp/') # DESIGN YOUR PIPELINE pipe = Hyperpipe( 'custom_estimator_pipe', optimizer='random_grid_search', optimizer_params={'n_configurations': 2}, metrics=['accuracy', 'precision', 'recall', 'balanced_accuracy'], best_config_metric='accuracy', outer_cv=KFold(n_splits=2), inner_cv=KFold(n_splits=2), verbosity=1, output_settings=settings) pipe += PipelineElement('MyCustomEstimator') pipe.fit(np.random.randn(30, 30), np.random.randint(0, 2, 30)) self.registry.delete('MyCustomEstimator') os.remove(os.path.join(self.custom_folder, 'CustomElements.json')) def test_estimator_check_during_register(self): with self.assertRaises(NotImplementedError): self.registry.register('MyCustomEstimatorNoFit', 'custom_estimator.CustomEstimatorNoFit', 'Estimator') with self.assertRaises(NotImplementedError): self.registry.register( 'MyCustomEstimatorNoPredict', 'custom_estimator.CustomEstimatorNoPredict', 'Estimator') with self.assertRaises(NotImplementedError): self.registry.register( 'MyCustomEstimatorNoEstimatorType', 'custom_estimator.CustomEstimatorNoEstimatorType', 'Estimator') with self.assertRaises(NotImplementedError): self.registry.register( 'MyCustomEstimatorNotReturningSelf', 'custom_estimator.CustomEstimatorNotReturningSelf', 'Estimator') e = self.registry.register( 'MyCustomEstimatorReturningFalsePredictions', 'custom_estimator.CustomEstimatorReturningFalsePredictions', 'Estimator') self.assertIsInstance(e, ValueError) e = self.registry.register( 'MyCustomEstimatorNotWorking', 'custom_estimator.CustomEstimatorNotWorking', 'Estimator') self.assertIsInstance(e, ValueError) os.remove(os.path.join(self.custom_folder, 'CustomElements.json')) def test_transformer_needs_covariates(self): self.registry.register( 'MyCustomTransformerNeedsCovariates', 'custom_transformer.CustomTransformerNeedsCovariates', 'Transformer') with self.assertRaises(ValueError): self.registry.register( 'MyCustomTransformerNeedsCovariatesWrongInterface', 'custom_transformer.CustomTransformerNeedsCovariatesWrongInterface', 'Transformer') def test_transformer_needs_y(self): self.registry.register('MyCustomTransformerNeedsY', 'custom_transformer.CustomTransformerNeedsY', 'Transformer') with self.assertRaises(ValueError): self.registry.register( 'MyCustomTransformerNeedsYWrongInterface', 'custom_transformer.CustomTransformerNeedsYWrongInterface', 'Transformer')