lichtung-plot-ndarray 0.1.0

Direct scientific plotting from logical ndarray views and columns
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Caller-computed result variables become reusable native annotation collections.
use crate::error::{binding, length, BindingResult};
use crate::input::sealed;
use crate::{
    AdapterError, ArrayElement, ArraySource, Attached, CategoryElement, CategoryInput, FacetCell,
    NumericElement, NumericInput, Pairwise, Significance, SignificanceMark, SignificanceTarget,
};
use ndarray::ArrayView1;

type Sequence<T> = (Vec<T>, bool);
/// Category variables, category/series pairs or explicitly native target scalars/sequences.
pub trait SignificanceTargets: sealed::Sealed {
    #[doc(hidden)]
    fn bind_targets(
        self,
        source: Option<&dyn ArraySource>,
    ) -> BindingResult<Sequence<SignificanceTarget>>;
}
/// Supplied p-values, typed native marks or explicitly mapped category variables.
pub trait SignificanceMarks: sealed::Sealed {
    #[doc(hidden)]
    fn bind_marks(
        self,
        source: Option<&dyn ArraySource>,
    ) -> BindingResult<Sequence<SignificanceMark>>;
}
/// A category variable, crossed category variables or explicit native facet cells.
pub trait SignificanceFacets: sealed::Sealed {
    #[doc(hidden)]
    fn bind_facets(self, source: Option<&dyn ArraySource>) -> BindingResult<Sequence<FacetCell>>;
}
fn categories(
    input: impl CategoryInput,
    source: Option<&dyn ArraySource>,
    role: &'static str,
) -> BindingResult<Vec<String>> {
    input
        .raw_category(source, role)?
        .1
        .into_iter()
        .enumerate()
        .map(|(slot, value)| {
            value.ok_or_else(|| binding(role, format!("missing category at slot {slot}")))
        })
        .collect()
}
fn p_values(
    input: impl NumericInput,
    source: Option<&dyn ArraySource>,
) -> BindingResult<Sequence<SignificanceMark>> {
    let values = input
        .raw_numeric(source, "significance p-values")?
        .1
        .into_iter()
        .enumerate()
        .map(|(slot, value)| {
            value.map(SignificanceMark::PValue).ok_or_else(|| {
                binding(
                    "significance p-values",
                    format!("missing value at slot {slot}"),
                )
            })
        })
        .collect::<BindingResult<_>>()?;
    Ok((values, false))
}
impl SignificanceTargets for usize {
    fn bind_targets(
        self,
        source: Option<&dyn ArraySource>,
    ) -> BindingResult<Sequence<SignificanceTarget>> {
        Ok((
            categories(self, source, "significance target")?
                .into_iter()
                .map(SignificanceTarget::category)
                .collect(),
            false,
        ))
    }
}
impl SignificanceMarks for usize {
    fn bind_marks(
        self,
        source: Option<&dyn ArraySource>,
    ) -> BindingResult<Sequence<SignificanceMark>> {
        p_values(self, source)
    }
}
impl SignificanceFacets for usize {
    fn bind_facets(self, source: Option<&dyn ArraySource>) -> BindingResult<Sequence<FacetCell>> {
        Ok((
            categories(self, source, "significance facet")?
                .into_iter()
                .map(FacetCell::single)
                .collect(),
            false,
        ))
    }
}
macro_rules! foreign_targets {
    ($ty:ty) => {
        impl<T: ArrayElement + CategoryElement> SignificanceTargets for $ty {
            fn bind_targets(
                self,
                source: Option<&dyn ArraySource>,
            ) -> BindingResult<Sequence<SignificanceTarget>> {
                Ok((
                    categories(self, source, "significance target")?
                        .into_iter()
                        .map(SignificanceTarget::category)
                        .collect(),
                    false,
                ))
            }
        }
        impl<T: ArrayElement + CategoryElement> SignificanceFacets for $ty {
            fn bind_facets(
                self,
                source: Option<&dyn ArraySource>,
            ) -> BindingResult<Sequence<FacetCell>> {
                Ok((
                    categories(self, source, "significance facet")?
                        .into_iter()
                        .map(FacetCell::single)
                        .collect(),
                    false,
                ))
            }
        }
    };
}
foreign_targets!(ArrayView1<'_, T>);
foreign_targets!((ArrayView1<'_, T>, ArrayView1<'_, bool>));
impl<T: ArrayElement + NumericElement> SignificanceMarks for ArrayView1<'_, T> {
    fn bind_marks(
        self,
        source: Option<&dyn ArraySource>,
    ) -> BindingResult<Sequence<SignificanceMark>> {
        p_values(self, source)
    }
}
impl<T: ArrayElement + NumericElement> SignificanceMarks
    for (ArrayView1<'_, T>, ArrayView1<'_, bool>)
{
    fn bind_marks(
        self,
        source: Option<&dyn ArraySource>,
    ) -> BindingResult<Sequence<SignificanceMark>> {
        p_values(self, source)
    }
}

