use crate::{
data::{Bounds, VariableValues},
plots::{BarInput, CoefficientInput, DumbbellEndpoints, HistogramGeometry, RaincloudGeometry},
scales::ColorSource,
CategoryValues, NumericValues,
};
#[derive(Debug, Clone)]
pub struct HistogramInput {
source: HistogramSource,
}
#[derive(Debug, Clone)]
enum HistogramSource {
Samples(NumericValues),
Geometry(std::sync::Arc<HistogramGeometry>),
}
impl HistogramInput {
#[must_use]
pub fn samples(values: NumericValues) -> Self {
Self {
source: HistogramSource::Samples(values),
}
}
#[must_use]
pub fn geometry(input: HistogramGeometry) -> Self {
Self {
source: HistogramSource::Geometry(std::sync::Arc::new(input)),
}
}
pub(crate) const fn samples_input(&self) -> Option<&NumericValues> {
match &self.source {
HistogramSource::Samples(values) => Some(values),
HistogramSource::Geometry(_) => None,
}
}
pub fn get_geometry(&self) -> Option<&HistogramGeometry> {
match &self.source {
HistogramSource::Samples(_) => None,
HistogramSource::Geometry(input) => Some(input),
}
}
pub(crate) fn geometry_mut(&mut self) -> Option<&mut HistogramGeometry> {
match &mut self.source {
HistogramSource::Samples(_) => None,
HistogramSource::Geometry(input) => Some(std::sync::Arc::make_mut(input)),
}
}
}
#[derive(Debug, Clone)]
pub struct RaincloudInput {
source: RaincloudSource,
}
#[derive(Debug, Clone)]
enum RaincloudSource {
Samples {
category: CategoryValues,
values: NumericValues,
},
Geometry(std::sync::Arc<RaincloudGeometry>),
}
impl RaincloudInput {
#[must_use]
pub fn samples(category: CategoryValues, values: NumericValues) -> Self {
Self {
source: RaincloudSource::Samples { category, values },
}
}
#[must_use]
pub fn geometry(input: RaincloudGeometry) -> Self {
Self {
source: RaincloudSource::Geometry(std::sync::Arc::new(input)),
}
}
pub(crate) const fn samples_input(&self) -> Option<(&CategoryValues, &NumericValues)> {
match &self.source {
RaincloudSource::Samples { category, values } => Some((category, values)),
RaincloudSource::Geometry(_) => None,
}
}
pub(crate) fn geometry_input(&self) -> Option<&RaincloudGeometry> {
match &self.source {
RaincloudSource::Samples { .. } => None,
RaincloudSource::Geometry(input) => Some(input),
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum AxisValues {
Numeric(NumericValues),
Category(CategoryValues),
Keys(crate::data::RawKeyValues),
}
impl From<NumericValues> for AxisValues {
fn from(values: NumericValues) -> Self {
Self::Numeric(values)
}
}
impl From<CategoryValues> for AxisValues {
fn from(values: CategoryValues) -> Self {
Self::Category(values)
}
}
impl AxisValues {
#[must_use]
pub fn name(&self) -> &str {
match self {
Self::Numeric(values) => values.name(),
Self::Category(values) => values.original_name(),
Self::Keys(values) => values.name(),
}
}
pub(crate) fn raw_name(&self) -> &str {
match self {
Self::Numeric(values) => values.raw_name(),
Self::Category(values) => values.name(),
Self::Keys(values) => values.name(),
}
}
pub fn len(&self) -> usize {
match self {
Self::Numeric(values) => values.len(),
Self::Category(values) => values.len(),
Self::Keys(values) => values.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub const fn get_numeric(&self) -> Option<&NumericValues> {
match self {
Self::Numeric(values) => Some(values),
_ => None,
}
}
pub const fn get_category(&self) -> Option<&CategoryValues> {
match self {
Self::Category(values) => Some(values),
_ => None,
}
}
pub const fn get_keys(&self) -> Option<&crate::data::RawKeyValues> {
match self {
Self::Keys(values) => Some(values),
_ => None,
}
}
pub fn is_valid(&self, index: usize) -> Option<bool> {
match self {
Self::Numeric(values) => values.is_valid(index),
Self::Category(values) => values.is_valid(index),
Self::Keys(values) => values.is_valid(index),
}
}
}
#[derive(Debug, Clone)]
pub(crate) enum PlotInput {
Scatter {
x: NumericValues,
y: NumericValues,
},
Histogram {
input: HistogramInput,
},
Density {
values: NumericValues,
},
Qq {
values: NumericValues,
},
PointInterval {
estimate: NumericValues,
category: CategoryValues,
bounds: Bounds,
},
Line {
x: AxisValues,
y: NumericValues,
},
Trajectory {
x: AxisValues,
y: NumericValues,
group: CategoryValues,
},
LinePath {
x: NumericValues,
y: NumericValues,
group: Option<CategoryValues>,
},
Interaction {
condition: CategoryValues,
estimate: NumericValues,
group: CategoryValues,
bounds: Bounds,
},
Coefficient {
input: CoefficientInput,
term: CategoryValues,
},
Forest {
estimate: NumericValues,
term: CategoryValues,
bounds: Bounds,
reference: f64,
},
Dumbbell {
category: CategoryValues,
left: NumericValues,
right: NumericValues,
endpoints: DumbbellEndpoints,
},
Heatmap {
x: CategoryValues,
y: CategoryValues,
fill: NumericValues,
},
Bar {
category: CategoryValues,
input: BarInput,
},
HorizontalBar {
category: CategoryValues,
input: BarInput,
},
StackedBar {
category: CategoryValues,
value: NumericValues,
series: CategoryValues,
},
StackedHorizontalBar {
category: CategoryValues,
value: NumericValues,
series: CategoryValues,
},
Boxplot {
category: CategoryValues,
value: NumericValues,
},
Violin {
category: CategoryValues,
value: NumericValues,
},
Raincloud {
input: RaincloudInput,
},
Regression {
x: NumericValues,
y: NumericValues,
},
Marginal {
x: NumericValues,
y: NumericValues,
},
Matrix {
variables: VariableValues,
},
Beeswarm {
value: NumericValues,
category: CategoryValues,
color: Option<ColorSource>,
},
Waterfall {
labels: CategoryValues,
deltas: NumericValues,
base: f64,
},
}
impl PlotInput {
pub(crate) fn len(&self) -> usize {
match self {
Self::Scatter { x, .. } | Self::Regression { x, .. } | Self::Marginal { x, .. } => {
x.len()
}
Self::Histogram { input } => input.samples_input().map_or(0, NumericValues::len),
Self::Density { values } | Self::Qq { values } => values.len(),
Self::PointInterval { estimate, .. }
| Self::Interaction { estimate, .. }
| Self::Forest { estimate, .. } => estimate.len(),
Self::Coefficient { input, .. } => input.values().len(),
Self::Line { x, .. } | Self::Trajectory { x, .. } => x.len(),
Self::LinePath { x, .. } => x.len(),
Self::Dumbbell { left, .. } => left.len(),
Self::Heatmap { x, .. }
| Self::Bar { category: x, .. }
| Self::HorizontalBar { category: x, .. }
| Self::StackedBar { category: x, .. }
| Self::StackedHorizontalBar { category: x, .. }
| Self::Boxplot { category: x, .. }
| Self::Violin { category: x, .. }
| Self::Beeswarm { category: x, .. } => x.len(),
Self::Raincloud { input } => input
.samples_input()
.map_or(0, |(category, _)| category.len()),
Self::Matrix { variables } => variables
.get_at(0)
.map_or(0, |variable| variable.values.len()),
Self::Waterfall { labels, .. } => labels.len(),
}
}
pub(crate) fn x_name(&self) -> Option<&str> {
match self {
Self::Scatter { x, .. } | Self::Regression { x, .. } | Self::Marginal { x, .. } => {
Some(x.raw_name())
}
Self::Histogram { input } => input.samples_input().map(NumericValues::raw_name),
Self::Density { values } | Self::Qq { values } => Some(values.raw_name()),
Self::PointInterval { estimate, .. } | Self::Forest { estimate, .. } => {
Some(estimate.raw_name())
}
Self::Coefficient { input, .. } => Some(input.values().raw_name()),
Self::Line { x, .. } | Self::Trajectory { x, .. } => Some(x.raw_name()),
Self::LinePath { x, .. } => Some(x.raw_name()),
Self::Interaction { condition, .. }
| Self::Dumbbell {
category: condition,
..
}
| Self::Heatmap { x: condition, .. }
| Self::Bar {
category: condition,
..
}
| Self::HorizontalBar {
category: condition,
..
}
| Self::StackedBar {
category: condition,
..
}
| Self::StackedHorizontalBar {
category: condition,
..
}
| Self::Boxplot {
category: condition,
..
}
| Self::Violin {
category: condition,
..
}
| Self::Waterfall {
labels: condition, ..
} => Some(condition.name()),
Self::Raincloud { input } => input.samples_input().map(|(category, _)| category.name()),
Self::Beeswarm { value, .. } => Some(value.raw_name()),
Self::Matrix { .. } => None,
}
}
pub(crate) fn y_name(&self) -> Option<&str> {
match self {
Self::Scatter { y, .. }
| Self::Line { y, .. }
| Self::Trajectory { y, .. }
| Self::LinePath { y, .. }
| Self::Regression { y, .. }
| Self::Marginal { y, .. } => Some(y.raw_name()),
Self::PointInterval { category, .. }
| Self::Coefficient { term: category, .. }
| Self::Forest { term: category, .. }
| Self::Beeswarm { category, .. } => Some(category.name()),
Self::Interaction { estimate, .. }
| Self::Dumbbell { left: estimate, .. }
| Self::StackedBar {
value: estimate, ..
}
| Self::StackedHorizontalBar {
value: estimate, ..
}
| Self::Boxplot {
value: estimate, ..
}
| Self::Violin {
value: estimate, ..
}
| Self::Waterfall {
deltas: estimate, ..
} => Some(estimate.raw_name()),
Self::Raincloud { input } => input.samples_input().map(|(_, values)| values.raw_name()),
Self::Bar { input, .. } | Self::HorizontalBar { input, .. } => {
Some(input.values().raw_name())
}
Self::Heatmap { y, .. } => Some(y.name()),
Self::Histogram { .. }
| Self::Density { .. }
| Self::Qq { .. }
| Self::Matrix { .. } => None,
}
}
pub(crate) const fn required_group(&self) -> Option<&CategoryValues> {
match self {
Self::Trajectory { group, .. } | Self::Interaction { group, .. } => Some(group),
Self::LinePath { group, .. } => group.as_ref(),
_ => None,
}
}
pub(crate) const fn required_series(&self) -> Option<&CategoryValues> {
match self {
Self::StackedBar { series, .. } | Self::StackedHorizontalBar { series, .. } => {
Some(series)
}
_ => None,
}
}
pub(crate) const fn continuous_color(&self) -> Option<&NumericValues> {
match self {
Self::Heatmap { fill, .. } => Some(fill),
Self::Beeswarm {
color: Some(ColorSource::Numeric(color)),
..
} => Some(color),
_ => None,
}
}
}