-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
280 lines (240 loc) · 8.11 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
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
import dash
from dash.dependencies import Output, Input
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objects as go
from collections import deque
import subprocess
from PIL import Image
from analytics.Analytics import ObluAnalytics
# For reading the last line of sensor data values stored in a file
def tail():
result = subprocess.run(['tail', '-1', 'sensor/GetData/steps.txt'], stdout=subprocess.PIPE)
return result.stdout.decode('utf-8')
# For offline steps plotting
# file = open('sensor/GetData/steps.txt', 'r')
file = open('analytics/steps_test.txt', 'r')
# ignoring the first line
file.readline()
img = Image.open('docs/rm3_1.png')
max_trail_limit = 15
X = deque(maxlen=max_trail_limit)
Y = deque(maxlen=max_trail_limit)
# counter = 0
interval = 1000 # Timer for updating the graph in msec
# Initial data
data = tail()
X.append(data.split(',')[1])
Y.append(data.split(',')[2])
external_stylesheets = [
'https://codepen.io/chriddyp/pen/bWLwgP.css',
{
'href': 'https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css',
'rel': 'stylesheet',
'integrity': 'sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO',
'crossorigin': 'anonymous'
}
]
# Variable for storing Departure Score from Analytics
S = deque(maxlen=max_trail_limit)
S.append(5)
# Variable for Time step
T = deque(maxlen=max_trail_limit)
T.append(1)
# Object for analytics
obj = ObluAnalytics(lag_vector_length=max_trail_limit)
UT, centroid, theta = obj.get_threshold_score('analytics/steps_train.txt')
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
style={'backgroundColor': '#111111'},
children=[
html.Div(
className="app-header",
children=[
html.Div('bluTrack', className="app-header--title"),
]
),
html.Div(
className="app-header",
children=[
html.P('Pedestrian dead reckoning and Outlier Detection in Trajectory Data',
className="app-header--sub-head")
],
),
# ------------two column divs start------
html.Div([
html.Div([
html.Div(
className="app-image",
children=[
html.Img(src=img, alt='bg_image')
]
),
html.Div(
className="app-plot",
id="app-plotid",
children=[
dcc.Graph(
id='live-graph',
animate=False,
config={
'autosizable': True,
'scrollZoom': True,
'displayModeBar': False,
}
)
],
),
dcc.Slider(
id='rotate-slider',
min=-180,
max=180,
step=2,
value=0
),
html.Div(id='slider-output-container'),
dcc.Interval(
id='graph-update',
interval=interval,
n_intervals=0
),
], className="six columns"),
html.Div([
# html.H3('Column 2'),
# dcc.Graph(id='g2', figure={'data': [{'y': [1, 2, 3]}]})
# html.P('Analytics here. Coming soon', className='header-analytics'),
dcc.Graph(id='app-analytics', animate=True),
dcc.Interval(id='analytics-update', interval=interval, n_intervals=0)
], className="six columns"),
], className="row"),
# ------two columns divs end------------
]
)
@app.callback(Output('app-analytics', 'figure'),
[Input('analytics-update', 'n_intervals')])
def update_analytics(n):
global S, obj, UT, centroid, theta, max_trail_limit
global X
global Y
# S.append(S[-1]+(random.uniform(-.5,.5)))
T.append(T[-1] + 1)
# print([pd.DataFrame(x) for x in zip(list(X),list(Y))])
# print(pd.DataFrame([sum(x)/2 for x in zip(list(X), list(Y))]))
# print('UT={}, centroid={}, theta={}'.format(UT, centroid, theta))
if len(T) >= max_trail_limit:
# stream = [sum(x)/2 for x in list(zip(list(X),list(Y)))]
# df = pd.DataFrame(list(X))
# df = df[0] / 2
score = obj.get_score(UT, centroid, X, Y)
# print(score)
S.append(score)
realtime_data = go.Scatter(
x=list(T),
y=list(S),
name='Realtime Anomaly Score',
mode='lines',
# showlegend=False,
)
threshold_data = go.Scatter(
x=[min(T), max(T) + 20],
y=[float(theta)] * 50,
name='Threshold Score',
mode='lines',
# showlegend=False,
)
layout = go.Layout(
# title='Analytics-Live',
xaxis=dict(range=[min(T), max(T) + 20]),
# yaxis=dict(range=[min(S),max(S)]),
yaxis=dict(range=[0, 100]),
template='plotly_dark',
uirevision='analytics-update',
)
fig = go.Figure(data=[realtime_data, threshold_data],
layout=layout)
fig.update_layout(xaxis_title="No of Steps",
yaxis_title="Score", )
legend = dict(
x=0,
y=1,
traceorder="normal",
font=dict(
family="sans-serif",
size=12,
color="black"
),
bgcolor="LightSteelBlue",
bordercolor="Black",
borderwidth=2
)
fig.update_layout(legend=legend)
return fig
@app.callback(Output('app-plotid', 'style'),
[Input('rotate-slider', 'value')])
def update_style(value):
return {'transform': 'rotate({}deg)'.format(value)}
@app.callback(
dash.dependencies.Output('slider-output-container', 'children'),
[dash.dependencies.Input('rotate-slider', 'value')])
def update_output(value):
return 'Drag the slider to set orientation of plot. Current: {} deg'.format(value)
@app.callback(
Output('live-graph', 'figure'),
[Input('graph-update', 'n_intervals')]
)
def update_graph(n):
global X
global Y
# global counter
# counter += interval/1000
# -----------------------------------------------------
# For real-time plot
# new_x = str(tail()).split(',')[1]
# new_y = str(tail()).split(',')[2]
# -----------------------------------------------------
# -----------------------------------------------------
# For simulating data saved in real-time
if not file == "":
file_data = file.readline().split(',')
print(file_data)
new_x, new_y = file_data[1], file_data[2]
else:
file.seek(0, 0)
# # print(new_x, new_y)
# -----------------------------------------------------
if not (X == new_x and Y == new_y):
X.append(new_x)
Y.append(new_y)
# # Add trace
plot_data = go.Scatter(
x=list(X),
y=list(Y),
name='Scatter',
mode='lines+markers'
# mode = 'lines'
)
x_range = [-20, 65]
y_range = [-30, 65]
layout = go.Layout(xaxis=dict(range=x_range),
yaxis=dict(range=y_range),
height=500,
showlegend=False,
uirevision='graph-update',
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
)
# Create figure
fig = go.Figure(
data=[plot_data],
layout=layout
)
# Fine tune layout
# fig.update_layout(template="plotly_white")
fig.update_xaxes(showticklabels=False, zeroline=False)
fig.update_yaxes(showticklabels=False, zeroline=False)
fig.update_layout(xaxis_showgrid=False, yaxis_showgrid=False)
fig.update_layout(dragmode='pan')
return fig
if __name__ == '__main__':
# app.run_server(debug=True, dev_tools_hot_reload=False)
app.run_server(debug=True)