impl<C: CategoryInput, S: CategoryInput> sealed::Sealed for (C, S) {}
impl<C: CategoryInput, S: CategoryInput> SignificanceTargets for (C, S) {
    fn bind_targets(
        self,
        source: Option<&dyn ArraySource>,
    ) -> BindingResult<Sequence<SignificanceTarget>> {
        let category = categories(self.0, source, "significance category")?;
        let series = categories(self.1, source, "significance series")?;
        length(
            "significance series",
            "series",
            category.len(),
            series.len(),
        )?;
        Ok((
            category
                .into_iter()
                .zip(series)
                .map(|(category, series)| SignificanceTarget::category_series(category, series))
                .collect(),
            false,
        ))
    }
}
impl<C: CategoryInput, S: CategoryInput> SignificanceFacets for (C, S) {
    fn bind_facets(self, source: Option<&dyn ArraySource>) -> BindingResult<Sequence<FacetCell>> {
        let row = categories(self.0, source, "significance facet row")?;
        let column = categories(self.1, source, "significance facet column")?;
        length(
            "significance facet column",
            "column",
            row.len(),
            column.len(),
        )?;
        Ok((
            row.into_iter()
                .zip(column)
                .map(|(row, column)| FacetCell::grid(row, column))
                .collect(),
            false,
        ))
    }
}
fn mapped_marks<B, I, S>(
    input: B,
    mapping: I,
    source: Option<&dyn ArraySource>,
) -> BindingResult<Sequence<SignificanceMark>>
where
    B: CategoryInput,
    I: IntoIterator<Item = (S, SignificanceMark)>,
    S: AsRef<str>,
{
    let mapping = mapping
        .into_iter()
        .map(|(key, mark)| (key.as_ref().to_owned(), mark))
        .collect::<Vec<_>>();
    let values = categories(input, source, "significance marks")?
        .into_iter()
        .map(|value| {
            let mut matches = mapping.iter().filter(|(key, _)| key == &value);
            let mark = matches
                .next()
                .ok_or_else(|| binding("significance marks", format!("unmapped key {value:?}")))?
                .1;
            if matches.next().is_some() {
                return Err(binding(
                    "significance marks",
                    format!("duplicate map key {value:?}"),
                ));
            }
            Ok(mark)
        })
        .collect::<BindingResult<_>>()?;
    Ok((values, false))
}
impl<B: CategoryInput, S: AsRef<str>, const N: usize> sealed::Sealed
    for (B, [(S, SignificanceMark); N])
{
}
impl<B: CategoryInput, S: AsRef<str>, const N: usize> SignificanceMarks
    for (B, [(S, SignificanceMark); N])
{
    fn bind_marks(
        self,
        source: Option<&dyn ArraySource>,
    ) -> BindingResult<Sequence<SignificanceMark>> {
        mapped_marks(self.0, self.1, source)
    }
}
impl<B: CategoryInput, S: AsRef<str>> sealed::Sealed for (B, Vec<(S, SignificanceMark)>) {}
impl<B: CategoryInput, S: AsRef<str>> SignificanceMarks for (B, Vec<(S, SignificanceMark)>) {
    fn bind_marks(
        self,
        source: Option<&dyn ArraySource>,
    ) -> BindingResult<Sequence<SignificanceMark>> {
        mapped_marks(self.0, self.1, source)
    }
}
impl<B: CategoryInput, S: AsRef<str>> sealed::Sealed for (B, &[(S, SignificanceMark)]) {}
impl<B: CategoryInput, S: AsRef<str>> SignificanceMarks for (B, &[(S, SignificanceMark)]) {
    fn bind_marks(
        self,
        source: Option<&dyn ArraySource>,
    ) -> BindingResult<Sequence<SignificanceMark>> {
        mapped_marks(
            self.0,
            self.1.iter().map(|(key, mark)| (key, *mark)),
            source,
        )
    }
}

