kuva 0.3.0

Scientific plotting library in Rust with various backends.
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
mod common;
use kuva::backend::svg::SvgBackend;
use kuva::plot::{ColorMap, Heatmap, PhyloTree};
use kuva::render::figure::Figure;
use kuva::render::layout::Layout;
use kuva::render::plots::Plot;
use kuva::render::render::render_multiple;

#[test]
fn test_heatmap_colorbar_values() {
    let data = vec![
        vec![10.0, 20.0, 30.0],
        vec![4.0, 50.0, 6.0],
        vec![7.0, 8.0, 90.0],
    ];

    let heatmap = Heatmap::new()
        .with_data(data)
        .with_values()
        // .with_color_map(ColorMap::Grayscale);
        .with_color_map(ColorMap::Viridis);
    // .with_color_map(ColorMap::Inferno);

    let plots = vec![Plot::Heatmap(heatmap)];

    let layout = Layout::auto_from_plots(&plots).with_title("Heatmap");
    // .with_x_categories(x_labels);

    let scene = render_multiple(plots, layout);
    let svg = SvgBackend.render_scene(&scene);
    common::write_test_output("test_outputs/heatmap_values.svg", svg.clone()).unwrap();

    // Basic sanity assertion
    assert!(svg.contains("<svg"));
}

#[test]
fn test_heatmap_colorbar() {
    let data = vec![
        vec![10.0, 20.0, 30.0],
        vec![4.0, 50.0, 6.0],
        vec![7.0, 8.0, 90.0],
    ];

    let heatmap = Heatmap::new()
        .with_data(data)
        .with_color_map(ColorMap::Viridis);

    let plots = vec![Plot::Heatmap(heatmap)];

    let layout = Layout::auto_from_plots(&plots).with_title("Heatmap with Colorbar");

    let scene = render_multiple(plots, layout);
    let svg = SvgBackend.render_scene(&scene);
    common::write_test_output("test_outputs/heatmap_colorbar.svg", svg.clone()).unwrap();

    assert!(svg.contains("<svg"));
    assert!(svg.contains("<rect")); // colorbar rects
}

/// Verify that `with_y_categories` reorders data rows such that desired_order[0]
/// lands at the TOP of the rendered heatmap (= last data row, bottom-to-top convention).
/// Desired order [C, B, A] → top-to-bottom → stored internally as [A, B, C] (bottom-to-top).
#[test]
fn test_heatmap_with_y_categories_reorders_data() {
    // Row labels in original order: A, B, C
    // Row A is distinctive: first column value is 99.0
    let data = vec![
        vec![99.0, 1.0, 2.0], // row A
        vec![3.0, 4.0, 5.0],  // row B
        vec![6.0, 7.0, 8.0],  // row C
    ];
    let row_labels: Vec<String> = ["A", "B", "C"].iter().map(|s| s.to_string()).collect();
    let col_labels: Vec<String> = ["x", "y", "z"].iter().map(|s| s.to_string()).collect();

    // Desired top-to-bottom order: C (top), B (mid), A (bottom)
    let desired_top_to_bottom: Vec<String> =
        ["C", "B", "A"].iter().map(|s| s.to_string()).collect();

    let heatmap = Heatmap::new()
        .with_data(data)
        .with_labels(row_labels, col_labels)
        .with_y_categories(desired_top_to_bottom);

    // Internally stored bottom-to-top: row 0 = A (bottom), row 1 = B, row 2 = C (top)
    assert_eq!(heatmap.data[0][0], 99.0, "data row 0 (bottom) should be A");
    assert_eq!(heatmap.data[1][0], 3.0, "data row 1 should be B");
    assert_eq!(heatmap.data[2][0], 6.0, "data row 2 (top) should be C");

    // row_labels is bottom-to-top — can be passed directly to Layout::with_y_categories
    let expected_row_labels: &[String] = &["A", "B", "C"]
        .iter()
        .map(|s| s.to_string())
        .collect::<Vec<_>>();
    assert_eq!(heatmap.row_labels.as_deref(), Some(expected_row_labels));

    // Render to SVG for visual inspection (C at top, A at bottom)
    let layout_cats = heatmap.row_labels.clone().unwrap();
    let plots = vec![Plot::Heatmap(heatmap)];
    let layout = Layout::auto_from_plots(&plots)
        .with_title("Heatmap — C top, B mid, A bottom")
        .with_y_categories(layout_cats);
    let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
    common::write_test_output("test_outputs/heatmap_y_categories.svg", svg.clone()).unwrap();
    assert!(svg.contains("<svg"));
}

