Skip to main content

lichtung_plot/
input.rs

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