-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
340 lines (295 loc) · 11.8 KB
/
test.html
File metadata and controls
340 lines (295 loc) · 11.8 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
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
<!DOCTYPE html>
<html>
<head>
<title>Oxygenation Index Dome Effect - Debug Mode</title>
<script src="https://cdn.plot.ly/plotly-2.24.2.min.js"></script>
<script src="https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#progress-container {
width: 100%;
background-color: #f1f1f1;
margin-bottom: 20px;
}
#progress-bar {
width: 0%;
height: 30px;
background-color: #4CAF50;
text-align: center;
line-height: 30px;
color: white;
transition: width 0.3s;
}
#debug-console {
border: 1px solid #ccc;
padding: 10px;
height: 200px;
overflow-y: scroll;
margin-bottom: 20px;
background-color: #f8f8f8;
font-family: monospace;
white-space: pre-wrap;
}
#calculation-steps {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin-bottom: 20px;
}
.step-container {
border: 1px solid #ddd;
padding: 10px;
flex: 1;
min-width: 200px;
}
table {
border-collapse: collapse;
width: 100%;
margin-top: 10px;
font-size: 0.9em;
}
th, td {
border: 1px solid #ddd;
padding: 6px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.error {
color: red;
font-weight: bold;
}
.warning {
color: orange;
}
.success {
color: green;
}
</style>
</head>
<body>
<h2>Oxygenation Index Dome Effect - Debug Mode</h2>
<div id="progress-container">
<div id="progress-bar">Initializing...</div>
</div>
<div id="debug-console"></div>
<div id="calculation-steps">
<div class="step-container" id="step1">
<h3>Step 1: Raw Data</h3>
<div id="raw-data-table">Loading...</div>
</div>
<div class="step-container" id="step2">
<h3>Step 2: OI Calculation</h3>
<div id="oi-calculation-table">Loading...</div>
</div>
<div class="step-container" id="step3">
<h3>Step 3: Polar Conversion</h3>
<div id="polar-conversion-table">Loading...</div>
</div>
</div>
<div id="plot" style="width: 100%; height: 600px;"></div>
<script type="text/javascript">
// Debug console functions
const debugConsole = document.getElementById('debug-console');
function logToConsole(message, type = 'info') {
const line = document.createElement('div');
line.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
line.className = type;
debugConsole.appendChild(line);
debugConsole.scrollTop = debugConsole.scrollHeight;
}
function updateProgress(percent, message) {
const progressBar = document.getElementById('progress-bar');
progressBar.style.width = percent + '%';
progressBar.textContent = message;
// Change color based on progress
if (percent < 30) progressBar.style.backgroundColor = '#ff5722';
else if (percent < 70) progressBar.style.backgroundColor = '#ffc107';
else progressBar.style.backgroundColor = '#4CAF50';
}
function createTable(data, containerId) {
const container = document.getElementById(containerId);
if (!data || data.length === 0) {
container.innerHTML = 'No data available';
return;
}
try {
let html = '<table><thead><tr>';
// Create header
Object.keys(data[0]).forEach(key => {
html += `<th>${key}</th>`;
});
html += '</tr></thead><tbody>';
// Create rows (limit to 10 for display)
const displayCount = Math.min(10, data.length);
for (let i = 0; i < displayCount; i++) {
html += '<tr>';
Object.values(data[i]).forEach(value => {
html += `<td>${value}</td>`;
});
html += '</tr>';
}
html += '</tbody></table>';
if (data.length > displayCount) {
html += `<div style="font-size:0.8em;color:#666;">Showing ${displayCount} of ${data.length} rows...</div>`;
}
container.innerHTML = html;
} catch (error) {
container.innerHTML = `Error creating table: ${error.message}`;
logToConsole(`Table creation error: ${error.message}`, 'error');
}
}
// Store the data globally
let plotData = null;
async function main() {
try {
logToConsole('Starting Pyodide initialization...');
updateProgress(5, 'Loading Pyodide runtime...');
// Load Pyodide
const pyodide = await loadPyodide({
stdout: text => logToConsole(`PY: ${text}`),
stderr: text => logToConsole(`PY-ERR: ${text}`, 'error')
});
logToConsole('Pyodide loaded successfully', 'success');
updateProgress(20, 'Loading Python packages...');
// Load required packages
await pyodide.loadPackage(['numpy', 'pandas']);
logToConsole('Python packages loaded', 'success');
updateProgress(30, 'Setting up Python environment...');
// Expose JavaScript functions to Python FIRST
pyodide.globals.set('js', {
displayRawData: rawData => {
logToConsole('Displaying raw data sample');
createTable(rawData, 'raw-data-table');
},
displayOICalculation: oiData => {
logToConsole('Displaying OI calculation sample');
createTable(oiData, 'oi-calculation-table');
},
displayPolarConversion: polarData => {
logToConsole('Displaying polar conversion sample');
createTable(polarData, 'polar-conversion-table');
},
updateProgress: updateProgress,
logToConsole: (message, type) => logToConsole(`PY->JS: ${message}`, type)
});
updateProgress(40, 'Generating data...');
// Run Python code with intermediate steps
await pyodide.runPythonAsync(`
import numpy as np
import pandas as pd
js.logToConsole("Starting data generation...", "info")
# Create the DataFrame with logging
fio2 = np.round(0.21 + 0.79 * np.random.rand(1000), 2)
map_values = np.round(np.random.uniform(low=5.0, high=50.0, size=1000))
pao2 = np.round(np.random.uniform(low=35.0, high=100.0, size=1000))
df = pd.DataFrame({
'FiO2': fio2,
'MAP': map_values,
'PaO2': pao2,
})
js.logToConsole(f"Generated DataFrame with {len(df)} rows", "info")
js.updateProgress(50, "Processing raw data...")
# Send raw data to JS for display
raw_data = df.head(10).to_dict('records')
js.displayRawData(raw_data)
# Calculate OI
js.logToConsole("Calculating Oxygenation Index...", "info")
df['OI'] = np.round((df['FiO2'] * df['MAP'] * 100) / df['PaO2'])
js.updateProgress(60, "Calculating OI values...")
# Send OI calculation to JS
oi_data = df[['FiO2', 'MAP', 'PaO2', 'OI']].head(10).to_dict('records')
js.displayOICalculation(oi_data)
# Normalize PaO2
js.logToConsole("Normalizing PaO2 values...", "info")
df['PaO2_normalized'] = np.round((df['PaO2'] - df['PaO2'].min()) / (df['PaO2'].max() - df['PaO2'].min()), 2)
js.updateProgress(70, "Normalizing values...")
# Convert to polar coordinates
js.logToConsole("Converting to polar coordinates...", "info")
r = np.round(df['PaO2_normalized'] * 2, 2)
theta = np.round(df['FiO2'] * 2 * np.pi, 2)
df['x'] = np.round(r * np.cos(theta), 2)
df['y'] = np.round(r * np.sin(theta), 2)
js.updateProgress(80, "Coordinate conversion...")
# Send polar conversion to JS
polar_data = df[['PaO2_normalized', 'FiO2', 'x', 'y']].head(10).to_dict('records')
js.displayPolarConversion(polar_data)
# Prepare final data for plotting
js.logToConsole("Preparing final data for visualization...", "info")
data = {
'x': df['x'].tolist(),
'y': df['y'].tolist(),
'z': df['OI'].tolist(),
'color': df['PaO2_normalized'].tolist(),
'text': ['PaO2: ' + str(p) + '<br>FiO2: ' + str(f) + '<br>MAP: ' + str(m) + '<br>OI: ' + str(o)
for p, f, m, o in zip(df['PaO2'], df['FiO2'], df['MAP'], df['OI'])]
}
js.logToConsole("Data preparation complete", "success")
js.updateProgress(90, "Rendering plot...")
# Store data in a global Python variable that we can access from JS
final_data = data
`);
// Get the final data from Python
plotData = pyodide.globals.get('final_data').toJs();
logToConsole('Data generation complete', 'success');
// Create the plot
renderPlot();
updateProgress(100, 'Complete!');
logToConsole('Visualization rendered successfully', 'success');
} catch (error) {
logToConsole(`Error: ${error.message}`, 'error');
updateProgress(0, `Error: ${error.message}`);
console.error(error);
}
}
function renderPlot() {
if (!plotData) {
logToConsole('No data available for plotting', 'error');
return;
}
try {
const trace = {
x: plotData.x,
y: plotData.y,
z: plotData.z,
text: plotData.text,
mode: 'markers',
marker: {
size: 5,
color: plotData.color,
colorscale: 'RdYlGn',
colorbar: { title: 'PaO2 Norm' },
showscale: true,
opacity: 0.8
},
type: 'scatter3d',
hoverinfo: 'text'
};
const layout = {
title: 'Oxygenation Index Dome Effect',
scene: {
xaxis: { title: 'X Coordinate' },
yaxis: { title: 'Y Coordinate' },
zaxis: { title: 'Oxygenation Index' },
camera: {
eye: { x: 1.5, y: 1.5, z: 0.8 }
}
},
autosize: true,
margin: { l: 0, r: 0, b: 0, t: 30 }
};
Plotly.newPlot('plot', [trace], layout);
logToConsole('3D plot rendered successfully', 'success');
} catch (error) {
logToConsole(`Plot rendering error: ${error.message}`, 'error');
}
}
// Start the application
main();
</script>
</body>
</html>