Skip to main content

shap_rs/plot/
mod.rs

1pub mod bar;
2pub mod beeswarm;
3pub mod decision;
4pub mod force;
5pub mod heatmap;
6pub mod html;
7pub mod interaction;
8pub mod scatter;
9pub mod svg;
10pub mod waterfall;
11
12#[cfg(test)]
13mod tests {
14    use crate::{Explanation, ShapError};
15    use ndarray::{array, Array3};
16
17    fn explanation() -> Explanation {
18        Explanation::new(
19            Array3::from_shape_vec((1, 2, 1), vec![1., -2.]).unwrap(),
20            array![[3.]],
21            array![[10., 20.]],
22        )
23        .unwrap()
24    }
25
26    #[test]
27    fn indexed_plots_report_precise_bounds_errors() {
28        let e = explanation();
29        assert!(matches!(
30            super::beeswarm::data(&e, 1),
31            Err(ShapError::InvalidOutputIndex { index: 1, .. })
32        ));
33        assert!(matches!(
34            super::waterfall::data(&e, 2, 0),
35            Err(ShapError::InvalidSampleIndex { index: 2, .. })
36        ));
37        assert!(matches!(
38            super::scatter::data(&e, 0, 0, Some(3)),
39            Err(ShapError::InvalidFeatureIndex { index: 3, .. })
40        ));
41        assert!(matches!(
42            super::heatmap::data(&e, 4),
43            Err(ShapError::InvalidOutputIndex { index: 4, .. })
44        ));
45    }
46
47    #[test]
48    fn plot_data_preserves_reconstruction_and_global_order() {
49        let e = explanation();
50        let force = super::force::data(&e, 0, 0).unwrap();
51        assert_eq!(force.output_value, 2.);
52        let waterfall = super::waterfall::data(&e, 0, 0).unwrap();
53        assert_eq!(waterfall[0].feature, 1);
54        let heatmap = super::heatmap::data(&e, 0).unwrap();
55        assert_eq!(heatmap.feature_order, vec![1, 0]);
56        let decision = super::decision::data(&e, 0).unwrap();
57        assert_eq!(decision[0].cumulative_values.last(), Some(&2.));
58        assert_eq!(super::beeswarm::data(&e, 0).unwrap().len(), 2);
59    }
60}