use super::PlotAdapter;
use crate::capture::Capture;
use crate::error::{binding, length, BindingResult};
use crate::{
binding::{
AlphaInput, AlphaValuesInput, ArraySource, CategoryInput, NumericInput, SourceRowsInput,
},
data::Bounds,
kind,
scales::{AlphaEncoding, AlphaValues},
AdapterError, CategoryValues, NumericValues,
};
fn mapped_numeric(
capture: &Capture<'_>,
input: impl NumericInput,
mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
copies: usize,
rows: bool,
expected: Option<usize>,
) -> BindingResult<AlphaValues> {
let native = input.native_slots();
let copies = if native { 1 } else { copies };
let raw = capture.bind(input, 1, |input, source| {
input.bind_numeric(source, "alpha map")
})?;
let len = raw.len();
let values = mapper(raw)?;
length("alpha mapper output", values.name(), len, values.len())?;
crate::alpha::expand(values, expected, copies, rows && !native)
}
fn mapped_categories(
capture: &Capture<'_>,
input: impl CategoryInput,
mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
copies: usize,
rows: bool,
expected: Option<usize>,
) -> BindingResult<AlphaValues> {
let native = input.native_slots();
let copies = if native { 1 } else { copies };
let raw = capture.bind(input, 1, |input, source| {
input.bind_category(source, "alpha map")
})?;
let len = raw.len();
let values = mapper(raw)?;
length("alpha mapper output", values.name(), len, values.len())?;
crate::alpha::expand(values, expected, copies, rows && !native)
}
impl<K: kind::Marker> PlotAdapter<'_, K> {
fn column_alpha_keys<I, S, B>(
&self,
capture: &Capture<'_>,
column_key: impl SourceRowsInput,
other_keys: I,
) -> BindingResult<Vec<CategoryValues>>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
let expansion = self
.bindings
.expansion
.as_ref()
.ok_or_else(|| binding("column alpha", "no generated variable dimension"))?;
let mut keys = crate::alpha::keys(capture, other_keys)?;
keys.push(expansion.column_key(capture, self.source.as_deref(), column_key)?);
Ok(keys)
}
}
fn bin_alpha<I, S, B>(
capture: &Capture<'_>,
lower: impl NumericInput,
upper: impl NumericInput,
facets: I,
values: impl AlphaValuesInput,
) -> BindingResult<AlphaEncoding>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
let bounds = Bounds::new(
capture.bind(lower, 1, |input, source| {
input.bind_numeric(source, "bin lower")
})?,
capture.bind(upper, 1, |input, source| {
input.bind_numeric(source, "bin upper")
})?,
);
Ok(AlphaEncoding::bins(
bounds,
crate::alpha::keys(capture, facets)?,
capture.alpha_values(values)?,
)?)
}
macro_rules! alpha_target {
($cap:ident,$base:ident,$cells:ident,$linear:ident,$map:ident,$categories:ident,$cell_map:ident,$cell_categories:ident,
$single:ident,$single_map:ident,$single_categories:ident,$keyed:ident,$keyed_data:ident,$keyed_map:ident,$keyed_map_data:ident,
$keyed_categories:ident,$keyed_categories_data:ident,$columns:ident,$columns_data:ident,$columns_map:ident,$columns_map_data:ident,$columns_categories:ident,$columns_categories_data:ident) => {
impl<K: kind::$cap> PlotAdapter<'_, K> {
pub fn $base(
mut self,
input: impl AlphaInput,
) -> std::result::Result<Self, AdapterError> {
let encoding = self.capture().alpha(
input,
self.alpha_len(),
self.row_copies(),
self.bindings.cell_shape().is_some(),
)?;
self.plot = self.plot.$base(encoding)?;
Ok(self)
}
pub fn $cells(
mut self,
input: impl AlphaInput,
) -> std::result::Result<Self, AdapterError> {
let encoding = self.capture().alpha(input, self.alpha_len(), 1, false)?;
self.plot = self.plot.$base(encoding)?;
Ok(self)
}
pub fn $linear(
self,
input: impl NumericInput,
domain: (f64, f64),
range: (f64, f64),
) -> std::result::Result<Self, AdapterError> {
self.$map(input, |values| AlphaValues::linear(values, domain, range))
}
pub fn $map(
self,
input: impl NumericInput,
mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError> {
let values = mapped_numeric(
&self.capture(),
input,
mapper,
self.row_copies(),
self.bindings.cell_shape().is_some(),
self.alpha_len(),
)?;
self.$base(AlphaEncoding::observations(values))
}
pub fn $categories(
self,
input: impl CategoryInput,
mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError> {
let values = mapped_categories(
&self.capture(),
input,
mapper,
self.row_copies(),
self.bindings.cell_shape().is_some(),
self.alpha_len(),
)?;
self.$base(AlphaEncoding::observations(values))
}
pub fn $cell_map(
self,
input: impl NumericInput,
mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError> {
let values =
mapped_numeric(&self.capture(), input, mapper, 1, false, self.alpha_len())?;
self.$base(AlphaEncoding::observations(values))
}
pub fn $cell_categories(
self,
input: impl CategoryInput,
mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError> {
let values =
mapped_categories(&self.capture(), input, mapper, 1, false, self.alpha_len())?;
self.$base(AlphaEncoding::observations(values))
}
pub fn $single(
self,
input: impl AlphaValuesInput,
) -> std::result::Result<Self, AdapterError> {
let values = self.capture().alpha_values(input)?;
self.$base(AlphaEncoding::single(values)?)
}
pub fn $single_map(
self,
input: impl NumericInput,
mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError> {
let values = mapped_numeric(&self.capture(), input, mapper, 1, false, Some(1))?;
self.$base(AlphaEncoding::single(values)?)
}
pub fn $single_categories(
self,
input: impl CategoryInput,
mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError> {
let values = mapped_categories(&self.capture(), input, mapper, 1, false, Some(1))?;
self.$base(AlphaEncoding::single(values)?)
}
pub fn $keyed<I, S, B>(
self,
keys: I,
input: impl AlphaValuesInput,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
let capture = self.capture();
let keys = crate::alpha::keys(&capture, keys)?;
let values = capture.alpha_values(input)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
pub fn $keyed_data<I, S, B>(
self,
source: impl ArraySource,
keys: I,
input: impl AlphaValuesInput,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
source.validate()?;
let capture = Capture::new(Some(&source));
let keys = crate::alpha::keys(&capture, keys)?;
let values = capture.alpha_values(input)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
pub fn $keyed_map<I, S, B>(
self,
keys: I,
input: impl NumericInput,
mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
let capture = self.capture();
let values = mapped_numeric(&capture, input, mapper, 1, false, None)?;
let keys = crate::alpha::keys(&capture, keys)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
pub fn $keyed_map_data<I, S, B>(
self,
source: impl ArraySource,
keys: I,
input: impl NumericInput,
mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
source.validate()?;
let capture = Capture::new(Some(&source));
let values = mapped_numeric(&capture, input, mapper, 1, false, None)?;
let keys = crate::alpha::keys(&capture, keys)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
pub fn $keyed_categories<I, S, B>(
self,
keys: I,
input: impl CategoryInput,
mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
let capture = self.capture();
let values = mapped_categories(&capture, input, mapper, 1, false, None)?;
let keys = crate::alpha::keys(&capture, keys)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
pub fn $keyed_categories_data<I, S, B>(
self,
source: impl ArraySource,
keys: I,
input: impl CategoryInput,
mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
source.validate()?;
let capture = Capture::new(Some(&source));
let values = mapped_categories(&capture, input, mapper, 1, false, None)?;
let keys = crate::alpha::keys(&capture, keys)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
pub fn $columns<I, S, B>(
self,
column_key: impl SourceRowsInput,
other_keys: I,
values: impl AlphaValuesInput,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
let capture = self.capture();
let keys = self.column_alpha_keys(&capture, column_key, other_keys)?;
let values = capture.alpha_values(values)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
pub fn $columns_data<I, S, B>(
self,
source: impl ArraySource,
column_key: impl SourceRowsInput,
other_keys: I,
values: impl AlphaValuesInput,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
source.validate()?;
let capture = Capture::new(Some(&source));
let keys = self.column_alpha_keys(&capture, column_key, other_keys)?;
let values = capture.alpha_values(values)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
pub fn $columns_map<I, S, B>(
self,
column_key: impl SourceRowsInput,
other_keys: I,
input: impl NumericInput,
mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
let capture = self.capture();
let values = mapped_numeric(&capture, input, mapper, 1, false, None)?;
let keys = self.column_alpha_keys(&capture, column_key, other_keys)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
pub fn $columns_map_data<I, S, B>(
self,
source: impl ArraySource,
column_key: impl SourceRowsInput,
other_keys: I,
input: impl NumericInput,
mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
source.validate()?;
let capture = Capture::new(Some(&source));
let values = mapped_numeric(&capture, input, mapper, 1, false, None)?;
let keys = self.column_alpha_keys(&capture, column_key, other_keys)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
pub fn $columns_categories<I, S, B>(
self,
column_key: impl SourceRowsInput,
other_keys: I,
input: impl CategoryInput,
mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
let capture = self.capture();
let values = mapped_categories(&capture, input, mapper, 1, false, None)?;
let keys = self.column_alpha_keys(&capture, column_key, other_keys)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
pub fn $columns_categories_data<I, S, B>(
self,
source: impl ArraySource,
column_key: impl SourceRowsInput,
other_keys: I,
input: impl CategoryInput,
mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
source.validate()?;
let capture = Capture::new(Some(&source));
let values = mapped_categories(&capture, input, mapper, 1, false, None)?;
let keys = self.column_alpha_keys(&capture, column_key, other_keys)?;
self.$base(AlphaEncoding::keyed(keys, values)?)
}
}
};
}
alpha_target!(
PointAlpha,
point_alpha,
point_alpha_cells,
point_alpha_linear,
point_alpha_map,
point_alpha_map_categories,
point_alpha_cells_map,
point_alpha_cells_map_categories,
point_alpha_single,
point_alpha_single_map,
point_alpha_single_map_categories,
point_alpha_keyed,
point_alpha_keyed_from_data,
point_alpha_keyed_map,
point_alpha_keyed_map_from_data,
point_alpha_keyed_map_categories,
point_alpha_keyed_map_categories_from_data,
point_alpha_columns,
point_alpha_columns_from_data,
point_alpha_columns_map,
point_alpha_columns_map_from_data,
point_alpha_columns_map_categories,
point_alpha_columns_map_categories_from_data
);
alpha_target!(
LineAlpha,
line_alpha,
line_alpha_cells,
line_alpha_linear,
line_alpha_map,
line_alpha_map_categories,
line_alpha_cells_map,
line_alpha_cells_map_categories,
line_alpha_single,
line_alpha_single_map,
line_alpha_single_map_categories,
line_alpha_keyed,
line_alpha_keyed_from_data,
line_alpha_keyed_map,
line_alpha_keyed_map_from_data,
line_alpha_keyed_map_categories,
line_alpha_keyed_map_categories_from_data,
line_alpha_columns,
line_alpha_columns_from_data,
line_alpha_columns_map,
line_alpha_columns_map_from_data,
line_alpha_columns_map_categories,
line_alpha_columns_map_categories_from_data
);
alpha_target!(
FillAlpha,
fill_alpha,
fill_alpha_cells,
fill_alpha_linear,
fill_alpha_map,
fill_alpha_map_categories,
fill_alpha_cells_map,
fill_alpha_cells_map_categories,
fill_alpha_single,
fill_alpha_single_map,
fill_alpha_single_map_categories,
fill_alpha_keyed,
fill_alpha_keyed_from_data,
fill_alpha_keyed_map,
fill_alpha_keyed_map_from_data,
fill_alpha_keyed_map_categories,
fill_alpha_keyed_map_categories_from_data,
fill_alpha_columns,
fill_alpha_columns_from_data,
fill_alpha_columns_map,
fill_alpha_columns_map_from_data,
fill_alpha_columns_map_categories,
fill_alpha_columns_map_categories_from_data
);
impl PlotAdapter<'_, kind::Histogram> {
pub fn fill_alpha_bins<I, S, B>(
self,
lower: impl NumericInput,
upper: impl NumericInput,
facets: I,
values: impl AlphaValuesInput,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
let encoding = bin_alpha(&self.capture(), lower, upper, facets, values)?;
self.fill_alpha(encoding)
}
pub fn fill_alpha_bins_from_data<I, S, B>(
self,
source: impl ArraySource,
lower: impl NumericInput,
upper: impl NumericInput,
facets: I,
values: impl AlphaValuesInput,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
source.validate()?;
let capture = Capture::new(Some(&source));
let encoding = bin_alpha(&capture, lower, upper, facets, values)?;
self.fill_alpha(encoding)
}
}
macro_rules! bin_mapping {
($name:ident,$from:ident,$input:ident,$value:ty,$map:ident) => {
impl PlotAdapter<'_, kind::Histogram> {
pub fn $name<I, S, B>(
self,
lower: impl NumericInput,
upper: impl NumericInput,
facets: I,
input: impl $input,
mapper: impl FnOnce($value) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
let capture = self.capture();
let values = $map(&capture, input, mapper, 1, false, None)?;
let encoding = bin_alpha(&capture, lower, upper, facets, values)?;
self.fill_alpha(encoding)
}
pub fn $from<I, S, B>(
self,
source: impl ArraySource,
lower: impl NumericInput,
upper: impl NumericInput,
facets: I,
input: impl $input,
mapper: impl FnOnce($value) -> crate::Result<AlphaValues>,
) -> std::result::Result<Self, AdapterError>
where
I: IntoIterator<Item = (S, B)>,
S: AsRef<str>,
B: CategoryInput,
{
source.validate()?;
let capture = Capture::new(Some(&source));
let values = $map(&capture, input, mapper, 1, false, None)?;
let encoding = bin_alpha(&capture, lower, upper, facets, values)?;
self.fill_alpha(encoding)
}
}
};
}
bin_mapping!(
fill_alpha_bins_map,
fill_alpha_bins_map_from_data,
NumericInput,
NumericValues,
mapped_numeric
);
bin_mapping!(
fill_alpha_bins_map_categories,
fill_alpha_bins_map_categories_from_data,
CategoryInput,
CategoryValues,
mapped_categories
);