Skip to content

Commit

Permalink
optimize neuro
Browse files Browse the repository at this point in the history
add new example
debug pipeline stacking
  • Loading branch information
RLeenings committed Jun 5, 2019
1 parent a43655b commit f5cb427
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
9 changes: 5 additions & 4 deletions photonai/base/PhotonBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,8 @@ def transform(self, X, y=None, **kwargs):
if hasattr(self.base_element, 'transform'):
return self.adjusted_delegate_call(self.base_element.transform, X, y, **kwargs)
elif hasattr(self.base_element, 'predict', **kwargs):
raise Warning("used prediction instead of transform " + self.name)
# Logger().warn("used prediction instead of transform " + self.name)
# raise Warning()
return self.base_element.predict(X)
else:
Logger().error('BaseException: transform-predict-mess')
Expand Down Expand Up @@ -1863,7 +1864,7 @@ def __init__(self, name: str, stacking_elements=None, voting: bool=False):
self.__iadd__(item_to_stack)

# in case any of the children needs y or the covariates, we have to request them
self.needs_y = True
self.needs_y = False
self.needs_covariates = True

def __iadd__(self, item):
Expand Down Expand Up @@ -1944,7 +1945,7 @@ def fit(self, data, targets=None, **kwargs):
"""
for name, element in self.pipe_elements.items():
# Todo: parallellize fitting
element.fit(data, targets)
element.fit(data, targets, **kwargs)
return self

def predict(self, data, targets=None, **kwargs):
Expand Down Expand Up @@ -2009,7 +2010,7 @@ def stack_data(cls, a, b):
New matrix, that is a and b horizontally joined
"""
if a.size == 0:
if a is None or (isinstance(a, np.ndarray) and a.size == 0):
a = b
else:
# Todo: check for right dimensions!
Expand Down
2 changes: 1 addition & 1 deletion photonai/examples/neuro_image_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
best_config_metric='mean_absolute_error', # after hyperparameter search, the metric declares the winner config
outer_cv=KFold(n_splits=2), # repeat hyperparameter search three times
inner_cv=KFold(n_splits=2), # test each configuration ten times respectively,
verbosity=1)
verbosity=2)


root_folder = "/spm-data/Scratch/spielwiese_ramona/test_photon_neuro/*.nii"
Expand Down
26 changes: 6 additions & 20 deletions photonai/examples/stacking_example.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
from photonai.base.PhotonBase import Hyperpipe, PipelineElement, PipelineStacking, PipelineBranch, OutputSettings
from photonai.base.PhotonBase import Hyperpipe, PipelineElement, PipelineStacking,
from photonai.optimization.Hyperparameters import FloatRange, IntegerRange, Categorical
from photonai.investigator.Investigator import Investigator
from photonai.configuration.Register import PhotonRegister
from sklearn.model_selection import KFold

from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(True)


my_pipe = Hyperpipe('basic_stacking',
optimizer='grid_search',
optimizer='sk_opt',
metrics=['accuracy', 'precision', 'recall'],
best_config_metric='accuracy',
outer_cv=KFold(n_splits=3),
inner_cv=KFold(n_splits=10),
verbosity=1)

tree_branch = PipelineBranch('first_branch')
tree_branch += PipelineElement('StandardScaler')
tree_branch += PipelineElement('DecisionTreeClassifier', {'criterion': ['gini'],
'min_samples_split': IntegerRange(2, 4)})

svm_branch = PipelineBranch('svm_branch')
svm_branch += PipelineElement('StandardScaler')
svm_branch += PipelineElement('SVC', {'kernel': Categorical(['rbf', 'linear']),
'C': FloatRange(0.5, 2, "linspace", num=3)})


my_pipe += PipelineElement('StandardScaler')
my_pipe_stack = PipelineStacking('final_stack', voting=False)
my_pipe_stack += svm_branch
my_pipe_stack += tree_branch
my_pipe_stack += PipelineElement('DecisionTreeClassifier', hyperparameters={'criterion': ['gini'],
'min_samples_split': IntegerRange(2, 4)})

my_pipe_stack += PipelineElement('LinearSVC', hyperparameters={'C': FloatRange(0.5, 25)})
my_pipe += my_pipe_stack

my_pipe += PipelineElement('SVC', {'kernel': Categorical(['rbf', 'linear'])})

my_pipe.fit(X, y)

# Investigator.load_from_file("basic_svm_pipe", 'my_tree.p')

debug = True
45 changes: 45 additions & 0 deletions photonai/examples/subpipelines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from photonai.base.PhotonBase import Hyperpipe, PipelineElement, PipelineStacking, PipelineBranch
from photonai.optimization.Hyperparameters import FloatRange, IntegerRange, Categorical
from photonai.investigator.Investigator import Investigator
from photonai.configuration.Register import PhotonRegister
from sklearn.model_selection import KFold
from sklearn.datasets import load_breast_cancer

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)

# BRANCH WITH QUANTILTRANSFORMER AND DECISIONTREECLASSIFIER
tree_qua_branch = PipelineBranch('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 = PipelineBranch('svm_branch')
svm_mima_branch += PipelineElement('MinMaxScaler')
svm_mima_branch += PipelineElement('SVC', {'kernel': Categorical(['rbf', 'linear']),
'C':2.0},gamma='auto')

# BRANCH WITH StandardScaler AND KNeighborsClassifier
knn_sta_branch = PipelineBranch('neighbour_branch')
knn_sta_branch += PipelineElement('StandardScaler')
knn_sta_branch += PipelineElement('KNeighborsClassifier')

# voting = True to mean the result of every branch
my_pipe_stack = PipelineStacking('final_stack', voting=True)
my_pipe_stack += tree_qua_branch
my_pipe_stack += svm_mima_branch
my_pipe_stack += knn_sta_branch

my_pipe += my_pipe_stack

my_pipe += PipelineElement('LogisticRegression', solver='lbfgs')

my_pipe.fit(X, y)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from setuptools import setup, find_packages


__version__ = '0.4.0'
__version__ = '0.5.0'

setup(
name='photonai',
Expand Down

0 comments on commit f5cb427

Please sign in to comment.