forked from priyadivya24/Visualization_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisual_Fastarrow.py
221 lines (187 loc) · 8.53 KB
/
Visual_Fastarrow.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
import os
import time
import pandas as pd
import plotly.express as px
import dash
from dash import dcc
from dash import html
import dash_bootstrap_components as dbc
from plotly.subplots import make_subplots
from dash.dependencies import Input, Output
from dash_bootstrap_templates import load_figure_template
import fastparquet
def read_parquet_files(subfolder_path):
files = [f for f in os.listdir(subfolder_path) if f.endswith('.parquet')]
tables = [fastparquet.ParquetFile(os.path.join(subfolder_path, f)).to_pandas() for f in files]
return pd.concat(tables, ignore_index=True)
def get_dropdown_options(main_folders, sub_folders, subsub_folder):
if None in (main_folders, sub_folders, subsub_folder):
return []
main_folders = str(main_folders)
sub_folders = str(sub_folders)
subsub_folder = str(subsub_folder)
subsubsubfolder_path = os.path.join(main_folders, sub_folders, subsub_folder)
subsubsubfolders = [f for f in os.listdir(subsubsubfolder_path) if
os.path.isdir(os.path.join(subsubsubfolder_path, f))]
return [{'label': folder, 'value': folder} for folder in subsubsubfolders]
def get_initial_date_range(main_folders, sub_folders, subsub_folder):
subsubsubfolder_path = os.path.join(main_folders, sub_folders, subsub_folder)
files = [f for f in os.listdir(subsubsubfolder_path) if f.endswith('.parquet')]
if not files:
return None, None
all_dates = []
for file in files:
file_path = os.path.join(subsubsubfolder_path, file)
df = fastparquet.ParquetFile(file_path).to_pandas()
all_dates.extend(df['timestamp'])
min_date = min(all_dates)
max_date = max(all_dates)
return min_date, max_date
def convert_timestamp(df):
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='s')
return df
def downsample_data(df, timestamp_col='timestamp', value_col='data'):
df_resampled = df.groupby(pd.Grouper(key=timestamp_col, freq='1D'))[value_col].mean().reset_index()
return df_resampled
def update_line_plot(selected_subfolder, start_date, end_date, main_folders, sub_folders, subsub_folder):
if selected_subfolder is None:
return px.line()
subfolder_path = os.path.join(main_folders, sub_folders, subsub_folder, selected_subfolder)
df = read_parquet_files(subfolder_path)
df = convert_timestamp(df)
if start_date and end_date:
df = df[(df['timestamp'] >= start_date) & (df['timestamp'] <= end_date)]
elif start_date:
df = df[df['timestamp'] >= start_date]
elif end_date:
df = df[df['timestamp'] >= end_date]
df = downsample_data(df)
fig = make_subplots(specs=[[{'secondary_y': True}]])
fig.add_trace(px.line(df, x='timestamp', y='data').data[0])
return fig
def create_layout(main_folders, sub_folders, subsub_folders_1, sub_folders2, subsub_folders_2, subsub_folders_3,
subsub_folders_4):
return html.Div([
html.H1("Dashboard"),
html.Div([
html.H2("XTIN.MLO1 and SLO1 Dashboards"),
dcc.DatePickerRange(
id='date1',
display_format='YYYY-MM-DD',
start_date=None,
end_date=None,
style={'margin-bottom': '20px'}
),
dbc.Row([
dbc.Col([
dcc.Dropdown(
id='subfolder-dropdown',
options=get_dropdown_options(main_folders, sub_folders, subsub_folders_1),
value='CTRL0.OUT.MEAN.RD',
placeholder="Select Subfolder"
),
], width=6),
dbc.Col([
dcc.Dropdown(
id='subfolder-dropdown-2',
options=get_dropdown_options(main_folders, sub_folders, subsub_folders_2),
value='CTRL0.OUT.MEAN.RD',
placeholder="Select Subfolder"
),
], width=6),
]),
dbc.Row([
dbc.Col(dcc.Graph(id='line-plot-1', config={'displayModeBar': False}), width=6),
dbc.Col(dcc.Graph(id='line-plot-2', config={'displayModeBar': False}), width=6),
]),
]),
html.Div([
html.H2("LSU"),
dcc.DatePickerRange(
id='date2',
display_format='YYYY-MM-DD',
start_date=None,
end_date=None,
style={'margin-bottom': '20px'}
),
dbc.Row([
dbc.Col([
dcc.Dropdown(
id='subfolder-dropdown-3',
options=get_dropdown_options(main_folders, sub_folders2, subsub_folders_3),
value='FMC1.MD22.0.POSITION.RD',
placeholder="Select Subfolder"
),
], width=6),
dbc.Col([
dcc.Dropdown(
id='subfolder-dropdown-4',
options=get_dropdown_options(main_folders, sub_folders2, subsub_folders_4),
value='LSU.2.CTRL_IN.MEAN.RD',
placeholder="Select Subfolder"
),
], width=6),
]),
dbc.Row([
dbc.Col(dcc.Graph(id='line-plot-3', config={'displayModeBar': False}), width=6),
dbc.Col(dcc.Graph(id='line-plot-4', config={'displayModeBar': False}), width=6),
]),
]),
], style={'width': '80%', 'margin': 'auto', 'display': 'block'})
main_folder = 'XFEL.SYNC'
sub_folder = 'LASER.LOCK.XLO'
sub_folder2 = 'LINK.LOCK'
subsub_folder_1 = 'XTIN.MLO1'
subsub_folder_2 = 'XHEXP1.SLO1'
subsub_folder_3 = 'XTIN.AMC8.ACTUATOR'
subsub_folder_4 = 'XTIN.AMC8.CONTROLLER'
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.LUX])
load_figure_template('LUX')
app.layout = create_layout(main_folder, sub_folder, subsub_folder_1, sub_folder2, subsub_folder_2, subsub_folder_3,
subsub_folder_4, )
@app.callback(Output('line-plot-1', 'figure'),
[Input('subfolder-dropdown', 'value'),
Input('date1', 'start_date'),
Input('date1', 'end_date')])
def update_graph_1(selected_subfolder, start_date, end_date):
# Track time taken for execution
start = time.perf_counter()
fig = update_line_plot(selected_subfolder, start_date, end_date, main_folder, sub_folder, subsub_folder_1)
end = time.perf_counter()
print(f'Update Graph1 Plot Function Completed in {round(end - start, 2)} seconds')
return fig
@app.callback(Output('line-plot-2', 'figure'),
[Input('subfolder-dropdown-2', 'value'),
Input('date1', 'start_date'),
Input('date1', 'end_date')])
def update_graph_2(selected_subfolder, start_date, end_date):
# Track time taken for execution
start = time.perf_counter()
fig = update_line_plot(selected_subfolder, start_date, end_date, main_folder, sub_folder, subsub_folder_2)
end = time.perf_counter()
print(f'Update Graph2 Plot Function Completed in {round(end - start, 2)} seconds')
return fig
@app.callback(Output('line-plot-3', 'figure'),
[Input('subfolder-dropdown-3', 'value'),
Input('date2', 'start_date'),
Input('date2', 'end_date')])
def update_graph_3(selected_subfolder, start_date, end_date):
# Track time taken for execution
start = time.perf_counter()
fig = update_line_plot(selected_subfolder, start_date, end_date, main_folder, sub_folder2, subsub_folder_3)
end = time.perf_counter()
print(f'Update Graph3 Plot Function Completed in {round(end - start, 2)} seconds')
return fig
@app.callback(Output('line-plot-4', 'figure'),
[Input('subfolder-dropdown-4', 'value'),
Input('date2', 'start_date'),
Input('date2', 'end_date')])
def update_graph_4(selected_subfolder, start_date, end_date):
# Track time taken for execution
start = time.perf_counter()
fig = update_line_plot(selected_subfolder, start_date, end_date, main_folder, sub_folder2, subsub_folder_4)
end = time.perf_counter()
print(f'Update Graph4 Plot Function Completed in {round(end - start, 2)} seconds')
return fig
if __name__ == '__main__':
app.run_server(debug=True)