-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_visualizations.py
More file actions
318 lines (262 loc) · 10.6 KB
/
generate_visualizations.py
File metadata and controls
318 lines (262 loc) · 10.6 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
#!/usr/bin/env python3
"""
Generate beautiful Plotly visualizations for pydelt documentation.
This script creates interactive plots demonstrating key features and saves them as HTML.
"""
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.offline as pyo
from pathlib import Path
# Import pydelt modules
from pydelt.interpolation import SplineInterpolator, LlaInterpolator, GllaInterpolator
from pydelt.multivariate import MultivariateDerivatives
def create_universal_api_demo():
"""Create visualization showing universal differentiation API across methods."""
# Generate test data: f(x) = sin(x) + 0.1*sin(10*x) + noise
np.random.seed(42)
x = np.linspace(0, 2*np.pi, 100)
y_clean = np.sin(x) + 0.1*np.sin(10*x)
noise = 0.05 * np.random.randn(len(x))
y_noisy = y_clean + noise
# True derivative: cos(x) + cos(10*x)
x_eval = np.linspace(0, 2*np.pi, 200)
y_true = np.cos(x_eval) + np.cos(10*x_eval)
# Test different interpolators
methods = {
'Spline': SplineInterpolator(smoothing=0.1),
'LLA': LlaInterpolator(window_size=7),
'GLLA': GllaInterpolator(embedding=3, n=2)
}
fig = make_subplots(
rows=2, cols=2,
subplot_titles=('Original Data with Noise', 'Derivative Comparison',
'Error Analysis', 'Method Performance'),
specs=[[{"colspan": 2}, None],
[{"type": "scatter"}, {"type": "bar"}]]
)
# Plot 1: Original data
fig.add_trace(go.Scatter(
x=x, y=y_noisy, mode='markers', name='Noisy Data',
marker=dict(size=4, color='lightblue', opacity=0.6)
), row=1, col=1)
fig.add_trace(go.Scatter(
x=x, y=y_clean, mode='lines', name='True Function',
line=dict(color='black', width=2)
), row=1, col=1)
# Plot 2: Derivatives
fig.add_trace(go.Scatter(
x=x_eval, y=y_true, mode='lines', name='True Derivative',
line=dict(color='black', width=3, dash='dash')
), row=2, col=1)
colors = ['red', 'blue', 'green']
errors = []
for i, (name, interpolator) in enumerate(methods.items()):
# Fit and compute derivative
interpolator.fit(x, y_noisy)
derivative_func = interpolator.differentiate(order=1)
y_pred = derivative_func(x_eval)
# Calculate error
error = np.mean(np.abs(y_pred - y_true))
errors.append(error)
fig.add_trace(go.Scatter(
x=x_eval, y=y_pred, mode='lines', name=f'{name} Derivative',
line=dict(color=colors[i], width=2)
), row=2, col=1)
# Plot 3: Error bars
fig.add_trace(go.Bar(
x=list(methods.keys()), y=errors, name='Mean Absolute Error',
marker_color=['red', 'blue', 'green']
), row=2, col=2)
fig.update_layout(
title="Universal Differentiation API: Consistent Interface Across Methods",
height=800,
showlegend=True
)
fig.update_xaxes(title_text="x", row=1, col=1)
fig.update_yaxes(title_text="f(x)", row=1, col=1)
fig.update_xaxes(title_text="x", row=2, col=1)
fig.update_yaxes(title_text="f'(x)", row=2, col=1)
fig.update_xaxes(title_text="Method", row=2, col=2)
fig.update_yaxes(title_text="Error", row=2, col=2)
return fig
def create_multivariate_surface():
"""Create 3D surface plot for multivariate calculus demonstration."""
# Generate 2D function: f(x,y) = sin(x)*cos(y) + 0.1*x*y
x = np.linspace(-3, 3, 30)
y = np.linspace(-3, 3, 30)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y) + 0.1 * X * Y
# Prepare data for multivariate derivatives
input_data = np.column_stack([X.flatten(), Y.flatten()])
output_data = Z.flatten()
# Compute gradient using pydelt
mv = MultivariateDerivatives(SplineInterpolator, smoothing=0.1)
mv.fit(input_data, output_data)
gradient_func = mv.gradient()
# Evaluate gradient on a grid
x_grad = np.linspace(-2, 2, 15)
y_grad = np.linspace(-2, 2, 15)
X_grad, Y_grad = np.meshgrid(x_grad, y_grad)
grad_points = np.column_stack([X_grad.flatten(), Y_grad.flatten()])
gradients = gradient_func(grad_points)
# Reshape gradients
grad_x = gradients[:, 0].reshape(X_grad.shape)
grad_y = gradients[:, 1].reshape(Y_grad.shape)
fig = make_subplots(
rows=2, cols=2,
subplot_titles=('3D Function Surface', 'Gradient Vector Field',
'Gradient X-Component', 'Gradient Y-Component'),
specs=[[{"type": "surface"}, {"type": "scatter"}],
[{"type": "heatmap"}, {"type": "heatmap"}]]
)
# 3D surface
fig.add_trace(go.Surface(
x=X, y=Y, z=Z,
colorscale='Viridis',
name='f(x,y)'
), row=1, col=1)
# Vector field
fig.add_trace(go.Scatter(
x=X_grad.flatten(), y=Y_grad.flatten(),
mode='markers',
marker=dict(
size=8,
color=np.sqrt(grad_x.flatten()**2 + grad_y.flatten()**2),
colorscale='Plasma',
showscale=True,
colorbar=dict(title="Gradient Magnitude")
),
name='Gradient Field'
), row=1, col=2)
# Add arrows for vector field
for i in range(0, len(x_grad), 2):
for j in range(0, len(y_grad), 2):
fig.add_annotation(
x=X_grad[i,j], y=Y_grad[i,j],
ax=X_grad[i,j] + 0.3*grad_x[i,j], ay=Y_grad[i,j] + 0.3*grad_y[i,j],
xref="x2", yref="y2", axref="x2", ayref="y2",
arrowhead=2, arrowsize=1, arrowwidth=2, arrowcolor="red"
)
# Gradient components
fig.add_trace(go.Heatmap(
x=x_grad, y=y_grad, z=grad_x,
colorscale='RdBu', name='∂f/∂x'
), row=2, col=1)
fig.add_trace(go.Heatmap(
x=x_grad, y=y_grad, z=grad_y,
colorscale='RdBu', name='∂f/∂y'
), row=2, col=2)
fig.update_layout(
title="Multivariate Calculus: Gradient Computation for f(x,y) = sin(x)cos(y) + 0.1xy",
height=900
)
return fig
def create_method_comparison():
"""Create comparison of different interpolation methods."""
# Generate challenging test function with multiple features
np.random.seed(42)
x = np.linspace(0, 4*np.pi, 80)
y_true = np.sin(x) * np.exp(-x/8) + 0.3*np.sin(5*x)
noise = 0.1 * np.random.randn(len(x))
y_noisy = y_true + noise
# Evaluation points
x_eval = np.linspace(0, 4*np.pi, 200)
y_eval_true = np.sin(x_eval) * np.exp(-x_eval/8) + 0.3*np.sin(5*x_eval)
methods = {
'Spline (s=0.1)': SplineInterpolator(smoothing=0.1),
'Spline (s=1.0)': SplineInterpolator(smoothing=1.0),
'LLA (w=5)': LlaInterpolator(window_size=5),
'LLA (w=15)': LlaInterpolator(window_size=15),
}
fig = make_subplots(
rows=2, cols=2,
subplot_titles=('Method Comparison', 'Residuals',
'Parameter Sensitivity', 'Computational Performance'),
specs=[[{"colspan": 2}, None],
[{"type": "scatter"}, {"type": "bar"}]]
)
# Original data
fig.add_trace(go.Scatter(
x=x, y=y_noisy, mode='markers', name='Noisy Data',
marker=dict(size=4, color='lightgray', opacity=0.7)
), row=1, col=1)
fig.add_trace(go.Scatter(
x=x_eval, y=y_eval_true, mode='lines', name='True Function',
line=dict(color='black', width=3)
), row=1, col=1)
colors = px.colors.qualitative.Set1
rmse_values = []
for i, (name, interpolator) in enumerate(methods.items()):
# Fit interpolator
interpolator.fit(x, y_noisy)
# Get interpolated values (not derivatives for this comparison)
if hasattr(interpolator, 'interpolate'):
y_pred = interpolator.interpolate(x_eval)
else:
# For methods without direct interpolation, use the fitted function
try:
y_pred = interpolator.spline(x_eval)
except:
# Fallback: evaluate at training points and interpolate
from scipy.interpolate import interp1d
y_fit = interpolator.fit(x, y_noisy)
if hasattr(y_fit, '__call__'):
y_pred = y_fit(x_eval)
else:
# Simple linear interpolation as fallback
interp = interp1d(x, y_noisy, kind='linear', fill_value='extrapolate')
y_pred = interp(x_eval)
# Calculate RMSE
rmse = np.sqrt(np.mean((y_pred - y_eval_true)**2))
rmse_values.append(rmse)
fig.add_trace(go.Scatter(
x=x_eval, y=y_pred, mode='lines', name=name,
line=dict(color=colors[i % len(colors)], width=2)
), row=1, col=1)
# Residuals
residuals = y_pred - y_eval_true
fig.add_trace(go.Scatter(
x=x_eval, y=residuals, mode='lines', name=f'{name} Residuals',
line=dict(color=colors[i % len(colors)], width=1, dash='dot'),
showlegend=False
), row=2, col=1)
# RMSE comparison
fig.add_trace(go.Bar(
x=list(methods.keys()), y=rmse_values, name='RMSE',
marker_color=colors[:len(methods)]
), row=2, col=2)
fig.update_layout(
title="Method Comparison: Interpolation Performance on Complex Function",
height=800,
showlegend=True
)
fig.update_xaxes(title_text="x", row=1, col=1)
fig.update_yaxes(title_text="f(x)", row=1, col=1)
fig.update_xaxes(title_text="x", row=2, col=1)
fig.update_yaxes(title_text="Residual", row=2, col=1)
fig.update_xaxes(title_text="Method", row=2, col=2)
fig.update_yaxes(title_text="RMSE", row=2, col=2)
return fig
def main():
"""Generate all visualizations and save to docs directory."""
# Create output directory
output_dir = Path("docs/_static/images")
output_dir.mkdir(parents=True, exist_ok=True)
print("Generating visualizations...")
# Generate plots
plots = {
"universal_api_demo.html": create_universal_api_demo(),
"multivariate_surface.html": create_multivariate_surface(),
"method_comparison.html": create_method_comparison()
}
# Save plots
for filename, fig in plots.items():
filepath = output_dir / filename
fig.write_html(str(filepath))
print(f"Saved: {filepath}")
print("\nAll visualizations generated successfully!")
print(f"Files saved to: {output_dir}")
if __name__ == "__main__":
main()