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 rows: bool,
59 reference: Option<&DataSourceRef>,
60 ) -> BindingResult<AlphaEncoding>;
61}
62impl AlphaInput for AlphaEncoding {
63 fn bind_alpha(
64 self,
65 _: Option<&dyn ArraySource>,
66 _: Option<usize>,
67 _: usize,
68 _: bool,
69 _: Option<&DataSourceRef>,
70 ) -> BindingResult<AlphaEncoding> {
71 Ok(self)
72 }
73}
74impl AlphaInput for f64 {
75 fn bind_alpha(
76 self,
77 _: Option<&dyn ArraySource>,
78 _: Option<usize>,
79 _: usize,
80 _: bool,
81 _: Option<&DataSourceRef>,
82 ) -> BindingResult<AlphaEncoding> {
83 Ok(AlphaEncoding::fixed(self)?)
84 }
85}
86impl<T: AlphaValuesInput> AlphaInput for T {
87 fn bind_alpha(
88 self,
89 source: Option<&dyn ArraySource>,
90 expected: Option<usize>,
91 copies: usize,
92 rows: bool,
93 reference: Option<&DataSourceRef>,
94 ) -> BindingResult<AlphaEncoding> {
95 let native = self.native_slots();
96 let copies = if native { 1 } else { copies };
97 let values = expand(
98 self.bind_alpha_values(source, reference)?,
99 expected,
100 copies,
101 rows && !native,
102 )?;
103 Ok(AlphaEncoding::observations(values))
104 }
105}
106
107pub(crate) fn expand(
108 values: AlphaValues,
109 expected: Option<usize>,
110 copies: usize,
111 rows: bool,
112) -> BindingResult<AlphaValues> {
113 if copies == 0 {
114 return Err(binding("alpha", "no generated columns"));
115 }
116 if let Some(expected) = expected {
117 crate::error::length("alpha", values.name(), expected / copies, values.len())?;
118 }
119 if rows || copies != 1 {
120 Ok(values.repeat_rows(copies)?)
121 } else {
122 Ok(values)
123 }
124}
125pub(crate) fn keys<I, S, B>(
126 capture: &Capture<'_>,
127 keys: I,
128) -> BindingResult<Vec<crate::CategoryValues>>
129where
130 I: IntoIterator<Item = (S, B)>,
131 S: AsRef<str>,
132 B: CategoryInput,
133{
134 keys.into_iter()
135 .map(|(field, input)| {
136 let values = capture.bind(input, 1, |input, source| {
137 input.bind_category(source, "alpha selector")
138 })?;
139 Ok(values.with_field_name(field.as_ref()))
140 })
141 .collect()
142}