kuva 0.1.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
use std::sync::Arc;

use crate::plot::scatter::{ScatterPlot, TrendLine};
use crate::plot::line::LinePlot;
use crate::plot::bar::BarPlot;
use crate::plot::histogram::Histogram;
use crate::plot::boxplot::BoxPlot;
use crate::plot::violin::ViolinPlot;
use crate::plot::brick::BrickPlot;

use crate::plot::{Heatmap, Histogram2D, PiePlot, SeriesPlot};
use crate::plot::band::BandPlot;
use crate::plot::waterfall::{WaterfallPlot, WaterfallKind};
use crate::plot::strip::StripPlot;
use crate::plot::volcano::VolcanoPlot;
use crate::plot::manhattan::ManhattanPlot;
use crate::plot::dotplot::DotPlot;
use crate::plot::upset::UpSetPlot;
use crate::plot::stacked_area::StackedAreaPlot;
use crate::plot::candlestick::CandlestickPlot;
use crate::plot::contour::ContourPlot;
use crate::plot::chord::ChordPlot;
use crate::plot::sankey::SankeyPlot;
use crate::plot::phylo::PhyloTree;
use crate::plot::synteny::SyntenyPlot;
use crate::plot::legend::ColorBarInfo;
use crate::render::render_utils;


pub enum Plot {
    Scatter(ScatterPlot),
    Line(LinePlot),
    Bar(BarPlot),
    Histogram(Histogram),
    Histogram2d(Histogram2D),
    Box(BoxPlot),
    Violin(ViolinPlot),
    Series(SeriesPlot),
    Pie(PiePlot),
    Heatmap(Heatmap),
    Brick(BrickPlot),
    Band(BandPlot),
    Waterfall(WaterfallPlot),
    Strip(StripPlot),
    Volcano(VolcanoPlot),
    Manhattan(ManhattanPlot),
    DotPlot(DotPlot),
    UpSet(UpSetPlot),
    StackedArea(StackedAreaPlot),
    Candlestick(CandlestickPlot),
    Contour(ContourPlot),
    Chord(ChordPlot),
    Sankey(SankeyPlot),
    PhyloTree(PhyloTree),
    Synteny(SyntenyPlot),
}

fn bounds_from_2d<I>(points: I) -> Option<((f64, f64), (f64, f64))> 
    where
        I: IntoIterator,
        I::Item: Into<(f64, f64)>,
    {

    // extract values
    let mut vals = Vec::new();

    for (x, y) in points.into_iter().map(Into::into) {
        vals.push((x, y));
    }

    if vals.is_empty() {
        return None;
    }
    let (mut x_min, mut x_max) = (vals[0].0, vals[0].0);
    let (mut y_min, mut y_max) = (vals[0].1, vals[0].1);
    for (x, y) in vals.into_iter() {
        x_min = x_min.min(x);
        x_max = x_max.max(x);
        y_min = y_min.min(y);
        y_max = y_max.max(y);
    }
    Some(((x_min, x_max), (y_min, y_max)))
}

fn _bounds_from_1d(points: &[f64]) -> Option<((f64, f64), (f64, f64))> {
    if points.is_empty() {
        return None;
    }
    let (mut min_val, mut max_val) = (points[0], points[0]);
    for i in points {
        min_val = min_val.min(*i);
        max_val = max_val.max(*i);
       
    }
    
    Some(((0.0f64, points.len() as f64), (min_val, max_val)))
}



impl Plot {
    /// Set the primary color for single-color plot types.
    /// Multi-element plots (Bar, Pie, Brick) and grid plots (Heatmap, Histogram2d) are skipped.
    pub fn set_color(&mut self, color: &str) {
        match self {
            Plot::Scatter(s) => s.color = color.into(),
            Plot::Line(l) => l.color = color.into(),
            Plot::Series(s) => s.color = color.into(),
            Plot::Histogram(h) => h.color = color.into(),
            Plot::Box(b) => b.color = color.into(),
            Plot::Violin(v) => v.color = color.into(),
            Plot::Band(b) => b.color = color.into(),
            Plot::Strip(s) => s.color = color.into(),
            _ => {}
        }
    }

