-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
297 lines (251 loc) · 7.81 KB
/
visualization.py
File metadata and controls
297 lines (251 loc) · 7.81 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
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
"""
Visualization module: Provides data visualization functionality
"""
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
def create_speed_time_chart(df: pd.DataFrame) -> go.Figure:
"""
Create speed-time curve chart
Args:
df (pd.DataFrame): DataFrame containing speed, time and cruising markers
Returns:
plotly.graph_objects.Figure: Speed-time chart
"""
# Create chart
fig = px.line(df, x='timestamp', y='speed_kmh')
# Add cruising section highlighting
cruising_df = df[df['is_cruising']]
fig.add_scatter(
x=cruising_df['timestamp'],
y=cruising_df['speed_kmh'],
mode='markers',
marker=dict(color='green', size=5),
name='Cruising'
)
# Set up layout
fig.update_layout(
title='Riding Speed Curve',
xaxis_title='Time',
yaxis_title='Speed (km/h)',
legend_title='Data Type',
hovermode='closest'
)
return fig
def create_speed_distribution(df: pd.DataFrame) -> go.Figure:
"""
Create speed distribution histogram
Args:
df (pd.DataFrame): DataFrame containing speed and cruising markers
Returns:
plotly.graph_objects.Figure: Speed distribution chart
"""
# Create subplots
fig = make_subplots(rows=1, cols=2, subplot_titles=('All Data', 'Cruising Only'))
# Speed distribution of all data
fig.add_trace(
go.Histogram(
x=df['speed_kmh'],
nbinsx=30,
name='All',
marker_color='blue',
opacity=0.7
),
row=1, col=1
)
# Speed distribution of cruising sections only
fig.add_trace(
go.Histogram(
x=df[df['is_cruising']]['speed_kmh'],
nbinsx=30,
name='Cruising',
marker_color='green',
opacity=0.7
),
row=1, col=2
)
# Set layout
fig.update_layout(
title='Speed Distribution Histogram',
xaxis_title='Speed (km/h)',
yaxis_title='Frequency',
bargap=0.1,
showlegend=False
)
fig.update_xaxes(title_text='Speed (km/h)', row=1, col=1)
fig.update_xaxes(title_text='Speed (km/h)', row=1, col=2)
fig.update_yaxes(title_text='Frequency', row=1, col=1)
return fig
def create_summary_charts(df: pd.DataFrame, result: dict) -> go.Figure:
"""
Create ride overview charts
Args:
df (pd.DataFrame): Processed DataFrame
result (dict): Calculation results
Returns:
plotly.graph_objects.Figure: Overview charts
"""
# Create subplots
fig = make_subplots(
rows=1,
cols=2,
specs=[[{"type": "pie"}, {"type": "indicator"}]]
)
# Add cruising ratio pie chart
fig.add_trace(
go.Pie(
labels=['Cruising', 'Non-Cruising'],
values=[df['is_cruising'].sum(), len(df) - df['is_cruising'].sum()],
marker_colors=['green', 'red']
),
row=1, col=1
)
# Add cruising speed indicator
fig.add_trace(
go.Indicator(
value=result.get('cruising_speed', 0),
title={'text': "Cruising Speed (km/h)"},
mode="gauge+number",
gauge={
'axis': {'range': [0, max(df['speed_kmh'].max(), 50)]},
'bar': {'color': "green"},
'steps': [
{'range': [0, result.get('cruising_speed', 0)*0.8], 'color': "lightgray"},
{'range': [result.get('cruising_speed', 0)*0.8, result.get('cruising_speed', 0)*1.2], 'color': "gray"}
]
}
),
row=1, col=2
)
# Set layout
fig.update_layout(
title='Ride Overview',
height=400
)
return fig
def create_power_analysis_chart(df: pd.DataFrame, result: dict) -> go.Figure:
"""
Create power analysis chart with raw power, 30s avg power, and NP
Args:
df (pd.DataFrame): DataFrame containing power data
result (dict): Calculation results with NP
Returns:
plotly.graph_objects.Figure: Power analysis chart
"""
# Skip if no power data
if 'power' not in df.columns:
fig = go.Figure()
fig.update_layout(
title='Power Analysis (No power data available)',
xaxis_title='Time',
yaxis_title='Power (W)'
)
return fig
# Create chart
fig = go.Figure()
# Add raw power data
fig.add_trace(
go.Scatter(
x=df['timestamp'],
y=df['power'],
mode='lines',
line=dict(color='lightblue', width=1),
name='Instant Power'
)
)
# Add 30s rolling average power if available
if 'power_30s_avg' in df.columns:
fig.add_trace(
go.Scatter(
x=df['timestamp'],
y=df['power_30s_avg'],
mode='lines',
line=dict(color='blue', width=2),
name='30s Avg Power'
)
)
# Add NP horizontal line
if result.get('normalized_power'):
fig.add_shape(
type="line",
x0=df['timestamp'].min(),
y0=result['normalized_power'],
x1=df['timestamp'].max(),
y1=result['normalized_power'],
line=dict(color="red", width=2, dash="dash"),
)
# Add NP text label
fig.add_annotation(
x=df['timestamp'].max(),
y=result['normalized_power'],
text=f"NP: {result['normalized_power']:.0f}W",
showarrow=False,
yshift=10,
font=dict(color="red")
)
# Set up layout
fig.update_layout(
title='Power Analysis',
xaxis_title='Time',
yaxis_title='Power (W)',
legend_title='Data Type',
hovermode='closest'
)
return fig
def create_power_distribution(df: pd.DataFrame, result: dict) -> go.Figure:
"""
Create power distribution histogram
Args:
df (pd.DataFrame): DataFrame containing power data
result (dict): Calculation results
Returns:
plotly.graph_objects.Figure: Power distribution chart
"""
# Skip if no power data
if 'power' not in df.columns:
fig = go.Figure()
fig.update_layout(
title='Power Distribution (No power data available)',
xaxis_title='Power (W)',
yaxis_title='Frequency'
)
return fig
# Create subplots
fig = make_subplots(rows=1, cols=1, subplot_titles=('Power Distribution',))
# Power distribution histogram
fig.add_trace(
go.Histogram(
x=df['power'],
nbinsx=30,
name='Power',
marker_color='blue',
opacity=0.7
)
)
# Add vertical lines for avg power and NP
if 'avg_power' in result and result['avg_power'] is not None:
fig.add_vline(
x=result['avg_power'],
line_dash="solid",
line_color="green",
annotation_text=f"Avg: {result['avg_power']:.0f}W",
annotation_position="top right"
)
if 'normalized_power' in result and result['normalized_power'] is not None:
fig.add_vline(
x=result['normalized_power'],
line_dash="dash",
line_color="red",
annotation_text=f"NP: {result['normalized_power']:.0f}W",
annotation_position="top right"
)
# Set layout
fig.update_layout(
title='Power Distribution',
xaxis_title='Power (W)',
yaxis_title='Frequency',
bargap=0.1,
showlegend=False
)
return fig