Graph Measure Pipeline example¶
This example will show you how you can use graph measures directly in your hyperpipe.
In [1]:
Copied!
from photonai.base import Hyperpipe, PipelineElement
from photonai_graph.GraphUtilities import get_random_connectivity_data, get_random_labels
from sklearn.model_selection import KFold
from photonai.base import Hyperpipe, PipelineElement
from photonai_graph.GraphUtilities import get_random_connectivity_data, get_random_labels
from sklearn.model_selection import KFold
Generate random matrices to simulate connectivity matrices
In [2]:
Copied!
X = get_random_connectivity_data(number_of_nodes=50, number_of_individuals=200)
y = get_random_labels(l_type="regression", number_of_labels=200)
X = get_random_connectivity_data(number_of_nodes=50, number_of_individuals=200)
y = get_random_labels(l_type="regression", number_of_labels=200)
Design your Pipeline
In [3]:
Copied!
my_pipe = Hyperpipe('basic_measure_pipe',
inner_cv=KFold(n_splits=5),
outer_cv=KFold(n_splits=5),
optimizer='grid_search',
metrics=['mean_absolute_error'],
best_config_metric='mean_absolute_error')
my_pipe.add(PipelineElement('GraphConstructorThreshold', threshold=0.95))
my_pipe.add(PipelineElement('GraphMeasureTransform', graph_functions={"global_efficiency": {},
"local_efficiency": {},
"average_clustering": {},
"degree_centrality": {},
"betweenness_centrality": {},
"overall_reciprocity": {}}))
my_pipe.add(PipelineElement("SVR"))
my_pipe = Hyperpipe('basic_measure_pipe',
inner_cv=KFold(n_splits=5),
outer_cv=KFold(n_splits=5),
optimizer='grid_search',
metrics=['mean_absolute_error'],
best_config_metric='mean_absolute_error')
my_pipe.add(PipelineElement('GraphConstructorThreshold', threshold=0.95))
my_pipe.add(PipelineElement('GraphMeasureTransform', graph_functions={"global_efficiency": {},
"local_efficiency": {},
"average_clustering": {},
"degree_centrality": {},
"betweenness_centrality": {},
"overall_reciprocity": {}}))
my_pipe.add(PipelineElement("SVR"))
Element not supported right now:GraphMeasureTransform
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[3], line 10 1 my_pipe = Hyperpipe('basic_measure_pipe', 2 inner_cv=KFold(n_splits=5), 3 outer_cv=KFold(n_splits=5), 4 optimizer='grid_search', 5 metrics=['mean_absolute_error'], 6 best_config_metric='mean_absolute_error') 8 my_pipe.add(PipelineElement('GraphConstructorThreshold', threshold=0.95)) ---> 10 my_pipe.add(PipelineElement('GraphMeasureTransform', graph_functions={"global_efficiency": {}, 11 "local_efficiency": {}, 12 "average_clustering": {}, 13 "degree_centrality": {}, 14 "betweenness_centrality": {}, 15 "overall_reciprocity": {}})) 17 my_pipe.add(PipelineElement("SVR")) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/photon_elements.py:109, in PipelineElement.__init__(self, name, hyperparameters, test_disabled, disabled, base_element, batch_size, **kwargs) 106 else: 107 # if even after reload the element does not appear, it is not supported 108 logger.error('Element not supported right now:' + name) --> 109 raise NameError('Element not supported right now:', name) 110 else: 111 self.base_element = base_element NameError: ('Element not supported right now:', 'GraphMeasureTransform')
Finally fit the pipeline to your data
In [4]:
Copied!
my_pipe.fit(X, y)
my_pipe.fit(X, y)
===================================================================================================== PHOTONAI ANALYSIS: basic_measure_pipe ===================================================================================================== Last element in Hyperpipe should be an estimator. Traceback (most recent call last): File "/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/hyperpipe.py", line 1041, in fit self.check_for_imbalanced_data() File "/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/hyperpipe.py", line 836, in check_for_imbalanced_data if self.estimation_type == 'classifier': File "/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/hyperpipe.py", line 642, in estimation_type raise NotImplementedError("Last element in Hyperpipe should be an estimator.") NotImplementedError: Last element in Hyperpipe should be an estimator.
Traceback (most recent call last): File "/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/hyperpipe.py", line 1041, in fit self.check_for_imbalanced_data() File "/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/hyperpipe.py", line 836, in check_for_imbalanced_data if self.estimation_type == 'classifier': File "/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/hyperpipe.py", line 642, in estimation_type raise NotImplementedError("Last element in Hyperpipe should be an estimator.") NotImplementedError: Last element in Hyperpipe should be an estimator.
--------------------------------------------------------------------------- NotImplementedError Traceback (most recent call last) Cell In[4], line 1 ----> 1 my_pipe.fit(X, y) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/hyperpipe.py:1119, in Hyperpipe.fit(self, data, targets, **kwargs) 1117 logger.error(traceback.format_exc()) 1118 traceback.print_exc() -> 1119 raise e 1120 finally: 1121 if self.nr_of_processes > 1: File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/hyperpipe.py:1041, in Hyperpipe.fit(self, data, targets, **kwargs) 1039 # sanity check data with setup 1040 if not self.ignore_sanity_checks: -> 1041 self.check_for_imbalanced_data() 1043 # create photon pipeline 1044 self._prepare_pipeline() File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/hyperpipe.py:836, in Hyperpipe.check_for_imbalanced_data(self) 835 def check_for_imbalanced_data(self): --> 836 if self.estimation_type == 'classifier': 837 targets = np.unique(self.data.y) 838 num_classes = len(targets) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/base/hyperpipe.py:642, in Hyperpipe.estimation_type(self) 640 estimation_type = getattr(self.elements[-1], '_estimator_type') 641 if estimation_type is None: --> 642 raise NotImplementedError("Last element in Hyperpipe should be an estimator.") 643 else: 644 return estimation_type NotImplementedError: Last element in Hyperpipe should be an estimator.