-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
187 lines (169 loc) · 5.99 KB
/
app.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import redis
import dash
import dash_daq as daq
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import numpy as np
NAMESPACE = "filterbank-directory-monitor"
external_stylesheets = []
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
client = redis.StrictRedis("apsuse-monitor-redis")
# from redis client:
# filterbank-directory-monitor:directory /TEST/
# filterbank-directory-monitor:coherent:file arse.fil
# filterbank-directory-monitor:coherent:bandpass [] #packed numpy array
# filterbank-directory-monitor:incoherent:file other_arse.fil
# filterbank-directory-monitor:incoherent:bandpass [] #packed numpy array
colors = {
'background': '#212124', # To match Grafana background
'text': '#7FDBFF',
'background-color': '#212124'
}
h4_style = {
'color': '#CCCCCC',
'margin-bottom': "0px",
'margin-left': "10px",
'padding-top': "0px"
}
h4_style_top = {
'color': '#CCCCCC',
'margin-bottom': "0px",
'margin-left': "10px",
'padding-top': "0px"
}
text_style = {
"color": colors['text'],
"font-family": "arial,helvetica",
"font-size": 16,
"margin-bottom": "0px"
}
app.layout = html.Div(children=[
html.H4(id='directory-label', children="Current directory: ", style=h4_style_top),
html.H4(id='cb-file-label', children="CB file: ", style=h4_style),
html.H4(id='ib-file-label', children="IB file: ", style=h4_style),
dcc.Graph(
id='bandpass-graph',
figure={
'data': [
{'x': [0], 'y': [0], 'type': 'line', 'name': 'Coherent Beam'},
{'x': [0], 'y': [0], 'type': 'line', 'name': 'Incoherent Beam'}
],
'layout': {
'title': 'Bandpass Monitor',
'xaxis': {
'title': 'Frequency (MHz)'
},
'yaxis': {
'title': 'Statistic'
},
'transition': {
'duration': 500,
'easing': 'cubic-in-out'
},
'legend': {
'orientation': 'h',
'yanchor': 'bottom',
'y': -0.2
},
'plot_bgcolor': colors['background'],
'paper_bgcolor': colors['background'],
'font': {
'color': colors['text']
}
}
},
),
dcc.Interval(
id='interval-component',
interval=10*1000,
n_intervals=0
),
html.Div(id='controls-div', children=[
html.Div(id='toggle-div', children=[
daq.ToggleSwitch(
id='hold-toggle', value=False, size=50,
label={
"label": "Prevent updates",
"style": text_style
},
labelPosition="top",
className=".toggle-switch")
],
style={
'float': 'left',
'left': '30px',
'width': '200px',
'background-color': '#212124'
}),
html.Div(id='dropdown-div', children=[
html.Label([
"Select statistic",
dcc.Dropdown(
id='statistic-dropdown',
options=[
{'label': 'Mean', 'value': 'mean'},
{'label': 'Standard deviation', 'value': 'std'}
],
value='mean',
style=text_style
)],
style=text_style)],
style={
'float': 'left',
'left': '30px',
'width': '200px',
'background-color': '#212124'
}),
],
style={
'background-color': '#212124'
})
], style=colors)
def upack_numpy_array(packed):
"""
Unpack a numpy array from a string
:param packed: Numpy array that has been packed as
[('frequency', 'float32', ), ('power', 'float32')]
:type packed: { type_description }
"""
return np.frombuffer(packed, dtype=[
("frequency", "float32"),
("mean", "float32"),
("std", "float32")])
@app.callback(
[Output(component_id='bandpass-graph', component_property='figure'),
Output(component_id='directory-label', component_property='children'),
Output(component_id='cb-file-label', component_property='children'),
Output(component_id='ib-file-label', component_property='children')],
[Input('interval-component', 'n_intervals'),
Input('statistic-dropdown', 'value')],
[State('bandpass-graph', 'figure'),
State('hold-toggle', 'value')])
def update_plot(n_intervals, stat_selection, figure, hold):
if hold:
raise PreventUpdate
for line in figure['data']:
if line["name"] == 'Coherent Beam':
beam = upack_numpy_array(
client.get("{}:coherent:bandpass".format(NAMESPACE)))
elif line["name"] == 'Incoherent Beam':
beam = upack_numpy_array(
client.get("{}:incoherent:bandpass".format(NAMESPACE)))
else:
print("Unknown data set: '{}'".fomat(line["name"]))
line["x"] = beam["frequency"]
line["y"] = beam[stat_selection]
figure["layout"]["uirevision"] = True
# I would like to update the figure title
# here but it seems to be bugged out
dir_label = "Current directory: {}".format(
client.get("{}:directory".format(NAMESPACE)).decode())
cb_file = "CB file: {}".format(
client.get("{}:coherent:file".format(NAMESPACE)).decode())
ib_file = "IB file: {}".format(
client.get("{}:incoherent:file".format(NAMESPACE)).decode())
return figure, dir_label, cb_file, ib_file
if __name__ == '__main__':
app.run_server(host="0.0.0.0", debug=True)