plotkit-core 0.5.0

Core types and logic for the plotkit plotting library
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
//! Violin plot builder methods and kernel density estimation.
//!
//! Provides a fluent builder API for configuring [`ViolinArtist`] instances,
//! along with a Gaussian kernel density estimation (KDE) function used to
//! compute the shape of each violin.

use crate::artist::ViolinArtist;
use crate::primitives::Color;

/// Computes the linear-interpolation percentile of a sorted slice.
///
/// Uses the same interpolation method as NumPy's default (`linear`).
/// `p` must be in the range [0.0, 1.0].
fn percentile(sorted: &[f64], p: f64) -> f64 {
    assert!(!sorted.is_empty(), "percentile requires non-empty data");
    if sorted.len() == 1 {
        return sorted[0];
    }
    let idx = p * (sorted.len() - 1) as f64;
    let lo = idx.floor() as usize;
    let hi = lo + 1;
    let frac = idx - lo as f64;
    if hi >= sorted.len() {
        sorted[sorted.len() - 1]
    } else {
        sorted[lo] * (1.0 - frac) + sorted[hi] * frac
    }
}

/// Computes the standard deviation of a sorted slice of finite values.
///
/// Returns 0.0 for slices with fewer than 2 elements.
fn std_dev(sorted: &[f64]) -> f64 {
    let n = sorted.len();
    if n < 2 {
        return 0.0;
    }
    let mean: f64 = sorted.iter().sum::<f64>() / n as f64;
    let variance = sorted.iter().map(|&v| (v - mean).powi(2)).sum::<f64>() / n as f64;
    variance.sqrt()
}

/// Computes the bandwidth for kernel density estimation using Silverman's rule.
///
/// The bandwidth is calculated as:
/// `h = 0.9 * min(std, IQR / 1.34) * n^(-1/5)`
///
/// Falls back to a small positive value when the data has zero spread
/// (e.g. all identical values or a single data point).
pub fn silverman_bandwidth(sorted: &[f64]) -> f64 {
    let n = sorted.len();
    if n < 2 {
        return 1.0;
    }
    let sd = std_dev(sorted);
    let q1 = percentile(sorted, 0.25);
    let q3 = percentile(sorted, 0.75);
    let iqr = q3 - q1;

    let spread = if sd > 0.0 && iqr > 0.0 {
        sd.min(iqr / 1.34)
    } else if sd > 0.0 {
        sd
    } else if iqr > 0.0 {
        iqr / 1.34
    } else {
        // All values identical -- use a small positive bandwidth.
        return 1.0;
    };

    0.9 * spread * (n as f64).powf(-0.2)
}

/// Evaluates a Gaussian kernel density estimate at a set of points.
///
/// For each point in `eval_points`, the density is estimated by summing
/// Gaussian kernels centered on each data value, scaled by `bandwidth`.
///
/// # Arguments
/// * `data` - The source data values.
/// * `bandwidth` - The smoothing bandwidth (standard deviation of each kernel).
/// * `eval_points` - The x-values at which to evaluate the density.
///
/// # Returns
/// A vector of density values, one per eval point.
pub fn gaussian_kde(data: &[f64], bandwidth: f64, eval_points: &[f64]) -> Vec<f64> {
    let n = data.len() as f64;
    let inv_bw = 1.0 / bandwidth;
    let norm = 1.0 / (n * bandwidth * (2.0 * std::f64::consts::PI).sqrt());

    eval_points
        .iter()
        .map(|&x| {
            let sum: f64 = data
                .iter()
                .map(|&xi| {
                    let u = (x - xi) * inv_bw;
                    (-0.5 * u * u).exp()
                })
                .sum();
            sum * norm
        })
        .collect()
}

impl ViolinArtist {
    /// Sets the fill color.
    ///
    /// Applies the given [`Color`] to every violin rendered by this artist.
    pub fn color(&mut self, color: Color) -> &mut Self {
        self.color = color;
        self
    }

    /// Sets the legend label.
    ///
    /// When a legend is displayed on the figure, this label will appear
    /// next to the color swatch for this violin plot.
    pub fn label(&mut self, label: &str) -> &mut Self {
        self.label = Some(label.to_string());
        self
    }

