-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
199 lines (176 loc) · 6.68 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
#!/usr/bin/env python3
import base64
import io
import plotly.graph_objs as go
import cufflinks as cf
import xml.etree.ElementTree as etree
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import pandas as pd
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
app.config['suppress_callback_exceptions'] = True
colors = {
"graphBackground": "#F5F5F5",
"background": "#ffffff",
"text": "#000000"
}
config = {
'toImageButtonOptions':
{
'width': 1000,
'height': 600,
'format': 'png',
'filename': 'dyno'
}
}
app.layout = html.Div([
dcc.Upload(
id='upload-data',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px 0'
},
# Allow multiple files to be uploaded
multiple=True
),
dcc.Graph(id='Mygraph', config=config, style={'height': 'calc(100vh - 80px)', 'width': '100%', 'overflow': 'hidden'}),
html.Div(id='output-data-upload')
], style={'margin': '0', 'padding': '0', 'overflow': 'hidden'})
def parse_horacio_resio(hr):
""" Horacio Resio dyno software uses a kind-of CSV
format (not pure CSV 'tho), so we need to make
some transformations over the file header to get
something parsable """
hr.drop(hr.index[0], inplace=True) # Remove "Kgm Cv" line
hr.rename(columns={'RPM_VEH': 'rpm', 'POT_RUEDA': 'hp'},
inplace=True) # Normalize column data
# Convert wheel torque to crank torque. This is an "estimation" handled by a constant
hr['tq'] = hr['hp'] * 716 / hr['rpm']
hr.drop(['TIEMPO', 'RPM_ROD', 'TORQUE', 'POT_PER', 'POT_CIGUE', 'SENSOR', 'AUX1', 'SENSOR.1', 'AUX2',
'SENSOR.2', 'AUX3', 'SENSOR.3', 'AUX4', 'SENSOR.4', 'AUX5'], axis=1, inplace=True) # Remove unused columns
hr = hr.iloc[::-1] # Invert dataframe
return hr
def parse_mwd(root):
""" MWD uses a XML format file to store the data,
although it is a good format, the way the devs
at MWD handled this is quite tricky """
rpm_samples = []
tq_samples = []
hp_samples = []
for reg in root.iter('Ensayo'): # Party starts here
root1 = etree.Element('root')
root1 = reg
for canal_virtual in root1.iter('CanalVirtual'):
root2 = etree.Element('root')
root2 = canal_virtual
# Pick just the required columns
for nombre in root2.iter('Nombre'):
root3 = etree.Element('root')
root3 = nombre
if root3.text and (root3.text.lower() == "rpm motor" or root3.text.lower() == "RPM Motor Filtrada"):
for muestras in root2.iter('Muestra'):
rpm_samples = muestras.text.split(", ")
if root3.text == "Torque Corr":
for muestras in root2.iter('Muestra'):
tq_samples = muestras.text.split(", ")
if root3.text == "Potencia Corr":
for muestras in root2.iter('Muestra'):
hp_samples = muestras.text.split(", ")
mwd = pd.DataFrame( # Build a dataframe with the sanitized output
{'rpm': rpm_samples,
'tq': tq_samples,
'hp': hp_samples
})
return mwd
def build_table(contents, filename):
""" This is the table displayed at bottom. It's
useful to debug as well as check some values """
df = parse_data(contents, filename)
return html.Div([
html.H5(filename),
dash_table.DataTable(
data=df.to_dict('records'),
columns=[{'name': i, 'id': i} for i in df.columns]
)
])
def parse_data(contents, filename):
""" This is the way I handle the different file formats,
as everyone have its complications and settings """
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'csv' in filename:
# Generic format with rpm, hp, tq header
df = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
elif 'ine' in filename:
# Horacio Resio
df = parse_horacio_resio(pd.read_csv(
io.StringIO(decoded.decode('iso-8859-1')), skiprows=range(0, 24), delim_whitespace=True))
elif 'ad3' in filename:
# MWD
df = parse_mwd(etree.parse(io.StringIO(
decoded.decode('iso-8859-1'))).getroot())
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
return df
@app.callback(Output('Mygraph', 'figure'),
[
Input('upload-data', 'contents'),
Input('upload-data', 'filename')
])
def update_graph(contents, filename):
""" The graph handles opne or more dyno plots at the same time.
To help identify which plot belongs to each file, I
append the filename (without extension) to the legend """
fig = {
'layout': go.Layout(
plot_bgcolor=colors["graphBackground"],
paper_bgcolor=colors["graphBackground"],
yaxis=dict(
nticks=30,
range=[0, None],
))
}
if contents:
dfx1 = pd.DataFrame()
for content, filen in zip(contents, filename):
dfx = parse_data(content, filen)
dfx.columns = dfx.columns.str.replace(
'hp', 'whp %s' % filen.rsplit('.', 1)[0])
dfx.columns = dfx.columns.str.replace(
'tq', 'tq %s' % filen.rsplit('.', 1)[0])
dfx1 = pd.concat([dfx, dfx1])
dfx1 = dfx1.set_index(['rpm']) # This is our X index
fig['data'] = dfx1.iplot(asFigure=True, kind='line',
mode='lines+markers', size=1).data
return fig
@app.callback(Output('output-data-upload', 'children'),
[Input('upload-data', 'contents')],
[State('upload-data', 'filename')])
def update_output(list_of_contents, list_of_names):
if list_of_contents is not None:
children = [
build_table(c, n) for c, n in
zip(list_of_contents, list_of_names)]
return children
if __name__ == '__main__':
app.run_server(host="0.0.0.0", port=5001, debug=False)