/// Verify that `with_x_categories` reorders data columns to match the given label order.
#[test]
fn test_heatmap_with_x_categories_reorders_data() {
    // Column labels in original order: x, y, z
    // Column z (index 2) has distinctive values: 10, 20, 30
    let data = vec![
        vec![1.0, 2.0, 10.0],
        vec![3.0, 4.0, 20.0],
        vec![5.0, 6.0, 30.0],
    ];
    let row_labels: Vec<String> = ["A", "B", "C"].iter().map(|s| s.to_string()).collect();
    let col_labels: Vec<String> = ["x", "y", "z"].iter().map(|s| s.to_string()).collect();

    // Desired column order: z, x, y
    let desired: Vec<String> = ["z", "x", "y"].iter().map(|s| s.to_string()).collect();

    let heatmap = Heatmap::new()
        .with_data(data)
        .with_labels(row_labels, col_labels)
        .with_x_categories(desired.clone());

    // After reordering, column 0 should be z (10, 20, 30)
    assert_eq!(
        heatmap.data[0][0], 10.0,
        "col 0 row 0 should be z-value for A"
    );
    assert_eq!(
        heatmap.data[1][0], 20.0,
        "col 0 row 1 should be z-value for B"
    );
    assert_eq!(
        heatmap.data[2][0], 30.0,
        "col 0 row 2 should be z-value for C"
    );
    assert_eq!(heatmap.col_labels.as_deref(), Some(desired.as_slice()));

    let plots = vec![Plot::Heatmap(heatmap)];
    let layout = Layout::auto_from_plots(&plots)
        .with_title("Heatmap — cols reordered z, x, y")
        .with_x_categories(desired);
    let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
    common::write_test_output("test_outputs/heatmap_x_categories.svg", svg.clone()).unwrap();
    assert!(svg.contains("<svg"));
}

/// Full phylo+heatmap alignment workflow: build a tree from a distance matrix,
/// get leaf order, reorder heatmap rows to match, and render both side by side
/// using Figure so the tree and heatmap appear in adjacent cells.
#[test]
fn test_phylo_heatmap_alignment() {
    let labels_str = ["Wolf", "Cat", "Whale", "Human"];
    let labels: Vec<String> = labels_str.iter().map(|s| s.to_string()).collect();

    // Pairwise distance matrix — Wolf/Cat close, Whale/Human close
    let dist = vec![
        vec![0.0, 0.5, 0.9, 0.8],
        vec![0.5, 0.0, 0.9, 0.8],
        vec![0.9, 0.9, 0.0, 0.7],
        vec![0.8, 0.8, 0.7, 0.0],
    ];

    let tree = PhyloTree::from_distance_matrix(&labels_str, &dist).with_phylogram();
    let leaf_order = tree.leaf_labels_top_to_bottom(); // top-to-bottom tree order

    // Build a heatmap using the same distance matrix, rows reordered to match tree.
    // with_y_categories takes top-to-bottom order; first leaf ends up at the top of
    // the heatmap. row_labels is stored bottom-to-top for Layout::with_y_categories.
    let heatmap = Heatmap::new()
        .with_data(dist)
        .with_labels(labels, vec![])
        .with_y_categories(leaf_order.clone());

    // The last leaf (bottom of tree) should be in data row 0 (bottom of heatmap).
    let last_leaf = leaf_order.last().unwrap().as_str();
    let last_leaf_idx_in_original = labels_str.iter().position(|&s| s == last_leaf).unwrap();
    assert_eq!(
        heatmap.data[0][last_leaf_idx_in_original], 0.0,
        "diagonal must be 0.0: bottom-of-tree leaf should be in data row 0"
    );

    // row_labels is bottom-to-top — pass directly to Layout::with_y_categories
    let layout_cats = heatmap.row_labels.clone().unwrap();

    let tree_plots = vec![Plot::PhyloTree(tree)];
    let heatmap_plots = vec![Plot::Heatmap(heatmap)];

    let tree_layout = Layout::auto_from_plots(&tree_plots).with_title("UPGMA Tree");
    let heatmap_layout = Layout::auto_from_plots(&heatmap_plots)
        .with_title("Distance Matrix")
        .with_y_categories(layout_cats);

    // 1 row × 2 cols: tree on left, heatmap on right
    let figure = Figure::new(1, 2)
        .with_plots(vec![tree_plots, heatmap_plots])
        .with_layouts(vec![tree_layout, heatmap_layout])
        .with_title("Phylo + Heatmap — aligned leaf order");

    let svg = SvgBackend.render_scene(&figure.render());
    common::write_test_output("test_outputs/heatmap_phylo_alignment.svg", svg.clone()).unwrap();
    assert!(svg.contains("<svg"));
}

