-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.py
More file actions
224 lines (191 loc) · 7.75 KB
/
html.py
File metadata and controls
224 lines (191 loc) · 7.75 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
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from pathlib import Path
import json
def create_interactive_html(data_root='PHMDC2019_Data', output_html='signal_plots.html'):
"""Create interactive HTML with signal plots and toggle buttons"""
data_path = Path(data_root)
# Find all directories containing signal files
signal_dirs = set()
for signal_file in data_path.glob('**/signal_*.csv'):
signal_dirs.add(signal_file.parent)
signal_dirs = sorted(signal_dirs)
print(f"Found {len(signal_dirs)} directories with signal files")
# Calculate appropriate vertical spacing
num_rows = len(signal_dirs)
vertical_spacing = 0.01 if num_rows > 10 else 0.02
# Create subplots (one row per folder)
fig = make_subplots(
rows=num_rows,
cols=1,
subplot_titles=[f"{d.parent.name}/{d.name}" for d in signal_dirs],
vertical_spacing=vertical_spacing
)
# Track trace indices for each subplot
trace_mapping = {}
for idx, signal_dir in enumerate(signal_dirs, 1):
signal_1 = signal_dir / 'signal_1.csv'
signal_2 = signal_dir / 'signal_2.csv'
# Store the starting trace index for this subplot
current_trace_idx = len(fig.data)
# Add Signal 1 traces (visible by default)
if signal_1.exists():
df1 = pd.read_csv(signal_1)
fig.add_trace(
go.Scatter(x=df1['time'], y=df1['ch1'],
name=f'CH1',
line=dict(color='blue', width=1),
visible=True,
showlegend=False),
row=idx, col=1
)
fig.add_trace(
go.Scatter(x=df1['time'], y=df1['ch2'],
name=f'CH2',
line=dict(color='red', width=1),
visible=True,
showlegend=False),
row=idx, col=1
)
# Add Signal 2 traces (hidden by default)
if signal_2.exists():
df2 = pd.read_csv(signal_2)
fig.add_trace(
go.Scatter(x=df2['time'], y=df2['ch1'],
name=f'CH1',
line=dict(color='darkblue', width=1),
visible=False,
showlegend=False),
row=idx, col=1
)
fig.add_trace(
go.Scatter(x=df2['time'], y=df2['ch2'],
name=f'CH2',
line=dict(color='darkred', width=1),
visible=False,
showlegend=False),
row=idx, col=1
)
# Store indices: [signal1_ch1, signal1_ch2, signal2_ch1, signal2_ch4]
trace_mapping[idx] = list(range(current_trace_idx, len(fig.data)))
# Create buttons for toggling between Signal 1 and Signal 2
buttons = []
# Button for Signal 1
visible_signal1 = []
for idx in range(1, num_rows + 1):
indices = trace_mapping[idx]
# Show first 2 traces (Signal 1), hide last 2 (Signal 2)
visible_signal1.extend([True, True, False, False] if len(indices) == 4 else [True, True])
buttons.append(dict(
label="Signal 1 (Press 1)",
method="update",
args=[{"visible": visible_signal1}]
))
# Button for Signal 2
visible_signal2 = []
for idx in range(1, num_rows + 1):
indices = trace_mapping[idx]
# Hide first 2 traces (Signal 1), show last 2 (Signal 2)
visible_signal2.extend([False, False, True, True] if len(indices) == 4 else [False, False])
buttons.append(dict(
label="Signal 2 (Press 2)",
method="update",
args=[{"visible": visible_signal2}]
))
# Update layout with buttons
fig.update_layout(
height=300 * num_rows,
title_text="Signal Plots - Use buttons or press 1/2 to switch, S to toggle",
updatemenus=[
dict(
type="buttons",
direction="left",
buttons=buttons,
pad={"r": 10, "t": 10},
showactive=True,
x=0.0,
xanchor="left",
y=1.02,
yanchor="bottom"
)
]
)
# Update axes labels
fig.update_xaxes(title_text="Time (s)")
fig.update_yaxes(title_text="Signal Value")
# Write HTML
fig.write_html(output_html)
# Add keyboard shortcuts via JavaScript injection
with open(output_html, 'r') as f:
html_content = f.read()
# Convert Python boolean lists to JavaScript format using json.dumps
js_visible_signal1 = json.dumps(visible_signal1)
js_visible_signal2 = json.dumps(visible_signal2)
# Get total number of traces
total_traces = len(fig.data)
# JavaScript for keyboard shortcuts - FIXED: use trace indices
keyboard_script = f"""
<script>
// Track current signal state (1 or 2)
let currentSignal = 1;
// Visibility arrays for each signal
const visibilitySignal1 = {js_visible_signal1};
const visibilitySignal2 = {js_visible_signal2};
const totalTraces = {total_traces};
document.addEventListener('keydown', function(event) {{
const key = event.key.toLowerCase();
// Get the plot div
const plotDiv = document.querySelector('.js-plotly-plot');
if (!plotDiv) {{
console.error('Could not find plot div');
return;
}}
// Press '1' for Signal 1
if (key === '1') {{
const traceIndices = Array.from({{length: totalTraces}}, (_, i) => i);
Plotly.restyle(plotDiv, 'visible', visibilitySignal1, traceIndices);
currentSignal = 1;
event.preventDefault();
console.log('Switched to Signal 1');
}}
// Press '2' for Signal 2
else if (key === '2') {{
const traceIndices = Array.from({{length: totalTraces}}, (_, i) => i);
Plotly.restyle(plotDiv, 'visible', visibilitySignal2, traceIndices);
currentSignal = 2;
event.preventDefault();
console.log('Switched to Signal 2');
}}
// Press 'S' to toggle between signals
else if (key === 's') {{
const traceIndices = Array.from({{length: totalTraces}}, (_, i) => i);
if (currentSignal === 1) {{
Plotly.restyle(plotDiv, 'visible', visibilitySignal2, traceIndices);
currentSignal = 2;
console.log('Toggled to Signal 2');
}} else {{
Plotly.restyle(plotDiv, 'visible', visibilitySignal1, traceIndices);
currentSignal = 1;
console.log('Toggled to Signal 1');
}}
event.preventDefault();
}}
}});
// Show keyboard shortcuts hint
console.log('Keyboard shortcuts: Press 1 (Signal 1), 2 (Signal 2), S (Toggle)');
</script>
"""
# Insert the script before closing </body> tag
html_content = html_content.replace('</body>', f'{keyboard_script}</body>')
with open(output_html, 'w') as f:
f.write(html_content)
print(f"Interactive HTML saved to: {output_html}")
print(f"Total height: {300 * num_rows}px")
print(f"Added toggle buttons for Signal 1/Signal 2")
print(f"Keyboard shortcuts: Press 1 (Signal 1), 2 (Signal 2), S (Toggle)")
if __name__ == "__main__":
create_interactive_html(
data_root="/Users/darrylad/Darryl/Research/18. Fatigue Crack Growth in Aluminum Lap Joint",
output_html="signal_plots.html"
)