Skip to main content

lichtung_plot_ndarray/frontend/
status.rs

1use super::PlotAdapter;
2use crate::error::{binding, BindingResult};
3use crate::{kind, AdapterError, BooleanInput, CategoryInput, EstimateStatus};
4
5impl<K: kind::EstimateStatus> PlotAdapter<'_, K> {
6    /// Explicitly map Boolean true/false to Estimated/NotEstimable; null is an error.
7    pub fn estimate_status_boolean(
8        mut self,
9        input: impl BooleanInput,
10    ) -> std::result::Result<Self, AdapterError> {
11        if input.cell_shape().is_some() || input.selected_columns().is_some() {
12            return Err(binding(
13                "estimate status",
14                "expected one Boolean variable, not a cell grid",
15            ));
16        }
17        let (name, values) = input.raw_boolean(self.source.as_deref(), "estimate status")?;
18        self.aligned("estimate status", &name, values.len())?;
19        let statuses = values
20            .into_iter()
21            .enumerate()
22            .map(|(slot, value)| match value {
23                Some(true) => Ok(EstimateStatus::Estimated),
24                Some(false) => Ok(EstimateStatus::NotEstimable),
25                None => Err(binding(
26                    "estimate status",
27                    format!("missing Boolean at slot {slot}"),
28                )),
29            })
30            .collect::<BindingResult<Vec<_>>>()?;
31        self.plot = self.plot.estimate_status(statuses);
32        Ok(self)
33    }
34    /// Map exact raw categorical statuses, rejecting missing/unmapped observations.
35    pub fn estimate_status_categorical<I, S>(
36        mut self,
37        input: impl CategoryInput,
38        mapping: I,
39    ) -> std::result::Result<Self, AdapterError>
40    where
41        I: IntoIterator<Item = (S, EstimateStatus)>,
42        S: AsRef<str>,
43    {
44        let (name, values) = input.raw_category(self.source.as_deref(), "estimate status")?;
45        self.aligned("estimate status", &name, values.len())?;
46        let mapping = mapping
47            .into_iter()
48            .map(|(key, status)| (key.as_ref().to_owned(), status))
49            .collect::<Vec<_>>();
50        let statuses = values
51            .into_iter()
52            .enumerate()
53            .map(|(slot, value)| {
54                let value = value.ok_or_else(|| {
55                    binding(
56                        "estimate status",
57                        format!("missing category at slot {slot}"),
58                    )
59                })?;
60                let mut matches = mapping.iter().filter(|(key, _)| key == &value);
61                let status = matches
62                    .next()
63                    .ok_or_else(|| {
64                        binding(
65                            "estimate status",
66                            format!("unmapped category {value:?} at slot {slot}"),
67                        )
68                    })?
69                    .1;
70                if matches.next().is_some() {
71                    return Err(binding(
72                        "estimate status",
73                        format!("duplicate mapping for {value:?}"),
74                    ));
75                }
76                Ok(status)
77            })
78            .collect::<BindingResult<Vec<_>>>()?;
79        self.plot = self.plot.estimate_status(statuses);
80        Ok(self)
81    }
82}