Passed
Push — master ( 6b2285...b7fe1a )
by Simon
02:03 queued 11s
created

Insight.plot_performance()   B

Complexity

Conditions 4

Size

Total Lines 58
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 58
rs 8.8478
c 0
b 0
f 0
cc 4
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import numpy as np
6
import pandas as pd
7
import plotly as py
8
import plotly.graph_objects as go
9
import plotly.express as px
10
11
from ... import Memory
12
from ... import Hyperactive
13
14
15
class Insight:
16
    def __init__(self, search_config, X, y):
17
        self.search_config = search_config
18
        self.X = X
19
        self.y = y
20
21
    def plot_performance(self, runs=3, path=None, optimizers="all"):
22
        if optimizers == "all":
23
            optimizers = [
24
                "HillClimbing",
25
                "StochasticHillClimbing",
26
                "TabuSearch",
27
                "RandomSearch",
28
                "RandomRestartHillClimbing",
29
                "RandomAnnealing",
30
                "SimulatedAnnealing",
31
                "StochasticTunneling",
32
                "ParallelTempering",
33
                "ParticleSwarm",
34
                "EvolutionStrategy",
35
                "Bayesian",
36
            ]
37
38
        eval_times = []
39
        total_times = []
40
        for run in range(runs):
41
42
            eval_time = []
43
            total_time = []
44
            for optimizer in optimizers:
45
                opt = Hyperactive(self.X, self.y, memory=False)
46
                opt.search(self.search_config, n_iter=3, optimizer=optimizer)
47
48
                eval_time.append(opt.eval_time)
49
                total_time.append(opt.total_time)
50
51
            eval_time = np.array(eval_time)
52
            total_time = np.array(total_time)
53
54
            eval_times.append(eval_time)
55
            total_times.append(total_time)
56
57
        eval_times = np.array(eval_times)
58
        total_times = np.array(total_times)
59
        opt_times = np.subtract(total_times, eval_times)
60
61
        opt_time_mean = opt_times.mean(axis=0)
62
        eval_time_mean = eval_times.mean(axis=0)
63
        total_time_mean = total_times.mean(axis=0)
64
65
        # opt_time_std = opt_times.std(axis=0)
66
        # eval_time_std = eval_times.std(axis=0)
67
68
        eval_time = eval_time_mean / total_time_mean
69
        opt_time = opt_time_mean / total_time_mean
70
71
        fig = go.Figure(
72
            data=[
73
                go.Bar(name="Eval time", x=optimizers, y=eval_time),
74
                go.Bar(name="Opt time", x=optimizers, y=opt_time),
75
            ]
76
        )
77
        fig.update_layout(barmode="stack")
78
        py.offline.plot(fig, filename="sampleplot.html")
79
80
    def plot_search_path(self, path=None, optimizers=["HillClimbing"]):
81
        for optimizer in optimizers:
82
            opt = Hyperactive(self.X, self.y, memory=False, verbosity=10)
83
            opt.search(self.search_config, n_iter=20, optimizer=optimizer)
84
85
            pos_list = opt.pos_list
86
            score_list = opt.score_list
87
88
            pos_list = np.array(pos_list)
89
            score_list = np.array(score_list)
90
91
            pos_list = np.squeeze(pos_list)
92
            score_list = np.squeeze(score_list)
93
94
            df = pd.DataFrame(
95
                {
96
                    "n_neighbors": pos_list[:, 0],
97
                    "leaf_size": pos_list[:, 1],
98
                    "score": score_list,
99
                }
100
            )
101
102
            layout = go.Layout(xaxis=dict(range=[0, 50]), yaxis=dict(range=[0, 50]))
103
            fig = go.Figure(
104
                data=go.Scatter(
105
                    x=df["n_neighbors"],
106
                    y=df["leaf_size"],
107
                    mode="lines+markers",
108
                    marker=dict(
109
                        size=10,
110
                        color=df["score"],
111
                        colorscale="Viridis",  # one of plotly colorscales
112
                        showscale=True,
113
                    ),
114
                ),
115
                layout=layout,
116
            )
117
118
            py.offline.plot(fig, filename="search_path" + optimizer + ".html")
119