-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
247 lines (200 loc) · 9.27 KB
/
app.py
File metadata and controls
247 lines (200 loc) · 9.27 KB
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
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
import os
from typing import List, Dict
import tempfile
def load_dataframe(filepath: str) -> pd.DataFrame:
"""
Loads CSV or Excel file into Pandas DataFrame.
Args:
filepath: The path to the file to load.
Returns:
the corresponding DataFrame.
"""
try:
if filepath.endswith(".csv"):
df = pd.read_csv(filepath)
elif filepath.endswith(".xlsx"):
df = pd.read_excel(filepath)
else:
st.error(f"Unsupported file type: {filepath}")
except Exception as e:
st.error(f"Error loading {filepath}: {e}")
return df
def plot_data(data: pd.DataFrame, x_axis: str, y_axes: List[str], plot_title: str, plot_type: str = "Line Plot") -> go.Figure:
"""
Plots the data based on user selections using Plotly. Returns the Plotly figure.
Args:
data: The Pandas DataFrame containing the data to plot.
x_axis: The column name for the x-axis.
y_axis: The column name for the y-axis.
plot_title: Title of the plot
plot_type: The type of plot to create ("Line Plot" or "Scatter Plot").
Returns:
A Plotly Figure object.
"""
fig = go.Figure()
for y_col in y_axes:
if plot_type == "Line Plot":
fig.add_trace(go.Scatter(x=data[x_axis], y=data[y_col], mode='lines', name=y_col))
elif plot_type == "Scatter Plot":
fig.add_trace(go.Scatter(x=data[x_axis], y=data[y_col], mode='markers', name=y_col))
else:
st.error(f"Unsupported plot type: {plot_type}")
return go.Figure()
legend=dict(
font=dict(size=14, color="#FFFFFF"),
bgcolor="rgba(0,0,0,0)",
bordercolor="#4a4a4a",
borderwidth=1)
fig.update_layout(
title=plot_title,
xaxis_title=x_axis,
yaxis_title=", ".join(y_axes),
plot_bgcolor="#111111",
paper_bgcolor="#111111",
font_color="#FFFFFF",
xaxis=dict(gridcolor="#4a4a4a", zerolinecolor="#4a4a4a"),
yaxis=dict(gridcolor="#4a4a4a", zerolinecolor="#4a4a4a"),
legend=legend
)
return fig
def get_plot_download_link(fig, filename="plot.html"):
"""
Saves a Plotly figure to an HTML file and returns a download link.
"""
with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as tmpfile:
fig.write_html(tmpfile.name)
with open(tmpfile.name, "rb") as f:
st.download_button(
label="Downlaod Interactive Plot and Open in Browser",
data=f,
file_name=filename,
mime="text/html"
)
def main():
"""
Main function to run the Streamlit app.
"""
# Set page configuration
st.set_page_config(
page_title="NFR-25 DAQ Interface",
page_icon="NFR",
layout="wide",
initial_sidebar_state="expanded"
)
# Main content
st.title("NFR 25 DAQ Interface")
st.write("Northwestern Formula Racing's 2025 DAQ data analysis tool.")
# Sidebar
st.sidebar.title("Navigation")
st.sidebar.write("Use the sidebar to select the Drive Day data.")
# # File uploader in the sidebar for multiple files
# uploaded_files = st.sidebar.file_uploader("Select the drive day", type=["csv", "xlsx"], accept_multiple_files=True)
uploaded_files = None
select_log_system = None
select_day = None
select_csv = None
#Selecting the Drive Day
DATA_DIR = 'out'#hardcoded for now
log_systems = []#list of log systems
for folder in os.listdir(DATA_DIR):
if os.path.isdir(os.path.join(DATA_DIR,folder)):
log_systems.append(folder)
log_systems = sorted(log_systems)#sort the folders for easy select
select_log_system = st.sidebar.selectbox('Select the Log System', log_systems, key='selected_log_system')
if select_log_system:
folder_path = os.path.join(DATA_DIR, select_log_system)#path to the selected folder
day_folders = [] #list of driveday folder paths
for folder in os.listdir(folder_path):
if os.path.isdir(os.path.join(folder_path,folder)):#if its a folder
day_folders.append(folder)
day_folders = sorted(day_folders)#sort the folders for easy select
select_day = st.sidebar.selectbox('Select the Drive Day', day_folders, key='selected_day')
#Selecting the Files
if select_day:
folder_path = os.path.join(folder_path, select_day)#path to the selected folder
csv_files = []#list of csv files for that day
for file in os.listdir(folder_path):
if file.endswith(".csv"):
csv_files.append(file)
select_csv = st.sidebar.selectbox("Select Data File", csv_files, key="selected_csv")
st.sidebar.selectbox("Type of Visualizations", ["Linear", "Special"], key="selected_type")
if st.session_state.selected_type == "Linear":
num_plots = st.sidebar.selectbox("Number of Plots", [1, 2, 3, 4], index=0) # default 2
##
if select_csv:#if you have selected a csv
fullfilepath = os.path.join(folder_path,select_csv )
df = load_dataframe(filepath=fullfilepath)#load selected csv into dataframe
df = df.reset_index(drop=False)
df.rename(columns={'index':'Time-index'}, inplace=True)
#timesteps
st.write("File uploaded successfully!")
st.write("Preview of the data:")
st.write(df)
plot_configs = [] # List to store plot configurations
show_plots = False #flag
if num_plots > 1:
# Create a 2x2 grid for plot input
cols = st.columns(2)
rows = [cols[i % 2] for i in range(num_plots)]
for i in range(num_plots):
with rows[i]:
st.subheader(f"Plot {i + 1}")
# Use a unique key for each selectbox
x_axis_key = f"x_axis_{i}"
y_axis_key = f"y_axis_{i}"
plot_type_key = f"plot_type_{i}"
x_axis = st.selectbox(f"Select X-axis variable for Plot {i + 1}", df.columns, key=x_axis_key)
y_axes = st.multiselect(f"Select Y-axis variable for Plot {i + 1}", df.columns, key=y_axis_key)
plot_type = st.selectbox(f"Select Plot Type for Plot {i + 1}", ["Line Plot", "Scatter Plot"], key=plot_type_key)
if y_axes:#only if the user has selected plotting values
plot_configs.append({
"x_axis": x_axis,
"y_axis": y_axes,#could be a list
"plot_type": plot_type,
"df": df, #store df
"title": f"{', '.join(y_axes)} vs {x_axis}"
})
else:
st.subheader("Plot 1")
x_axis = st.selectbox("Select X-axis variable for Plot 1", df.columns, key="x_axis_0")
y_axes = st.multiselect("Select Y-axis variable for Plot 1", df.columns, key="y_axis_0")
plot_type = st.selectbox("Select Plot Type for Plot 1", ["Line Plot", "Scatter Plot"], key="plot_type_0")
if y_axes: # Only if the user has selected plotting values
plot_configs.append({
"x_axis": x_axis,
"y_axis": y_axes,
"plot_type": plot_type,
"df": df,
"title": f"{', '.join(y_axes)} vs {x_axis}"
})
# Generate Plots Button
if st.button("Generate Plots"):
show_plots = True #set flag
if show_plots and plot_configs:
# Create a 2x2 grid for displaying the plots
if num_plots > 1:
plot_cols = st.columns(2)
plot_rows = [plot_cols[i % 2] for i in range(num_plots)]
else:
plot_cols = st.columns(1)
plot_rows = [plot_cols[i] for i in range(num_plots)]
for i, config in enumerate(plot_configs):
with plot_rows[i]:
fig = plot_data(data=config["df"], x_axis=config["x_axis"], y_axes=config["y_axis"], plot_type=config["plot_type"], plot_title=config['title'])
st.plotly_chart(fig, use_container_width=True)
get_plot_download_link(fig, filename=f"plot_{i + 1}.html")
else: #special graphs
#Sidebar
st.sidebar.selectbox("Choose your graph", ["Not yet supported. Contact DAQ team!"], key="selected_special_graph")
#Main
st.write("Non-linear graphs will be supported here!")
st.write("If you came here by mistake, please select 'Linear' Visualization on the sidebar to go home")
# Add a map of Northwestern University with a pin on Ford Design Center
st.title("Welcome to Northwestern Formula Racing 2025")
ford_design_center_coords = [42.056459, -87.675267]
st.map(pd.DataFrame([{"lat": ford_design_center_coords[0], "lon": ford_design_center_coords[1]}]), color="#4E2A84", zoom=14)
if __name__ == "__main__":
main()