    pub fn bounds(&self) -> Option<((f64, f64), (f64, f64))> {
        match self {
            
            Plot::Scatter(s) => {
                let ((mut x_min, mut x_max), (mut y_min, mut y_max)) = bounds_from_2d(&s.data).expect("ScatterPlot bounds requires non-empty data");

                // Expand with error bars
                for point in &s.data {
                    let x_lo = point.x - point.x_err.map_or(0.0, |e| e.0);
                    let x_hi = point.x + point.x_err.map_or(0.0, |e| e.1);
                    let y_lo = point.y - point.y_err.map_or(0.0, |e| e.0);
                    let y_hi = point.y + point.y_err.map_or(0.0, |e| e.1);

                    x_min = x_min.min(x_lo);
                    x_max = x_max.max(x_hi);
                    y_min = y_min.min(y_lo);
                    y_max = y_max.max(y_hi);
                }

                // Expand for band
                if let Some(ref band) = s.band {
                    for &y in &band.y_lower { y_min = y_min.min(y); }
                    for &y in &band.y_upper { y_max = y_max.max(y); }
                }

                // Expand for trend line
                if let Some(trend) = s.trend {
                    let TrendLine::Linear = trend;
                        if let Some((slope, intercept, _)) = render_utils::linear_regression(&s.data) {
                            let y_start = slope * x_min + intercept;
                            let y_end = slope * x_max + intercept;

                            y_min = y_min.min(y_start).min(y_end);
                            y_max = y_max.max(y_start).max(y_end);
                        }
                }

                Some(((x_min, x_max), (y_min, y_max)))
            },
            Plot::Line(p) => {
                let ((x_min, x_max), (mut y_min, mut y_max)) = bounds_from_2d(&p.data)?;
                if let Some(ref band) = p.band {
                    for &y in &band.y_lower { y_min = y_min.min(y); }
                    for &y in &band.y_upper { y_max = y_max.max(y); }
                }
                Some(((x_min, x_max), (y_min, y_max)))
            }
            // Plot::Series(s) => bounds_from_1d(&s.values),
            Plot::Series(sp) => {
                if sp.values.is_empty() {
                    None
                } else {
                    let x_min = 0.0;
                    let x_max = sp.values.len() as f64 - 1.0;
            
                    let mut y_min = f64::INFINITY;
                    let mut y_max = f64::NEG_INFINITY;
            
                    for &v in &sp.values {
                        y_min = y_min.min(v);
                        y_max = y_max.max(v);
                    }
            
                    Some(((x_min, x_max), (y_min, y_max)))
                }
            }
            Plot::Bar(bp) => {

                if bp.groups.is_empty() {
                    None
                }
                else {
                    let x_min = 0.5;
                    let x_max = bp.groups.len() as f64 + 0.5;
                    let y_min = 0.0;

                    let mut y_max = f64::NEG_INFINITY;
                    if bp.stacked {
                        for group in &bp.groups {
                            let sum: f64 = group.bars.iter().map(|b| b.value).sum();
                            y_max = y_max.max(sum);
                        }
                    } else {
                        for group in &bp.groups {
                            for bar in &group.bars {
                                y_max = y_max.max(bar.value);
                            }
                        }
                    }

                    Some(((x_min, x_max), (y_min, y_max)))
                }
            }
            Plot::Histogram(h) => {
                let range = h.range?;
                let bins = h.bins;
                let bin_width = (range.1 - range.0) / bins as f64;

                let mut counts = vec![0usize; bins];
                for &value in &h.data {
                    if value < range.0 || value > range.1 {
                        continue;
                    }
                    let bin = ((value - range.0) / bin_width).floor() as usize;
                    let bin = if bin == bins { bin - 1 } else { bin };
                    counts[bin] += 1;
                }

                let max_y = if h.normalize {
                    1.0
                } else {
                    *counts.iter().max().unwrap_or(&1) as f64
                };

                Some((range, (0.0, max_y)))
            }
            Plot::Box(bp) => {
                if bp.groups.is_empty() {
                    None
                }
                else {
                    let x_min = 0.5;
                    let x_max = bp.groups.len() as f64 + 0.5;
            
                    let mut y_min = f64::INFINITY;
                    let mut y_max = f64::NEG_INFINITY;
                    for g in &bp.groups {
                        if g.values.is_empty() { continue; }
                        let mut vals = g.values.clone();
                        vals.sort_by(|a, b| a.total_cmp(b));
                        let q1 = render_utils::percentile(&vals, 25.0);
                        let q3 = render_utils::percentile(&vals, 75.0);
                        let iqr = q3 - q1;
                        let lo = q1 - 1.5 * iqr;
                        let hi = q3 + 1.5 * iqr;
                        y_min = y_min.min(lo);
                        y_max = y_max.max(hi);
                    }
            
                    Some(((x_min, x_max), (y_min, y_max)))
                }
            }
            Plot::Violin(vp) => {
                if vp.groups.is_empty() {
                    None
                } else {
                    let x_min = 0.5;
                    let x_max = vp.groups.len() as f64 + 0.5;

                    let mut y_min = f64::INFINITY;
                    let mut y_max = f64::NEG_INFINITY;

                    for group in &vp.groups {
                        if group.values.is_empty() { continue; }
                        let g_min = group.values.iter().cloned().fold(f64::INFINITY, f64::min);
                        let g_max = group.values.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
                        let h = vp.bandwidth.unwrap_or_else(|| render_utils::silverman_bandwidth(&group.values));
                        y_min = y_min.min(g_min - 3.0 * h);
                        y_max = y_max.max(g_max + 3.0 * h);
                    }

                    Some(((x_min, x_max), (y_min, y_max)))
                }
            }
            Plot::Pie(_) => {
                // Centered at (0.0, 0.0) and rendered to fit the layout box
                Some(((-1.0, 1.0), (-1.0, 1.0))) 
            }
            Plot::Heatmap(hm) => {
                let rows = hm.data.len();
                let cols = hm.data.first().map_or(0, |row| row.len());
                // Use 0.5-based bounds so integer positions (1, 2, ...) land on
                // cell centres, matching DotPlot convention and making the axis
                // label code (y_val = i + 1.0) align with cell midpoints.
                Some(((0.5, cols as f64 + 0.5), (0.5, rows as f64 + 0.5)))
            }
            Plot::Histogram2d(h2d) => {
                // Return the physical axis range so the layout is calibrated in
                // data coordinates, matching the physical coords used by the renderer.
                Some(((h2d.x_range.0, h2d.x_range.1), (h2d.y_range.0, h2d.y_range.1)))
            }
            Plot::Band(b) => {
                if b.x.is_empty() { return None; }
                let x_min = b.x.iter().cloned().fold(f64::INFINITY, f64::min);
                let x_max = b.x.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
                let y_min = b.y_lower.iter().cloned().fold(f64::INFINITY, f64::min);
                let y_max = b.y_upper.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
                Some(((x_min, x_max), (y_min, y_max)))
            }
            Plot::Waterfall(wp) => {
                if wp.bars.is_empty() { return None; }
                let x_min = 0.5;
                let x_max = wp.bars.len() as f64 + 0.5;
                let mut running = 0.0_f64;
                let mut y_min = 0.0_f64;
                let mut y_max = 0.0_f64;
                for bar in &wp.bars {
                    match bar.kind {
                        WaterfallKind::Delta => {
                            let base = running;
                            running += bar.value;
                            y_min = y_min.min(base).min(running);
                            y_max = y_max.max(base).max(running);
                        }
                        WaterfallKind::Total => {
                            y_min = y_min.min(0.0).min(running);
                            y_max = y_max.max(0.0).max(running);
                        }
                        WaterfallKind::Difference { from, to } => {
                            y_min = y_min.min(from).min(to);
                            y_max = y_max.max(from).max(to);
                        }
                    }
                }
                Some(((x_min, x_max), (y_min, y_max)))
            }
            Plot::Strip(sp) => {
                if sp.groups.is_empty() { return None; }
                let x_min = 0.5;
                let x_max = sp.groups.len() as f64 + 0.5;
                let mut y_min = f64::INFINITY;
                let mut y_max = f64::NEG_INFINITY;
                for g in &sp.groups {
                    for &v in &g.values {
                        y_min = y_min.min(v);
                        y_max = y_max.max(v);
                    }
                }
                if y_min == f64::INFINITY { return None; }
                Some(((x_min, x_max), (y_min, y_max)))
            }
            Plot::Volcano(vp) => {
                if vp.points.is_empty() { return None; }
                let floor = vp.floor();
                let mut x_min = f64::INFINITY;
                let mut x_max = f64::NEG_INFINITY;
                let mut y_max = f64::NEG_INFINITY;
                for p in &vp.points {
                    x_min = x_min.min(p.log2fc);
                    x_max = x_max.max(p.log2fc);
                    let y = -(p.pvalue.max(floor)).log10();
                    y_max = y_max.max(y);
                }
                Some(((x_min, x_max), (0.0, y_max)))
            }
            Plot::Manhattan(mp) => {
                if mp.points.is_empty() { return None; }
                let floor = mp.floor();
                let x_min = mp.spans.iter().map(|s| s.x_start).fold(f64::INFINITY, f64::min);
                let x_max = mp.spans.iter().map(|s| s.x_end).fold(f64::NEG_INFINITY, f64::max);
                if !x_min.is_finite() { return None; }
                // Ensure genome-wide threshold is always visible
                let y_max = mp.points.iter()
                    .map(|p| -(p.pvalue.max(floor)).log10())
                    .fold(mp.genome_wide, f64::max);
                Some(((x_min, x_max), (0.0, y_max)))
            }
            Plot::DotPlot(dp) => {
                if dp.x_categories.is_empty() { return None; }
                Some(((0.5, dp.x_categories.len() as f64 + 0.5),
                      (0.5, dp.y_categories.len() as f64 + 0.5)))
            }
            Plot::UpSet(_) => {
                // Dummy bounds — UpSet renders in pixel space and ignores map_x/map_y.
                Some(((0.0, 1.0), (0.0, 1.0)))
            }
            Plot::StackedArea(sa) => {
                if sa.x.is_empty() || sa.series.is_empty() { return None; }
                let x_min = sa.x.iter().cloned().fold(f64::INFINITY, f64::min);
                let x_max = sa.x.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
                let n = sa.x.len();
                let y_max = if sa.normalized {
                    100.0
                } else {
                    (0..n)
                        .map(|i| sa.series.iter().map(|s| s.get(i).copied().unwrap_or(0.0)).sum::<f64>())
                        .fold(0.0_f64, f64::max)
                };
                Some(((x_min, x_max), (0.0, y_max)))
            }
            Plot::Candlestick(cp) => {
                if cp.candles.is_empty() { return None; }
                let continuous = cp.candles.iter().any(|c| c.x.is_some());
                let (x_min, x_max) = if continuous {
                    (
                        cp.candles.iter().filter_map(|c| c.x).fold(f64::INFINITY, f64::min),
                        cp.candles.iter().filter_map(|c| c.x).fold(f64::NEG_INFINITY, f64::max),
                    )
                } else {
                    (0.5, cp.candles.len() as f64 + 0.5)
                };
                let y_min = cp.candles.iter().map(|c| c.low).fold(f64::INFINITY, f64::min);
                let y_max = cp.candles.iter().map(|c| c.high).fold(f64::NEG_INFINITY, f64::max);
                Some(((x_min, x_max), (y_min, y_max)))
            }
            Plot::Contour(cp) => {
                if cp.z.is_empty() { return None; }
                let x_min = cp.x_coords.iter().cloned().fold(f64::INFINITY, f64::min);
                let x_max = cp.x_coords.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
                let y_min = cp.y_coords.iter().cloned().fold(f64::INFINITY, f64::min);
                let y_max = cp.y_coords.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
                Some(((x_min, x_max), (y_min, y_max)))
            }
            Plot::Chord(_) => {
                // Rendered in pixel space; dummy bounds satisfy Layout::auto_from_plots.
                Some(((0.0, 1.0), (0.0, 1.0)))
            }
            Plot::Sankey(_) => {
                // Rendered in pixel space; dummy bounds satisfy Layout::auto_from_plots.
                Some(((0.0, 1.0), (0.0, 1.0)))
            }
            Plot::PhyloTree(_) => {
                // Rendered in pixel space; dummy bounds satisfy Layout::auto_from_plots.
                Some(((0.0, 1.0), (0.0, 1.0)))
            }
            Plot::Synteny(_) => {
                // Rendered in pixel space; dummy bounds satisfy Layout::auto_from_plots.
                Some(((0.0, 1.0), (0.0, 1.0)))
            }
            Plot::Brick(bp) => {
                let rows = if let Some(ref exp) = bp.strigar_exp {
                    exp.len()
                } else {
                    bp.sequences.len()
                };

                let max_width = if let Some(ref exp) = bp.strigar_exp {
                    if let Some(ref ml) = bp.motif_lengths {
                        // Variable-width: sum motif lengths per row
                        exp.iter().map(|s| {
                            s.chars().map(|c| *ml.get(&c).unwrap_or(&1) as f64).sum::<f64>()
                        }).fold(0.0f64, f64::max)
                    } else {
                        exp.iter().map(|s| s.len()).max().unwrap_or(0) as f64
                    }
                } else {
                    bp.sequences.iter().map(|s| s.len()).max().unwrap_or(0) as f64
                };

                // Strigar mode: always start at 0 (comparing repeat lengths directly).
                // DNA mode with per-row offsets: find the true x extent across all rows.
                let (x_min, x_max) = if bp.strigar_exp.is_some() {
                    (0.0, max_width)
                } else if let Some(ref offsets) = bp.x_offsets {
                    let seqs = &bp.sequences;
                    let mut lo = f64::INFINITY;
                    let mut hi = f64::NEG_INFINITY;
                    for (i, seq) in seqs.iter().enumerate() {
                        let off = offsets.get(i).copied().flatten().unwrap_or(bp.x_offset);
                        lo = lo.min(0.0 - off);
                        hi = hi.max(seq.len() as f64 - off);
                    }
                    (lo, hi)
                } else {
                    (0.0 - bp.x_offset, max_width - bp.x_offset)
                };
                Some(((x_min, x_max), (0.0, rows as f64)))
            }
        }
    }

