Skip to main content

lichtung_plot/plots/qq/
result.rs

1//! Actual normal probability pairs and their original sample relations.
2
3use std::sync::Arc;
4
5use crate::{
6    data::{
7        DataComponentInfo, DataDomainInfo, DataDomainRef, DataPayload, DataRole, MemberAddress,
8        SourceMembers,
9    },
10    elements::ElementRef,
11    stats::results::{numeric_column, ResultDomains},
12    NumericValues, Result,
13};
14
15/// A probability plot's ordered scientific pairs and reference parameters.
16#[derive(Debug, Clone)]
17pub struct ProbabilityPlotResult(Arc<ProbabilityData>);
18
19#[derive(Debug)]
20struct ProbabilityData {
21    plotting_positions: NumericValues,
22    theoretical: NumericValues,
23    observed: NumericValues,
24    reference_line: Option<(f64, f64)>,
25    reference_mode: crate::plots::QqLine,
26    domains: ResultDomains,
27}
28
29impl ProbabilityPlotResult {
30    /// Actual plotting positions (probabilities in increasing order).
31    pub fn plotting_positions(&self) -> &NumericValues {
32        &self.0.plotting_positions
33    }
34    /// Standard-normal theoretical quantiles at the actual positions.
35    pub fn theoretical(&self) -> &NumericValues {
36        &self.0.theoretical
37    }
38    /// Actual observed values in their paired quantile order.
39    pub fn observed(&self) -> &NumericValues {
40        &self.0.observed
41    }
42    /// Actual reference line as (slope, intercept), if selected.
43    pub fn reference_line(&self) -> Option<(f64, f64)> {
44        self.0.reference_line
45    }
46    /// Reference-line policy used in this preparation.
47    pub fn reference_mode(&self) -> crate::plots::QqLine {
48        self.0.reference_mode
49    }
50    /// The actual quantile pair domain.
51    pub fn domains(&self) -> &[DataDomainInfo] {
52        self.0.domains.infos()
53    }
54    /// Original observation paired with this plotting position.
55    pub fn members(&self, address: impl Into<MemberAddress>) -> Result<Option<SourceMembers>> {
56        self.0.domains.members(&address.into())
57    }
58    /// Original observation in this actual result domain.
59    pub fn members_in(
60        &self,
61        domain: &DataDomainRef,
62        address: impl Into<MemberAddress>,
63    ) -> Result<Option<SourceMembers>> {
64        self.0.domains.members_in(domain, &address.into())
65    }
66}
67
68impl super::PreparedQq {
69    pub(crate) fn scientific_result(
70        &self,
71        source: &crate::builder::PlotDescription,
72        owner: &ElementRef,
73    ) -> Result<ProbabilityPlotResult> {
74        let originals = source.primary_layer_data(&owner.declaration());
75        let samples = crate::data::select_value(&originals, DataRole::Value.into())?;
76        let plotting_positions = numeric_column(
77            "plotting_position",
78            self.positions
79                .as_ref()
80                .expect("actual captured plotting positions")
81                .iter()
82                .copied(),
83        )?;
84        let theoretical = numeric_column("theoretical", self.theoretical.iter().copied())?;
85        let slots = crate::data::source::collect_source(
86            self.alphas.iter().map(|observation| Ok(observation.slot)),
87        )?;
88        let observed = samples.numeric()?.select(&slots)?;
89        let members = crate::data::source::collect_source(self.alphas.iter().map(|observation| {
90            Ok(Some(SourceMembers::from_slots(
91                &[&samples],
92                [observation.slot],
93            )?))
94        }))?;
95        let components = crate::data::source::collect_source(
96            [
97                ("plotting_positions", &plotting_positions),
98                ("theoretical", &theoretical),
99                ("observed", &observed),
100            ]
101            .into_iter()
102            .map(|(name, value)| {
103                DataComponentInfo::scientific_with_members(
104                    name,
105                    &DataPayload::Numeric(value.clone()),
106                    &members,
107                )
108            }),
109        )?;
110        let mut domains = ResultDomains::default();
111        domains.push(owner, None, components, members)?;
112        Ok(ProbabilityPlotResult(crate::data::source::source_arc(
113            ProbabilityData {
114                plotting_positions,
115                theoretical,
116                observed,
117                reference_line: self.line.map(|line| (line.slope, line.intercept)),
118                reference_mode: source.qq_line_spec(),
119                domains,
120            },
121        )?))
122    }
123}
124
125crate::orchestrator::science::stat_result_api!(Qq, ProbabilityPlotResult, |source, data, layer| {
126    super::PreparedQq::new(source, data)?.scientific_result(source, layer)
127});