Skip to main content

lichtung_plot/geometry/
stats.rs

1//! Owning result domains of the actual supplied or evaluated raincloud geometry.
2
3use crate::{
4    data::{
5        DataComponentInfo, DataDomainInfo, DataDomainRef, DataPayload, KeyValues, MemberAddress,
6        SourceMembers, SourceRows,
7    },
8    elements::ElementRef,
9    plots::DensityProfiles,
10    stats::results::ResultDomains,
11    Error, Result,
12};
13
14use super::RaincloudGeometry;
15
16#[derive(Debug)]
17pub(super) struct RaincloudScience {
18    domains: ResultDomains,
19    profiles: Option<DensityProfiles>,
20}
21
22impl RaincloudGeometry {
23    /// The original KDE and auxiliary statistics when this geometry was
24    /// evaluated from samples. Supplied widths alone do not define density.
25    pub fn get_profiles(&self) -> Option<&DensityProfiles> {
26        self.result.as_ref()?.profiles.as_ref()
27    }
28
29    /// Three actual result domains, when captured by scientific preparation.
30    pub fn domains(&self) -> &[DataDomainInfo] {
31        self.result
32            .as_ref()
33            .map_or(&[], |result| result.domains.infos())
34    }
35
36    /// Read a member in the specified result domain.
37    pub fn members_in(
38        &self,
39        domain: &DataDomainRef,
40        address: impl Into<MemberAddress>,
41    ) -> Result<Option<SourceMembers>> {
42        self.result
43            .as_ref()
44            .ok_or_else(|| {
45                Error::Data("raincloud geometry has no captured scientific domains".into())
46            })?
47            .domains
48            .members_in(domain, &address.into())
49    }
50
51    /// Read a member when there is a unique actual result domain.
52    pub fn members(&self, address: impl Into<MemberAddress>) -> Result<Option<SourceMembers>> {
53        self.result
54            .as_ref()
55            .ok_or_else(|| {
56                Error::Data("raincloud geometry has no captured scientific domains".into())
57            })?
58            .domains
59            .members(&address.into())
60    }
61
62    pub(crate) fn with_result_domains(
63        mut self,
64        owner: &ElementRef,
65        relations: [Vec<Option<SourceMembers>>; 3],
66        rows: Option<&SourceRows>,
67        profiles: Option<DensityProfiles>,
68    ) -> Result<Self> {
69        let key_component = |name, keys: KeyValues| {
70            DataComponentInfo::captured(
71                name,
72                &match keys {
73                    KeyValues::Strings(values) => DataPayload::Category(values),
74                    KeyValues::Raw(values) => DataPayload::RawKeys(values),
75                },
76            )
77        };
78        let mut summary = vec![key_component("category", self.category())?];
79        let mut density = vec![key_component("density_category", self.density_category())?];
80        let mut points = vec![key_component("point_category", self.point_category())?];
81        for (components, name, keys) in [
82            (&mut summary, "hue", self.hue()),
83            (&mut density, "density_hue", self.density_hue()),
84            (&mut points, "point_hue", self.point_hue()),
85        ] {
86            if let Some(keys) = keys {
87                crate::data::source::reserve_source(components, 1)?;
88                components.push(key_component(name, keys)?);
89            }
90        }
91        for (index, (components, fields)) in [
92            (
93                &mut summary,
94                &[
95                    ("q1", &self.data.q1),
96                    ("median", &self.data.median),
97                    ("q3", &self.data.q3),
98                ][..],
99            ),
100            (
101                &mut density,
102                &[
103                    ("density_y", &self.data.density_y),
104                    ("normalized_width", &self.data.normalized_width),
105                ][..],
106            ),
107            (
108                &mut points,
109                &[
110                    ("point_y", &self.data.point_y),
111                    ("normalized_strip", &self.data.normalized_strip),
112                ][..],
113            ),
114        ]
115        .into_iter()
116        .enumerate()
117        {
118            crate::data::source::reserve_source(components, fields.len())?;
119            for (name, value) in fields {
120                components.push(DataComponentInfo::scientific_with_members(
121                    name,
122                    &DataPayload::Numeric((*value).clone()),
123                    &relations[index],
124                )?);
125            }
126        }
127        let mut domains = ResultDomains::default();
128        for ((components, source_rows), members) in
129            [(summary, None), (density, None), (points, rows)]
130                .into_iter()
131                .zip(relations)
132        {
133            let info = DataDomainInfo::bound(
134                DataDomainRef::scientific(owner, None),
135                members.len(),
136                source_rows,
137                components,
138            );
139            domains.push_info(info, members)?;
140        }
141        self.result = Some(crate::data::source::source_arc(RaincloudScience {
142            domains,
143            profiles,
144        })?);
145        Ok(self)
146    }
147}