ndhistogram/axis/
variablecyclic.rs

1use crate::error::AxisError;
2
3use super::{Axis, BinInterval, VariableNoFlow};
4use std::fmt::{Debug, Display};
5
6use num_traits::Num;
7
8/// A wrap-around axis with variable-sized bins.
9///
10/// A wrap-around axis with variable-sized bins, constructed from a list of bin
11/// edges.
12///
13/// For floating point types, infinities and NaN do not map to any bin.
14///
15/// # Examples
16/// 1D histogram with cyclic variable sized azimuthal angle binning.
17/// ```
18/// use ndhistogram::{ndhistogram, Histogram};
19/// use ndhistogram::axis::{Axis, BinInterval, VariableCyclic};
20/// use std::f64::consts::PI;
21/// # fn main() -> Result<(), ndhistogram::Error> {
22/// let mut hist = ndhistogram!(VariableCyclic::new(vec![0.0, PI/2.0, PI, 2.0*PI])?; i32);
23/// let angle = 0.1;
24/// hist.fill(&angle); // fills the first bin
25/// hist.fill(&(angle + 2.0*PI)); // wraps around and fills the same first bin
26/// assert_eq!(hist.value(&angle), Some(&2));
27/// # Ok(()) }
28#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
29#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
30pub struct VariableCyclic<T = f64> {
31    #[cfg_attr(feature = "serde", serde(flatten))]
32    axis: VariableNoFlow<T>,
33}
34
35impl<T> VariableCyclic<T>
36where
37    T: PartialOrd + Copy,
38{
39    /// Create a wrap-around axis with variable binning given a set of bin edges.
40    ///
41    /// If fewer than 2 edges are provided, or if the edges cannot be
42    /// sorted (for example when given NAN) an error is returned.
43    pub fn new<I: IntoIterator<Item = T>>(bin_edges: I) -> Result<Self, AxisError> {
44        Ok(Self {
45            axis: VariableNoFlow::new(bin_edges)?,
46        })
47    }
48
49    /// Low edge of axis (excluding wrap-around)
50    #[inline]
51    pub fn low(&self) -> &T {
52        self.axis.low()
53    }
54
55    /// High edge of axis (excluding wrap-around)
56    #[inline]
57    pub fn high(&self) -> &T {
58        self.axis.high()
59    }
60}
61
62impl<T> Axis for VariableCyclic<T>
63where
64    T: PartialOrd + Copy + Num,
65{
66    type Coordinate = T;
67    type BinInterval = BinInterval<T>;
68
69    #[inline]
70    fn index(&self, coordinate: &Self::Coordinate) -> Option<usize> {
71        let (mut x, hi, lo) = (*coordinate, *self.axis.high(), *self.axis.low());
72        let range = hi - lo;
73        x = (x - lo) % range;
74        if x < T::zero() {
75            x = range + x;
76        }
77        x = x + lo;
78        self.axis.index(&x)
79    }
80
81    #[inline]
82    fn num_bins(&self) -> usize {
83        self.axis.num_bins()
84    }
85
86    #[inline]
87    fn bin(&self, index: usize) -> Option<Self::BinInterval> {
88        self.axis.bin(index)
89    }
90}
91
92impl<T> Display for VariableCyclic<T>
93where
94    T: PartialOrd + Copy + Display,
95{
96    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97        write!(
98            f,
99            "Axis{{# bins={}, range=[{}, {}), class={}}}",
100            self.axis.num_bins(),
101            self.axis.low(),
102            self.axis.high(),
103            stringify!(VariableCyclic)
104        )
105    }
106}
107
108impl<'a, T> IntoIterator for &'a VariableCyclic<T>
109where
110    VariableCyclic<T>: Axis,
111{
112    type Item = (usize, <VariableCyclic<T> as Axis>::BinInterval);
113    type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
114    fn into_iter(self) -> Self::IntoIter {
115        self.iter()
116    }
117}