Skip to main content

lichtung_plot_ndarray/
alpha.rs

1//! Exact bytes and native mapping declarations, snapshotted at each binding.
2use crate::error::{binding, BindingResult};
3use crate::input::sealed;
4use crate::{AlphaEncoding, AlphaValues, ArrayElement, ArraySource, CategoryInput, CategoryValues};
5use ndarray::ArrayView1;
6
7impl sealed::Sealed for AlphaEncoding {
8    fn native_slots(&self) -> bool {
9        true
10    }
11}
12impl sealed::Sealed for AlphaValues {
13    fn native_slots(&self) -> bool {
14        true
15    }
16}
17impl sealed::Sealed for u8 {}
18/// Exact non-null u8 variables or already-materialized native alpha values.
19pub trait AlphaValuesInput: sealed::Sealed {
20    #[doc(hidden)]
21    fn bind_alpha_values(self, source: Option<&dyn ArraySource>) -> BindingResult<AlphaValues>;
22}
23impl AlphaValuesInput for AlphaValues {
24    fn bind_alpha_values(self, _: Option<&dyn ArraySource>) -> BindingResult<AlphaValues> {
25        Ok(self)
26    }
27}
28impl AlphaValuesInput for u8 {
29    fn bind_alpha_values(self, _: Option<&dyn ArraySource>) -> BindingResult<AlphaValues> {
30        Ok(AlphaValues::new("Alpha", [self])?)
31    }
32}
33impl AlphaValuesInput for usize {
34    fn bind_alpha_values(self, source: Option<&dyn ArraySource>) -> BindingResult<AlphaValues> {
35        let source = crate::source::lookup(source, self, "alpha bytes")?;
36        let values = source.byte_column(self, "alpha bytes")?;
37        Ok(AlphaValues::new(
38            crate::source::identity(Some(source), self, "alpha bytes")?,
39            values,
40        )?)
41    }
42}
43impl<T: ArrayElement> AlphaValuesInput for ArrayView1<'_, T> {
44    fn bind_alpha_values(self, _: Option<&dyn ArraySource>) -> BindingResult<AlphaValues> {
45        Ok(AlphaValues::new(
46            "Alpha",
47            crate::input::bytes(self, None, "alpha bytes")?,
48        )?)
49    }
50}
51impl<T: ArrayElement> AlphaValuesInput for (ArrayView1<'_, T>, ArrayView1<'_, bool>) {
52    fn bind_alpha_values(self, _: Option<&dyn ArraySource>) -> BindingResult<AlphaValues> {
53        Ok(AlphaValues::new(
54            "Alpha",
55            crate::input::bytes(self.0, Some(self.1), "alpha bytes")?,
56        )?)
57    }
58}
59/// Observation bytes or an unchanged native encoding; units remain native contracts.
60pub trait AlphaInput: sealed::Sealed {
61    #[doc(hidden)]
62    fn bind_alpha(
63        self,
64        source: Option<&dyn ArraySource>,
65        expected: Option<usize>,
66        copies: usize,
67    ) -> BindingResult<AlphaEncoding>;
68}
69impl AlphaInput for AlphaEncoding {
70    fn bind_alpha(
71        self,
72        _: Option<&dyn ArraySource>,
73        _: Option<usize>,
74        _: usize,
75    ) -> BindingResult<AlphaEncoding> {
76        Ok(self)
77    }
78}
79fn observations(
80    input: impl AlphaValuesInput,
81    source: Option<&dyn ArraySource>,
82    expected: Option<usize>,
83    copies: usize,
84) -> BindingResult<AlphaEncoding> {
85    let copies = if input.native_slots() { 1 } else { copies };
86    Ok(AlphaEncoding::observations(expand(
87        input.bind_alpha_values(source)?,
88        expected,
89        copies,
90    )?))
91}
92impl AlphaInput for AlphaValues {
93    fn bind_alpha(
94        self,
95        source: Option<&dyn ArraySource>,
96        expected: Option<usize>,
97        copies: usize,
98    ) -> BindingResult<AlphaEncoding> {
99        observations(self, source, expected, copies)
100    }
101}
102impl AlphaInput for usize {
103    fn bind_alpha(
104        self,
105        source: Option<&dyn ArraySource>,
106        expected: Option<usize>,
107        copies: usize,
108    ) -> BindingResult<AlphaEncoding> {
109        observations(self, source, expected, copies)
110    }
111}
112impl<T: ArrayElement> AlphaInput for ArrayView1<'_, T> {
113    fn bind_alpha(
114        self,
115        source: Option<&dyn ArraySource>,
116        expected: Option<usize>,
117        copies: usize,
118    ) -> BindingResult<AlphaEncoding> {
119        observations(self, source, expected, copies)
120    }
121}
122impl<T: ArrayElement> AlphaInput for (ArrayView1<'_, T>, ArrayView1<'_, bool>) {
123    fn bind_alpha(
124        self,
125        source: Option<&dyn ArraySource>,
126        expected: Option<usize>,
127        copies: usize,
128    ) -> BindingResult<AlphaEncoding> {
129        observations(self, source, expected, copies)
130    }
131}
132
133pub(crate) fn expand(
134    values: AlphaValues,
135    expected: Option<usize>,
136    copies: usize,
137) -> BindingResult<AlphaValues> {
138    if copies == 0 {
139        return Err(binding("alpha", "no generated columns"));
140    }
141    if let Some(expected) = expected {
142        crate::error::length("alpha", values.name(), expected / copies, values.len())?;
143    }
144    if copies == 1 {
145        Ok(values)
146    } else {
147        Ok(values.select((0..values.len()).flat_map(|row| std::iter::repeat_n(row, copies)))?)
148    }
149}
150pub(crate) fn keys<I, S, B>(
151    source: Option<&dyn ArraySource>,
152    keys: I,
153) -> BindingResult<Vec<CategoryValues>>
154where
155    I: IntoIterator<Item = (S, B)>,
156    S: AsRef<str>,
157    B: CategoryInput,
158{
159    keys.into_iter()
160        .map(|(field, input)| {
161            let (_, values) = input.raw_category(source, "alpha selector")?;
162            Ok(CategoryValues::nullable(field.as_ref(), values)?)
163        })
164        .collect()
165}