Skip to main content

lichtung_plot/
input.rs

1//! Closed semantic inputs for supported plot families.
2
3use crate::{
4    BarInput, Bounds, CategoryValues, CoefficientInput, DumbbellEndpoints, HistogramGeometry,
5    NumericValues, RaincloudGeometry,
6};
7
8/// Closed owned source for a histogram.
9#[derive(Debug, Clone)]
10pub struct HistogramInput {
11    source: HistogramSource,
12}
13
14#[derive(Debug, Clone)]
15enum HistogramSource {
16    Samples(NumericValues),
17    Geometry(std::sync::Arc<HistogramGeometry>),
18}
19
20impl HistogramInput {
21    /// Compute histogram geometry from one numeric sample.
22    #[must_use]
23    pub fn samples(values: NumericValues) -> Self {
24        Self {
25            source: HistogramSource::Samples(values),
26        }
27    }
28
29    /// Render complete caller-computed density-axis geometry.
30    #[must_use]
31    pub fn geometry(input: HistogramGeometry) -> Self {
32        Self {
33            source: HistogramSource::Geometry(std::sync::Arc::new(input)),
34        }
35    }
36
37    pub(crate) const fn samples_input(&self) -> Option<&NumericValues> {
38        match &self.source {
39            HistogramSource::Samples(values) => Some(values),
40            HistogramSource::Geometry(_) => None,
41        }
42    }
43
44    pub(crate) fn geometry_input(&self) -> Option<&HistogramGeometry> {
45        match &self.source {
46            HistogramSource::Samples(_) => None,
47            HistogramSource::Geometry(input) => Some(input),
48        }
49    }
50}
51
52/// Closed owned source for a raincloud plot.
53#[derive(Debug, Clone)]
54pub struct RaincloudInput {
55    source: RaincloudSource,
56}
57
58#[derive(Debug, Clone)]
59enum RaincloudSource {
60    Samples {
61        category: CategoryValues,
62        values: NumericValues,
63    },
64    Geometry(std::sync::Arc<RaincloudGeometry>),
65}
66
67impl RaincloudInput {
68    /// Compute raincloud geometry from aligned categories and numeric samples.
69    #[must_use]
70    pub fn samples(category: CategoryValues, values: NumericValues) -> Self {
71        Self {
72            source: RaincloudSource::Samples { category, values },
73        }
74    }
75
76    /// Render complete caller-computed raincloud geometry.
77    #[must_use]
78    pub fn geometry(input: RaincloudGeometry) -> Self {
79        Self {
80            source: RaincloudSource::Geometry(std::sync::Arc::new(input)),
81        }
82    }
83
84    pub(crate) const fn samples_input(&self) -> Option<(&CategoryValues, &NumericValues)> {
85        match &self.source {
86            RaincloudSource::Samples { category, values } => Some((category, values)),
87            RaincloudSource::Geometry(_) => None,
88        }
89    }
90
91    pub(crate) fn geometry_input(&self) -> Option<&RaincloudGeometry> {
92        match &self.source {
93            RaincloudSource::Samples { .. } => None,
94            RaincloudSource::Geometry(input) => Some(input),
95        }
96    }
97}
98
99/// Native values accepted by a line plot's horizontal axis.
100#[derive(Debug, Clone)]
101pub enum AxisValues {
102    /// Continuous numeric axis values.
103    Numeric(NumericValues),
104    /// Ordered categorical axis values.
105    Category(CategoryValues),
106}
107
108impl From<NumericValues> for AxisValues {
109    fn from(values: NumericValues) -> Self {
110        Self::Numeric(values)
111    }
112}
113
114impl From<CategoryValues> for AxisValues {
115    fn from(values: CategoryValues) -> Self {
116        Self::Category(values)
117    }
118}
119
120impl AxisValues {
121    /// Semantic value name used for inferred labels.
122    #[must_use]
123    pub fn name(&self) -> &str {
124        match self {
125            Self::Numeric(values) => values.name(),
126            Self::Category(values) => values.name(),
127        }
128    }
129
130    pub(crate) fn len(&self) -> usize {
131        match self {
132            Self::Numeric(values) => values.len(),
133            Self::Category(values) => values.len(),
134        }
135    }
136}
137
138#[derive(Debug, Clone)]
139pub(crate) enum PlotInput {
140    Scatter {
141        x: NumericValues,
142        y: NumericValues,
143    },
144    Histogram {
145        input: HistogramInput,
146    },
147    Density {
148        values: NumericValues,
149    },
150    Qq {
151        values: NumericValues,
152    },
153    PointInterval {
154        estimate: NumericValues,
155        category: CategoryValues,
156        bounds: Bounds,
157    },
158    Line {
159        x: AxisValues,
160        y: NumericValues,
161    },
162    Trajectory {
163        x: AxisValues,
164        y: NumericValues,
165        group: CategoryValues,
166    },
167    LinePath {
168        x: NumericValues,
169        y: NumericValues,
170        group: CategoryValues,
171    },
172    Interaction {
173        condition: CategoryValues,
174        estimate: NumericValues,
175        group: CategoryValues,
176        bounds: Bounds,
177    },
178    Coefficient {
179        input: CoefficientInput,
180        term: CategoryValues,
181    },
182    Forest {
183        estimate: NumericValues,
184        term: CategoryValues,
185        bounds: Bounds,
186        reference: f64,
187    },
188    Dumbbell {
189        category: CategoryValues,
190        left: NumericValues,
191        right: NumericValues,
192        endpoints: DumbbellEndpoints,
193    },
194    Heatmap {
195        x: CategoryValues,
196        y: CategoryValues,
197        fill: NumericValues,
198    },
199    Bar {
200        category: CategoryValues,
201        input: BarInput,
202    },
203    HorizontalBar {
204        category: CategoryValues,
205        input: BarInput,
206    },
207    StackedBar {
208        category: CategoryValues,
209        value: NumericValues,
210        series: CategoryValues,
211    },
212    StackedHorizontalBar {
213        category: CategoryValues,
214        value: NumericValues,
215        series: CategoryValues,
216    },
217    Boxplot {
218        category: CategoryValues,
219        value: NumericValues,
220    },
221    Violin {
222        category: CategoryValues,
223        value: NumericValues,
224    },
225    Raincloud {
226        input: RaincloudInput,
227    },
228    Regression {
229        x: NumericValues,
230        y: NumericValues,
231    },
232    Marginal {
233        x: NumericValues,
234        y: NumericValues,
235    },
236    Matrix {
237        variables: Vec<NumericValues>,
238    },
239    Beeswarm {
240        value: NumericValues,
241        category: CategoryValues,
242        color: NumericValues,
243    },
244    Waterfall {
245        labels: CategoryValues,
246        deltas: NumericValues,
247        base: f64,
248    },
249}
250
251impl PlotInput {
252    pub(crate) fn len(&self) -> usize {
253        match self {
254            Self::Scatter { x, .. } | Self::Regression { x, .. } | Self::Marginal { x, .. } => {
255                x.len()
256            }
257            Self::Histogram { input } => input.samples_input().map_or(0, NumericValues::len),
258            Self::Density { values } | Self::Qq { values } => values.len(),
259            Self::PointInterval { estimate, .. }
260            | Self::Interaction { estimate, .. }
261            | Self::Forest { estimate, .. } => estimate.len(),
262            Self::Coefficient { input, .. } => input.values().len(),
263            Self::Line { x, .. } | Self::Trajectory { x, .. } => x.len(),
264            Self::LinePath { x, .. } => x.len(),
265            Self::Dumbbell { left, .. } => left.len(),
266            Self::Heatmap { x, .. }
267            | Self::Bar { category: x, .. }
268            | Self::HorizontalBar { category: x, .. }
269            | Self::StackedBar { category: x, .. }
270            | Self::StackedHorizontalBar { category: x, .. }
271            | Self::Boxplot { category: x, .. }
272            | Self::Violin { category: x, .. }
273            | Self::Beeswarm { category: x, .. } => x.len(),
274            Self::Raincloud { input } => input
275                .samples_input()
276                .map_or(0, |(category, _)| category.len()),
277            Self::Matrix { variables } => variables.first().map_or(0, NumericValues::len),
278            Self::Waterfall { labels, .. } => labels.len(),
279        }
280    }
281
282    pub(crate) fn x_name(&self) -> Option<&str> {
283        match self {
284            Self::Scatter { x, .. } | Self::Regression { x, .. } | Self::Marginal { x, .. } => {
285                Some(x.name())
286            }
287            Self::Histogram { input } => input.samples_input().map(NumericValues::name),
288            Self::Density { values } | Self::Qq { values } => Some(values.name()),
289            Self::PointInterval { estimate, .. } | Self::Forest { estimate, .. } => {
290                Some(estimate.name())
291            }
292            Self::Coefficient { input, .. } => Some(input.values().name()),
293            Self::Line { x, .. } | Self::Trajectory { x, .. } => Some(x.name()),
294            Self::LinePath { x, .. } => Some(x.name()),
295            Self::Interaction { condition, .. }
296            | Self::Dumbbell {
297                category: condition,
298                ..
299            }
300            | Self::Heatmap { x: condition, .. }
301            | Self::Bar {
302                category: condition,
303                ..
304            }
305            | Self::HorizontalBar {
306                category: condition,
307                ..
308            }
309            | Self::StackedBar {
310                category: condition,
311                ..
312            }
313            | Self::StackedHorizontalBar {
314                category: condition,
315                ..
316            }
317            | Self::Boxplot {
318                category: condition,
319                ..
320            }
321            | Self::Violin {
322                category: condition,
323                ..
324            }
325            | Self::Waterfall {
326                labels: condition, ..
327            } => Some(condition.name()),
328            Self::Raincloud { input } => input.samples_input().map(|(category, _)| category.name()),
329            Self::Beeswarm { value, .. } => Some(value.name()),
330            Self::Matrix { .. } => None,
331        }
332    }
333
334    pub(crate) fn y_name(&self) -> Option<&str> {
335        match self {
336            Self::Scatter { y, .. }
337            | Self::Line { y, .. }
338            | Self::Trajectory { y, .. }
339            | Self::LinePath { y, .. }
340            | Self::Regression { y, .. }
341            | Self::Marginal { y, .. } => Some(y.name()),
342            Self::PointInterval { category, .. }
343            | Self::Coefficient { term: category, .. }
344            | Self::Forest { term: category, .. }
345            | Self::Beeswarm { category, .. } => Some(category.name()),
346            Self::Interaction { estimate, .. }
347            | Self::Dumbbell { left: estimate, .. }
348            | Self::StackedBar {
349                value: estimate, ..
350            }
351            | Self::StackedHorizontalBar {
352                value: estimate, ..
353            }
354            | Self::Boxplot {
355                value: estimate, ..
356            }
357            | Self::Violin {
358                value: estimate, ..
359            }
360            | Self::Waterfall {
361                deltas: estimate, ..
362            } => Some(estimate.name()),
363            Self::Raincloud { input } => input.samples_input().map(|(_, values)| values.name()),
364            Self::Bar { input, .. } | Self::HorizontalBar { input, .. } => {
365                Some(input.values().name())
366            }
367            Self::Heatmap { y, .. } => Some(y.name()),
368            Self::Histogram { .. }
369            | Self::Density { .. }
370            | Self::Qq { .. }
371            | Self::Matrix { .. } => None,
372        }
373    }
374
375    pub(crate) const fn required_group(&self) -> Option<&CategoryValues> {
376        match self {
377            Self::Trajectory { group, .. }
378            | Self::LinePath { group, .. }
379            | Self::Interaction { group, .. } => Some(group),
380            _ => None,
381        }
382    }
383
384    pub(crate) const fn required_series(&self) -> Option<&CategoryValues> {
385        match self {
386            Self::StackedBar { series, .. } | Self::StackedHorizontalBar { series, .. } => {
387                Some(series)
388            }
389            _ => None,
390        }
391    }
392
393    pub(crate) const fn continuous_color(&self) -> Option<&NumericValues> {
394        match self {
395            Self::Heatmap { fill, .. } => Some(fill),
396            Self::Beeswarm { color, .. } => Some(color),
397            _ => None,
398        }
399    }
400}