/// Refactor regression: ensure `colorbar_linear` produces the same colorbar
/// output for Heatmap as the pre-refactor inlined closure. Pins a few
/// characteristic bytes from the gradient so that silent normalization drift
/// would be caught.
#[test]
fn test_heatmap_colorbar_regression() {
    let data = vec![vec![0.0, 50.0, 100.0], vec![25.0, 75.0, 50.0]];
    let heatmap = Heatmap::new()
        .with_data(data)
        .with_color_map(ColorMap::Viridis);
    let plots = vec![Plot::Heatmap(heatmap)];
    let layout = Layout::auto_from_plots(&plots).with_title("Colorbar regression");
    let svg = SvgBackend.render_scene(&render_multiple(plots, layout));

    // The colorbar tick axis should show the full data range — min 0, max 100.
    assert!(
        svg.contains(">0<") || svg.contains(">0.0"),
        "colorbar min tick (0) should appear"
    );
    assert!(
        svg.contains(">100<") || svg.contains(">100.0"),
        "colorbar max tick (100) should appear"
    );
    // Viridis endpoints — first stop should be dark-purple-ish, last should be
    // yellow-ish. The SVG gradient inlines stops as hex colors.
    // Viridis(0.0) = #440154, Viridis(1.0) = #fde725 (colorous crate).
    assert!(
        svg.contains("#440154") || svg.contains("#450154"),
        "Viridis gradient should start near #440154"
    );
    assert!(
        svg.contains("#fde725") || svg.contains("#fdea10"),
        "Viridis gradient should end near #fde725"
    );
}

#[test]
fn test_heatmap_x_range() {
    let data = vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]];
    let hm = Heatmap::new().with_data(data).with_x_range(-10.0, 10.0);
    let plots = vec![Plot::Heatmap(hm)];
    let layout = Layout::auto_from_plots(&plots).with_x_label("X");
    let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
    assert!(svg.contains("<svg"));
    // x-axis should reflect the custom range
    assert!(svg.contains("-10") || svg.contains("10"));
}

#[test]
fn test_heatmap_y_range() {
    let data = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
    let hm = Heatmap::new().with_data(data).with_y_range(-4.0, 4.0);
    let plots = vec![Plot::Heatmap(hm)];
    let layout = Layout::auto_from_plots(&plots).with_y_label("Y");
    let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
    assert!(svg.contains("<svg"));
    assert!(svg.contains("-4") || svg.contains("4"));
}

#[test]
fn test_heatmap_xy_range() {
    let data = vec![
        vec![10.0, 20.0, 30.0, 40.0],
        vec![50.0, 60.0, 70.0, 80.0],
        vec![90.0, 80.0, 70.0, 60.0],
        vec![50.0, 40.0, 30.0, 20.0],
    ];
    let hm = Heatmap::new()
        .with_data(data)
        .with_x_range(-10.0, 10.0)
        .with_y_range(-4.0, 4.0);
    let plots = vec![Plot::Heatmap(hm)];
    let layout = Layout::auto_from_plots(&plots)
        .with_title("Scalar Field")
        .with_x_label("X (m)")
        .with_y_label("Y (m)");
    let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
    std::fs::create_dir_all("test_outputs").unwrap();
    common::write_test_output("test_outputs/heatmap_xy_range.svg", &svg).unwrap();
    assert!(svg.contains("<svg"));
}

#[test]
fn test_heatmap_default_range_unchanged() {
    // Verify default behaviour is identical to before: bounds are 0.5..cols+0.5
    let data = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
    let hm = Heatmap::new().with_data(data);
    let plots = vec![Plot::Heatmap(hm)];
    let b = plots[0].bounds().unwrap();
    assert_eq!(b, ((0.5, 2.5), (0.5, 2.5)));
}

#[test]
fn test_heatmap_custom_range_bounds() {
    let data = vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]];
    let hm = Heatmap::new()
        .with_data(data)
        .with_x_range(-10.0, 10.0)
        .with_y_range(-4.0, 4.0);
    let plots = vec![Plot::Heatmap(hm)];
    let b = plots[0].bounds().unwrap();
    assert_eq!(b, ((-10.0, 10.0), (-4.0, 4.0)));
}

#[test]
fn test_heatmap_cell_size_default() {
    let hm = Heatmap::new().with_data(vec![vec![1.0, 2.0], vec![3.0, 4.0]]);
    assert!(
        (hm.cell_size - 0.99).abs() < 1e-9,
        "default cell_size should be 0.99"
    );
}

