-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize.py
More file actions
134 lines (123 loc) · 4.12 KB
/
visualize.py
File metadata and controls
134 lines (123 loc) · 4.12 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'''Author: Lorenzo Carlassara - mail: lorenzo.carlassara@mail.polimi.it
'''
import numpy as np
import plotly.graph_objs as go
from ipywidgets import interact, widgets
def update_layout_of_graph(fig: go.Figure,title: str = 'Plot')->go.Figure:
fig.update_layout(
width=800,
height=600,
autosize=False,
plot_bgcolor='rgba(0,0,0,0)',
title=title,
)
fig.update_layout(plot_bgcolor='rgba(0,0,0,0)',
xaxis_title = 'input values',
yaxis_title = 'output values',
legend=dict(orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1),
title={
'x': 0.5,
'xanchor': 'center'
})
fig.update_xaxes(showline=True, linewidth=1, linecolor='black')
fig.update_yaxes(showline=True, linewidth=1, linecolor='black')
return fig
def uncertainty_area_scatter(
visible: bool = True,
x_lines: np.array = np.array([]),
y_upper: np.array = np.array([]),
y_lower: np.array = np.array([]),
name: str = "mean plus/minus standard deviation",
legend_group: str = 'acqf',
showlegend: bool = False,
) -> go.Scatter:
return go.Scatter(
visible=visible,
x=np.concatenate((x_lines, x_lines[::-1])), # x, then x reversed
# upper, then lower reversed
y=np.concatenate((y_upper, y_lower[::-1])),
fill='toself',
fillcolor='rgba(189,195,199,0.5)',
line=dict(color='rgba(200,200,200,0)'),
hoverinfo="skip",
name= name,
legendgroup = legend_group,
showlegend=showlegend,
)
def line_scatter(
visible: bool = True,
x_lines: np.array = np.array([]),
y_lines: np.array = np.array([]),
name_line: str = 'Predicted function',
legend_group: str = 'acqf',
color: str = 'blue',
showlegend: bool = True,
) -> go.Scatter:
# Adding the lines
return go.Scatter(
visible=visible,
line=dict(color=color, width=2),
x=x_lines,
y=y_lines,
name=name_line,
legendgroup = legend_group,
showlegend= showlegend
)
def test_scatter(
visible: bool = True,
x_dots: np.array = np.array([]),
y_dots: np.array = np.array([]),
name_dots: str = 'Test',
showlegend: bool = True
) -> go.Scatter:
# Adding the dots
return go.Scatter(
x=x_dots,
visible=visible,
y=y_dots,
mode="markers",
name=name_dots,
marker=dict(color='green', size=7),
showlegend=showlegend
)
def dot_scatter(
visible: bool = True,
x_dots: np.array = np.array([]),
y_dots: np.array = np.array([]),
name_dots: str = 'Obs',
legend_group: str = 'acqf',
color: str = 'red',
showlegend: bool = False
) -> go.Scatter:
# Adding the dots
return go.Scatter(
x=x_dots,
visible=visible,
y=y_dots,
mode="markers",
legendgroup = legend_group,
name=name_dots,
marker=dict(color=color, size=7),
showlegend=showlegend
)
import torch, gpytorch
def plot_GPR(data_x, data_y, model, x, legend_group, color=None, visible=True) -> list:
with torch.no_grad(), gpytorch.settings.fast_pred_var():
observed = model(x)
lower, upper = observed.confidence_region()
data = []
data.append(
uncertainty_area_scatter(
x_lines=x.numpy(),
y_lower=lower.numpy(),
y_upper=upper.numpy(),
name=f"uncertainty",
legend_group = legend_group,
visible=visible))
data.append(line_scatter(x_lines=x.numpy(), y_lines=observed.mean.numpy(), visible=visible, name_line =legend_group, color=color, legend_group = legend_group))
data.append(dot_scatter(x_dots=data_x.numpy(), y_dots=data_y.numpy(), visible=visible, color=color, legend_group = legend_group))
return data