lichtung_plot_ndarray/
alpha.rs1use crate::error::{binding, BindingResult};
3use crate::input::sealed;
4use crate::{
5 binding::{ArraySource, CategoryInput, NumericInput},
6 scales::{AlphaEncoding, AlphaValues},
7};
8use crate::{capture::Capture, data::DataSourceRef};
9
10impl sealed::Sealed for AlphaEncoding {
11 fn native_slots(&self) -> bool {
12 true
13 }
14}
15impl sealed::Sealed for AlphaValues {
16 fn native_slots(&self) -> bool {
17 true
18 }
19}
20impl sealed::Sealed for f64 {}
21pub trait AlphaValuesInput: sealed::Sealed {
23 #[doc(hidden)]
24 fn bind_alpha_values(
25 self,
26 source: Option<&dyn ArraySource>,
27 reference: Option<&DataSourceRef>,
28 ) -> BindingResult<AlphaValues>;
29}
30impl AlphaValuesInput for AlphaValues {
31 fn bind_alpha_values(
32 self,
33 _: Option<&dyn ArraySource>,
34 _: Option<&DataSourceRef>,
35 ) -> BindingResult<AlphaValues> {
36 Ok(self)
37 }
38}
39impl<T: NumericInput> AlphaValuesInput for T {
40 fn bind_alpha_values(
41 self,
42 source: Option<&dyn ArraySource>,
43 reference: Option<&DataSourceRef>,
44 ) -> BindingResult<AlphaValues> {
45 let capture = Capture::existing(source, reference);
46 let values = capture.bind(self, 1, |input, source| input.bind_numeric(source, "Alpha"))?;
47 Ok(AlphaValues::try_from(values)?)
48 }
49}
50pub trait AlphaInput: sealed::Sealed {
52 #[doc(hidden)]
53 fn bind_alpha(
54 self,
55 source: Option<&dyn ArraySource>,
56 expected: Option<usize>,
57 copies: usize,
58 reference: Option<&DataSourceRef>,
59 ) -> BindingResult<AlphaEncoding>;
60}
61impl AlphaInput for AlphaEncoding {
62 fn bind_alpha(
63 self,
64 _: Option<&dyn ArraySource>,
65 _: Option<usize>,
66 _: usize,
67 _: Option<&DataSourceRef>,
68 ) -> BindingResult<AlphaEncoding> {
69 Ok(self)
70 }
71}
72impl AlphaInput for f64 {
73 fn bind_alpha(
74 self,
75 _: Option<&dyn ArraySource>,
76 _: Option<usize>,
77 _: usize,
78 _: Option<&DataSourceRef>,
79 ) -> BindingResult<AlphaEncoding> {
80 Ok(AlphaEncoding::fixed(self)?)
81 }
82}
83impl<T: AlphaValuesInput> AlphaInput for T {
84 fn bind_alpha(
85 self,
86 source: Option<&dyn ArraySource>,
87 expected: Option<usize>,
88 copies: usize,
89 reference: Option<&DataSourceRef>,
90 ) -> BindingResult<AlphaEncoding> {
91 let copies = if self.native_slots() { 1 } else { copies };
92 let values = expand(self.bind_alpha_values(source, reference)?, expected, copies)?;
93 Ok(AlphaEncoding::observations(values))
94 }
95}
96
97pub(crate) fn expand(
98 values: AlphaValues,
99 expected: Option<usize>,
100 copies: usize,
101) -> BindingResult<AlphaValues> {
102 if copies == 0 {
103 return Err(binding("alpha", "no generated columns"));
104 }
105 if let Some(expected) = expected {
106 crate::error::length("alpha", values.name(), expected / copies, values.len())?;
107 }
108 if copies == 1 {
109 Ok(values)
110 } else {
111 Ok(values.select((0..values.len()).flat_map(|row| std::iter::repeat_n(row, copies)))?)
112 }
113}
114pub(crate) fn keys<I, S, B>(
115 capture: &Capture<'_>,
116 keys: I,
117) -> BindingResult<Vec<crate::CategoryValues>>
118where
119 I: IntoIterator<Item = (S, B)>,
120 S: AsRef<str>,
121 B: CategoryInput,
122{
123 keys.into_iter()
124 .map(|(field, input)| {
125 let values = capture.bind(input, 1, |input, source| {
126 input.bind_category(source, "alpha selector")
127 })?;
128 Ok(values.with_field_name(field.as_ref()))
129 })
130 .collect()
131}