1use crate::{CategoryValues, Error, NumericValues};
4
5mod histogram;
6mod raincloud;
7mod stats;
8
9pub(crate) use histogram::{validate_kde, validate_rug_count};
10pub use histogram::{HistogramBinsData, HistogramGeometry, HistogramRugData};
11pub(crate) use raincloud::validate_raincloud_counts;
12
13#[derive(Debug, Clone)]
18pub struct RaincloudGeometry {
19 pub(crate) data: std::sync::Arc<RaincloudData>,
20 result: Option<std::sync::Arc<stats::RaincloudScience>>,
21}
22
23#[derive(Debug, Clone)]
24pub(crate) struct RaincloudData {
25 pub(crate) category_dimension: String,
27 pub(crate) value_dimension: String,
29 pub(crate) hue_dimension: Option<String>,
31 pub(crate) category: CategoryValues,
33 pub(crate) hue: Option<CategoryValues>,
35 pub(crate) q1: NumericValues,
37 pub(crate) median: NumericValues,
39 pub(crate) q3: NumericValues,
41 pub(crate) density_category: CategoryValues,
43 pub(crate) density_hue: Option<CategoryValues>,
45 pub(crate) density_y: NumericValues,
47 pub(crate) normalized_width: NumericValues,
49 pub(crate) point_category: CategoryValues,
51 pub(crate) point_hue: Option<CategoryValues>,
53 pub(crate) point_y: NumericValues,
55 pub(crate) normalized_strip: NumericValues,
57}
58
59impl PartialEq for RaincloudGeometry {
60 fn eq(&self, other: &Self) -> bool {
61 self.data.category_dimension == other.data.category_dimension
62 && self.data.value_dimension == other.data.value_dimension
63 && self.data.hue_dimension == other.data.hue_dimension
64 && [
65 (&self.data.category, &other.data.category),
66 (&self.data.density_category, &other.data.density_category),
67 (&self.data.point_category, &other.data.point_category),
68 ]
69 .into_iter()
70 .all(|(left, right)| category_equal(left, right))
71 && [
72 (&self.data.hue, &other.data.hue),
73 (&self.data.density_hue, &other.data.density_hue),
74 (&self.data.point_hue, &other.data.point_hue),
75 ]
76 .into_iter()
77 .all(|(left, right)| match (left, right) {
78 (Some(left), Some(right)) => category_equal(left, right),
79 (None, None) => true,
80 _ => false,
81 })
82 && [
83 (&self.data.q1, &other.data.q1),
84 (&self.data.median, &other.data.median),
85 (&self.data.q3, &other.data.q3),
86 (&self.data.density_y, &other.data.density_y),
87 (&self.data.normalized_width, &other.data.normalized_width),
88 (&self.data.point_y, &other.data.point_y),
89 (&self.data.normalized_strip, &other.data.normalized_strip),
90 ]
91 .into_iter()
92 .all(|(left, right)| numeric_equal(left, right))
93 }
94}
95
96pub(crate) fn numeric_equal(left: &NumericValues, right: &NumericValues) -> bool {
97 left.name() == right.name()
98 && left.len() == right.len()
99 && (0..left.len()).all(|row| left.get(row) == right.get(row))
100}
101
102fn category_equal(left: &CategoryValues, right: &CategoryValues) -> bool {
103 if left.original_name() != right.original_name() || left.len() != right.len() {
104 return false;
105 }
106 let left = left.key_values();
107 let right = right.key_values();
108 (0..left.len()).all(|row| left.key_at(row) == right.key_at(row))
109}
110
111fn dimension(value: &str, role: &str) -> Result<(), Error> {
112 if value.trim().is_empty() {
113 Err(Error::Data(format!("{role} must be nonempty").into()))
114 } else {
115 Ok(())
116 }
117}
118
119fn aligned(domain: &str, expected: usize, columns: &[(&str, usize)]) -> Result<(), Error> {
120 for &(name, actual) in columns {
121 if actual != expected {
122 use crate::errors::{Diagnostic, DiagnosticKind, ErrorContext, OperationPhase};
123 return Err(Error::Data(
124 Diagnostic::new(
125 DiagnosticKind::ShapeMismatch {
126 expected: vec![expected],
127 actual: vec![actual],
128 },
129 format!("{domain} column {name:?} has {actual} rows; expected {expected}"),
130 )
131 .with_context(ErrorContext::new().with_operation(OperationPhase::Bind)),
132 ));
133 }
134 }
135 Ok(())
136}
137
138pub(crate) fn finite(values: &NumericValues, row: usize, role: &str) -> Result<f64, Error> {
139 let value = values.get(row).flatten();
140 value.filter(|value| value.is_finite()).ok_or_else(|| {
141 use crate::{
142 data::{MemberAddress, RawKey},
143 errors::{Diagnostic, DiagnosticKind, ErrorContext, OperationPhase},
144 };
145 let binding = values
146 .field_key()
147 .cloned()
148 .unwrap_or_else(|| RawKey::from(values.name()));
149 Error::Data(
150 Diagnostic::new(
151 DiagnosticKind::InvalidValue {
152 value: value.map(RawKey::Float),
153 constraint: "present finite numeric value".to_owned(),
154 },
155 format!(
156 "{role} column {:?} requires a present finite value at row {row}",
157 values.name()
158 ),
159 )
160 .with_context(
161 ErrorContext::new()
162 .with_operation(OperationPhase::Prepare)
163 .with_binding(binding)
164 .with_member(MemberAddress::Slot(row)),
165 ),
166 )
167 })
168}
169
170pub(crate) fn category_value<'a>(
171 values: &'a CategoryValues,
172 row: usize,
173 role: &str,
174) -> Result<&'a str, Error> {
175 let value = values.get(row).flatten().ok_or_else(|| {
176 Error::Data(
177 format!(
178 "{role} column {:?} has a missing key at row {row}",
179 values.original_name()
180 )
181 .into(),
182 )
183 })?;
184 dimension(value, role)?;
185 Ok(value)
186}
187
188#[cfg(test)]
189mod validation_tests;