/// Parse all (x, width) pairs from SVG `<rect>` elements.
fn parse_rect_xw(svg: &str) -> Vec<(f64, f64)> {
    let mut out = Vec::new();
    for chunk in svg.split("<rect ") {
        let x = chunk
            .split("x=\"")
            .nth(1)
            .and_then(|s| s.split('"').next())
            .and_then(|s| s.parse::<f64>().ok());
        let w = chunk
            .split("width=\"")
            .nth(1)
            .and_then(|s| s.split('"').next())
            .and_then(|s| s.parse::<f64>().ok());
        if let (Some(x), Some(w)) = (x, w) {
            out.push((x, w));
        }
    }
    out
}

/// Find the first run of exactly `n` consecutive rects that all have the same width
/// (within 1px). This isolates the heatmap cell batch from the colorbar (50 slices)
/// and background/legend rects (different widths).
fn extract_cell_rects_n(rects: &[(f64, f64)], n: usize) -> Vec<(f64, f64)> {
    let mut i = 0;
    while i + n <= rects.len() {
        let w0 = rects[i].1;
        let run: Vec<_> = rects[i..]
            .iter()
            .take_while(|&&(_, w)| (w - w0).abs() < 1.0)
            .copied()
            .collect();
        if run.len() == n {
            return run;
        }
        i += run.len().max(1);
    }
    vec![]
}

#[test]
fn test_heatmap_cell_size_gap_default() {
    // Default cell_size=0.99: each rendered rect width should be < the natural step width.
    // Use a 1-row × 4-col grid on a fixed-width canvas to make the step predictable.
    let data = vec![vec![1.0, 2.0, 3.0, 4.0]];
    let hm = Heatmap::new().with_data(data);
    let plots = vec![Plot::Heatmap(hm)];
    let layout = Layout::auto_from_plots(&plots).with_width(500.0);
    let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
    std::fs::create_dir_all("test_outputs").unwrap();
    common::write_test_output("test_outputs/heatmap_cell_size_gap.svg", &svg).unwrap();

    let all_rects = parse_rect_xw(&svg);
    let cells = extract_cell_rects_n(&all_rects, 4);
    assert_eq!(cells.len(), 4, "expected 4 cell rects");

    // Adjacent cells must have a gap: right edge of cell[j] < left edge of cell[j+1].
    let mut sorted = cells.clone();
    sorted.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
    for w in sorted.windows(2) {
        let right = w[0].0 + w[0].1;
        let next_left = w[1].0;
        assert!(
            right < next_left + 1e-3,
            "default cell_size=0.99: right edge {right:.3} should be < next left {next_left:.3}"
        );
    }
}

#[test]
fn test_heatmap_cell_size_flush() {
    // cell_size=1.0: cells must overlap (right edge of cell[j] > left edge of cell[j+1])
    // so SVG anti-aliasing hairlines are covered.
    let data = vec![vec![1.0, 2.0, 3.0, 4.0]];
    let hm = Heatmap::new().with_data(data).with_cell_size(1.0);
    assert!((hm.cell_size - 1.0).abs() < 1e-9);
    let plots = vec![Plot::Heatmap(hm)];
    let layout = Layout::auto_from_plots(&plots).with_width(500.0);
    let svg = SvgBackend.render_scene(&render_multiple(plots, layout));
    common::write_test_output("test_outputs/heatmap_flush.svg", &svg).unwrap();

    let all_rects = parse_rect_xw(&svg);
    let cells = extract_cell_rects_n(&all_rects, 4);
    assert_eq!(cells.len(), 4, "expected 4 cell rects");

    // Adjacent cells must overlap: right edge of cell[j] > left edge of cell[j+1].
    let mut sorted = cells.clone();
    sorted.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
    for w in sorted.windows(2) {
        let right = w[0].0 + w[0].1;
        let next_left = w[1].0;
        assert!(
            right > next_left,
            "cell_size=1.0: right edge {right:.3} should overlap next left {next_left:.3}"
        );
    }
}

#[test]
fn test_heatmap_cell_size_clamp() {
    let hm = Heatmap::new()
        .with_data(vec![vec![1.0]])
        .with_cell_size(2.0);
    assert!(
        (hm.cell_size - 1.0).abs() < 1e-9,
        "cell_size should be clamped to 1.0"
    );
    let hm2 = Heatmap::new()
        .with_data(vec![vec![1.0]])
        .with_cell_size(0.1);
    assert!(
        (hm2.cell_size - 0.5).abs() < 1e-9,
        "cell_size should be clamped to 0.5"
    );
}