-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
427 lines (380 loc) · 16.9 KB
/
main.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import base64
import io
import dash
from dash import dcc, html, Input, Output, State, ctx, dash_table, Patch
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
import numpy as np
from scipy.interpolate import griddata
from datetime import datetime
from io import StringIO
from pykrige.ok import OrdinaryKriging
from analytics_app.DataAnalytics import *
from analytics_app.Kriging import *
# Inicializa o app Dash
app = dash.Dash(__name__,
suppress_callback_exceptions=True,
external_stylesheets=[dbc.themes.BOOTSTRAP],
meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1.0"}],)
app.title = "Data Explorer - Profile Report and EDA App"
server = app.server
# Layout do aplicativo
app.layout = html.Div([
html.Div([
html.Img(src='assets/logo.png', style={'height': '100px', 'margin-left': 'auto', 'margin-right': 'auto'}),
], style={'text-align': 'center', 'margin-bottom': '10px'}),
dcc.Tabs(id="tabs", children=[
dcc.Tab(label='File Upload', value='tab-upload'),
dcc.Tab(label='Profile Report', value='tab-report'),
dcc.Tab(label='2D Chart', value='tab-2d'),
dcc.Tab(label='3D Chart', value='tab-3d'),
dcc.Tab(label='3D Kriging Interpolation', value='tab-kriging'),
dcc.Tab(label='Parallel Coordinates Plot', value='tab-parcoords'),
], value='tab-upload'),
html.Div(id='tabs-content'),
dcc.Store(id='store-data') # Componente para armazenar os dados
])
@app.callback(Output('tabs-content', 'children'),
Input('tabs', 'value'),
State('store-data', 'data'))
def render_content(tab, data):
if tab == 'tab-upload':
return html.Div([
html.Div([
html.Br(),
dcc.Upload(
id='upload-data',
children=html.Div(['Drag or ', html.A('select an Excel file')]),
style={
'width': '500px', 'height': '60px', 'lineHeight': '60px',
'borderWidth': '1px', 'borderStyle': 'dashed', 'borderRadius': '5px',
'textAlign': 'center', 'margin': 'auto'
},
multiple=False
),
], style={'textAlign': 'center'}),
html.Div([
html.H5(id='file-name-output')
], style={'textAlign': 'center'}),
])
elif tab == 'tab-report' and data is not None:
df = pd.read_json(StringIO(data), orient='split')
return html.Div([
html.Div([
html.Br(),
html.Button('Create Report', id='create-report-btn', n_clicks=0,
style={'backgroundColor': 'orange', 'color': 'white', 'fontWeight': 'bold', 'fontSize': '20px',
'margin': 'auto'}),
], style={'textAlign': 'center'}),
html.Br(),
dbc.Spinner(spinner_style={"width": "3rem", "height": "3rem"}, children=[html.Div(id="macro-output")]),
html.Div([
html.Br(),
html.Iframe(id='html-viewer', src="", width='80%', height='600'),
], style={'display': 'flex', 'justifyContent': 'center', 'alignItems': 'center', 'marginBottom': '10px'}),
])
elif tab == 'tab-2d' and data is not None:
df = pd.read_json(StringIO(data), orient='split')
options = [{'label': i, 'value': i} for i in df.columns]
return html.Div([
dcc.Dropdown(id='x-axis-column-2d', options=options),
dcc.Dropdown(id='y-axis-column-2d', options=options),
dcc.Graph(id='graph-2d', style={'height': '600px', 'width': '100%'}),
])
elif tab == 'tab-3d' and data is not None:
df = pd.read_json(StringIO(data), orient='split')
options = [{'label': i, 'value': i} for i in df.columns]
return html.Div([
dcc.Dropdown(id='x-axis-column-3d', options=options),
dcc.Dropdown(id='y-axis-column-3d', options=options),
dcc.Dropdown(id='z-axis-column-3d', options=options),
dcc.Graph(id='graph-3d', style={'height': '600px', 'width': '100%'}),
])
elif tab == 'tab-parcoords' and data is not None:
df = pd.read_json(StringIO(data), orient='split')
return html.Div([
dcc.Graph(id='graph-parcoords'),
html.Br(),
html.Div([
html.Button('Download Descriptive Statistics', id='btn-stat-download', n_clicks=0,
style={'backgroundColor': 'orange', 'color': 'white', 'fontWeight': 'bold',
'fontSize': '20px',
'marginRight': '10px'}),
dcc.Download(id="download-excel")
], style={'display': 'flex', 'flex-direction': 'column', 'alignItems': 'center', 'justifyContent': 'center',
'marginBottom': '10px'}),
dash_table.DataTable(id='table',
columns=[{'id': 'index', 'name': 'index'}] + [{'id': i, 'name': i} for i in
df.columns],
style_table={'overflowX': 'scroll'}),
dcc.Store(id='activefilters', data={})
])
elif tab == 'tab-kriging' and data is not None:
df = pd.read_json(StringIO(data), orient='split')
options = [{'label': i, 'value': i} for i in df.columns]
return html.Div([
html.Div([
html.H4('Select the 2 Independent Variables', style={'text-align': 'center'}),
dcc.Dropdown(id='independent-vars-3d', options=options, multi=True, value=[],
style={'width': '400px', 'margin': '0 auto'}),
], style={'display': 'flex', 'flex-direction': 'column', 'alignItems': 'center', 'justifyContent': 'center',
'marginBottom': '10px'}),
html.Div([
html.Br(),
html.H4('Select the Dependent Variable (1 variable)', style={'text-align': 'center'}),
dcc.Dropdown(id='dependent-var-3d', options=options, multi=False, value=None,
style={'width': '400px', 'margin': '0 auto'}),
], style={'display': 'flex', 'flex-direction': 'column', 'alignItems': 'center', 'justifyContent': 'center',
'marginBottom': '10px'}),
html.Div([
html.Br(),
html.Button('Generate Kriging Interpolation', id='generate-kriging-3d', n_clicks=0,
style={'backgroundColor': 'orange', 'color': 'white', 'fontWeight': 'bold',
'fontSize': '20px',
'marginRight': '10px'}
),
html.Br(),
html.Div(id='kriging-plot-output-3d')
], style={'display': 'flex', 'flex-direction': 'column', 'alignItems': 'center', 'justifyContent': 'center',
'marginBottom': '10px'}),
], style={'display': 'flex', 'flex-direction': 'column', 'alignItems': 'center', 'justifyContent': 'center',
'width': '100%'})
return html.Div([
html.Br(),
html.H2("Please select a file in the 'File Upload' tab.")
], style={'display': 'flex', 'justifyContent': 'center', 'alignItems': 'center', 'marginBottom': '10px'}),
@app.callback(
Output('store-data', 'data'),
Input('upload-data', 'contents'),
State('upload-data', 'filename'),
prevent_initial_call=True
)
def store_data(contents, filename):
if contents is not None:
df = parse_contents(contents, filename)
if isinstance(df, pd.DataFrame):
return df.to_json(date_format='iso', orient='split')
return None
@app.callback(
Output('graph-2d', 'figure'),
[Input('x-axis-column-2d', 'value'),
Input('y-axis-column-2d', 'value')],
State('store-data', 'data')
)
def update_graph_2d(xaxis_column_name, yaxis_column_name, data):
if data is not None:
df = pd.read_json(StringIO(data), orient='split')
if 'Experiment' in df.columns:
# Se existir, remover a coluna 'Experiment'
df = df.drop(columns=['Experiment'])
if xaxis_column_name and yaxis_column_name:
return {
'data': [go.Scatter(
x=df[xaxis_column_name],
y=df[yaxis_column_name],
mode='markers'
)],
'layout': go.Layout(
xaxis={'title': xaxis_column_name},
yaxis={'title': yaxis_column_name},
margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
hovermode='closest'
)
}
return {'data': []}
@app.callback(
Output('graph-3d', 'figure'),
[Input('x-axis-column-3d', 'value'),
Input('y-axis-column-3d', 'value'),
Input('z-axis-column-3d', 'value')],
State('store-data', 'data')
)
def update_graph_3d(xaxis_column_name, yaxis_column_name, zaxis_column_name, data):
if data:
df = pd.read_json(StringIO(data), orient='split')
if 'Experiment' in df.columns:
# Se existir, remover a coluna 'Experiment'
df = df.drop(columns=['Experiment'])
if xaxis_column_name and yaxis_column_name and zaxis_column_name:
df[xaxis_column_name] = pd.to_numeric(df[xaxis_column_name], errors='coerce')
df[yaxis_column_name] = pd.to_numeric(df[yaxis_column_name], errors='coerce')
df[zaxis_column_name] = pd.to_numeric(df[zaxis_column_name], errors='coerce')
# Preparação dos dados para o gráfico 3D
x_unique = np.linspace(df[xaxis_column_name].min(), df[xaxis_column_name].max(), num=500)
y_unique = np.linspace(df[yaxis_column_name].min(), df[yaxis_column_name].max(), num=500)
x_grid, y_grid = np.meshgrid(x_unique, y_unique)
z_grid = griddata(
(df[xaxis_column_name], df[yaxis_column_name]),
df[zaxis_column_name],
(x_grid, y_grid),
method='cubic'
)
return {
'data': [go.Surface(z=z_grid, x=x_unique, y=y_unique)],
'layout': go.Layout(
autosize=True,
margin=dict(l=50, r=50, b=30, t=30, pad=4),
scene=dict(
xaxis=dict(title=xaxis_column_name),
yaxis=dict(title=yaxis_column_name),
zaxis=dict(title=zaxis_column_name),
aspectmode='cube',
)
)
}
return {'data': []}
def parse_contents(contents, filename):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'xlsx' in filename:
df = pd.read_excel(io.BytesIO(decoded))
if 'Experiment' in df.columns:
# Se existir, remover a coluna 'Experiment'
df = df.drop(columns=['Experiment'])
else:
return None
return df
except Exception as e:
print(e)
return None
@app.callback([Output('html-viewer', 'src', allow_duplicate=True),
Output("macro-output", "children", allow_duplicate=True),],
Input('create-report-btn', 'n_clicks'),
[State('store-data', 'data')],
prevent_initial_call=True
)
def update_output(n_clicks, data):
if data:
df = pd.read_json(StringIO(data), orient='split')
if 'Experiment' in df.columns:
# Se existir, remover a coluna 'Experiment'
df = df.drop(columns=['Experiment'])
if df is not None:
data_analytics(df)
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
Html_Page = f"assets/relatorio_analise.html?update={timestamp}"
return Html_Page, ""
else:
Html_Page = ""
return Html_Page, ""
else:
Html_Page = ""
return Html_Page, ""
@app.callback(
Output('graph-parcoords', 'figure'),
[Input('store-data', 'data')]
)
def update_graph_parcoords(data):
if data:
df = pd.read_json(StringIO(data), orient='split')
if 'Experiment' in df.columns:
# Se existir, remover a coluna 'Experiment'
df = df.drop(columns=['Experiment'])
fig = px.parallel_coordinates(df, color=df.columns[-1])
return fig
return go.Figure()
@app.callback(
Output('kriging-plot-output-3d', 'children'), # Assumindo que você tenha um componente para o plot
[Input('generate-kriging-3d', 'n_clicks')],
[State('independent-vars-3d', 'value'),
State('dependent-var-3d', 'value'),
State('store-data', 'data')]
)
def update_kriging_plot(n_clicks, independent_vars, dependent_var, data):
if n_clicks > 0 and data is not None and len(independent_vars) == 2 and dependent_var is not None:
df = pd.read_json(StringIO(data), orient='split')
if 'Experiment' in df.columns:
# Se existir, remover a coluna 'Experiment'
df = df.drop(columns=['Experiment'])
# Realiza a interpolação Kriging
OK3D, gridx, gridy, z, ss = perform_kriging_3d(independent_vars, dependent_var, df)
# Cria um gráfico de superfície com os resultados da interpolação
fig = go.Figure(data=[go.Surface(z=z, x=gridx, y=gridy)])
fig.update_layout(autosize=True,
scene=dict(
xaxis_title=independent_vars[0],
yaxis_title=independent_vars[1],
zaxis_title=dependent_var,
aspectmode='cube'
),
margin=dict(l=50, r=50, b=30, t=30, pad=4),
height=600,
width=800,
)
return dcc.Graph(figure=fig)
return "Select two independent variables and one dependent variable and click 'Generate Kriging Interpolation' to see the result."
@app.callback(
Output('table', 'data'),
Input("activefilters", "data"),
State('store-data', 'data')
)
def udpate_table(data, store_data_value):
df = pd.read_json(StringIO(store_data_value), orient='split')
if 'Experiment' in df.columns:
# Se existir, remover a coluna 'Experiment'
df = df.drop(columns=['Experiment'])
if data:
dff = df.copy()
for col in data:
if data[col]:
rng = data[col][0]
if isinstance(rng[0], list):
# if multiple choices combine df
dff3 = pd.DataFrame(columns=df.columns)
for i in rng:
dff2 = dff[dff[col].between(i[0], i[1])]
dff3 = pd.concat([dff3, dff2])
dff = dff3
else:
# if one choice
dff = dff[dff[col].between(rng[0], rng[1])]
descriptive_stats = dff.describe().reset_index()
return descriptive_stats.to_dict('records')
descriptive_stats = df.describe().reset_index()
return descriptive_stats.to_dict('records')
@app.callback(
Output('activefilters', 'data'),
Input("graph-parcoords", "restyleData"),
State('store-data', 'data')
)
def updateFilters(data, store_data_value):
df = pd.read_json(StringIO(store_data_value), orient='split')
if 'Experiment' in df.columns:
# Se existir, remover a coluna 'Experiment'
df = df.drop(columns=['Experiment'])
dims = df.columns
if data:
key = list(data[0].keys())[0]
col = dims[int(key.split('[')[1].split(']')[0])]
newData = Patch()
newData[col] = data[0][key]
return newData
return {}
# Callback para gerar e baixar o arquivo Excel
@app.callback(
Output("download-excel", "data"),
Input("btn-stat-download", "n_clicks"),
State('table', 'data'),
prevent_initial_call=True
)
def generate_excel(n_clicks, stat_data_value):
filename = "assets/statistics.xlsx"
df = pd.DataFrame(stat_data_value)
if 'Experiment' in df.columns:
# Se existir, remover a coluna 'Experiment'
df = df.drop(columns=['Experiment'])
df.to_excel(filename, index=False)
return dcc.send_file(filename)
# Callback para exibir o nome do arquivo após o upload
@app.callback(Output('file-name-output', 'children'),
[Input('upload-data', 'contents'),
Input('upload-data', 'filename')])
def update_output(contents, filename):
if contents is not None:
TextoH2 = filename + " loaded!"
return html.H2(TextoH2)
if __name__ == '__main__':
app.run_server(host='127.0.0.3', port=8080, debug=False)