Exemplo n.º 1
0
    def load_whole_brain(self, use_cached=False):
        if not use_cached:
            atlas_info = AtlasInfo(
                atlas_name='mni_icbm152_t1_tal_nlin_sym_09a_mask',
                mask_threshold=.5,
                roi_names='all',
                extraction_mode='vec')

            atlas = PipelineElement('BrainAtlas', {},
                                    atlas_info_object=atlas_info)
            self.whole_brain, _, _ = atlas.transform(self.vbm_paths)

            if not os.path.exists(self.folder + 'data/cache/'):
                os.mkdir(self.folder + 'data/cache/')
            if self.tiv_rescaled:
                np.save(self.folder + 'data/cache/whole_brain_residuals.npy',
                        self.whole_brain)
            else:
                np.save(
                    self.folder +
                    'data/cache/whole_brain_residuals_no_tiv_rescaling.npy',
                    self.whole_brain)
        else:
            if self.tiv_rescaled:
                self.whole_brain = np.load(
                    self.folder + 'data/cache/whole_brain_residuals.npy')
            else:
                self.whole_brain = np.load(
                    self.folder +
                    'data/cache/whole_brain_residuals_no_tiv_rescaling.npy')
Exemplo n.º 2
0
    def generate_hyperpipes(self):
        if self.atlas_info_object.roi_names_runtime:
            self.rois = self.atlas_info_object.roi_names_runtime
            #
            # self.outer_pipe = Hyperpipe(self.atlas_name + 'outer_pipe', optimizer='grid_search',
            #                        metrics=['accuracy'], hyperparameter_specific_config_cv_object=
            #                        ShuffleSplit(n_splits=1, test_size=0.2, random_state=3),
            #                        hyperparameter_search_cv_object=
                #                        ShuffleSplit(n_splits=1, test_size=0.2, random_state=3),
                #                        eval_final_performance=True)

            inner_pipe_list = {}
            for i in range(len(self.rois)):
                tmp_inner_pipe = Hyperpipe(self.atlas_name + '_' + str(self.rois[i]), optimizer='grid_search',
                                           inner_cv=ShuffleSplit(n_splits=1, test_size=0.2, random_state=3),
                                           eval_final_performance=False, verbose=Logger().verbosity_level,
                                           best_config_metric=self.best_config_metric, metrics=self.metrics)

                # at first set a filter element

                roi_filter_element = RoiFilterElement(i)
                tmp_inner_pipe.filter_element = roi_filter_element

                # secondly add all other items
                for pipe_item in self.hyperpipe_elements:
                    tmp_inner_pipe += PipelineElement.create(pipe_item[0], pipe_item[1], **pipe_item[2])

                inner_pipe_list[self.rois[i]] = tmp_inner_pipe

            self.pipeline_fusion = PipelineStacking('multiple_source_pipes', inner_pipe_list.values(), voting=False)
Exemplo n.º 3
0
def create_hyperpipe():
    mongo_settings = PersistOptions(mongodb_connect_url="mongodb://trap-umbriel:27017/photon_tests",
                                    save_predictions=False,
                                    save_feature_importances=False)

    my_pipe = Hyperpipe('basic_svm_pipe',
                        optimizer='grid_search',
                        metrics=['accuracy', 'precision', 'recall'],
                        best_config_metric='accuracy',
                        outer_cv=KFold(n_splits=3),
                        inner_cv=KFold(n_splits=3),
                        calculate_metrics_across_folds=True,
                        eval_final_performance=True,
                        verbosity=1, persist_options=mongo_settings)

    my_pipe += PipelineElement('StandardScaler')
    my_pipe += PipelineElement('SVC', {'kernel': Categorical(['rbf', 'linear']),
                                       'C': FloatRange(0.5, 2, "linspace", num=3)})
    return my_pipe
Exemplo n.º 4
0
from photonai.optimization.SpeedHacks import MinimumPerformance

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

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


svm = PipelineElement('SVC', {'kernel': Categorical(['rbf', 'linear']),
                                   'C': FloatRange(0.5, 2, "linspace", num=5)})

tree = PipelineElement('DecisionTreeClassifier',  {'criterion': Categorical(['gini', 'entropy']),
                                                   'min_samples_split': IntegerRange(2, 5)})

switch = PipelineSwitch('estimator_switch')
switch += svm
switch += tree

my_pipe += PipelineElement('StandardScaler')
my_pipe += switch

my_pipe.fit(X, y)


Investigator.show(my_pipe)
                          'accuracy', 'precision', 'recall',
                          'balanced_accuracy', 'sensitivity', 'specificity',
                          'f1_score', 'auc'
                      ],
                      best_config_metric='f1_score',
                      outer_cv=outer_cv,
                      inner_cv=inner_cv,
                      eval_final_performance=True,
                      verbosity=1,
                      output_settings=output_settings,
                      groups=group_var)

# Add transformer elements
hyperpipe += PipelineElement("SimpleImputer",
                             hyperparameters={},
                             test_disabled=False,
                             missing_values=np.nan,
                             strategy='mean',
                             fill_value=0)
hyperpipe += PipelineElement("StandardScaler",
                             hyperparameters={},
                             test_disabled=False,
                             with_mean=True,
                             with_std=True)
hyperpipe += PipelineElement(
    "PCA",
    hyperparameters={'n_components': IntegerRange(5, 150)},
    test_disabled=False)