macro_rules! native_sequence {
    ($ty:ty,$trait:ident,$method:ident) => {
        impl sealed::Sealed for $ty {}
        impl $trait for $ty {
            fn $method(self, _: Option<&dyn ArraySource>) -> BindingResult<Sequence<$ty>> {
                Ok((vec![self], true))
            }
        }
        impl sealed::Sealed for Vec<$ty> {}
        impl $trait for Vec<$ty> {
            fn $method(self, _: Option<&dyn ArraySource>) -> BindingResult<Sequence<$ty>> {
                Ok((self, false))
            }
        }
        impl sealed::Sealed for &[$ty] {}
        impl $trait for &[$ty] {
            fn $method(self, _: Option<&dyn ArraySource>) -> BindingResult<Sequence<$ty>> {
                Ok((self.to_vec(), false))
            }
        }
        impl<const N: usize> sealed::Sealed for [$ty; N] {}
        impl<const N: usize> $trait for [$ty; N] {
            fn $method(self, _: Option<&dyn ArraySource>) -> BindingResult<Sequence<$ty>> {
                Ok((self.into(), false))
            }
        }
    };
}
native_sequence!(SignificanceTarget, SignificanceTargets, bind_targets);
native_sequence!(SignificanceMark, SignificanceMarks, bind_marks);
native_sequence!(FacetCell, SignificanceFacets, bind_facets);
fn count(parts: &[(usize, bool)]) -> BindingResult<usize> {
    let len = parts
        .iter()
        .find(|(_, constant)| !constant)
        .map_or(1, |(len, _)| *len);
    for &(actual, constant) in parts {
        if !constant {
            length("significance", "result variable", len, actual)?;
        }
    }
    Ok(len)
}
fn member<T: Clone>(values: &Sequence<T>, row: usize) -> T {
    values.0[if values.1 { 0 } else { row }].clone()
}
fn pairwise(
    source: Option<&dyn ArraySource>,
    left: impl SignificanceTargets,
    right: impl SignificanceTargets,
    marks: impl SignificanceMarks,
    facets: Option<&Sequence<FacetCell>>,
) -> BindingResult<Vec<Significance<Pairwise>>> {
    let left = left.bind_targets(source)?;
    let right = right.bind_targets(source)?;
    let marks = marks.bind_marks(source)?;
    let mut sizes = vec![
        (left.0.len(), left.1),
        (right.0.len(), right.1),
        (marks.0.len(), marks.1),
    ];
    if let Some(facets) = facets {
        sizes.push((facets.0.len(), facets.1));
    }
    let len = count(&sizes)?;
    let mut result = crate::snapshot::allocate(len, "significance")?;
    for row in 0..len {
        let mut value =
            Significance::between(member(&left, row), member(&right, row), member(&marks, row));
        if let Some(facets) = facets {
            value = value.facet(member(facets, row));
        }
        result.push(value);
    }
    Ok(result)
}
fn attached(
    source: Option<&dyn ArraySource>,
    target: impl SignificanceTargets,
    marks: impl SignificanceMarks,
    facets: Option<&Sequence<FacetCell>>,
) -> BindingResult<Vec<Significance<Attached>>> {
    let target = target.bind_targets(source)?;
    let marks = marks.bind_marks(source)?;
    let mut sizes = vec![(target.0.len(), target.1), (marks.0.len(), marks.1)];
    if let Some(facets) = facets {
        sizes.push((facets.0.len(), facets.1));
    }
    let len = count(&sizes)?;
    let mut result = crate::snapshot::allocate(len, "significance")?;
    for row in 0..len {
        let mut value = Significance::at(member(&target, row), member(&marks, row));
        if let Some(facets) = facets {
            value = value.facet(member(facets, row));
        }
        result.push(value);
    }
    Ok(result)
}
#[allow(
    clippy::unnecessary_wraps,
    reason = "uniform fallible facet binding in API macros"
)]
fn no_facet(_: Option<&dyn ArraySource>) -> BindingResult<Option<Sequence<FacetCell>>> {
    Ok(None)
}
fn one_facet(
    source: Option<&dyn ArraySource>,
    facet: impl SignificanceFacets,
) -> BindingResult<Option<Sequence<FacetCell>>> {
    Ok(Some(facet.bind_facets(source)?))
}
fn grid_facet(
    source: Option<&dyn ArraySource>,
    row: impl CategoryInput,
    column: impl CategoryInput,
) -> BindingResult<Option<Sequence<FacetCell>>> {
    Ok(Some((row, column).bind_facets(source)?))
}
macro_rules! pairwise_api {
    ($name:ident,$from:ident,($($arg:ident:$ty:ident),*),$facets:expr) => {
        /// Prepare reusable native pairwise annotations from direct result variables.
        pub fn $name(left:impl SignificanceTargets,right:impl SignificanceTargets,marks:impl SignificanceMarks,$($arg:impl $ty),*)->std::result::Result<Vec<Significance<Pairwise>>,AdapterError> {let source=None;pairwise(source,left,right,marks,($facets)(source,$($arg),*)?.as_ref())}
        /// Prepare reusable native pairwise annotations using an independent array source.
        pub fn $from(source:impl ArraySource,left:impl SignificanceTargets,right:impl SignificanceTargets,marks:impl SignificanceMarks,$($arg:impl $ty),*)->std::result::Result<Vec<Significance<Pairwise>>,AdapterError> {source.validate()?;let source=Some(&source as &dyn ArraySource);pairwise(source,left,right,marks,($facets)(source,$($arg),*)?.as_ref())}
        impl<K:crate::kind::Significance<Pairwise>> crate::PlotAdapter<'_,K> {
            /// Append pairwise results in supplied order, independent of primary observation count.
            pub fn $name(self,left:impl SignificanceTargets,right:impl SignificanceTargets,marks:impl SignificanceMarks,$($arg:impl $ty),*)->std::result::Result<Self,AdapterError> {let source=self.source.as_deref();let values=pairwise(source,left,right,marks,($facets)(source,$($arg),*)?.as_ref())?;Ok(self.significance(values))}
            /// Append pairwise results from an independently bound array source.
            pub fn $from(self,source:impl ArraySource,left:impl SignificanceTargets,right:impl SignificanceTargets,marks:impl SignificanceMarks,$($arg:impl $ty),*)->std::result::Result<Self,AdapterError> {Ok(self.significance($from(source,left,right,marks,$($arg),*)?))}
        }
    };
}
pairwise_api!(
    significance_pairwise,
    significance_pairwise_from_data,
    (),
    no_facet
);
pairwise_api!(significance_pairwise_in_facet,significance_pairwise_in_facet_from_data,(facet:SignificanceFacets),one_facet);
pairwise_api!(significance_pairwise_in_grid,significance_pairwise_in_grid_from_data,(row:CategoryInput,column:CategoryInput),grid_facet);
macro_rules! attached_api {
    ($name:ident,$from:ident,($($arg:ident:$ty:ident),*),$facets:expr) => {
        /// Prepare reusable native attached annotations from direct result variables.
        pub fn $name(target:impl SignificanceTargets,marks:impl SignificanceMarks,$($arg:impl $ty),*)->std::result::Result<Vec<Significance<Attached>>,AdapterError> {let source=None;attached(source,target,marks,($facets)(source,$($arg),*)?.as_ref())}
        /// Prepare reusable native attached annotations using an independent array source.
        pub fn $from(source:impl ArraySource,target:impl SignificanceTargets,marks:impl SignificanceMarks,$($arg:impl $ty),*)->std::result::Result<Vec<Significance<Attached>>,AdapterError> {source.validate()?;let source=Some(&source as &dyn ArraySource);attached(source,target,marks,($facets)(source,$($arg),*)?.as_ref())}
        impl<K:crate::kind::Significance<Attached>> crate::PlotAdapter<'_,K> {
            /// Append attached results in supplied order with their native placement type.
            pub fn $name(self,target:impl SignificanceTargets,marks:impl SignificanceMarks,$($arg:impl $ty),*)->std::result::Result<Self,AdapterError> {let source=self.source.as_deref();let values=attached(source,target,marks,($facets)(source,$($arg),*)?.as_ref())?;Ok(self.significance(values))}
            /// Append attached results from an independently bound array source.
            pub fn $from(self,source:impl ArraySource,target:impl SignificanceTargets,marks:impl SignificanceMarks,$($arg:impl $ty),*)->std::result::Result<Self,AdapterError> {Ok(self.significance($from(source,target,marks,$($arg),*)?))}
        }
    };
}
attached_api!(
    significance_attached,
    significance_attached_from_data,
    (),
    no_facet
);
attached_api!(significance_attached_in_facet,significance_attached_in_facet_from_data,(facet:SignificanceFacets),one_facet);
attached_api!(significance_attached_in_grid,significance_attached_in_grid_from_data,(row:CategoryInput,column:CategoryInput),grid_facet);