ndhistogram/axis/variablecyclic.rs
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
use crate::error::AxisError;
use super::{Axis, BinInterval, VariableNoFlow};
use std::fmt::{Debug, Display};
use num_traits::Num;
/// A wrap-around axis with variable-sized bins.
///
/// A wrap-around axis with variable-sized bins, constructed from a list of bin
/// edges.
///
/// For floating point types, infinities and NaN do not map to any bin.
///
/// # Examples
/// 1D histogram with cyclic variable sized azimuthal angle binning.
/// ```
/// use ndhistogram::{ndhistogram, Histogram};
/// use ndhistogram::axis::{Axis, BinInterval, VariableCyclic};
/// use std::f64::consts::PI;
/// # fn main() -> Result<(), ndhistogram::Error> {
/// let mut hist = ndhistogram!(VariableCyclic::new(vec![0.0, PI/2.0, PI, 2.0*PI])?; i32);
/// let angle = 0.1;
/// hist.fill(&angle); // fills the first bin
/// hist.fill(&(angle + 2.0*PI)); // wraps around and fills the same first bin
/// assert_eq!(hist.value(&angle), Some(&2));
/// # Ok(()) }
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VariableCyclic<T = f64> {
#[cfg_attr(feature = "serde", serde(flatten))]
axis: VariableNoFlow<T>,
}
impl<T> VariableCyclic<T>
where
T: PartialOrd + Copy,
{
/// Create a wrap-around axis with variable binning given a set of bin edges.
///
/// If fewer than 2 edges are provided, or if the edges cannot be
/// sorted (for example when given NAN) an error is returned.
pub fn new<I: IntoIterator<Item = T>>(bin_edges: I) -> Result<Self, AxisError> {
Ok(Self {
axis: VariableNoFlow::new(bin_edges)?,
})
}
/// Low edge of axis (excluding wrap-around)
#[inline]
pub fn low(&self) -> &T {
self.axis.low()
}
/// High edge of axis (excluding wrap-around)
#[inline]
pub fn high(&self) -> &T {
self.axis.high()
}
}
impl<T> Axis for VariableCyclic<T>
where
T: PartialOrd + Copy + Num,
{
type Coordinate = T;
type BinInterval = BinInterval<T>;
#[inline]
fn index(&self, coordinate: &Self::Coordinate) -> Option<usize> {
let (mut x, hi, lo) = (*coordinate, *self.axis.high(), *self.axis.low());
let range = hi - lo;
x = (x - lo) % range;
if x < T::zero() {
x = range + x;
}
x = x + lo;
self.axis.index(&x)
}
#[inline]
fn num_bins(&self) -> usize {
self.axis.num_bins()
}
#[inline]
fn bin(&self, index: usize) -> Option<Self::BinInterval> {
self.axis.bin(index)
}
}
impl<T> Display for VariableCyclic<T>
where
T: PartialOrd + Copy + Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Axis{{# bins={}, range=[{}, {}), class={}}}",
self.axis.num_bins(),
self.axis.low(),
self.axis.high(),
stringify!(VariableCyclic)
)
}
}
impl<'a, T> IntoIterator for &'a VariableCyclic<T>
where
VariableCyclic<T>: Axis,
{
type Item = (usize, <VariableCyclic<T> as Axis>::BinInterval);
type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}