    /// Sets the opacity.
    ///
    /// The value is clamped to the range [0.0, 1.0], where 0.0 is fully
    /// transparent and 1.0 is fully opaque.
    pub fn alpha(&mut self, alpha: f64) -> &mut Self {
        self.alpha = alpha.clamp(0.0, 1.0);
        self
    }

    /// Sets the maximum width of each violin shape.
    ///
    /// The value is clamped to [0.1, 2.0]. This controls the visual width
    /// of the widest part of the violin.
    pub fn widths(&mut self, widths: f64) -> &mut Self {
        self.widths = widths.clamp(0.1, 2.0);
        self
    }

    /// Sets the x-positions for each violin.
    ///
    /// When `None` (the default), violins are placed at 1.0, 2.0, 3.0, etc.
    pub fn positions(&mut self, positions: Vec<f64>) -> &mut Self {
        self.positions = Some(positions);
        self
    }

    /// Controls whether the median line is drawn.
    ///
    /// When `true` (the default), a horizontal line is drawn at the median
    /// of each dataset inside the violin shape.
    pub fn show_median(&mut self, show: bool) -> &mut Self {
        self.show_median = show;
        self
    }

    /// Controls whether Q1/Q3 quartile lines are drawn.
    ///
    /// When `true` (the default), thin horizontal lines are drawn at the
    /// first and third quartiles inside the violin shape.
    pub fn show_quartiles(&mut self, show: bool) -> &mut Self {
        self.show_quartiles = show;
        self
    }

    /// Sets the KDE bandwidth manually.
    ///
    /// Overrides the automatically computed Silverman's rule bandwidth.
    /// A value of 0.0 or negative resets to automatic bandwidth selection.
    pub fn bw_method(&mut self, bw: f64) -> &mut Self {
        self.bw_method = bw;
        self
    }

    /// Returns the x-position for a given violin index.
    ///
    /// Uses custom positions if set, otherwise defaults to 1-based indexing.
    pub fn position_for(&self, index: usize) -> f64 {
        self.positions
            .as_ref()
            .and_then(|p| p.get(index).copied())
            .unwrap_or(index as f64 + 1.0)
    }

    /// Computes the data-space bounding box `(xmin, xmax, ymin, ymax)`.
    ///
    /// The x-bounds span from the leftmost position minus half the max width
    /// to the rightmost position plus half the max width. The y-bounds span
    /// from the minimum data value to the maximum data value across all
    /// datasets. Falls back to `(0.0, 1.0, 0.0, 1.0)` when there are no
    /// datasets.
    pub fn data_bounds(&self) -> (f64, f64, f64, f64) {
        if self.datasets.is_empty() {
            return (0.0, 1.0, 0.0, 1.0);
        }

        let half_w = self.widths / 2.0;
        let mut xmin = f64::INFINITY;
        let mut xmax = f64::NEG_INFINITY;
        let mut ymin = f64::INFINITY;
        let mut ymax = f64::NEG_INFINITY;

        for (i, dataset) in self.datasets.iter().enumerate() {
            let pos = self.position_for(i);
            xmin = xmin.min(pos - half_w);
            xmax = xmax.max(pos + half_w);
            for &v in dataset {
                if v.is_finite() {
                    ymin = ymin.min(v);
                    ymax = ymax.max(v);
                }
            }
        }

        if !xmin.is_finite() {
            xmin = 0.0;
        }
        if !xmax.is_finite() {
            xmax = 1.0;
        }
        if !ymin.is_finite() {
            ymin = 0.0;
        }
        if !ymax.is_finite() {
            ymax = 1.0;
        }

        (xmin, xmax, ymin, ymax)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    /// Helper to create a sample ViolinArtist for tests.
    fn sample_violin() -> ViolinArtist {
        ViolinArtist {
            datasets: vec![vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]],
            positions: None,
            widths: 0.7,
            show_median: true,
            show_quartiles: true,
            color: Color::TAB_BLUE,
            alpha: 0.7,
            label: None,
            bw_method: 0.0,
        }
    }

    #[test]
    fn silverman_bandwidth_basic() {
        let sorted = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let bw = silverman_bandwidth(&sorted);
        assert!(bw > 0.0, "bandwidth must be positive");
        assert!(bw < 10.0, "bandwidth should be reasonable");
    }

