Skip to content

Commit 03c780f

Browse files
committed
histogram/grid.rs: Docs
- Added some doc tests - Minor reformatting
1 parent 66bc977 commit 03c780f

File tree

1 file changed

+120
-9
lines changed

1 file changed

+120
-9
lines changed

src/histogram/grid.rs

Lines changed: 120 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ use std::ops::Range;
3434
/// +---+-------+-+
3535
/// ```
3636
///
37-
/// # Example:
37+
/// # Examples
3838
///
3939
/// ```
4040
/// use ndarray::{Array, array};
41-
/// use ndarray_stats::{HistogramExt,
42-
/// histogram::{Histogram, Grid, GridBuilder,
43-
/// Edges, Bins, strategies::Auto}};
41+
/// use ndarray_stats::{
42+
/// histogram::{strategies::Auto, Bins, Edges, Grid, GridBuilder, Histogram},
43+
/// HistogramExt,
44+
/// };
4445
/// use noisy_float::types::{N64, n64};
4546
///
4647
/// // 1-dimensional observations, as a (n_observations, 1) 2-d matrix
@@ -84,11 +85,37 @@ impl<A: Ord> From<Vec<Bins<A>>> for Grid<A> {
8485

8586
impl<A: Ord> Grid<A> {
8687
/// Returns `n`, the number of dimensions of the region partitioned by the grid.
88+
///
89+
/// # Examples
90+
///
91+
/// ```
92+
/// use ndarray_stats::histogram::{Edges, Bins, Grid};
93+
///
94+
/// let edges = Edges::from(vec![0, 1]);
95+
/// let bins = Bins::new(edges);
96+
/// let square_grid = Grid::from(vec![bins.clone(), bins.clone()]);
97+
///
98+
/// assert_eq!(square_grid.ndim(), 2usize)
99+
/// ```
87100
pub fn ndim(&self) -> usize {
88101
self.projections.len()
89102
}
90103

91104
/// Returns the number of bins along each coordinate axis.
105+
///
106+
/// # Examples
107+
///
108+
/// ```
109+
/// use ndarray_stats::histogram::{Edges, Bins, Grid};
110+
///
111+
/// let edges_x = Edges::from(vec![0, 1]);
112+
/// let edges_y = Edges::from(vec![-1, 0, 1]);
113+
/// let bins_x = Bins::new(edges_x);
114+
/// let bins_y = Bins::new(edges_y);
115+
/// let square_grid = Grid::from(vec![bins_x, bins_y]);
116+
///
117+
/// assert_eq!(square_grid.shape(), vec![1usize, 2usize]);
118+
/// ```
92119
pub fn shape(&self) -> Vec<usize> {
93120
self.projections.iter().map(|e| e.len()).collect()
94121
}
@@ -103,7 +130,49 @@ impl<A: Ord> Grid<A> {
103130
///
104131
/// Returns `None` if the point is outside the grid.
105132
///
106-
/// **Panics** if `point.len()` does not equal `self.ndim()`.
133+
/// # Panics
134+
///
135+
/// Panics if `point.len()` does not equal `self.ndim()`.
136+
///
137+
/// # Examples
138+
///
139+
/// Basic usage:
140+
///
141+
/// ```
142+
/// use ndarray::array;
143+
/// use ndarray_stats::histogram::{Edges, Bins, Grid};
144+
/// use noisy_float::types::n64;
145+
///
146+
/// let edges = Edges::from(vec![n64(-1.), n64(0.), n64(1.)]);
147+
/// let bins = Bins::new(edges);
148+
/// let square_grid = Grid::from(vec![bins.clone(), bins.clone()]);
149+
///
150+
/// // `0.` and `-0.7` falls in 1-st and 0-th bin respectively
151+
/// assert_eq!(
152+
/// square_grid.index_of(&array![n64(0.), n64(-0.7)]),
153+
/// Some(vec![1, 0]),
154+
/// );
155+
/// // `1.` is outside of the grid, return `None`
156+
/// assert_eq!(
157+
/// square_grid.index_of(&array![n64(0.), n64(1.)]),
158+
/// None,
159+
/// );
160+
/// ```
161+
/// A panic upon incompatible `point` length:
162+
///
163+
/// ```should_panic
164+
/// # use ndarray::array;
165+
/// # use ndarray_stats::histogram::{Edges, Bins, Grid};
166+
/// # use noisy_float::types::n64;
167+
/// # let edges = Edges::from(vec![n64(-1.), n64(0.), n64(1.)]);
168+
/// # let bins = Bins::new(edges);
169+
/// # let square_grid = Grid::from(vec![bins.clone(), bins.clone()]);
170+
/// assert_eq!(
171+
/// // the point has 3 dimensions, the grid expected 2 dimensions
172+
/// square_grid.index_of(&array![n64(0.), n64(-0.7), n64(0.5)]),
173+
/// Some(vec![1, 0, 1]),
174+
/// );
175+
/// ```
107176
pub fn index_of<S>(&self, point: &ArrayBase<S, Ix1>) -> Option<Vec<usize>>
108177
where
109178
S: Data<Elem = A>,
@@ -129,8 +198,47 @@ impl<A: Ord + Clone> Grid<A> {
129198
/// `I_{i_0}x...xI_{i_{n-1}}`, an `n`-dimensional bin, where `I_{i_j}` is
130199
/// the `i_j`-th interval on the `j`-th projection of the grid on the coordinate axes.
131200
///
132-
/// **Panics** if at least one among `(i_0, ..., i_{n-1})` is out of bounds on the respective
201+
/// # Panics
202+
///
203+
/// Panics if at least one among `(i_0, ..., i_{n-1})` is out of bounds on the respective
133204
/// coordinate axis - i.e. if there exists `j` such that `i_j >= self.projections[j].len()`.
205+
///
206+
/// # Examples
207+
///
208+
/// Basic usage:
209+
///
210+
/// ```
211+
/// use ndarray::array;
212+
/// use ndarray_stats::histogram::{Edges, Bins, Grid};
213+
///
214+
/// let edges_x = Edges::from(vec![0, 1, 2]);
215+
/// let edges_y = Edges::from(vec![3, 4, 5]);
216+
/// let bins_x = Bins::new(edges_x);
217+
/// let bins_y = Bins::new(edges_y);
218+
/// let square_grid = Grid::from(vec![bins_x, bins_y]);
219+
///
220+
/// assert_eq!(
221+
/// square_grid.index(&[0, 1]),
222+
/// vec![0..1, 4..5],
223+
/// );
224+
/// ```
225+
///
226+
/// A panic upon out-of-bound:
227+
///
228+
/// ```should_panic
229+
/// # use ndarray::array;
230+
/// # use ndarray_stats::histogram::{Edges, Bins, Grid};
231+
/// # let edges_x = Edges::from(vec![0, 1, 2]);
232+
/// # let edges_y = Edges::from(vec![3, 4, 5]);
233+
/// # let bins_x = Bins::new(edges_x);
234+
/// # let bins_y = Bins::new(edges_y);
235+
/// # let square_grid = Grid::from(vec![bins_x, bins_y]);
236+
/// assert_eq!(
237+
/// // out-of-bound on `edges_y`
238+
/// square_grid.index(&[0, 2]),
239+
/// vec![0..1, 1..2],
240+
/// );
241+
/// ```
134242
pub fn index(&self, index: &[usize]) -> Vec<Range<A>> {
135243
assert_eq!(
136244
index.len(),
@@ -146,8 +254,8 @@ impl<A: Ord + Clone> Grid<A> {
146254
}
147255
}
148256

149-
/// `GridBuilder`, given a [`strategy`] and some observations, returns a [`Grid`]
150-
/// instance for [`histogram`] computation.
257+
/// Given a [`strategy`] and some observations, returns a [`Grid`] instance for [`histogram`]
258+
/// computation.
151259
///
152260
/// [`Grid`]: struct.Grid.html
153261
/// [`histogram`]: trait.HistogramExt.html
@@ -165,11 +273,14 @@ where
165273
/// it returns a `GridBuilder` instance that has learned the required parameter
166274
/// to build a [`Grid`] according to the specified [`strategy`].
167275
///
168-
/// It returns `Err` if it is not possible to build a [`Grid`] given
276+
/// # Errors
277+
///
278+
/// It returns [`BinsBuildError`] if it is not possible to build a [`Grid`] given
169279
/// the observed data according to the chosen [`strategy`].
170280
///
171281
/// [`Grid`]: struct.Grid.html
172282
/// [`strategy`]: strategies/index.html
283+
/// [`BinsBuildError`]: errors/enum.BinsBuildError.html
173284
pub fn from_array<S>(array: &ArrayBase<S, Ix2>) -> Result<Self, BinsBuildError>
174285
where
175286
S: Data<Elem = A>,

0 commit comments

Comments
 (0)