Skip to main content

facett_plotters/
violin.rs

1//! **Violin plot** — a mirrored kernel-density silhouette per group. The KDE
2//! ([`kde`]) is a pure function (Gaussian kernel, Silverman bandwidth) sampled
3//! on an evenly-spaced grid; [`ViolinView`] paints the mirrored density as a
4//! filled polygon and overlays the median line.
5
6use egui::{Align2, FontId, Pos2, Sense, Stroke, Ui, vec2};
7use facett_core::{Facet, FacetCaps, theme};
8
9use crate::boxplot::{Quartiles, paint_tooltip};
10
11/// One density curve: `grid[i]` (the value axis) carries density `dens[i]`.
12#[derive(Clone, Debug, PartialEq)]
13pub struct Density {
14    pub grid: Vec<f64>,
15    pub dens: Vec<f64>,
16    /// Peak density (for normalising the silhouette width).
17    pub peak: f64,
18}
19
20/// Silverman's rule-of-thumb bandwidth: `1.06 · σ · n^(−1/5)`. Falls back to a
21/// small positive width on a degenerate (zero-variance) sample so the kernel
22/// stays well-defined.
23pub fn silverman_bandwidth(values: &[f64]) -> f64 {
24    let n = values.len();
25    if n < 2 {
26        return 1.0;
27    }
28    let mean = values.iter().sum::<f64>() / n as f64;
29    let var = values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (n as f64 - 1.0);
30    let sd = var.sqrt();
31    let bw = 1.06 * sd * (n as f64).powf(-0.2);
32    if bw > 0.0 { bw } else { 1.0 }
33}
34
35/// Gaussian-kernel density estimate of `values`, evaluated on `bins` evenly
36/// spaced points spanning `[min, max]` padded by one bandwidth. `None` for an
37/// empty sample.
38pub fn kde(values: &[f64], bins: usize) -> Option<Density> {
39    let v: Vec<f64> = values.iter().copied().filter(|x| x.is_finite()).collect();
40    if v.is_empty() {
41        return None;
42    }
43    let bins = bins.max(2);
44    let bw = silverman_bandwidth(&v);
45    let (mut lo, mut hi) = (v[0], v[0]);
46    for &x in &v {
47        lo = lo.min(x);
48        hi = hi.max(x);
49    }
50    // Pad so the tails of the kernel are visible.
51    lo -= bw;
52    hi += bw;
53    let span = (hi - lo).max(1e-9);
54    let inv = 1.0 / (bw * (std::f64::consts::TAU).sqrt());
55    let n = v.len() as f64;
56    let mut grid = Vec::with_capacity(bins);
57    let mut dens = Vec::with_capacity(bins);
58    let mut peak = 0.0_f64;
59    for i in 0..bins {
60        let x = lo + span * (i as f64) / (bins as f64 - 1.0);
61        let mut d = 0.0;
62        for &xi in &v {
63            let u = (x - xi) / bw;
64            d += (-0.5 * u * u).exp();
65        }
66        d *= inv / n;
67        peak = peak.max(d);
68        grid.push(x);
69        dens.push(d);
70    }
71    Some(Density { grid, dens, peak })
72}
73
74/// A violin plot: named groups of raw samples.
75pub struct ViolinView {
76    pub title: String,
77    pub groups: Vec<(String, Vec<f64>)>,
78    /// KDE resolution (grid points per group).
79    pub bins: usize,
80}
81
82impl ViolinView {
83    pub fn new(title: impl Into<String>, groups: Vec<(String, Vec<f64>)>) -> Self {
84        Self { title: title.into(), groups, bins: 64 }
85    }
86
87    fn densities(&self) -> Vec<(String, Density, f64)> {
88        self.groups
89            .iter()
90            .filter_map(|(name, vals)| {
91                let d = kde(vals, self.bins)?;
92                let median = Quartiles::compute(vals)?.median;
93                Some((name.clone(), d, median))
94            })
95            .collect()
96    }
97
98    fn value_range(&self, dens: &[(String, Density, f64)]) -> Option<(f64, f64)> {
99        let mut r: Option<(f64, f64)> = None;
100        for (_, d, _) in dens {
101            for &g in &d.grid {
102                r = Some(match r {
103                    None => (g, g),
104                    Some((lo, hi)) => (lo.min(g), hi.max(g)),
105                });
106            }
107        }
108        r
109    }
110
111    fn to_csv(&self) -> String {
112        let mut s = String::from("group,n,median,peak_density\n");
113        for (name, d, median) in self.densities() {
114            let n = self.groups.iter().find(|(g, _)| g == &name).map(|(_, v)| v.len()).unwrap_or(0);
115            s.push_str(&format!("{name},{n},{median},{}\n", d.peak));
116        }
117        s
118    }
119}
120
121impl Facet for ViolinView {
122    fn title(&self) -> &str {
123        &self.title
124    }
125    fn caps(&self) -> FacetCaps {
126        FacetCaps::NONE.themeable().copyable().resizable()
127    }
128    fn copy(&mut self) -> Option<String> {
129        Some(self.to_csv())
130    }
131    fn ui(&mut self, ui: &mut Ui) {
132        let th = theme(ui);
133        let dens = self.densities();
134        let (rect, resp) = ui.allocate_exact_size(ui.available_size(), Sense::hover());
135        let painter = ui.painter_at(rect);
136        painter.rect_filled(rect, egui::CornerRadius::ZERO, th.bg);
137        let Some((vmin, vmax)) = self.value_range(&dens) else {
138            painter.text(rect.center(), Align2::CENTER_CENTER, "no data", FontId::proportional(13.0), th.text_dim);
139            return;
140        };
141        let plot = rect.shrink2(vec2(28.0, 24.0));
142        let span = (vmax - vmin).max(1e-9);
143        let y_of = |v: f64| plot.bottom() - ((v - vmin) / span) as f32 * plot.height();
144        let n = dens.len().max(1);
145        let slot = plot.width() / n as f32;
146        let half_w = (slot * 0.42).min(80.0);
147        let hover = resp.hover_pos();
148        let mut tip: Option<(Pos2, String)> = None;
149
150        for (i, (name, d, median)) in dens.iter().enumerate() {
151            let cx = plot.left() + slot * (i as f32 + 0.5);
152            let color = facett_core::hash_color(name);
153            let peak = d.peak.max(1e-12);
154            // Build the mirrored silhouette: right edge down, left edge up.
155            let mut poly: Vec<Pos2> = Vec::with_capacity(d.grid.len() * 2);
156            for k in 0..d.grid.len() {
157                let w = (d.dens[k] / peak) as f32 * half_w;
158                poly.push(Pos2::new(cx + w, y_of(d.grid[k])));
159            }
160            for k in (0..d.grid.len()).rev() {
161                let w = (d.dens[k] / peak) as f32 * half_w;
162                poly.push(Pos2::new(cx - w, y_of(d.grid[k])));
163            }
164            painter.add(egui::Shape::convex_polygon(poly.clone(), color.gamma_multiply(0.40), Stroke::new(1.2, th.accent)));
165            // Median line across the body.
166            let mw = {
167                // density at the median, for the line half-width
168                let idx = d.grid.iter().enumerate().min_by(|a, b| {
169                    (a.1 - median).abs().partial_cmp(&(b.1 - median).abs()).unwrap()
170                }).map(|(i, _)| i).unwrap_or(0);
171                (d.dens[idx] / peak) as f32 * half_w
172            };
173            painter.line_segment([Pos2::new(cx - mw, y_of(*median)), Pos2::new(cx + mw, y_of(*median))], Stroke::new(2.0, th.glow));
174            painter.text(Pos2::new(cx, plot.bottom() + 12.0), Align2::CENTER_CENTER, name, FontId::proportional(11.0), th.text);
175
176            if let Some(hp) = hover {
177                if hp.x >= cx - slot * 0.5 && hp.x < cx + slot * 0.5 && plot.y_range().contains(hp.y) {
178                    // map cursor y back to a value
179                    let v = vmin + ((plot.bottom() - hp.y) / plot.height()) as f64 * span;
180                    tip = Some((hp, format!("{name}\nvalue {v:.3}\nmedian {median:.3}\npeak {:.4}", d.peak)));
181                }
182            }
183        }
184        if let Some((at, text)) = tip {
185            paint_tooltip(&painter, &th, at, &text);
186        }
187    }
188    fn state_json(&self) -> serde_json::Value {
189        serde_json::json!({
190            "kind": "violin",
191            "bins": self.bins,
192            "groups": self.densities().iter().map(|(n, d, m)| serde_json::json!({
193                "name": n, "median": m, "peak": d.peak, "grid": d.grid.len(),
194            })).collect::<Vec<_>>(),
195        })
196    }
197}
198
199#[cfg(test)]
200mod tests {
201    use super::*;
202
203    #[test]
204    fn kde_grid_and_density_lengths_match_bins() {
205        let d = kde(&[1.0, 2.0, 2.0, 3.0, 5.0], 32).unwrap();
206        assert_eq!(d.grid.len(), 32);
207        assert_eq!(d.dens.len(), 32);
208        assert!(d.peak > 0.0);
209    }
210
211    #[test]
212    fn kde_density_is_nonnegative_and_peaks_near_mode() {
213        // bimodal-ish: cluster at 0 and at 10
214        let mut data = vec![];
215        for _ in 0..20 { data.push(0.0); }
216        for _ in 0..20 { data.push(10.0); }
217        let d = kde(&data, 100).unwrap();
218        assert!(d.dens.iter().all(|&x| x >= 0.0));
219        // the densest grid point should sit near one of the two clusters
220        let (peak_i, _) = d.dens.iter().enumerate().max_by(|a, b| a.1.partial_cmp(b.1).unwrap()).unwrap();
221        let peak_x = d.grid[peak_i];
222        assert!(peak_x.abs() < 2.0 || (peak_x - 10.0).abs() < 2.0, "peak at {peak_x} should be near 0 or 10");
223    }
224
225    #[test]
226    fn kde_integrates_to_about_one() {
227        // A proper density integrates to ~1 over a wide grid.
228        let data: Vec<f64> = (0..200).map(|i| (i as f64) * 0.05).collect();
229        let d = kde(&data, 400).unwrap();
230        let dx = d.grid[1] - d.grid[0];
231        let area: f64 = d.dens.iter().sum::<f64>() * dx;
232        assert!((area - 1.0).abs() < 0.05, "area {area} should be ~1");
233    }
234
235    #[test]
236    fn kde_empty_is_none() {
237        assert!(kde(&[], 16).is_none());
238        assert!(kde(&[f64::NAN], 16).is_none());
239    }
240
241    #[test]
242    fn silverman_positive_even_for_constant_sample() {
243        assert!(silverman_bandwidth(&[5.0, 5.0, 5.0]) > 0.0);
244        assert!(silverman_bandwidth(&[]) > 0.0);
245    }
246
247    #[test]
248    fn csv_header_and_rows() {
249        let v = ViolinView::new("t", vec![("a".into(), vec![1.0, 2.0, 3.0])]);
250        let csv = v.to_csv();
251        assert!(csv.starts_with("group,n,median,peak_density\n"));
252        assert_eq!(csv.lines().count(), 2);
253    }
254}