This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.py
392 lines (308 loc) · 13 KB
/
index.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import os
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import plotly.graph_objects as go
import plotly.express as px
from plotly.colors import n_colors
import redis
import json
import datetime as dt
import time
import requests
from typing import Tuple
import pandas as pd
import numpy as np
from itertools import repeat
# Custom component
from feet_animation import FeetAnimation
REDIS_HOST = os.getenv('REDIS_HOST') or 'localhost'
store = redis.Redis(REDIS_HOST)
def map_value_to_RGB(value: float) -> Tuple[float, float, float]:
"""Linear interpolation from sensor value to RGB color"""
smallest, biggest = 0, 1023 # From doctor Zawadzki's documentation
value_range = [smallest, biggest]
# Mapping from white to green
red_range = [255, 85]
green_range = [255, 255]
blue_range = [255, 85]
r = np.interp(value, value_range, red_range)
g = np.interp(value, value_range, green_range)
b = np.interp(value, value_range, blue_range)
return r, g, b
def create_RGB_string(rgb: Tuple[float, float, float]) -> str:
"""Create rgb string for Plotly"""
value = ', '.join((str(c) for c in rgb))
return f'rgb({value})'
def map_value_to_RGB_string(value: float) -> str:
"""Support function to join mapping value and converting to Ploty string"""
return create_RGB_string(map_value_to_RGB(value))
def make_table(values, cell_colors=None):
"""Create table for displaying sensor values from values and corresponding colors"""
table = go.Figure(
layout=dict(
title=dict(
text='Sensor values over last period',
font=dict(
size=30,
color='black'
)
)
),
data=[
go.Table(
header=dict(values=[c.replace('_', ' ') for c in values.keys()],
fill_color='paleturquoise',
align='left'),
cells=dict(values=list(values.values()),
fill=dict(color=cell_colors))
)]
)
return table
def make_anomaly_histogram(transformed):
fig = go.Figure(
data=[go.Histogram(x=transformed['time'])])
return fig
def make_foot_pressure_indicator(sensor_number, value, previous_value=None) -> dict:
"""Create figure of analog meter displaying value of singe pressure sensor"""
return dict(
data=[
dict(
type='indicator',
mode='number+delta+gauge',
title=f'Foot pressure sensor {sensor_number}',
value=value,
delta=dict(reference=previous_value, relative=False),
gauge=dict(
axis=dict(visible=True, range=[0, 1023])
),
# domain=dict(x=[0, .6])
)
],
layout=dict(width=500, height=370)
)
external_scripts = ['https://kit.fontawesome.com/620ce16426.js']
app = dash.Dash(__name__, external_stylesheets=[
dbc.themes.BOOTSTRAP], external_scripts=external_scripts)
app.layout = html.Div(
className='wrapper',
children=[
# Store current person id
dcc.Store(id='current-id', storage_type='session'),
# Store last anomaly
dcc.Store(id='last-anomaly'),
dcc.Interval(id='interval-component',
interval=1*1000,
n_intervals=0),
html.Nav(className='navbar navbar-dark bg-primary',
children=[
html.A(className='navbar-brand', children=[
html.I(className='fas fa-shoe-prints fa-rotate-270 mr-1'),
'Magic feet'
]),
html.A(className='nav-item', style={'color': 'white'}, href='https://github.com/SiwyKrzysiek/magic-foots', children=[
'See it on Github',
html.I(className='fab fa-github ml-1'),
])
]
),
html.Main(className='container-fluid mb-4', children=[
# Project description
html.Section(className='text-center my-4', children=[
html.H3(
'Final project for Python programming and data visualization'),
html.Div(
[
dbc.Button(
['More info', html.I(
className='ml-2 fas fa-info-circle')],
id='collapse-button',
className='mb-3',
color='info'
),
dbc.Collapse(
dbc.Card(dbc.CardBody(
'This project attempts to visualize data gathered live from pressure sensors placed on feet of 6 participants.')),
id='collapse',
),
],
className='my-3'
)
]
),
# Select person
dcc.Tabs(id="tabs", className='nav-item', value='1', children=[
dcc.Tab(label='Person one', value='1'),
dcc.Tab(label='Person two', value='2'),
dcc.Tab(label='Person three', value='3'),
dcc.Tab(label='Person four', value='4'),
dcc.Tab(label='Person five', value='5'),
dcc.Tab(label='Person six', value='6'),
]),
html.Section(className='tabs-content', children=[
# Person name
html.H1(className='display-4', children=[
html.I(className='fas fa-user-circle mr-2'),
html.Span(id='person-name')
]),
html.Section(className='shadow bg-white rounded mt-5', children=[
dcc.Graph(id='table', className='table-light'),
]),
dbc.Row(justify="center", align="center", children=[
dbc.Col(className='shadow bg-white rounded', children=[
html.H4('Sensor placement',
className='text-center py-3'),
FeetAnimation(id='feet-animation')
], style={'margin-left': '15px'}),
dbc.Col(
html.Div(id='single-sensor-container', className='sensor', children=[ # Display single selected sensor
dcc.Tabs(id='single-sensor-tabs', value='1',
children=[dcc.Tab(label=str(i), value=str(i), className='single-sensor-tab') for i in range(0, 6)]),
dcc.Graph(id='singe-sensor-indicator')
])
)
]
),
html.Section(className='shadow bg-white rounded container-fluid pt-3', children=[
html.H3('Anomaly accumulation histogram'),
html.Div(id='last_anomaly_mess'),
dcc.Graph(id='anomaly_graph', className='anomaly_graph')
]),
]),
]),
html.Footer(className='footer', children=[
html.H1('PW EE'),
html.P('© 2020', className='mb-1'),
html.P('Authors: Mara Kruk and Krzysztof Dąbrowski', className='mb-0')
])
])
@app.callback(
Output("collapse", "is_open"),
[Input("collapse-button", "n_clicks")],
[State("collapse", "is_open")],
)
def toggle_collapse(n, is_open):
"""Toggle more info about project"""
if n:
return not is_open
return is_open
@app.callback([Output('last_anomaly_mess', 'children'),
Output('last_anomaly_mess', 'style')],
[Input('last-anomaly', 'modified_timestamp')],
[State('last-anomaly', 'data')])
def update_last_anomaly(ts, data):
"""Display data about most recent anomaly"""
if ts is None:
PreventUpdate
data = data or {'time': None, 'sensor': None}
time_d = data['time']
if time_d == None:
return "", {'display': 'hidden'}
sensor = data['sensor']
label = f'Last anomaly was {time_d} at sensors {sensor}'
return label, {}
@app.callback([Output('current-id', 'data'),
Output('person-name', 'children')
],
[Input('tabs', 'value')])
def on_person_tab_change(new_id):
"""Switch current person"""
key = f'personData{new_id}'
preson = json.loads(store.lrange(key, 0, 0)[0])
firstName, lastName, birthdate = preson['firstname'], preson['lastname'], preson['birthdate']
person_description = f'{firstName} {lastName} {birthdate}'
return (
new_id,
person_description
)
@app.callback([Output('anomaly_graph', 'figure'),
Output('last-anomaly', 'data')],
[Input('interval-component', 'n_intervals'),
Input('current-id', 'data')],
[State('last-anomaly', 'data')])
def update_anomaly_histogram(n_intervals, current_id, last_anomaly_data):
"""Generate anomaly histogram and display it"""
if current_id is None:
raise PreventUpdate
key = f'personData{current_id}'
rawList = store.lrange(key, 0, -1) # -1 is the last element
data = [json.loads(d.decode()) for d in rawList]
last_anomaly_data = {'time': None, 'sensor': None}
transformed = {'time': []}
latest_anomalies = None
for record in data:
time = dt.datetime.fromtimestamp(record['timestamp'])
anomalies = sum((s['anomaly'] for s in record['trace']['sensors']))
if anomalies > 0 and (latest_anomalies is None or latest_anomalies['time'] < time):
latest_anomalies = {
'data': record['trace']['sensors'], 'time': time}
transformed['time'].extend(repeat(time, anomalies))
if latest_anomalies is not None:
abnormal_sensors = (s['id']
for s in latest_anomalies['data'] if s['anomaly'])
sensors_message = ' ,'.join((f'Sensor {i}' for i in abnormal_sensors))
last_anomaly_data = {
'time': latest_anomalies['time'].strftime("%m/%d/%Y, %H:%M:%S"),
'sensor': sensors_message
}
return make_anomaly_histogram(transformed), last_anomaly_data
@app.callback(Output('feet-animation', 'sensorValues'),
[Input('interval-component', 'n_intervals'),
Input('current-id', 'data')])
def update_feet_animation(_, current_id):
"""Pass sensor data to Feet Animation custom component"""
if current_id is None:
raise PreventUpdate
key = f'personData{current_id}'
rawList = store.lrange(key, 0, 0)
data = [json.loads(d.decode()) for d in rawList][0]
values = [sensor['value'] for sensor in data['trace']['sensors']]
return values
@app.callback(Output('table', 'figure'),
[Input('interval-component', 'n_intervals'),
Input('current-id', 'data')],)
def update_table(n_intervals, current_id):
"""Create data for main table and draw it"""
if current_id is None:
raise PreventUpdate
TABLE_SIZE = 20
key = f'personData{current_id}'
rawList = store.lrange(key, 0, TABLE_SIZE)
data = [json.loads(d.decode()) for d in rawList]
values = {'time': [], 'sensor_0': [], 'sensor_1': [],
'sensor_2': [], 'sensor_3': [], 'sensor_4': [], 'sensor_5': []}
for value in data:
datetime = dt.datetime.fromtimestamp(value['timestamp'])
values['time'].append(datetime.strftime("%m/%d/%Y, %H:%M:%S"))
sensors = value['trace']['sensors']
for s in sensors:
id = s['id']
key = f'sensor_{id}'
values[key].append(s['value'])
colors = {}
for key in values.keys():
if key == 'time':
colors[key] = ['white'] * len(values[key])
else:
colors[key] = [map_value_to_RGB_string(v) for v in values[key]]
return make_table(values, list(colors.values()))
@app.callback(Output('singe-sensor-indicator', 'figure'),
[Input('interval-component', 'n_intervals'),
Input('single-sensor-tabs', 'value'),
Input('current-id', 'data')])
def update_singe_sensor_indicator(_, selected_sensor, current_id):
"""Update analog style indicator"""
if current_id is None or selected_sensor is None:
raise PreventUpdate
key = f'personData{current_id}'
rawList = store.lrange(key, 0, 1)
data = [json.loads(d.decode()) for d in rawList]
values = (value['trace']['sensors'][int(selected_sensor)]['value']
for value in data)
return make_foot_pressure_indicator(selected_sensor, *values)
if __name__ == '__main__':
debug = False if os.environ.get('PRODUCTION') == 'true' else True
app.run_server(debug=debug, host='0.0.0.0')