Realm

Plot gallery

Build retained multi-axis figures, render them through OA’s Vulkan compositor, using current Rust and Python programs.

TuPaired source6 outputsUi

Source

SurfaceRepository pathSHA-256
Rustsdk/rs/tutorials/plot/tu_plot_gallery/tu_plot_gallery.rs472886996b2eaa69
Pythonsdk/py/tutorials/plot/tu_plot_gallery/tu_plot_gallery.pyb95065fd49617980
Presentationsdk/asset/docs/plot/*.pngVerified per image

Example

A figure owns layout and axes state. Each axes records semantic artists; rendering lowers the retained figure through the shared compositor and returns an oa::Image. The language switch replaces the complete checked source fragment.

1use std::f32::consts::PI;
2
3use oa::plot::{Axes, BarStyle, FigureConfig, HeatmapStyle, LineStyle, ScatterStyle, Theme};
4use oa::{Color, Engine};
5
6// ── Style helpers ─────────────────────────────────────────────────────────────
7
8fn line(color: Color, label: &str, width: f32, antialias_samples: u32) -> LineStyle {
9 LineStyle {
10 color,
11 label: label.to_owned(),
12 width,
13 antialias_samples,
14 ..Default::default()
15 }
16}
17
18fn points(color: Color, label: &str, radius: f32) -> ScatterStyle {
19 ScatterStyle {
20 color,
21 label: label.to_owned(),
22 radius,
23 ..Default::default()
24 }
25}
26
27fn bars(color: Color, label: &str, gap: f32) -> BarStyle {
28 BarStyle {
29 color,
30 label: label.to_owned(),
31 gap,
32 ..Default::default()
33 }
34}
35
36fn gradient(a: Color, b: Color, t: f32) -> Color {
37 let t = t.clamp(0.0, 1.0);
38 Color::new(
39 a.r + (b.r - a.r) * t,
40 a.g + (b.g - a.g) * t,
41 a.b + (b.b - a.b) * t,
42 a.a + (b.a - a.a) * t,
43 )
44}
45
46// ── Named colors matching the donor palette ───────────────────────────────────
47
48fn accent() -> Color {
49 Color::new(0.24, 0.65, 1.00, 1.0)
50}
51fn success() -> Color {
52 Color::new(0.35, 0.82, 0.54, 1.0)
53}
54fn purple() -> Color {
55 Color::new(0.75, 0.45, 1.00, 1.0)
56}
57fn cyan() -> Color {
58 Color::new(0.30, 0.87, 0.89, 1.0)
59}
60fn warning() -> Color {
61 Color::new(1.00, 0.80, 0.22, 1.0)
62}
63fn pink() -> Color {
64 Color::new(1.00, 0.55, 0.75, 1.0)
65}
66fn gray() -> Color {
67 Color::new(0.565, 0.565, 0.565, 1.0)
68}
69
70// ── Individual axes builders ──────────────────────────────────────────────────
71
72// [oa-plot-intro-begin]
73fn intro_figure() -> oa::plot::Figure {
74 let mut fig = oa::plot::Figure::new(FigureConfig {
75 title: "oa::plot intro".to_owned(),
76 rows: 1,
77 cols: 2,
78 width: 1280,
79 height: 560,
80 h_spacing: 36,
81 padding: 34,
82 theme: Theme::Dark,
83 ..Default::default()
84 });
85 fig.title("One retained figure — Rust and C++ parity");
86
87 let steps = [0.0f32, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0];
88 let train = [1.00f32, 0.78, 0.61, 0.48, 0.37, 0.29, 0.23, 0.19];
89 let validation = [1.04f32, 0.83, 0.66, 0.53, 0.43, 0.35, 0.30, 0.27];
90 let ideal = [0.0f32, 0.25, 0.50, 0.75, 1.0];
91 let confidence = [0.10f32, 0.30, 0.50, 0.70, 0.90];
92 let accuracy = [0.08f32, 0.34, 0.47, 0.74, 0.88];
93
94 let curves = fig.ax(0, 0);
95 curves.title("training curves");
96 curves.x_label("optimizer step");
97 curves.y_label("cross entropy");
98 curves.limits(0.0, 7.0, 0.0, 1.1);
99 curves.plot_xy(&steps, &train, line(accent(), "train", 1.6, 4));
100 curves.plot_xy(&steps, &validation, line(success(), "validation", 1.6, 4));
101
102 let quality = fig.ax(0, 1);
103 quality.title("Calibration");
104 quality.x_label("confidence");
105 quality.y_label("observed accuracy");
106 quality.limits(0.0, 1.0, 0.0, 1.0);
107 quality.plot_xy(&ideal, &ideal, line(gray(), "ideal", 1.35, 4));
108 quality.scatter(&confidence, &accuracy, points(cyan(), "model", 3.5));
109 fig
110}
111// [oa-plot-intro-end]
112
113fn training_plot(ax: &mut Axes) {
114 let n = 72usize;
115 let train: Vec<f32> = (0..n)
116 .map(|i| {
117 let step = i as f32;
118 1.55 * (-step / 18.0).exp() + 0.035 * (step * 0.52).sin() + 0.11
119 })
120 .collect();
121 let validation: Vec<f32> = (0..n)
122 .map(|i| {
123 let step = i as f32;
124 1.45 * (-step / 21.0).exp() + 0.045 * (step * 0.39 + 0.8).sin() + 0.17
125 })
126 .collect();
127 ax.title("training curves");
128 ax.x_label("optimizer step");
129 ax.y_label("cross entropy");
130 ax.plot(&train, line(accent(), "train", 1.35, 4));
131 ax.plot(&validation, line(success(), "validation", 1.35, 4));
132}
133
134fn roc_plot(ax: &mut Axes) {
135 let n = 33usize;
136 let rate: Vec<f32> = (0..n).map(|i| i as f32 / (n - 1) as f32).collect();
137 let vision: Vec<f32> = rate
138 .iter()
139 .map(|&x| (1.0 - (-4.3 * x).exp()).clamp(0.0, 1.0))
140 .collect();
141 let fusion: Vec<f32> = rate
142 .iter()
143 .map(|&x| (1.0 - (-6.7 * x).exp()).clamp(0.0, 1.0))
144 .collect();
145 ax.title("ROC — threshold sweep");
146 ax.x_label("false positive rate");
147 ax.y_label("true positive rate");
148 ax.limits(0.0, 1.0, 0.0, 1.0);
149 ax.plot_xy(&rate, &vision, line(cyan(), "vision AUC 0.88", 1.35, 4));
150 ax.plot_xy(&rate, &fusion, line(success(), "fusion AUC 0.93", 1.35, 4));
151 ax.plot_xy(&rate, &rate, line(gray(), "chance", 1.35, 4));
152}
153
154fn precision_recall_plot(ax: &mut Axes) {
155 let n = 33usize;
156 let recall: Vec<f32> = (0..n).map(|i| i as f32 / (n - 1) as f32).collect();
157 let vision: Vec<f32> = recall
158 .iter()
159 .map(|&x| (0.98 - 0.40 * x.powf(1.6) - 0.025 * (x * 5.0 * PI).sin()).clamp(0.0, 1.0))
160 .collect();
161 let fusion: Vec<f32> = recall
162 .iter()
163 .map(|&x| (0.995 - 0.27 * x.powf(2.1) - 0.015 * (x * 4.0 * PI).sin()).clamp(0.0, 1.0))
164 .collect();
165 ax.title("Precision-recall");
166 ax.x_label("recall");
167 ax.y_label("precision");
168 ax.limits(0.0, 1.0, 0.0, 1.0);
169 ax.plot_xy(&recall, &vision, line(purple(), "vision AP 0.84", 1.35, 4));
170 ax.plot_xy(&recall, &fusion, line(pink(), "fusion AP 0.91", 1.35, 4));
171}
172
173fn score_histogram(ax: &mut Axes) {
174 let scores: Vec<f32> = (0..192usize)
175 .map(|i| {
176 let phase = i as f32;
177 let cluster = if i % 3 == 0 { 0.28 } else { 0.74 };
178 (cluster + 0.12 * (phase * 1.73).sin() + 0.045 * (phase * 0.37).cos()).clamp(0.0, 1.0)
179 })
180 .collect();
181 ax.title("confidence distribution");
182 ax.x_label("model score");
183 ax.y_label("samples");
184 ax.histogram(&scores, 20, bars(accent(), "validation scores", 0.20));
185}
186
187fn calibration_scatter(ax: &mut Axes) {
188 let n = 24usize;
189 let confidence: Vec<f32> = (0..n).map(|i| (i as f32 + 0.5) / n as f32).collect();
190 let accuracy: Vec<f32> = confidence
191 .iter()
192 .map(|&x| (x + 0.055 * (8.0 * PI * x).sin() - 0.025).clamp(0.0, 1.0))
193 .collect();
194 let ideal: Vec<f32> = confidence.clone();
195 ax.title("Calibration");
196 ax.x_label("confidence");
197 ax.y_label("observed accuracy");
198 ax.limits(0.0, 1.0, 0.0, 1.0);
199 ax.plot_xy(&confidence, &ideal, line(gray(), "ideal", 1.35, 4));
200 ax.scatter(&confidence, &accuracy, points(cyan(), "model", 3.5));
201}
202
203fn throughput_bars(ax: &mut Axes) {
204 let throughput = [0.42f32, 0.58, 0.71, 0.86, 0.79, 0.94];
205 ax.title("normalized throughput");
206 ax.x_label("kernel route");
207 ax.y_label("relative peak");
208 ax.limits(-0.5, 5.5, 0.0, 1.0);
209 ax.bar(&throughput, bars(warning(), "device routes", 0.24));
210}
211
212fn confusion_heatmap(ax: &mut Axes) {
213 let confusion = [
214 42.0f32, 2.0, 0.0, 1.0, 0.0, 3.0, 37.0, 2.0, 0.0, 1.0, 0.0, 2.0, 40.0, 3.0, 0.0, 1.0, 0.0, 2.0,
215 38.0, 2.0, 0.0, 1.0, 0.0, 2.0, 43.0,
216 ];
217 ax.title("confusion matrix");
218 ax.x_label("predicted class");
219 ax.y_label("reference class");
220 ax.heatmap(
221 &confusion,
222 5,
223 5,
224 HeatmapStyle {
225 colormap: 1,
226 auto_scale: true,
227 show_grid: true,
228 ..Default::default()
229 },
230 );
231 ax.grid(false);
232}
233
234fn wireframe(
235 ax: &mut Axes,
236 title: &str,
237 height_fn: impl Fn(f32, f32) -> f32,
238 near: Color,
239 far: Color,
240) {
241 const LINES: i32 = 13;
242 const SAMPLES: i32 = 29;
243 // Row sweeps.
244 for row in 0..LINES {
245 let v = -1.0 + 2.0 * row as f32 / (LINES - 1) as f32;
246 let mut px = vec![0.0f32; SAMPLES as usize];
247 let mut py = vec![0.0f32; SAMPLES as usize];
248 for col in 0..SAMPLES {
249 let u = -1.0 + 2.0 * col as f32 / (SAMPLES - 1) as f32;
250 let z = height_fn(u, v);
251 px[col as usize] = 0.50 + 0.34 * (u - v);
252 py[col as usize] = 0.58 - 0.16 * (u + v) - 0.23 * z;
253 }
254 let t = row as f32 / (LINES - 1) as f32;
255 ax.plot_xy(&px, &py, line(gradient(far, near, t), "", 1.15, 8));
256 }
257 // Column sweeps.
258 for col in 0..LINES {
259 let u = -1.0 + 2.0 * col as f32 / (LINES - 1) as f32;
260 let mut px = vec![0.0f32; SAMPLES as usize];
261 let mut py = vec![0.0f32; SAMPLES as usize];
262 for row in 0..SAMPLES {
263 let v = -1.0 + 2.0 * row as f32 / (SAMPLES - 1) as f32;
264 let z = height_fn(u, v);
265 px[row as usize] = 0.50 + 0.34 * (u - v);
266 py[row as usize] = 0.58 - 0.16 * (u + v) - 0.23 * z;
267 }
268 let t = col as f32 / (LINES - 1) as f32;
269 let mut col_style = line(gradient(far, near, t), "", 1.15, 8);
270 col_style.color.a = 0.78;
271 ax.plot_xy(&px, &py, col_style);
272 }
273 ax.title(title);
274 ax.limits(-0.20, 1.20, 0.02, 1.08);
275 ax.grid(false);
276 ax.legend(false);
277}
278
279fn loss_landscape(ax: &mut Axes) {
280 wireframe(
281 ax,
282 "projected loss landscape",
283 |x, y| {
284 let bowl = 0.42 * (x * x + 0.75 * y * y);
285 bowl + 0.18 * (2.7 * x).sin() * (3.1 * y).cos()
286 },
287 cyan(),
288 purple(),
289 );
290}
291
292fn gradient_basin(ax: &mut Axes) {
293 wireframe(
294 ax,
295 "projected optimizer basin",
296 |x, y| {
297 let radius = (x * x + y * y).sqrt();
298 0.34 * radius + 0.17 * (9.0 * radius).cos() * (-1.8 * radius).exp()
299 },
300 success(),
301 warning(),
302 );
303}
304
305fn gallery(theme: Theme) -> oa::plot::Figure {
306 let mut fig = oa::plot::Figure::new(FigureConfig {
307 title: "OA Plot gallery".to_owned(),
308 rows: 3,
309 cols: 3,
310 width: 1600,
311 height: 1050,
312 h_spacing: 32,
313 v_spacing: 36,
314 padding: 34,
315 theme,
316 ..Default::default()
317 });
318 fig.title("OA Plot — GPU-composed ML diagnostics");
319 training_plot(fig.ax(0, 0));
320 roc_plot(fig.ax(0, 1));
321 precision_recall_plot(fig.ax(0, 2));
322 score_histogram(fig.ax(1, 0));
323 calibration_scatter(fig.ax(1, 1));
324 throughput_bars(fig.ax(1, 2));
325 confusion_heatmap(fig.ax(2, 0));
326 loss_landscape(fig.ax(2, 1));
327 gradient_basin(fig.ax(2, 2));
328 fig
329}
330
331fn evaluation_figure() -> oa::plot::Figure {
332 let mut fig = oa::plot::Figure::new(FigureConfig {
333 title: "OA model evaluation".to_owned(),
334 rows: 1,
335 cols: 2,
336 width: 1280,
337 height: 560,
338 h_spacing: 36,
339 padding: 34,
340 ..Default::default()
341 });
342 fig.title("Model evaluation — ROC and precision-recall");
343 roc_plot(fig.ax(0, 0));
344 precision_recall_plot(fig.ax(0, 1));
345 fig
346}
347
348fn diagnostics_figure() -> oa::plot::Figure {
349 let mut fig = oa::plot::Figure::new(FigureConfig {
350 title: "OA model diagnostics".to_owned(),
351 rows: 2,
352 cols: 2,
353 width: 1200,
354 height: 900,
355 h_spacing: 32,
356 v_spacing: 36,
357 padding: 34,
358 ..Default::default()
359 });
360 fig.title("training and evaluation diagnostics");
361 training_plot(fig.ax(0, 0));
362 score_histogram(fig.ax(0, 1));
363 calibration_scatter(fig.ax(1, 0));
364 confusion_heatmap(fig.ax(1, 1));
365 fig
366}
367
368fn landscapes_figure() -> oa::plot::Figure {
369 let mut fig = oa::plot::Figure::new(FigureConfig {
370 title: "OA projected landscapes".to_owned(),
371 rows: 1,
372 cols: 2,
373 width: 1280,
374 height: 560,
375 h_spacing: 36,
376 padding: 34,
377 ..Default::default()
378 });
379 fig.title("scalar-field projections — explicit-X wireframes");
380 loss_landscape(fig.ax(0, 0));
381 gradient_basin(fig.ax(0, 1));
382 fig
383}
384
385// ── Entry point ───────────────────────────────────────────────────────────────
386
387fn usage(program: &str) {
388 eprintln!("usage: {program} [--output DIR] [--headless]");
389}
390
391fn main() -> anyhow::Result<()> {
392 let mut output = std::path::PathBuf::from("artifact/plot");
393 let mut show = true;
394
395 let args: Vec<String> = std::env::args().collect();
396 let mut i = 1;
397 while i < args.len() {
398 match args[i].as_str() {
399 "--headless" => {
400 show = false;
401 }
402 "--output" if i + 1 < args.len() => {
403 i += 1;
404 output = std::path::PathBuf::from(&args[i]);
405 }
406 "--help" => {
407 usage(&args[0]);
408 return Ok(());
409 }
410 other => {
411 eprintln!("unknown argument: {other}");
412 usage(&args[0]);
413 std::process::exit(1);
414 }
415 }
416 i += 1;
417 }
418
419 std::fs::create_dir_all(&output).map_err(|e| {
420 eprintln!("could not create output dir: {e}");
421 e
422 })?;
423
424 let engine = if show {
425 let window = oa::SdlWindow::new("OA Plot presentation probe", 1, 1)?;
426 let extensions = window.required_instance_extensions()?;
427 Engine::builder().instance_extensions(extensions).build()?
428 } else {
429 Engine::new()?
430 };
431
432 let mut dark = gallery(Theme::Dark);
433 let mut light = gallery(Theme::Light);
434 let mut intro = intro_figure();
435 let mut evaluation = evaluation_figure();
436 let mut diagnostics = diagnostics_figure();
437 let mut landscapes = landscapes_figure();
438
439 println!("OA Plot walkthrough artifacts:");
440
441 let save = |fig: &mut oa::plot::Figure, name: &str| -> anyhow::Result<()> {
442 let path = output.join(name);
443 fig.save_to(&engine, path.to_str().unwrap_or(name))?;
444 println!(" {}", path.display());
445 Ok(())
446 };
447
448 save(&mut intro, "oa-plot-intro-dark.png")?;
449 save(&mut dark, "oa-plot-gallery-dark.png")?;
450 save(&mut light, "oa-plot-gallery-light.png")?;
451 save(&mut evaluation, "oa-plot-evaluation-dark.png")?;
452 save(&mut diagnostics, "oa-plot-diagnostics-dark.png")?;
453 save(&mut landscapes, "oa-plot-landscapes-dark.png")?;
454
455 if show {
456 dark.show(&engine)?;
457 }
458 Ok(())
459}
460

Present

These earlier gallery renders illustrate dark and light diagnostics, evaluation curves, training layouts, and scalar-field landscapes. They retain their original presentation provenance and are not new Rust render evidence.

OA Plot C++ and Python parity intro
Six generated OA Plot examples