    #[test]
    fn silverman_bandwidth_single_value() {
        let bw = silverman_bandwidth(&[42.0]);
        assert!((bw - 1.0).abs() < f64::EPSILON, "single value should return fallback bandwidth");
    }

    #[test]
    fn silverman_bandwidth_identical_values() {
        let sorted = vec![5.0, 5.0, 5.0, 5.0, 5.0];
        let bw = silverman_bandwidth(&sorted);
        assert!((bw - 1.0).abs() < f64::EPSILON, "identical values should return fallback bandwidth");
    }

    #[test]
    fn gaussian_kde_basic() {
        let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let bw = 1.0;
        let eval_points: Vec<f64> = (0..11).map(|i| i as f64 * 0.5).collect();
        let density = gaussian_kde(&data, bw, &eval_points);
        assert_eq!(density.len(), eval_points.len());
        // All density values should be non-negative.
        for &d in &density {
            assert!(d >= 0.0, "density must be non-negative");
        }
    }

    #[test]
    fn gaussian_kde_integrates_roughly_to_one() {
        let data: Vec<f64> = (0..100).map(|i| i as f64 * 0.1).collect();
        let bw = silverman_bandwidth(&data);
        // Evaluate on a fine grid covering the data range with margin.
        let n_points = 1000;
        let lo = -2.0;
        let hi = 12.0;
        let step = (hi - lo) / n_points as f64;
        let eval_points: Vec<f64> = (0..=n_points).map(|i| lo + i as f64 * step).collect();
        let density = gaussian_kde(&data, bw, &eval_points);
        let integral: f64 = density.iter().sum::<f64>() * step;
        assert!(
            (integral - 1.0).abs() < 0.1,
            "KDE integral should be approximately 1.0, got {integral}"
        );
    }

    #[test]
    fn gaussian_kde_single_point() {
        let data = vec![5.0];
        let bw = 1.0;
        let density = gaussian_kde(&data, bw, &[5.0]);
        // Peak should be at the data point.
        assert!(density[0] > 0.0);
    }

    #[test]
    fn gaussian_kde_symmetry() {
        let data = vec![0.0];
        let bw = 1.0;
        let d_left = gaussian_kde(&data, bw, &[-1.0]);
        let d_right = gaussian_kde(&data, bw, &[1.0]);
        assert!(
            (d_left[0] - d_right[0]).abs() < 1e-10,
            "KDE should be symmetric around single data point"
        );
    }

    #[test]
    fn data_bounds_single_dataset() {
        let artist = sample_violin();
        let (xmin, xmax, ymin, ymax) = artist.data_bounds();
        // Default position for index 0 is 1.0, half width is 0.35.
        assert!((xmin - 0.65).abs() < f64::EPSILON);
        assert!((xmax - 1.35).abs() < f64::EPSILON);
        assert!((ymin - 1.0).abs() < f64::EPSILON);
        assert!((ymax - 10.0).abs() < f64::EPSILON);
    }

    #[test]
    fn data_bounds_multiple_datasets() {
        let artist = ViolinArtist {
            datasets: vec![
                vec![1.0, 2.0, 3.0],
                vec![10.0, 20.0, 30.0],
            ],
            positions: None,
            widths: 0.7,
            show_median: true,
            show_quartiles: true,
            color: Color::TAB_BLUE,
            alpha: 0.7,
            label: None,
            bw_method: 0.0,
        };
        let (xmin, xmax, ymin, ymax) = artist.data_bounds();
        // Position 0 -> 1.0, position 1 -> 2.0
        assert!((xmin - 0.65).abs() < f64::EPSILON);
        assert!((xmax - 2.35).abs() < f64::EPSILON);
        assert!((ymin - 1.0).abs() < f64::EPSILON);
        assert!((ymax - 30.0).abs() < f64::EPSILON);
    }

    #[test]
    fn data_bounds_custom_positions() {
        let artist = ViolinArtist {
            datasets: vec![vec![1.0, 2.0], vec![3.0, 4.0]],
            positions: Some(vec![5.0, 10.0]),
            widths: 1.0,
            show_median: true,
            show_quartiles: true,
            color: Color::TAB_BLUE,
            alpha: 0.7,
            label: None,
            bw_method: 0.0,
        };
        let (xmin, xmax, _ymin, _ymax) = artist.data_bounds();
        assert!((xmin - 4.5).abs() < f64::EPSILON);
        assert!((xmax - 10.5).abs() < f64::EPSILON);
    }

