def test_classification_9(self): for original_hyperpipe in self.hyperpipes: pipe = original_hyperpipe.copy_me() # crazy everything pipe += PipelineElement('StandardScaler') pipe += PipelineElement('SamplePairingClassification', {'draw_limit': [100], 'generator': Categorical(['nearest_pair', 'random_pair'])}, distance_metric='euclidean', test_disabled=True) # setup pipeline branches with half of the features each # if both PCAs are disabled, features are simply concatenated and passed to the final estimator source1_branch = Branch('source1_features') # first half of features (for Boston Housing, same as indices=[0, 1, 2, 3, 4, 5] source1_branch += DataFilter(indices=np.arange(start=0, stop=int(np.floor(self.X_shape[1] / 2)))) source1_branch += PipelineElement('PCA', hyperparameters={'n_components': Categorical([None, 5])}, test_disabled=True) source2_branch = Branch('source2_features') # second half of features (for Boston Housing, same is indices=[6, 7, 8, 9, 10, 11, 12] source2_branch += DataFilter(indices=np.arange(start=int(np.floor(self.X_shape[1] / 2)), stop=self.X_shape[1])) source2_branch += PipelineElement('PCA', hyperparameters={'n_components': Categorical([None, 5])}, test_disabled=True) # setup source branches and stack their output (i.e. horizontal concatenation) pipe += Stack('source_stack', elements=[source1_branch, source2_branch]) # final estimator with stack output as features pipe += PipelineElement('RandomForestClassifier', hyperparameters={ 'min_samples_split': FloatRange(start=.05, step=.1, stop=.26, range_type='range')}) self.run_hyperpipe(pipe, self.classification)
def setUp(self): self.X, self.y = load_breast_cancer(True) self.svc = PipelineElement('SVC', { 'C': [0.1, 1], 'kernel': ['rbf', 'sigmoid'] }) self.tree = PipelineElement('DecisionTreeClassifier', {'min_samples_split': [2, 3, 4]}) self.gpc = PipelineElement('GaussianProcessClassifier') self.pca = PipelineElement('PCA') self.estimator_branch = Branch('estimator_branch', [self.tree.copy_me()]) self.transformer_branch = Branch('transformer_branch', [self.pca.copy_me()]) self.estimator_switch = Switch( 'estimator_switch', [self.svc.copy_me(), self.tree.copy_me(), self.gpc.copy_me()]) self.estimator_switch_with_branch = Switch( 'estimator_switch_with_branch', [self.tree.copy_me(), self.estimator_branch.copy_me()]) self.transformer_switch_with_branch = Switch( 'transformer_switch_with_branch', [self.pca.copy_me(), self.transformer_branch.copy_me()]) self.switch_in_switch = Switch('Switch_in_switch', [ self.transformer_branch.copy_me(), self.transformer_switch_with_branch.copy_me() ])
def test_branch_in_branch(self): """ Test for deep Pipeline. """ my_pipe = Hyperpipe( "basic_stacking", optimizer="grid_search", metrics=["accuracy", "precision", "recall"], best_config_metric="f1_score", outer_cv=KFold(n_splits=2), inner_cv=KFold(n_splits=3), verbosity=1, cache_folder="./cache/", output_settings=OutputSettings(project_folder="./tmp/"), ) # BRANCH WITH QUANTILTRANSFORMER AND DECISIONTREECLASSIFIER tree_qua_branch = Branch("tree_branch") tree_qua_branch += PipelineElement("QuantileTransformer") tree_qua_branch += PipelineElement( "DecisionTreeClassifier", {"min_samples_split": IntegerRange(2, 4)}, criterion="gini", ) # BRANCH WITH MinMaxScaler AND DecisionTreeClassifier svm_mima_branch = Branch("svm_branch") svm_mima_branch += PipelineElement("MinMaxScaler") svm_mima_branch += PipelineElement( "SVC", { "kernel": ["rbf", "linear"], # Categorical(['rbf', 'linear']), "C": IntegerRange(0.01, 2.0), }, gamma="auto", ) # BRANCH WITH StandardScaler AND KNeighborsClassifier knn_sta_branch = Branch("neighbour_branch") knn_sta_branch += PipelineElement("StandardScaler") knn_sta_branch += PipelineElement("KNeighborsClassifier") # voting = True to mean the result of every branch my_pipe += Stack("final_stack", [tree_qua_branch, svm_mima_branch, knn_sta_branch]) my_pipe += PipelineElement("LogisticRegression", solver="lbfgs") json_transformer = JsonTransformer() pipe_json = json_transformer.create_json(my_pipe) my_pipe_reload = json_transformer.from_json(pipe_json) pipe_json_reload = pipe_json = json_transformer.create_json( my_pipe_reload) self.assertEqual(pipe_json, pipe_json_reload)
def test_classification_9(self): for original_hyperpipe in self.hyperpipes: pipe = original_hyperpipe.copy_me() # crazy everything pipe += PipelineElement("StandardScaler") pipe += PipelineElement( "SamplePairingClassification", { "draw_limit": [100], "generator": Categorical(["nearest_pair", "random_pair"]), }, distance_metric="euclidean", test_disabled=True, ) # setup pipeline branches with half of the features each # if both PCAs are disabled, features are simply concatenated and passed to the final estimator source1_branch = Branch("source1_features") # first half of features (for Boston Housing, same as indices=[0, 1, 2, 3, 4, 5] source1_branch += DataFilter(indices=np.arange( start=0, stop=int(np.floor(self.X_shape[1] / 2)))) source1_branch += PipelineElement( "PCA", hyperparameters={"n_components": Categorical([None, 5])}, test_disabled=True, ) source2_branch = Branch("source2_features") # second half of features (for Boston Housing, same is indices=[6, 7, 8, 9, 10, 11, 12] source2_branch += DataFilter(indices=np.arange( start=int(np.floor(self.X_shape[1] / 2)), stop=self.X_shape[1])) source2_branch += PipelineElement( "PCA", hyperparameters={"n_components": Categorical([None, 5])}, test_disabled=True, ) # setup source branches and stack their output (i.e. horizontal concatenation) pipe += Stack("source_stack", elements=[source1_branch, source2_branch]) # final estimator with stack output as features pipe += PipelineElement( "RandomForestClassifier", hyperparameters={ "min_samples_split": FloatRange(start=0.05, step=0.1, stop=0.26, range_type="range") }, ) self.run_hyperpipe(pipe, self.classification)
def __init__(self, name, nr_of_processes=1, output_img: bool = False): Branch.__init__(self, name) self._nr_of_processes = 1 self.local_cluster = None self.client = None self.nr_of_processes = nr_of_processes self.output_img = output_img self.has_hyperparameters = True self.needs_y = False self.needs_covariates = True self.current_config = None
def test_branch_in_branch(self): """ Test for deep Pipeline. """ my_pipe = Hyperpipe( 'basic_stacking', optimizer='grid_search', metrics=['accuracy', 'precision', 'recall'], best_config_metric='f1_score', outer_cv=KFold(n_splits=2), inner_cv=KFold(n_splits=3), verbosity=1, cache_folder="./cache/", output_settings=OutputSettings(project_folder='./tmp/')) # BRANCH WITH QUANTILTRANSFORMER AND DECISIONTREECLASSIFIER tree_qua_branch = Branch('tree_branch') tree_qua_branch += PipelineElement('QuantileTransformer') tree_qua_branch += PipelineElement( 'DecisionTreeClassifier', {'min_samples_split': IntegerRange(2, 4)}, criterion='gini') # BRANCH WITH MinMaxScaler AND DecisionTreeClassifier svm_mima_branch = Branch('svm_branch') svm_mima_branch += PipelineElement('MinMaxScaler') svm_mima_branch += PipelineElement( 'SVC', { 'kernel': ['rbf', 'linear'], # Categorical(['rbf', 'linear']), 'C': IntegerRange(0.01, 2.0) }, gamma='auto') # BRANCH WITH StandardScaler AND KNeighborsClassifier knn_sta_branch = Branch('neighbour_branch') knn_sta_branch += PipelineElement('StandardScaler') knn_sta_branch += PipelineElement('KNeighborsClassifier') # voting = True to mean the result of every branch my_pipe += Stack('final_stack', [tree_qua_branch, svm_mima_branch, knn_sta_branch]) my_pipe += PipelineElement('LogisticRegression', solver='lbfgs') json_transformer = JsonTransformer() pipe_json = json_transformer.create_json(my_pipe) my_pipe_reload = json_transformer.from_json(pipe_json) pipe_json_reload = pipe_json = json_transformer.create_json( my_pipe_reload) self.assertEqual(pipe_json, pipe_json_reload)
def test_prepare_photon_pipeline(self): test_branch = Branch('my_test_branch') test_branch += PipelineElement('SimpleImputer') test_branch += Switch('my_crazy_switch_bitch') test_branch += Stack('my_stacking_stack') test_branch += PipelineElement('SVC') generated_pipe = test_branch.prepare_photon_pipe(test_branch.elements) self.assertEqual(len(generated_pipe.named_steps), 4) for idx, element in enumerate(test_branch.elements): self.assertIs(generated_pipe.named_steps[element.name], element) self.assertIs(generated_pipe.elements[idx][1], test_branch.elements[idx])
def test_sanity_check_pipe(self): test_branch = Branch('my_test_branch') def callback_func(X, y, **kwargs): pass with self.assertRaises(Warning): my_callback = CallbackElement('final_element_callback', delegate_function=callback_func) test_branch += my_callback no_callback_pipe = test_branch.prepare_photon_pipe( test_branch.elements) test_branch.sanity_check_pipeline(no_callback_pipe) self.assertFalse(no_callback_pipe[-1] is not my_callback)
def test_copy_me(self): switch = Switch("my_copy_switch") switch += PipelineElement("StandardScaler") switch += PipelineElement("RobustScaler", test_disabled=True) stack = Stack("RandomStack") stack += PipelineElement("SVC") branch = Branch('Random_Branch') pca_hyperparameters = {'n_components': [5, 10]} branch += PipelineElement("PCA", hyperparameters=pca_hyperparameters) branch += PipelineElement("DecisionTreeClassifier") stack += branch photon_pipe = PhotonPipeline([("SimpleImputer", PipelineElement("SimpleImputer")), ("my_copy_switch", switch), ('RandomStack', stack), ('Callback1', CallbackElement('tmp_callback', np.mean)), ("PhotonVotingClassifier", PipelineElement("PhotonVotingClassifier"))]) copy_of_the_pipe = photon_pipe.copy_me() self.assertEqual(photon_pipe.random_state, copy_of_the_pipe.random_state) self.assertTrue(len(copy_of_the_pipe.elements) == 5) self.assertTrue(copy_of_the_pipe.elements[2][1].name == "RandomStack") self.assertTrue(copy_of_the_pipe.named_steps["my_copy_switch"].elements[1].test_disabled) self.assertDictEqual(copy_of_the_pipe.elements[2][1].elements[1].elements[0].hyperparameters, {"PCA__n_components": [5, 10]}) self.assertTrue(isinstance(copy_of_the_pipe.elements[3][1], CallbackElement)) self.assertTrue(copy_of_the_pipe.named_steps["tmp_callback"].delegate_function == np.mean)
def test_copy_me(self): branch = Branch('MyBranch', [self.scaler, self.pca]) copy = branch.copy_me() self.assertEqual(branch.random_state, copy.random_state) self.assertDictEqual(elements_to_dict(copy), elements_to_dict(branch)) copy = branch.copy_me() copy.elements[1].base_element.n_components = 3 self.assertNotEqual(copy.elements[1].base_element.n_components, branch.elements[1].base_element.n_components) fake_copy = branch fake_copy.elements[1].base_element.n_components = 3 self.assertEqual(fake_copy.elements[1].base_element.n_components, branch.elements[1].base_element.n_components)
def setup_crazy_pipe(self): # erase all, we need a complex and crazy task self.hyperpipe.elements = list() nmb_list = list() for i in range(5): nmb = ParallelBranch(name=str(i), nr_of_processes=i + 3) sp = PipelineElement( 'PCA', hyperparameters={'n_components': IntegerRange(1, 50)}) nmb += sp nmb_list.append(nmb) my_switch = Switch('disabling_test_switch') my_switch += nmb_list[0] my_switch += nmb_list[1] my_stack = Stack('stack_of_branches') for i in range(3): my_branch = Branch('branch_' + str(i + 2)) my_branch += PipelineElement('StandardScaler') my_branch += nmb_list[i + 2] my_stack += my_branch self.hyperpipe.add(my_stack) self.hyperpipe.add(PipelineElement('StandardScaler')) self.hyperpipe.add(my_switch) self.hyperpipe.add(PipelineElement('SVC')) return nmb_list
def test_recursive_disabling(self): list_of_elements_to_detect = self.setup_crazy_pipe() self.hyperpipe._pipe = Branch.prepare_photon_pipe( list_of_elements_to_detect) Hyperpipe.disable_multiprocessing_recursively(self.hyperpipe._pipe) self.assertTrue( [i.nr_of_processes == 1 for i in list_of_elements_to_detect])
def test_ask_advanced(self): """ Test advanced functionality of .ask() """ branch = Branch("branch") branch += PipelineElement("PCA") branch += PipelineElement("SVC", { "C": [0.1, 1], "kernel": ["rbf", "sigmoid"] }) pipe_switch = Switch( "switch", [ PipelineElement("StandardScaler"), PipelineElement("MaxAbsScaler") ], ) self.pipeline_elements = [ PipelineElement("StandardScaler"), PipelineElement( "PCA", hyperparameters={"n_components": IntegerRange(5, 20)}, test_disabled=True, ), pipe_switch, branch, Switch("Switch_in_switch", [branch, pipe_switch]), ] generated_elements = self.test_ask() self.assertIn("PCA__n_components", generated_elements) self.assertIn("Switch_in_switch__current_element", generated_elements) self.assertIn("branch__SVC__C", generated_elements) self.assertIn("branch__SVC__kernel", generated_elements) self.assertIn("switch__current_element", generated_elements)
def setup_crazy_pipe(self): # erase all, we need a complex and crazy task self.hyperpipe.elements = list() nmb_list = list() for i in range(5): nmb = NeuroBranch(name=str(i), nr_of_processes=i + 3) nmb += PipelineElement("SmoothImages") nmb_list.append(nmb) my_switch = Switch("disabling_test_switch") my_switch += nmb_list[0] my_switch += nmb_list[1] my_stack = Stack("stack_of_branches") for i in range(3): my_branch = Branch("branch_" + str(i + 2)) my_branch += PipelineElement("StandardScaler") my_branch += nmb_list[i + 2] my_stack += my_branch self.hyperpipe.add(my_stack) self.hyperpipe.add(PipelineElement("StandardScaler")) self.hyperpipe.add(my_switch) self.hyperpipe.add(PipelineElement("SVC")) return nmb_list
def test_ask_advanced(self): """ Test advanced functionality of .ask() """ branch = Branch('branch') branch += PipelineElement('PCA') branch += PipelineElement('SVC', { 'C': [0.1, 1], 'kernel': ['rbf', 'sigmoid'] }) pipe_switch = Switch('switch', [ PipelineElement("StandardScaler"), PipelineElement("MaxAbsScaler") ]) self.pipeline_elements = [ PipelineElement("StandardScaler"), PipelineElement( 'PCA', hyperparameters={'n_components': IntegerRange(5, 20)}, test_disabled=True), pipe_switch, branch, Switch('Switch_in_switch', [branch, pipe_switch]) ] generated_elements = self.test_ask() self.assertIn("PCA__n_components", generated_elements) self.assertIn("Switch_in_switch__current_element", generated_elements) self.assertIn("branch__SVC__C", generated_elements) self.assertIn("branch__SVC__kernel", generated_elements) self.assertIn("switch__current_element", generated_elements)
def test_estimator_type(self): def callback(X, y=None): pass transformer_branch = Branch( 'TransBranch', [PipelineElement('PCA'), PipelineElement('FastICA')]) classifier_branch = Branch('ClassBranch', [PipelineElement('SVC')]) regressor_branch = Branch('RegBranch', [PipelineElement('SVR')]) callback_branch = Branch( 'CallBranch', [PipelineElement('SVR'), CallbackElement('callback', callback)]) self.assertEqual(transformer_branch._estimator_type, None) self.assertEqual(classifier_branch._estimator_type, 'classifier') self.assertEqual(regressor_branch._estimator_type, 'regressor') self.assertEqual(callback_branch._estimator_type, None)
def setUp(self): self.X, self.y = load_breast_cancer(True) self.scaler = PipelineElement("StandardScaler", {'with_mean': True}) self.pca = PipelineElement('PCA', {'n_components': [1, 2]}, test_disabled=True, random_state=3) self.tree = PipelineElement('DecisionTreeClassifier', {'min_samples_split': [2, 3, 4]}, random_state=3) self.transformer_branch = Branch('MyBranch', [self.scaler, self.pca]) self.transformer_branch_sklearn = SKPipeline([("SS", StandardScaler()), ("PCA", PCA(random_state=3))]) self.estimator_branch = Branch('MyBranch', [self.scaler, self.pca, self.tree]) self.estimator_branch_sklearn = SKPipeline([ ("SS", StandardScaler()), ("PCA", PCA(random_state=3)), ("Tree", DecisionTreeClassifier(random_state=3)) ])
def setUp(self): self.svc_pipe_element = PipelineElement('SVC', {'C': [0.1, 1], 'kernel': ['rbf', 'sigmoid']}) self.lr_pipe_element = PipelineElement('DecisionTreeClassifier', {'min_samples_split': [2, 3, 4]}) self.pipe_switch = Switch('switch', [self.svc_pipe_element, self.lr_pipe_element]) self.branch = Branch('branch') self.branch += PipelineElement('PCA') self.branch += self.svc_pipe_element self.switch_in_switch = Switch('Switch_in_switch', [self.branch, self.pipe_switch])
def test_set_random_state(self): # we handle all elements in one method that is inherited so we capture them all in this test random_state = 53 my_branch = Branch("random_state_branch") my_branch += PipelineElement("StandardScaler") my_switch = Switch("transformer_Switch") my_switch += PipelineElement("LassoFeatureSelection") my_switch += PipelineElement("PCA") my_branch += my_switch my_stack = Stack("Estimator_Stack") my_stack += PipelineElement("SVR") my_stack += PipelineElement("Ridge") my_branch += my_stack my_branch += PipelineElement("ElasticNet") my_branch.random_state = random_state self.assertTrue(my_switch.elements[1].random_state == random_state) self.assertTrue( my_switch.elements[1].base_element.random_state == random_state) self.assertTrue(my_stack.elements[1].random_state == random_state) self.assertTrue( my_stack.elements[1].base_element.random_state == random_state)
def test_add(self): branch = Branch('MyBranch', [ PipelineElement('PCA', {'n_components': [5]}), PipelineElement('FastICA') ]) self.assertEqual(len(branch.elements), 2) self.assertDictEqual(branch._hyperparameters, {'MyBranch__PCA__n_components': [5]}) branch = Branch('MyBranch') branch += PipelineElement('PCA', {'n_components': [5]}) branch += PipelineElement('FastICA') self.assertEqual(len(branch.elements), 2) self.assertDictEqual(branch._hyperparameters, {'MyBranch__PCA__n_components': [5]}) # add doubled item branch += PipelineElement('PCA', {'n_components': [10, 20]}) self.assertEqual(branch.elements[-1].name, 'PCA2') self.assertDictEqual( branch.hyperparameters, { 'MyBranch__PCA__n_components': [5], 'MyBranch__PCA2__n_components': [10, 20] })
def test_recursive_cache_folder_propagation(self): list_of_elements = self.setup_crazy_pipe() self.hyperpipe._pipe = Branch.prepare_photon_pipe(self.hyperpipe.elements) self.hyperpipe.recursive_cache_folder_propagation( self.hyperpipe._pipe, self.cache_folder_path, "fold_id_123" ) for i, nmbranch in enumerate(list_of_elements): if i > 1: start_folder = os.path.join( self.cache_folder_path, "branch_" + nmbranch.name ) else: start_folder = self.cache_folder_path expected_folder = os.path.join(start_folder, nmbranch.name) self.assertEqual(nmbranch.base_element.cache_folder, expected_folder)
def test_classification_8(self): for original_hyperpipe in self.hyperpipes: pipe = original_hyperpipe.copy_me() pipe += PipelineElement('StandardScaler') # setup pipeline branches with half of the features each # if both PCAs are disabled, features are simply concatenated and passed to the final estimator source1_branch = Branch('source1_features') # first half of features (for Boston Housing, same as indices=[0, 1, 2, 3, 4, 5] source1_branch += DataFilter(indices=np.arange(start=0, stop=int(np.floor(self.X_shape[1] / 2)))) source1_branch += PipelineElement('ConfounderRemoval', {}, standardize_covariates=True, test_disabled=True, confounder_names=['cov1', 'cov2']) source1_branch += PipelineElement('PCA', hyperparameters={'n_components': Categorical([None, 5])}, test_disabled=True) source2_branch = Branch('source2_features') # second half of features (for Boston Housing, same is indices=[6, 7, 8, 9, 10, 11, 12] source2_branch += DataFilter(indices=np.arange(start=int(np.floor(self.X_shape[1] / 2)), stop=self.X_shape[1])) source2_branch += PipelineElement('ConfounderRemoval', {}, standardize_covariates=True, test_disabled=True, confounder_names=['cov1', 'cov2']) source2_branch += PipelineElement('PCA', hyperparameters={'n_components': Categorical([None, 5])}, test_disabled=True) # setup source branches and stack their output (i.e. horizontal concatenation) pipe += Stack('source_stack', elements=[source1_branch, source2_branch]) # final estimator with stack output as features # setup estimator switch and add it to the pipe switch = Switch('estimator_switch') switch += PipelineElement('SVC', hyperparameters={'kernel': Categorical(['linear', 'rbf']), 'C': Categorical([.01, 1, 5])}) switch += PipelineElement('RandomForestClassifier', hyperparameters={ 'min_samples_split': FloatRange(start=.05, step=.1, stop=.26, range_type='range')}) pipe += switch self.run_hyperpipe(pipe, self.classification)
def setUp(self): self.svc_pipe_element = PipelineElement("SVC", { "C": [0.1, 1], "kernel": ["rbf", "sigmoid"] }) self.lr_pipe_element = PipelineElement( "DecisionTreeClassifier", {"min_samples_split": [2, 3, 4]}) self.pipe_switch = Switch( "switch", [self.svc_pipe_element, self.lr_pipe_element]) self.branch = Branch("branch") self.branch += PipelineElement("PCA") self.branch += self.svc_pipe_element self.switch_in_switch = Switch("Switch_in_switch", [self.branch, self.pipe_switch])
def setUp(self): self.X, self.y = load_breast_cancer(True) self.pca = PipelineElement('PCA', {'n_components': [5, 10]}) self.scaler = PipelineElement('StandardScaler', {'with_mean': [True]}) self.svc = PipelineElement('SVC', {'C': [1, 2]}) self.tree = PipelineElement('DecisionTreeClassifier', {'min_samples_leaf': [3, 5]}) self.transformer_branch_1 = Branch('TransBranch1', [self.pca.copy_me()]) self.transformer_branch_2 = Branch('TransBranch2', [self.scaler.copy_me()]) self.estimator_branch_1 = Branch('EstBranch1', [self.svc.copy_me()]) self.estimator_branch_2 = Branch('EstBranch2', [self.tree.copy_me()]) self.transformer_stack = Stack( 'TransformerStack', [self.pca.copy_me(), self.scaler.copy_me()]) self.estimator_stack = Stack( 'EstimatorStack', [self.svc.copy_me(), self.tree.copy_me()]) self.transformer_branch_stack = Stack('TransBranchStack', [ self.transformer_branch_1.copy_me(), self.transformer_branch_2.copy_me() ]) self.estimator_branch_stack = Stack('EstBranchStack', [ self.estimator_branch_1.copy_me(), self.estimator_branch_2.copy_me() ]) self.stacks = [ ([self.pca, self.scaler], self.transformer_stack), ([self.svc, self.tree], self.estimator_stack), ([self.transformer_branch_1, self.transformer_branch_2], self.transformer_branch_stack), ([self.estimator_branch_1, self.estimator_branch_2], self.estimator_branch_stack) ]
def test_add(self): stack = Stack('MyStack', [ PipelineElement('PCA', {'n_components': [5]}), PipelineElement('FastICA') ]) self.assertEqual(len(stack.elements), 2) self.assertDictEqual(stack._hyperparameters, {'MyStack__PCA__n_components': [5]}) stack = Stack('MyStack') stack += PipelineElement('PCA', {'n_components': [5]}) stack += PipelineElement('FastICA') self.assertEqual(len(stack.elements), 2) self.assertDictEqual(stack._hyperparameters, {'MyStack__PCA__n_components': [5]}) def callback(X, y=None): pass stack = Stack('MyStack', [ PipelineElement('PCA'), CallbackElement('MyCallback', callback), Switch('MySwitch', [PipelineElement('PCA'), PipelineElement('FastICA')]), Branch('MyBranch', [PipelineElement('PCA')]) ]) self.assertEqual(len(stack.elements), 4) # test doubled item with self.assertRaises(ValueError): stack += stack.elements[0] stack += PipelineElement('PCA', {'n_components': [10, 20]}) self.assertEqual(stack.elements[-1].name, 'PCA2') self.assertDictEqual( stack.hyperparameters, { 'MyStack__MySwitch__current_element': [(0, 0), (1, 0)], 'MyStack__PCA2__n_components': [10, 20] })
neuro_branch += PipelineElement( "BrainAtlas", hyperparameters={}, rois=["Hippocampus_L", "Hippocampus_R", "Amygdala_L", "Amygdala_R"], atlas_name="AAL", extract_mode="vec", batch_size=20, ) # finally, add your neuro branch to your hyperpipe neuro_branch += CallbackElement("NeuroCallback", my_monitor) my_pipe += neuro_branch # my_pipe += CallbackElement('NeuroCallback', my_monitor) # now, add standard ML algorithms to your liking feature_engineering = Branch("FeatureEngineering") feature_engineering += PipelineElement("StandardScaler") my_pipe += feature_engineering my_pipe += CallbackElement("FECallback", my_monitor) my_pipe += PipelineElement( "SVR", hyperparameters={"kernel": Categorical(["rbf", "linear"])}, gamma="scale" ) # NOW TRAIN YOUR PIPELINE start_time = time.time() my_pipe.fit(X, y) elapsed_time = time.time() - start_time print(time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))
from photonai.base import Hyperpipe, PipelineElement, Stack, Branch, OutputSettings from photonai.optimization import IntegerRange, Categorical X, y = load_breast_cancer(True) my_pipe = Hyperpipe('basic_stacking', optimizer='grid_search', metrics=['accuracy', 'precision', 'recall'], best_config_metric='f1_score', outer_cv=KFold(n_splits=3), inner_cv=KFold(n_splits=10), verbosity=1, output_settings=OutputSettings(project_folder='./tmp/')) # BRANCH WITH QUANTILTRANSFORMER AND DECISIONTREECLASSIFIER tree_qua_branch = Branch('tree_branch') tree_qua_branch += PipelineElement('QuantileTransformer') tree_qua_branch += PipelineElement('DecisionTreeClassifier', {'min_samples_split': IntegerRange(2, 4)}, criterion='gini') # BRANCH WITH MinMaxScaler AND DecisionTreeClassifier svm_mima_branch = Branch('svm_branch') svm_mima_branch += PipelineElement('MinMaxScaler') svm_mima_branch += PipelineElement('SVC', { 'kernel': Categorical(['rbf', 'linear']), 'C': IntegerRange(0.01, 2.0) }, gamma='auto') # BRANCH WITH StandardScaler AND KNeighborsClassifier
class StackTests(unittest.TestCase): def setUp(self): self.X, self.y = load_breast_cancer(True) self.pca = PipelineElement('PCA', {'n_components': [5, 10]}) self.scaler = PipelineElement('StandardScaler', {'with_mean': [True]}) self.svc = PipelineElement('SVC', {'C': [1, 2]}) self.tree = PipelineElement('DecisionTreeClassifier', {'min_samples_leaf': [3, 5]}) self.transformer_branch_1 = Branch('TransBranch1', [self.pca.copy_me()]) self.transformer_branch_2 = Branch('TransBranch2', [self.scaler.copy_me()]) self.estimator_branch_1 = Branch('EstBranch1', [self.svc.copy_me()]) self.estimator_branch_2 = Branch('EstBranch2', [self.tree.copy_me()]) self.transformer_stack = Stack( 'TransformerStack', [self.pca.copy_me(), self.scaler.copy_me()]) self.estimator_stack = Stack( 'EstimatorStack', [self.svc.copy_me(), self.tree.copy_me()]) self.transformer_branch_stack = Stack('TransBranchStack', [ self.transformer_branch_1.copy_me(), self.transformer_branch_2.copy_me() ]) self.estimator_branch_stack = Stack('EstBranchStack', [ self.estimator_branch_1.copy_me(), self.estimator_branch_2.copy_me() ]) self.stacks = [ ([self.pca, self.scaler], self.transformer_stack), ([self.svc, self.tree], self.estimator_stack), ([self.transformer_branch_1, self.transformer_branch_2], self.transformer_branch_stack), ([self.estimator_branch_1, self.estimator_branch_2], self.estimator_branch_stack) ] def test_copy_me(self): for stack in self.stacks: stack = stack[1] copy = stack.copy_me() self.assertEqual(stack.random_state, copy.random_state) self.assertFalse( stack.elements[0].__dict__ == copy.elements[0].__dict__) self.assertDictEqual(elements_to_dict(stack), elements_to_dict(copy)) def test_horizontal_stacking(self): for stack in self.stacks: element_1 = stack[0][0] element_2 = stack[0][1] stack = stack[1] # fit elements Xt_1 = element_1.fit(self.X, self.y).transform(self.X, self.y) Xt_2 = element_2.fit(self.X, self.y).transform(self.X, self.y) Xt = stack.fit(self.X, self.y).transform(self.X, self.y) # output of transform() changes depending on whether it is an estimator stack or a transformer stack if isinstance(Xt, tuple): Xt = Xt[0] Xt_1 = Xt_1[0] Xt_2 = Xt_2[0] if len(Xt_1.shape) == 1: Xt_1 = np.reshape(Xt_1, (-1, 1)) Xt_2 = np.reshape(Xt_2, (-1, 1)) self.assertEqual(Xt.shape[1], Xt_1.shape[-1] + Xt_2.shape[-1]) def recursive_assertion(self, element_a, element_b): for key in element_a.keys(): if isinstance(element_a[key], np.ndarray): np.testing.assert_array_equal(element_a[key], element_b[key]) elif isinstance(element_a[key], dict): self.recursive_assertion(element_a[key], element_b[key]) else: self.assertEqual(element_a[key], element_b[key]) def test_fit(self): for elements, stack in [([self.pca, self.scaler], self.transformer_stack), ([self.svc, self.tree], self.estimator_stack)]: np.random.seed(42) stack = stack.fit(self.X, self.y) np.random.seed(42) for i, element in enumerate(elements): element = element.fit(self.X, self.y) element_dict = elements_to_dict(element) stack_dict = elements_to_dict(stack.elements[i]) self.recursive_assertion(element_dict, stack_dict) def test_transform(self): for elements, stack in self.stacks: np.random.seed(42) Xt_stack, _, _ = stack.fit(self.X, self.y).transform(self.X) np.random.seed(42) Xt_elements = None for i, element in enumerate(elements): Xt_element, _, _ = element.fit(self.X, self.y).transform(self.X) Xt_elements = PhotonDataHelper.stack_data_horizontally( Xt_elements, Xt_element) np.testing.assert_array_equal(Xt_stack, Xt_elements) def test_predict(self): for elements, stack in [ ([self.svc, self.tree], self.estimator_stack), ([self.estimator_branch_1, self.estimator_branch_2], self.estimator_branch_stack) ]: np.random.seed(42) stack = stack.fit(self.X, self.y) yt_stack = stack.predict(self.X) np.random.seed(42) Xt_elements = None for i, element in enumerate(elements): Xt_element = element.fit(self.X, self.y).predict(self.X) Xt_elements = PhotonDataHelper.stack_data_horizontally( Xt_elements, Xt_element) np.testing.assert_array_equal(yt_stack, Xt_elements) def test_predict_proba(self): for elements, stack in [ ([self.svc, self.tree], self.estimator_stack), ([self.estimator_branch_1, self.estimator_branch_2], self.estimator_branch_stack) ]: np.random.seed(42) stack = stack.fit(self.X, self.y) yt_stack = stack.predict_proba(self.X) np.random.seed(42) Xt_elements = None for i, element in enumerate(elements): Xt_element = element.fit(self.X, self.y).predict_proba(self.X) if Xt_element is None: Xt_element = element.fit(self.X, self.y).predict(self.X) Xt_elements = PhotonDataHelper.stack_data_horizontally( Xt_elements, Xt_element) np.testing.assert_array_equal(yt_stack, Xt_elements) def test_inverse_transform(self): with self.assertRaises(NotImplementedError): self.stacks[0][1].fit(self.X, self.y).inverse_transform(self.X) def test_set_params(self): trans_config = { 'PCA__n_components': 2, 'PCA__disabled': True, 'StandardScaler__with_mean': True } est_config = { 'SVC__C': 3, 'DecisionTreeClassifier__min_samples_leaf': 1 } # transformer stack self.transformer_stack.set_params(**trans_config) self.assertEqual( self.transformer_stack.elements[0].base_element.n_components, 2) self.assertEqual(self.transformer_stack.elements[0].disabled, True) self.assertEqual( self.transformer_stack.elements[1].base_element.with_mean, True) # estimator stack self.estimator_stack.set_params(**est_config) self.assertEqual(self.estimator_stack.elements[0].base_element.C, 3) self.assertEqual( self.estimator_stack.elements[1].base_element.min_samples_leaf, 1) with self.assertRaises(ValueError): self.estimator_stack.set_params(**{'any_weird_param': 1}) with self.assertRaises(ValueError): self.transformer_stack.set_params(**{'any_weird_param': 1}) def test_add(self): stack = Stack('MyStack', [ PipelineElement('PCA', {'n_components': [5]}), PipelineElement('FastICA') ]) self.assertEqual(len(stack.elements), 2) self.assertDictEqual(stack._hyperparameters, {'MyStack__PCA__n_components': [5]}) stack = Stack('MyStack') stack += PipelineElement('PCA', {'n_components': [5]}) stack += PipelineElement('FastICA') self.assertEqual(len(stack.elements), 2) self.assertDictEqual(stack._hyperparameters, {'MyStack__PCA__n_components': [5]}) def callback(X, y=None): pass stack = Stack('MyStack', [ PipelineElement('PCA'), CallbackElement('MyCallback', callback), Switch('MySwitch', [PipelineElement('PCA'), PipelineElement('FastICA')]), Branch('MyBranch', [PipelineElement('PCA')]) ]) self.assertEqual(len(stack.elements), 4) # test doubled item with self.assertRaises(ValueError): stack += stack.elements[0] stack += PipelineElement('PCA', {'n_components': [10, 20]}) self.assertEqual(stack.elements[-1].name, 'PCA2') self.assertDictEqual( stack.hyperparameters, { 'MyStack__MySwitch__current_element': [(0, 0), (1, 0)], 'MyStack__PCA2__n_components': [10, 20] }) def test_feature_importances(self): # single item self.estimator_stack.fit(self.X, self.y) self.assertIsNone(self.estimator_stack.feature_importances_) self.estimator_branch_stack.fit(self.X, self.y) self.assertIsNone(self.estimator_branch_stack.feature_importances_) def test_use_probabilities(self): self.estimator_stack.use_probabilities = True self.estimator_stack.fit(self.X, self.y) probas = self.estimator_stack.predict(self.X) self.assertEqual(probas.shape[1], 3) self.estimator_stack.use_probabilities = False self.estimator_stack.fit(self.X, self.y) preds = self.estimator_stack.predict(self.X) self.assertEqual(preds.shape[1], 2) probas = self.estimator_stack.predict_proba(self.X) self.assertEqual(probas.shape[1], 3)
class SwitchTests(unittest.TestCase): def setUp(self): self.X, self.y = load_breast_cancer(True) self.svc = PipelineElement('SVC', { 'C': [0.1, 1], 'kernel': ['rbf', 'sigmoid'] }) self.tree = PipelineElement('DecisionTreeClassifier', {'min_samples_split': [2, 3, 4]}) self.gpc = PipelineElement('GaussianProcessClassifier') self.pca = PipelineElement('PCA') self.estimator_branch = Branch('estimator_branch', [self.tree.copy_me()]) self.transformer_branch = Branch('transformer_branch', [self.pca.copy_me()]) self.estimator_switch = Switch( 'estimator_switch', [self.svc.copy_me(), self.tree.copy_me(), self.gpc.copy_me()]) self.estimator_switch_with_branch = Switch( 'estimator_switch_with_branch', [self.tree.copy_me(), self.estimator_branch.copy_me()]) self.transformer_switch_with_branch = Switch( 'transformer_switch_with_branch', [self.pca.copy_me(), self.transformer_branch.copy_me()]) self.switch_in_switch = Switch('Switch_in_switch', [ self.transformer_branch.copy_me(), self.transformer_switch_with_branch.copy_me() ]) def test_init(self): self.assertEqual(self.estimator_switch.name, 'estimator_switch') def test_hyperparams(self): # assert number of different configs to test # each config combi for each element: 4 for SVC and 3 for logistic regression = 7 self.assertEqual( len(self.estimator_switch.pipeline_element_configurations), 3) self.assertEqual( len(self.estimator_switch.pipeline_element_configurations[0]), 4) self.assertEqual( len(self.estimator_switch.pipeline_element_configurations[1]), 3) # hyperparameters self.assertDictEqual( self.estimator_switch.hyperparameters, { 'estimator_switch__current_element': [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (2, 0)] }) # config grid self.assertListEqual(self.estimator_switch.generate_config_grid(), [{ 'estimator_switch__current_element': (0, 0) }, { 'estimator_switch__current_element': (0, 1) }, { 'estimator_switch__current_element': (0, 2) }, { 'estimator_switch__current_element': (0, 3) }, { 'estimator_switch__current_element': (1, 0) }, { 'estimator_switch__current_element': (1, 1) }, { 'estimator_switch__current_element': (1, 2) }, { 'estimator_switch__current_element': (2, 0) }]) def test_set_params(self): # test for grid search false_config = {'current_element': 1} with self.assertRaises(ValueError): self.estimator_switch.set_params(**false_config) correct_config = {'current_element': (0, 1)} self.estimator_switch.set_params(**correct_config) self.assertEqual(self.estimator_switch.base_element.base_element.C, 0.1) self.assertEqual( self.estimator_switch.base_element.base_element.kernel, 'sigmoid') # test for other optimizers smac_config = {'SVC__C': 2, 'SVC__kernel': 'rbf'} self.estimator_switch.set_params(**smac_config) self.assertEqual(self.estimator_switch.base_element.base_element.C, 2) self.assertEqual( self.estimator_switch.base_element.base_element.kernel, 'rbf') def test_fit(self): np.random.seed(42) self.estimator_switch.set_params(**{'current_element': (1, 0)}) self.estimator_switch.fit(self.X, self.y) np.random.seed(42) self.tree.set_params(**{'min_samples_split': 2}) self.tree.fit(self.X, self.y) np.testing.assert_array_equal( self.tree.base_element.feature_importances_, self.estimator_switch.base_element.feature_importances_) def test_transform(self): self.transformer_switch_with_branch.set_params( **{'current_element': (0, 0)}) self.transformer_switch_with_branch.fit(self.X, self.y) self.pca.fit(self.X, self.y) switch_Xt, _, _ = self.transformer_switch_with_branch.transform(self.X) pca_Xt, _, _ = self.pca.transform(self.X) self.assertTrue(np.array_equal(pca_Xt, switch_Xt)) def test_predict(self): self.estimator_switch.set_params(**{'current_element': (1, 0)}) np.random.seed(42) self.estimator_switch.fit(self.X, self.y) self.tree.set_params(**{'min_samples_split': 2}) np.random.seed(42) self.tree.fit(self.X, self.y) switch_preds = self.estimator_switch.predict(self.X) tree_preds = self.tree.predict(self.X) self.assertTrue(np.array_equal(switch_preds, tree_preds)) def test_predict_proba(self): gpc = PipelineElement('GaussianProcessClassifier') svc = PipelineElement('SVC') switch = Switch('EstimatorSwitch', [gpc, svc]) switch.set_params(**{'current_element': (0, 0)}) np.random.seed(42) switch_probas = switch.fit(self.X, self.y).predict_proba(self.X) np.random.seed(42) gpr_probas = self.gpc.fit(self.X, self.y).predict_proba(self.X) self.assertTrue(np.array_equal(switch_probas, gpr_probas)) def test_inverse_transform(self): self.transformer_switch_with_branch.set_params( **{'current_element': (0, 0)}) self.transformer_switch_with_branch.fit(self.X, self.y) self.pca.fit(self.X, self.y) Xt_pca, _, _ = self.pca.transform(self.X) Xt_switch, _, _ = self.transformer_switch_with_branch.transform(self.X) X_pca, _, _ = self.pca.inverse_transform(Xt_pca) X_switch, _, _ = self.transformer_switch_with_branch.inverse_transform( Xt_switch) self.assertTrue(np.array_equal(Xt_pca, Xt_switch)) self.assertTrue(np.array_equal(X_pca, X_switch)) np.testing.assert_almost_equal(X_switch, self.X) def test_base_element(self): switch = Switch('switch', [self.svc, self.tree]) switch.set_params(**{'current_element': (1, 1)}) self.assertIs(switch.base_element, self.tree) self.assertIs(switch.base_element.base_element, self.tree.base_element) # other optimizer switch.set_params(**{'DecisionTreeClassifier__min_samples_split': 2}) self.assertIs(switch.base_element, self.tree) self.assertIs(switch.base_element.base_element, self.tree.base_element) def test_copy_me(self): switches = [ self.estimator_switch, self.estimator_switch_with_branch, self.transformer_switch_with_branch, self.switch_in_switch ] for switch in switches: copy = switch.copy_me() self.assertEqual(switch.random_state, copy.random_state) for i, element in enumerate(copy.elements): self.assertNotEqual(copy.elements[i], switch.elements[i]) switch = elements_to_dict(switch) copy = elements_to_dict(copy) self.assertDictEqual(copy, switch) def test_estimator_type(self): pca = PipelineElement('PCA') ica = PipelineElement('FastICA') svc = PipelineElement('SVC') svr = PipelineElement('SVR') tree_class = PipelineElement('DecisionTreeClassifier') tree_reg = PipelineElement('DecisionTreeRegressor') switch = Switch('MySwitch', [pca, svr]) with self.assertRaises(NotImplementedError): est_type = switch._estimator_type switch = Switch('MySwitch', [svc, svr]) with self.assertRaises(NotImplementedError): est_type = switch._estimator_type switch = Switch('MySwitch', [pca, ica]) self.assertEqual(switch._estimator_type, None) switch = Switch('MySwitch', [tree_class, svc]) self.assertEqual(switch._estimator_type, 'classifier') switch = Switch('MySwitch', [tree_reg, svr]) self.assertEqual(switch._estimator_type, 'regressor') self.assertEqual(self.estimator_switch._estimator_type, 'classifier') self.assertEqual(self.estimator_switch_with_branch._estimator_type, 'classifier') self.assertEqual(self.transformer_switch_with_branch._estimator_type, None) self.assertEqual(self.switch_in_switch._estimator_type, None) def test_add(self): self.assertEqual(len(self.estimator_switch.elements), 3) self.assertEqual(len(self.switch_in_switch.elements), 2) self.assertEqual(len(self.transformer_switch_with_branch.elements), 2) self.assertEqual( list(self.estimator_switch.elements_dict.keys()), ['SVC', 'DecisionTreeClassifier', 'GaussianProcessClassifier']) self.assertEqual( list(self.switch_in_switch.elements_dict.keys()), ['transformer_branch', 'transformer_switch_with_branch']) switch = Switch('MySwitch', [PipelineElement('PCA'), PipelineElement('FastICA')]) switch = Switch('MySwitch2') switch += PipelineElement('PCA') switch += PipelineElement('FastICA') # test doubled names with self.assertRaises(ValueError): self.estimator_switch += self.estimator_switch.elements[0] self.estimator_switch += PipelineElement("SVC") self.assertEqual(self.estimator_switch.elements[-1].name, "SVC2") self.estimator_switch += PipelineElement( "SVC", hyperparameters={'kernel': ['polynomial', 'sigmoid']}) self.assertEqual(self.estimator_switch.elements[-1].name, "SVC3") self.estimator_switch += PipelineElement("SVR") self.assertEqual(self.estimator_switch.elements[-1].name, "SVR") self.estimator_switch += PipelineElement("SVC") self.assertEqual(self.estimator_switch.elements[-1].name, "SVC4") # check that hyperparameters are renamed respectively self.assertEqual( self.estimator_switch.pipeline_element_configurations[4][0] ["SVC3__kernel"], 'polynomial') def test_feature_importances(self): self.estimator_switch.set_params(**{'current_element': (1, 0)}) self.estimator_switch.fit(self.X, self.y) self.assertTrue( len(self.estimator_switch.feature_importances_) == self.X.shape[1]) self.estimator_switch_with_branch.set_params( **{'current_element': (1, 0)}) self.estimator_switch_with_branch.fit(self.X, self.y) self.assertTrue( len(self.estimator_switch_with_branch.feature_importances_) == self.X.shape[1]) self.estimator_switch.set_params(**{'current_element': (2, 0)}) self.estimator_switch.fit(self.X, self.y) self.assertIsNone(self.estimator_branch.feature_importances_) self.switch_in_switch.set_params(**{'current_element': (1, 0)}) self.switch_in_switch.fit(self.X, self.y) self.assertIsNone(self.switch_in_switch.feature_importances_) self.estimator_switch.set_params(**{'current_element': (1, 0)}) self.switch_in_switch.fit(self.X, self.y) self.assertIsNone(self.switch_in_switch.feature_importances_)
class BranchTests(unittest.TestCase): def setUp(self): self.X, self.y = load_breast_cancer(True) self.scaler = PipelineElement("StandardScaler", {'with_mean': True}) self.pca = PipelineElement('PCA', {'n_components': [1, 2]}, test_disabled=True, random_state=3) self.tree = PipelineElement('DecisionTreeClassifier', {'min_samples_split': [2, 3, 4]}, random_state=3) self.transformer_branch = Branch('MyBranch', [self.scaler, self.pca]) self.transformer_branch_sklearn = SKPipeline([("SS", StandardScaler()), ("PCA", PCA(random_state=3))]) self.estimator_branch = Branch('MyBranch', [self.scaler, self.pca, self.tree]) self.estimator_branch_sklearn = SKPipeline([ ("SS", StandardScaler()), ("PCA", PCA(random_state=3)), ("Tree", DecisionTreeClassifier(random_state=3)) ]) def test_fit(self): self.estimator_branch_sklearn.fit(self.X, self.y) sk_pred = self.estimator_branch_sklearn.predict(self.X) self.estimator_branch.fit(self.X, self.y) branch_pred = self.estimator_branch.predict(self.X) self.assertTrue(np.array_equal(sk_pred, branch_pred)) def test_transform(self): Xt, _, _ = self.transformer_branch.fit(self.X, self.y).transform(self.X) Xt_sklearn = self.transformer_branch_sklearn.fit( self.X, self.y).transform(self.X) self.assertTrue(np.array_equal(Xt, Xt_sklearn)) def test_predict(self): y_pred = self.estimator_branch.fit(self.X, self.y).predict(self.X) y_pred_sklearn = self.estimator_branch_sklearn.fit( self.X, self.y).predict(self.X) np.testing.assert_array_equal(y_pred, y_pred_sklearn) def test_predict_proba(self): proba = self.estimator_branch.fit(self.X, self.y).predict_proba(self.X) proba_sklearn = self.estimator_branch_sklearn.fit( self.X, self.y).predict_proba(self.X) np.testing.assert_array_equal(proba, proba_sklearn) def test_inverse_transform(self): self.estimator_branch.fit(self.X, self.y) feature_importances = self.estimator_branch.elements[ -1].base_element.feature_importances_ Xt, _, _ = self.estimator_branch.inverse_transform(feature_importances) self.assertEqual(self.X.shape[1], Xt.shape[0]) def test_no_y_transformers(self): stacking_element = Stack("forbidden_stack") my_dummy = PipelineElement.create( "dummy", DummyNeedsCovariatesAndYTransformer(), {}) with self.assertRaises(NotImplementedError): stacking_element += my_dummy def test_copy_me(self): branch = Branch('MyBranch', [self.scaler, self.pca]) copy = branch.copy_me() self.assertEqual(branch.random_state, copy.random_state) self.assertDictEqual(elements_to_dict(copy), elements_to_dict(branch)) copy = branch.copy_me() copy.elements[1].base_element.n_components = 3 self.assertNotEqual(copy.elements[1].base_element.n_components, branch.elements[1].base_element.n_components) fake_copy = branch fake_copy.elements[1].base_element.n_components = 3 self.assertEqual(fake_copy.elements[1].base_element.n_components, branch.elements[1].base_element.n_components) def test_prepare_photon_pipeline(self): test_branch = Branch('my_test_branch') test_branch += PipelineElement('SimpleImputer') test_branch += Switch('my_crazy_switch_bitch') test_branch += Stack('my_stacking_stack') test_branch += PipelineElement('SVC') generated_pipe = test_branch.prepare_photon_pipe(test_branch.elements) self.assertEqual(len(generated_pipe.named_steps), 4) for idx, element in enumerate(test_branch.elements): self.assertIs(generated_pipe.named_steps[element.name], element) self.assertIs(generated_pipe.elements[idx][1], test_branch.elements[idx]) def test_sanity_check_pipe(self): test_branch = Branch('my_test_branch') def callback_func(X, y, **kwargs): pass with self.assertRaises(Warning): my_callback = CallbackElement('final_element_callback', delegate_function=callback_func) test_branch += my_callback no_callback_pipe = test_branch.prepare_photon_pipe( test_branch.elements) test_branch.sanity_check_pipeline(no_callback_pipe) self.assertFalse(no_callback_pipe[-1] is not my_callback) def test_prepare_pipeline(self): self.assertEqual(len(self.transformer_branch.elements), 2) config_grid = { 'MyBranch__PCA__n_components': [1, 2], 'MyBranch__PCA__disabled': [False, True], 'MyBranch__StandardScaler__with_mean': True } self.assertDictEqual(config_grid, self.transformer_branch._hyperparameters) def test_set_params(self): config = { 'PCA__n_components': 2, 'PCA__disabled': True, 'StandardScaler__with_mean': True } self.transformer_branch.set_params(**config) self.assertTrue( self.transformer_branch.base_element.elements[1][1].disabled) self.assertEqual( self.transformer_branch.base_element.elements[1] [1].base_element.n_components, 2) self.assertEqual( self.transformer_branch.base_element.elements[0] [1].base_element.with_mean, True) with self.assertRaises(ValueError): self.transformer_branch.set_params(**{'any_weird_param': 1}) def test_estimator_type(self): def callback(X, y=None): pass transformer_branch = Branch( 'TransBranch', [PipelineElement('PCA'), PipelineElement('FastICA')]) classifier_branch = Branch('ClassBranch', [PipelineElement('SVC')]) regressor_branch = Branch('RegBranch', [PipelineElement('SVR')]) callback_branch = Branch( 'CallBranch', [PipelineElement('SVR'), CallbackElement('callback', callback)]) self.assertEqual(transformer_branch._estimator_type, None) self.assertEqual(classifier_branch._estimator_type, 'classifier') self.assertEqual(regressor_branch._estimator_type, 'regressor') self.assertEqual(callback_branch._estimator_type, None) def test_add(self): branch = Branch('MyBranch', [ PipelineElement('PCA', {'n_components': [5]}), PipelineElement('FastICA') ]) self.assertEqual(len(branch.elements), 2) self.assertDictEqual(branch._hyperparameters, {'MyBranch__PCA__n_components': [5]}) branch = Branch('MyBranch') branch += PipelineElement('PCA', {'n_components': [5]}) branch += PipelineElement('FastICA') self.assertEqual(len(branch.elements), 2) self.assertDictEqual(branch._hyperparameters, {'MyBranch__PCA__n_components': [5]}) # add doubled item branch += PipelineElement('PCA', {'n_components': [10, 20]}) self.assertEqual(branch.elements[-1].name, 'PCA2') self.assertDictEqual( branch.hyperparameters, { 'MyBranch__PCA__n_components': [5], 'MyBranch__PCA2__n_components': [10, 20] }) def test_feature_importances(self): self.estimator_branch.fit(self.X, self.y) self.assertTrue( len(self.estimator_branch.feature_importances_) == self.X.shape[1]) self.estimator_branch.elements[-1] = PipelineElement( "GaussianProcessClassifier") self.estimator_branch.fit(self.X, self.y) self.assertIsNone(self.estimator_branch.feature_importances_) self.transformer_branch.fit(self.X, self.y) self.assertIsNone(self.transformer_branch.feature_importances_)