hyperpipe += PipelineElement("ImbalancedDataTransform",
                             hyperparameters={},
                             test_disabled=False,
                             method_name='RandomUnderSampler')
Exemplo n.º 6
0
                                save_predictions=False,
                                save_feature_importances=False,
                                local_file="my_tree.p",
                                log_filename="my_tree.log")


my_pipe = Hyperpipe('basic_stacking',
                    optimizer='grid_search',
                    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_stack = PipelineStacking('final_stack', voting=True)
my_pipe_stack += svm_branch
my_pipe_stack += tree_branch

my_pipe += my_pipe_stack
Exemplo n.º 7
0
    def load_single_networks(self, use_cached=False):
        if not use_cached:
            # load yeo rois
            atlas_info = AtlasInfo(
                atlas_name=
                'Yeo2011_7Networks_MNI152_FreeSurferConformed1mm_LiberalMask',
                roi_names='all',
                extraction_mode='vec')
            atlas = PipelineElement('BrainAtlas', {},
                                    atlas_info_object=atlas_info)
            rois, _, _ = atlas.transform(self.vbm_paths)

            # add subcortical
            atlas_info = AtlasInfo(
                atlas_name='VBM_AAL_mitWFU_higher_res_subcort_TH-HC_A_Cau_Pu',
                roi_names='all',
                extraction_mode='vec')
            atlas = PipelineElement('BrainAtlas', {},
                                    atlas_info_object=atlas_info)
            rois.append(atlas.transform(self.vbm_paths)[0])

            # add cerebellum
            atlas_info = AtlasInfo(atlas_name='VBM_Cerebellum_from_AAL',
                                   roi_names='all',
                                   extraction_mode='vec')
            atlas = PipelineElement('BrainAtlas', {},
                                    atlas_info_object=atlas_info)
            rois.append(atlas.transform(self.vbm_paths)[0])

            self.networks = rois

            X = np.empty((self.n, 0))
            filter_indices = OrderedDict()
            for i, network in enumerate(self.networks):
                filter_indices['network_' + str(i)] = np.arange(
                    X.shape[1], X.shape[1] + network.shape[1])
                X = np.concatenate([X, network], axis=1)
                print('Network {} Number of Voxels {}'.format(
                    i, network.shape[1]))

            self.networks_as_matrix = X
            self.filter_indices = filter_indices

            if not os.path.exists(self.folder + 'data/cache/'):
                os.mkdir(self.folder + 'data/cache/')
            for i, network in enumerate(self.networks):
                if self.tiv_rescaled:
                    np.save(
                        self.folder + 'data/cache/network_' + str(i) +
                        '_residuals.npy', network)
                else:
                    np.save(
                        self.folder + 'data/cache/network_' + str(i) +
                        '_residuals_no_tiv_rescaling.npy', network)
            np.save(self.folder + 'data/cache/filter_indices.npy',
                    self.filter_indices)
            if self.tiv_rescaled:
                np.save(self.folder + 'data/cache/networks_residuals.npy',
                        self.networks_as_matrix)
            else:
                np.save(
                    self.folder +
                    'data/cache/networks_residuals_no_tiv_rescaling.npy',
                    self.networks_as_matrix)
        else:
            self.networks = list()
            for i in range(9):
                if self.tiv_rescaled:
                    self.networks.append(
                        np.load(self.folder + 'data/cache/network_' + str(i) +
                                '_residuals.npy'))
                else:
                    self.networks.append(
                        np.load(self.folder + 'data/cache/network_' + str(i) +
                                '_residuals_no_tiv_rescaling.npy'))
            if self.tiv_rescaled:
                self.networks_as_matrix = np.load(
                    self.folder + 'data/cache/networks_residuals.npy')
            else:
                self.networks_as_matrix = np.load(
                    self.folder +
                    'data/cache/networks_residuals_no_tiv_rescaling.npy')
            self.filter_indices = np.load(self.folder +
                                          'data/cache/filter_indices.npy')
Exemplo n.º 8
0
    # skips next folds of inner cv if accuracy and precision in first fold are below 0.96.
    performance_constraints=[
        MinimumPerformance('accuracy', 0.96),
        MinimumPerformance('precision', 0.96)
    ],
    verbosity=1)  # get error, warn and info message                    )

# SHOW WHAT IS POSSIBLE IN THE CONSOLE
PhotonRegister.list()

# NOW FIND OUT MORE ABOUT A SPECIFIC ELEMENT
PhotonRegister.info('SVC')

# ADD ELEMENTS TO YOUR PIPELINE
# first normalize all features
my_pipe += PipelineElement('StandardScaler')
# then do feature selection using a PCA, specify which values to try in the hyperparameter search
my_pipe += PipelineElement('PCA',
                           hyperparameters={'n_components': [5, 10, None]},
                           test_disabled=True)
# engage and optimize the good old SVM for Classification
my_pipe += PipelineElement('SVC',
                           hyperparameters={
                               'kernel': Categorical(['rbf', 'linear']),
                               'C': FloatRange(0.5, 2, "linspace", num=5)
                           })

# NOW TRAIN YOUR PIPELINE
my_pipe.fit(X, y)

# AND SHOW THE RESULTS IN THE WEBBASED PHOTON INVESTIGATOR TOOL