Skip to content

How to query PHOTONAI's result logging

There is a whole bunch of information stored in PHOTONAI's result logging tree. Here we showcase some convenience function to easily retrieve useful result details.

 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import pandas as pd
import numpy as np
from sklearn.model_selection import StratifiedShuffleSplit

from photonai.base import Hyperpipe, PipelineElement
from photonai.optimization import FloatRange
from sklearn.datasets import fetch_openml

# blood-transfusion-service-center
blood_transfusion = fetch_openml(name='blood-transfusion-service-center')
X = blood_transfusion.data.values
y = blood_transfusion.target.values
y = (y == '2').astype(int)

my_pipe = Hyperpipe('results_example',
                    optimizer='sk_opt',
                    optimizer_params={'n_configurations': 15, 'acq_func_kwargs': {'kappa': 1}},
                    metrics=['accuracy', 'f1_score'],
                    best_config_metric='f1_score',
                    outer_cv=StratifiedShuffleSplit(n_splits=3, test_size=0.2),
                    inner_cv=StratifiedShuffleSplit(n_splits=4, test_size=0.2),
                    verbosity=0,
                    project_folder='./tmp')

# first normalize all features
my_pipe += PipelineElement('StandardScaler')
my_pipe += PipelineElement('SVC', hyperparameters={'C': FloatRange(0.1, 150)}, probability=True)

my_pipe.fit(X, y)

# Either, we continue working with the results directly now
handler = my_pipe.results_handler
#, or we load them again later.
# from photonai.processing import ResultsHandler
# handler = ResultsHandler().load_from_file(os.path.join(my_pipe.results.output_folder, "photon_results_file.json"))


# A table with properties and performance of each outer
# fold (and the overall run) is created with the following command.
performance_table = handler.get_performance_table()
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
    print(performance_table)
print(" ")

# We now analyze the optimization influence on the result.
config_evals = handler.get_config_evaluations()
for i, j in enumerate(config_evals['f1_score']):
    print("Standard deviation for fold {}: {}.".format(str(i), str(np.std(j))))
print(" ")

# To get an impression of the results,
# it is possible to take a closer look at the test_predictions.
best_config_preds = handler.get_test_predictions()
y_pred = best_config_preds['y_pred']
y_pred_probabilities = best_config_preds['probabilities']
y_true = best_config_preds['y_true']

# While some elements have been misclassified,
# we have a closer look to the elementwise probability.
for i in range(2, 6):
    attribute = "correct" if y_true[i] == y_pred[i] else "incorrect"
    print("Test-element {} was {} predicted "
          "with an assignment probability of {}.".format(str(i), attribute, str(y_pred_probabilities[i])))