Callback Elements

PHOTONAI implements pipeline callbacks which allow for live inspection of the data flowing through the pipeline at runtime. Callbacks act as pipeline elements and can be inserted at any point within the pipeline. They must define a function delegate which is called with the exact same data that the next pipeline step will receive.

Thereby, a developer may inspect e.g. the shape and values of the feature matrix after a sequence of transformations have been applied. Return values from the delegate functions are ignored, so that after returning from the delegate call, the original data is directly passed to the next processing step.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from sklearn.datasets import load_boston
from sklearn.model_selection import KFold
from photonai.base import Hyperpipe, PipelineElement, CallbackElement


# DEFINE CALLBACK ELEMENT
def my_monitor(X, y=None, **kwargs):
    print(X.shape)

    # here is a useless statement where you can easily set a breakpoint
    # and do fancy developer stuff
    debug = True

my_pipe = Hyperpipe('monitoring_pipe',
                    optimizer='grid_search',
                    metrics=['mean_squared_error', 'pearson_correlation'],
                    best_config_metric='mean_squared_error',
                    outer_cv=KFold(n_splits=3),
                    inner_cv=KFold(n_splits=3),
                    verbosity=1,
                    project_folder='./tmp/')

my_pipe += PipelineElement('StandardScaler')

my_pipe += PipelineElement('SamplePairingClassification',
                           hyperparameters={'draw_limit': [500, 1000, 10000]},
                           distance_metric='euclidean',
                           generator='nearest_pair',
                           test_disabled=True)

# here we inspect the data after augmentation
my_pipe += CallbackElement("monitor", my_monitor)

my_pipe += PipelineElement('RandomForestRegressor', hyperparameters={'n_estimators': [10, 100]})

X, y = load_boston(return_X_y=True)
my_pipe.fit(X, y)