    pub fn colorbar_info(&self) -> Option<ColorBarInfo> {
        match self {
            Plot::Heatmap(hm) => {
                let flat: Vec<f64> = hm.data.iter().flatten().cloned().collect();
                let min = flat.iter().cloned().fold(f64::INFINITY, f64::min);
                let max = flat.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
                let cmap = hm.color_map.clone();
                Some(ColorBarInfo {
                    map_fn: Arc::new(move |t| {
                        let norm = (t - min) / (max - min + f64::EPSILON);
                        cmap.map(norm.clamp(0.0, 1.0))
                    }),
                    min_value: min,
                    max_value: max,
                    label: None,
                })
            }
            Plot::Histogram2d(h2d) => {
                let max_count = h2d.bins.iter().flatten().copied().max().unwrap_or(1) as f64;
                let cmap = h2d.color_map.clone();
                Some(ColorBarInfo {
                    map_fn: Arc::new(move |t| {
                        let norm = t / max_count;
                        cmap.map(norm.clamp(0.0, 1.0))
                    }),
                    min_value: 0.0,
                    max_value: max_count,
                    label: Some("Count".to_string()),
                })
            }
            Plot::DotPlot(dp) => {
                let label = dp.color_legend_label.clone()?;
                let (min, max) = dp.color_range.unwrap_or_else(|| dp.color_extent());
                let cmap = dp.color_map.clone();
                Some(ColorBarInfo {
                    map_fn: Arc::new(move |t| {
                        let norm = (t - min) / (max - min + f64::EPSILON);
                        cmap.map(norm.clamp(0.0, 1.0))
                    }),
                    min_value: min,
                    max_value: max,
                    label: Some(label),
                })
            }
            Plot::Contour(cp) => {
                if !cp.filled { return None; }
                let (z_min, z_max) = cp.z_range();
                if !z_min.is_finite() || !z_max.is_finite() { return None; }
                let cmap = cp.color_map.clone();
                let label = cp.legend_label.clone();
                Some(ColorBarInfo {
                    map_fn: Arc::new(move |t| {
                        let norm = (t - z_min) / (z_max - z_min + f64::EPSILON);
                        cmap.map(norm.clamp(0.0, 1.0))
                    }),
                    min_value: z_min,
                    max_value: z_max,
                    label,
                })
            }
            _ => None,
        }
    }
}