use std::marker::PhantomData;
use crate::Error;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SignificanceMark {
PValue(f64),
NotSignificant,
OneStar,
TwoStars,
ThreeStars,
FourStars,
}
impl SignificanceMark {
pub(crate) fn glyph(self) -> Result<&'static str, Error> {
match self {
Self::PValue(value) if !value.is_finite() || !(0.0..=1.0).contains(&value) => {
Err(Error::Data(format!(
"significance p-value must be finite and in [0, 1]; got {value}"
)))
}
Self::PValue(value) if value >= 0.05 => Ok("ns"),
Self::PValue(value) if value < 0.0001 => Ok("****"),
Self::PValue(value) if value < 0.001 => Ok("***"),
Self::PValue(value) if value < 0.01 => Ok("**"),
Self::PValue(_) | Self::OneStar => Ok("*"),
Self::NotSignificant => Ok("ns"),
Self::TwoStars => Ok("**"),
Self::ThreeStars => Ok("***"),
Self::FourStars => Ok("****"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SignificanceTarget {
Category(String),
CategorySeries {
category: String,
series: String,
},
}
impl SignificanceTarget {
#[must_use]
pub fn category(value: impl Into<String>) -> Self {
Self::Category(value.into())
}
#[must_use]
pub fn category_series(category: impl Into<String>, series: impl Into<String>) -> Self {
Self::CategorySeries {
category: category.into(),
series: series.into(),
}
}
pub(crate) fn category_name(&self) -> &str {
match self {
Self::Category(category) | Self::CategorySeries { category, .. } => category,
}
}
pub(crate) fn series_name(&self) -> Option<&str> {
match self {
Self::Category(_) => None,
Self::CategorySeries { series, .. } => Some(series),
}
}
}
impl From<String> for SignificanceTarget {
fn from(value: String) -> Self {
Self::Category(value)
}
}
impl From<&str> for SignificanceTarget {
fn from(value: &str) -> Self {
Self::Category(value.to_owned())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FacetCell {
layout: FacetCellLayout,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum FacetCellLayout {
Single(String),
Grid { row: String, column: String },
}
impl FacetCell {
pub(crate) fn try_copy(&self) -> crate::Result<Self> {
match &self.layout {
FacetCellLayout::Single(level) => Ok(Self::single(
crate::data::source::copy_source_string(level)?,
)),
FacetCellLayout::Grid { row, column } => Ok(Self::grid(
crate::data::source::copy_source_string(row)?,
crate::data::source::copy_source_string(column)?,
)),
}
}
pub fn single_level(&self) -> Option<&str> {
if let FacetCellLayout::Single(level) = &self.layout {
Some(level)
} else {
None
}
}
pub fn grid_levels(&self) -> Option<(&str, &str)> {
if let FacetCellLayout::Grid { row, column } = &self.layout {
Some((row, column))
} else {
None
}
}
#[must_use]
pub fn single(level: impl Into<String>) -> Self {
Self {
layout: FacetCellLayout::Single(level.into()),
}
}
#[must_use]
pub fn grid(row: impl Into<String>, column: impl Into<String>) -> Self {
Self {
layout: FacetCellLayout::Grid {
row: row.into(),
column: column.into(),
},
}
}
pub(crate) const fn dimensions(&self) -> usize {
match self.layout {
FacetCellLayout::Single(_) => 1,
FacetCellLayout::Grid { .. } => 2,
}
}
fn validate(&self) -> Result<(), Error> {
match &self.layout {
FacetCellLayout::Single(level) => validate_text("facet level", level),
FacetCellLayout::Grid { row, column } => {
validate_text("facet row level", row)?;
validate_text("facet column level", column)
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Attached;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Pairwise;
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum SignificanceAnnotation {
Attached {
target: SignificanceTarget,
mark: SignificanceMark,
facet: Option<FacetCell>,
},
Pairwise {
endpoint_a: SignificanceTarget,
endpoint_b: SignificanceTarget,
mark: SignificanceMark,
facet: Option<FacetCell>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct Significance<P> {
annotation: SignificanceAnnotation,
placement: PhantomData<P>,
}
impl Significance<Attached> {
#[must_use]
pub fn at(target: impl Into<SignificanceTarget>, mark: SignificanceMark) -> Self {
Self {
annotation: SignificanceAnnotation::Attached {
target: target.into(),
mark,
facet: None,
},
placement: PhantomData,
}
}
}
impl Significance<Pairwise> {
#[must_use]
pub fn between(
endpoint_a: impl Into<SignificanceTarget>,
endpoint_b: impl Into<SignificanceTarget>,
mark: SignificanceMark,
) -> Self {
Self {
annotation: SignificanceAnnotation::Pairwise {
endpoint_a: endpoint_a.into(),
endpoint_b: endpoint_b.into(),
mark,
facet: None,
},
placement: PhantomData,
}
}
}
impl<P> Significance<P> {
#[must_use]
pub fn facet(mut self, cell: FacetCell) -> Self {
*self.annotation.facet_mut() = Some(cell);
self
}
pub(crate) fn into_annotation(self) -> SignificanceAnnotation {
self.annotation
}
}
impl SignificanceAnnotation {
pub(crate) const fn mark(&self) -> SignificanceMark {
match self {
Self::Attached { mark, .. } | Self::Pairwise { mark, .. } => *mark,
}
}
pub(crate) fn at_target(&self) -> Option<&SignificanceTarget> {
match self {
Self::Attached { target, .. } => Some(target),
Self::Pairwise { .. } => None,
}
}
pub(crate) fn endpoints(&self) -> Option<(&SignificanceTarget, &SignificanceTarget)> {
match self {
Self::Attached { .. } => None,
Self::Pairwise {
endpoint_a,
endpoint_b,
..
} => Some((endpoint_a, endpoint_b)),
}
}
pub(crate) const fn facet_cell(&self) -> Option<&FacetCell> {
match self {
Self::Attached { facet, .. } | Self::Pairwise { facet, .. } => facet.as_ref(),
}
}
pub(crate) fn validate(&self) -> Result<(), Error> {
self.mark().glyph()?;
match self {
Self::Attached { target, .. } => validate_target(target)?,
Self::Pairwise {
endpoint_a,
endpoint_b,
..
} => {
validate_target(endpoint_a)?;
validate_target(endpoint_b)?;
if endpoint_a == endpoint_b {
return Err(Error::Data(
"significance comparison endpoints must be different".into(),
));
}
}
}
if let Some(cell) = self.facet_cell() {
cell.validate()?;
}
Ok(())
}
fn facet_mut(&mut self) -> &mut Option<FacetCell> {
match self {
Self::Attached { facet, .. } | Self::Pairwise { facet, .. } => facet,
}
}
}
fn validate_target(target: &SignificanceTarget) -> Result<(), Error> {
validate_text("target category", target.category_name())?;
if let Some(series) = target.series_name() {
validate_text("target series", series)?;
}
Ok(())
}
fn validate_text(role: &str, value: &str) -> Result<(), Error> {
if value.trim().is_empty() || value.trim() != value || value.chars().any(char::is_control) {
return Err(Error::Data(format!(
"significance {role} must be nonempty, trimmed, and free of control characters"
)));
}
Ok(())
}