    #[test]
    fn data_bounds_empty() {
        let artist = ViolinArtist {
            datasets: vec![],
            positions: None,
            widths: 0.7,
            show_median: true,
            show_quartiles: true,
            color: Color::TAB_BLUE,
            alpha: 0.7,
            label: None,
            bw_method: 0.0,
        };
        assert_eq!(artist.data_bounds(), (0.0, 1.0, 0.0, 1.0));
    }

    #[test]
    fn data_bounds_nan_filtered() {
        let artist = ViolinArtist {
            datasets: vec![vec![f64::NAN, 1.0, 5.0, f64::NAN]],
            positions: None,
            widths: 0.7,
            show_median: true,
            show_quartiles: true,
            color: Color::TAB_BLUE,
            alpha: 0.7,
            label: None,
            bw_method: 0.0,
        };
        let (_xmin, _xmax, ymin, ymax) = artist.data_bounds();
        assert!((ymin - 1.0).abs() < f64::EPSILON);
        assert!((ymax - 5.0).abs() < f64::EPSILON);
    }

    #[test]
    fn builder_color() {
        let mut artist = sample_violin();
        artist.color(Color::TAB_RED);
        assert_eq!(artist.color, Color::TAB_RED);
    }

    #[test]
    fn builder_label() {
        let mut artist = sample_violin();
        artist.label("my violin");
        assert_eq!(artist.label.as_deref(), Some("my violin"));
    }

    #[test]
    fn builder_alpha() {
        let mut artist = sample_violin();
        artist.alpha(0.5);
        assert!((artist.alpha - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn builder_alpha_clamped() {
        let mut artist = sample_violin();
        artist.alpha(2.0);
        assert!((artist.alpha - 1.0).abs() < f64::EPSILON);
        artist.alpha(-1.0);
        assert!((artist.alpha - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn builder_widths() {
        let mut artist = sample_violin();
        artist.widths(0.5);
        assert!((artist.widths - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn builder_widths_clamped() {
        let mut artist = sample_violin();
        artist.widths(0.01);
        assert!((artist.widths - 0.1).abs() < f64::EPSILON);
        artist.widths(5.0);
        assert!((artist.widths - 2.0).abs() < f64::EPSILON);
    }

    #[test]
    fn builder_show_median() {
        let mut artist = sample_violin();
        artist.show_median(false);
        assert!(!artist.show_median);
    }

    #[test]
    fn builder_show_quartiles() {
        let mut artist = sample_violin();
        artist.show_quartiles(false);
        assert!(!artist.show_quartiles);
    }

    #[test]
    fn builder_bw_method() {
        let mut artist = sample_violin();
        artist.bw_method(0.5);
        assert!((artist.bw_method - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn builder_positions() {
        let mut artist = sample_violin();
        artist.positions(vec![2.0, 4.0, 6.0]);
        assert_eq!(artist.positions, Some(vec![2.0, 4.0, 6.0]));
    }

    #[test]
    fn position_for_default() {
        let artist = sample_violin();
        assert!((artist.position_for(0) - 1.0).abs() < f64::EPSILON);
        assert!((artist.position_for(2) - 3.0).abs() < f64::EPSILON);
    }

    #[test]
    fn position_for_custom() {
        let mut artist = sample_violin();
        artist.positions(vec![10.0, 20.0]);
        assert!((artist.position_for(0) - 10.0).abs() < f64::EPSILON);
        assert!((artist.position_for(1) - 20.0).abs() < f64::EPSILON);
        // Beyond custom positions, falls back to default.
        assert!((artist.position_for(2) - 3.0).abs() < f64::EPSILON);
    }

    #[test]
    fn percentile_basic() {
        let data = [1.0, 2.0, 3.0, 4.0];
        assert!((percentile(&data, 0.5) - 2.5).abs() < 1e-10);
    }

    #[test]
    fn std_dev_basic() {
        let data = vec![2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0];
        let sd = std_dev(&data);
        assert!((sd - 2.0).abs() < 1e-10);
    }

    #[test]
    fn std_dev_single() {
        assert!((std_dev(&[5.0]) - 0.0).abs() < f64::EPSILON);
    }
}