pub trait Axis {
type Coordinate;
type BinInterval;
// Required methods
fn index(&self, coordinate: &Self::Coordinate) -> Option<usize>;
fn num_bins(&self) -> usize;
fn bin(&self, index: usize) -> Option<Self::BinInterval>;
// Provided methods
fn indices(&self) -> Box<dyn Iterator<Item = usize>> { ... }
fn iter(&self) -> Box<dyn Iterator<Item = (usize, Self::BinInterval)> + '_> { ... }
fn bins(&self) -> Box<dyn Iterator<Item = Self::BinInterval> + '_> { ... }
fn num_dim(&self) -> usize { ... }
}
Expand description
An binned axis corresponding to one dimension of an N-dimensional Histogram.
An Axis is composed of a map from some coordinate space to linear bin number, and the inverse map. For examples see:
- Uniform,
- UniformNoFlow,
- UniformCyclic,
- Variable,
- VariableNoFlow,
- VariableCyclic,
- Category
- and CategoryNoFlow.
Most use cases should be covered by the builtin Axis implementations. However, you may implement the Axis trait if you have specialist needs.
§Examples
§Parity Axis
Imagine we wanted an 2-bin axis where even values where mapped to one bin and odd values to another bin. We could implement this with the following:
use ndhistogram::axis::Axis;
use ndhistogram::{ndhistogram, Histogram};
enum Parity {
Even,
Odd
}
struct ParityAxis {}
impl Axis for ParityAxis {
type Coordinate = i32;
type BinInterval = Parity;
fn index(&self, coordinate: &Self::Coordinate) -> Option<usize> {
if coordinate % 2 == 0 { Some(0) } else { Some(1) }
}
fn num_bins(&self) -> usize {
2
}
fn bin(&self, index: usize) -> Option<Self::BinInterval> {
if index == 0 { Some(Parity::Even) } else { Some(Parity::Odd) }
}
}
let mut hist = ndhistogram!(ParityAxis{}; i32);
hist.fill(&1);
hist.fill_with(&2, 4);
assert_eq!(hist.value(&1), Some(&1));
assert_eq!(hist.value(&2), Some(&4));
Required Associated Types§
Sourcetype Coordinate
type Coordinate
The type representing a location on this axis.
Sourcetype BinInterval
type BinInterval
The type of an interval representing the set of Coordinates that correspond to a histogram bin
Required Methods§
Sourcefn index(&self, coordinate: &Self::Coordinate) -> Option<usize>
fn index(&self, coordinate: &Self::Coordinate) -> Option<usize>
Map from coordinate to bin number. Returns an option as not all valid coordinates are necessarily contained within a bin.
Sourcefn bin(&self, index: usize) -> Option<Self::BinInterval>
fn bin(&self, index: usize) -> Option<Self::BinInterval>
Map from bin number to axis to the interval covering the range of coordinates that this bin contains. Returns an option in case an index >= Axis::num_bins is given.
Provided Methods§
Sourcefn iter(&self) -> Box<dyn Iterator<Item = (usize, Self::BinInterval)> + '_>
fn iter(&self) -> Box<dyn Iterator<Item = (usize, Self::BinInterval)> + '_>
An iterator over bin numbers and bin intervals
Sourcefn bins(&self) -> Box<dyn Iterator<Item = Self::BinInterval> + '_>
fn bins(&self) -> Box<dyn Iterator<Item = Self::BinInterval> + '_>
An iterator over bin intervals.