Struct ndarray_histogram::histogram::Bins
source · pub struct Bins<A: Ord + Send> { /* private fields */ }
Expand description
A sorted collection of non-overlapping 1-dimensional intervals.
Note that all intervals are left-closed and right-open.
Examples
use ndarray_histogram::histogram::{Bins, Edges};
use noisy_float::types::n64;
let edges = Edges::from(vec![n64(0.), n64(1.), n64(2.)]);
let bins = Bins::new(edges);
// first bin
assert_eq!(
bins.index(0),
n64(0.)..n64(1.) // n64(1.) is not included in the bin!
);
// second bin
assert_eq!(bins.index(1), n64(1.)..n64(2.));
Implementations§
source§impl<A: Ord + Send> Bins<A>
impl<A: Ord + Send> Bins<A>
sourcepub fn new(edges: Edges<A>) -> Self
pub fn new(edges: Edges<A>) -> Self
Returns a Bins
instance where each bin corresponds to two consecutive members of the given
Edges
, consuming the edges.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of bins in self
.
Examples
use ndarray_histogram::histogram::{Bins, Edges};
use noisy_float::types::n64;
let edges = Edges::from(vec![n64(0.), n64(1.), n64(2.)]);
let bins = Bins::new(edges);
assert_eq!(bins.len(), 2);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the number of bins is zero, i.e. if the number of edges is 0 or 1.
Examples
use ndarray_histogram::histogram::{Bins, Edges};
use noisy_float::types::{n64, N64};
// At least 2 edges is needed to represent 1 interval
let edges = Edges::from(vec![n64(0.), n64(1.), n64(3.)]);
let bins = Bins::new(edges);
assert_eq!(bins.is_empty(), false);
// No valid interval == Empty
let edges = Edges::<N64>::from(vec![]);
let bins = Bins::new(edges);
assert_eq!(bins.is_empty(), true);
let edges = Edges::from(vec![n64(0.)]);
let bins = Bins::new(edges);
assert_eq!(bins.is_empty(), true);
sourcepub fn index_of(&self, value: &A) -> Option<usize>
pub fn index_of(&self, value: &A) -> Option<usize>
Returns the index of the bin in self
that contains the given value
,
or returns None
if value
does not belong to any bins in self
.
Examples
Basic usage:
use ndarray_histogram::histogram::{Bins, Edges};
let edges = Edges::from(vec![0, 2, 4, 6]);
let bins = Bins::new(edges);
let value = 1;
// The first bin [0, 2) contains `1`
assert_eq!(bins.index_of(&1), Some(0));
// No bin contains 100
assert_eq!(bins.index_of(&100), None)
Chaining Bins::index
and Bins::index_of
to get the boundaries of the bin containing
the value:
assert_eq!(
// using `Option::map` to avoid panic on index out-of-bounds
bins.index_of(&1).map(|i| bins.index(i)),
Some(0..2)
);
sourcepub fn range_of(&self, value: &A) -> Option<Range<A>>where
A: Clone,
pub fn range_of(&self, value: &A) -> Option<Range<A>>where A: Clone,
Returns a range as the bin which contains the given value
, or returns None
otherwise.
Examples
use ndarray_histogram::histogram::{Bins, Edges};
let edges = Edges::from(vec![0, 2, 4, 6]);
let bins = Bins::new(edges);
// [0, 2) contains `1`
assert_eq!(bins.range_of(&1), Some(0..2));
// `10` is not in any interval
assert_eq!(bins.range_of(&10), None);