lichtung-plot 0.2.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
//! Owning result domains of the actual supplied or evaluated raincloud geometry.

use crate::{
    data::{
        DataComponentInfo, DataDomainInfo, DataDomainRef, DataPayload, KeyValues, MemberAddress,
        SourceMembers, SourceRows,
    },
    elements::ElementRef,
    plots::DensityProfiles,
    stats::results::ResultDomains,
    Error, Result,
};

use super::RaincloudGeometry;

#[derive(Debug)]
pub(super) struct RaincloudScience {
    domains: ResultDomains,
    profiles: Option<DensityProfiles>,
}

impl RaincloudGeometry {
    /// The original KDE and auxiliary statistics when this geometry was
    /// evaluated from samples. Supplied widths alone do not define density.
    pub fn get_profiles(&self) -> Option<&DensityProfiles> {
        self.result.as_ref()?.profiles.as_ref()
    }

    /// Three actual result domains, when captured by scientific preparation.
    pub fn domains(&self) -> &[DataDomainInfo] {
        self.result
            .as_ref()
            .map_or(&[], |result| result.domains.infos())
    }

    /// Read a member in the specified result domain.
    pub fn members_in(
        &self,
        domain: &DataDomainRef,
        address: impl Into<MemberAddress>,
    ) -> Result<Option<SourceMembers>> {
        self.result
            .as_ref()
            .ok_or_else(|| {
                Error::Data("raincloud geometry has no captured scientific domains".into())
            })?
            .domains
            .members_in(domain, &address.into())
    }

    /// Read a member when there is a unique actual result domain.
    pub fn members(&self, address: impl Into<MemberAddress>) -> Result<Option<SourceMembers>> {
        self.result
            .as_ref()
            .ok_or_else(|| {
                Error::Data("raincloud geometry has no captured scientific domains".into())
            })?
            .domains
            .members(&address.into())
    }

    pub(crate) fn with_result_domains(
        mut self,
        owner: &ElementRef,
        relations: [Vec<Option<SourceMembers>>; 3],
        rows: Option<&SourceRows>,
        profiles: Option<DensityProfiles>,
    ) -> Result<Self> {
        let key_component = |name, keys: KeyValues| {
            DataComponentInfo::captured(
                name,
                &match keys {
                    KeyValues::Strings(values) => DataPayload::Category(values),
                    KeyValues::Raw(values) => DataPayload::RawKeys(values),
                },
            )
        };
        let mut summary = vec![key_component("category", self.category())?];
        let mut density = vec![key_component("density_category", self.density_category())?];
        let mut points = vec![key_component("point_category", self.point_category())?];
        for (components, name, keys) in [
            (&mut summary, "hue", self.hue()),
            (&mut density, "density_hue", self.density_hue()),
            (&mut points, "point_hue", self.point_hue()),
        ] {
            if let Some(keys) = keys {
                crate::data::source::reserve_source(components, 1)?;
                components.push(key_component(name, keys)?);
            }
        }
        for (index, (components, fields)) in [
            (
                &mut summary,
                &[
                    ("q1", &self.data.q1),
                    ("median", &self.data.median),
                    ("q3", &self.data.q3),
                ][..],
            ),
            (
                &mut density,
                &[
                    ("density_y", &self.data.density_y),
                    ("normalized_width", &self.data.normalized_width),
                ][..],
            ),
            (
                &mut points,
                &[
                    ("point_y", &self.data.point_y),
                    ("normalized_strip", &self.data.normalized_strip),
                ][..],
            ),
        ]
        .into_iter()
        .enumerate()
        {
            crate::data::source::reserve_source(components, fields.len())?;
            for (name, value) in fields {
                components.push(DataComponentInfo::scientific_with_members(
                    name,
                    &DataPayload::Numeric((*value).clone()),
                    &relations[index],
                )?);
            }
        }
        let mut domains = ResultDomains::default();
        for ((components, source_rows), members) in
            [(summary, None), (density, None), (points, rows)]
                .into_iter()
                .zip(relations)
        {
            let info = DataDomainInfo::bound(
                DataDomainRef::scientific(owner, None),
                members.len(),
                source_rows,
                components,
            );
            domains.push_info(info, members)?;
        }
        self.result = Some(crate::data::source::source_arc(RaincloudScience {
            domains,
            profiles,
        })?);
        Ok(self)
    }
}