Struct ndarray_stats::histogram::Bins

source ·
pub struct Bins<A: Ord> { /* 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_stats::histogram::{Edges, Bins};
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> Bins<A>

source

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.

source

pub fn len(&self) -> usize

Returns the number of bins in self.

§Examples
use ndarray_stats::histogram::{Edges, Bins};
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
);
source

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_stats::histogram::{Edges, Bins};
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);
source

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_stats::histogram::{Edges, Bins};

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)
);
source

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_stats::histogram::{Edges, Bins};

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
);
source

pub fn index(&self, index: usize) -> Range<A>
where A: Clone,

Returns a range as the bin at the given index position.

§Panics

Panics if index is out of bounds.

§Examples
use ndarray_stats::histogram::{Edges, Bins};

let edges = Edges::from(vec![1, 5, 10, 20]);
let bins = Bins::new(edges);
assert_eq!(
    bins.index(1),
    5..10
);

Trait Implementations§

source§

impl<A: Clone + Ord> Clone for Bins<A>

source§

fn clone(&self) -> Bins<A>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<A: Debug + Ord> Debug for Bins<A>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<A: PartialEq + Ord> PartialEq for Bins<A>

source§

fn eq(&self, other: &Bins<A>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<A: Eq + Ord> Eq for Bins<A>

source§

impl<A: Ord> StructuralPartialEq for Bins<A>

Auto Trait Implementations§

§

impl<A> Freeze for Bins<A>

§

impl<A> RefUnwindSafe for Bins<A>
where A: RefUnwindSafe,

§

impl<A> Send for Bins<A>
where A: Send,

§

impl<A> Sync for Bins<A>
where A: Sync,

§

impl<A> Unpin for Bins<A>
where A: Unpin,

§

impl<A> UnwindSafe for Bins<A>
where A: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V