@@ -34,13 +34,14 @@ use std::ops::Range;
34
34
/// +---+-------+-+
35
35
/// ```
36
36
///
37
- /// # Example:
37
+ /// # Examples
38
38
///
39
39
/// ```
40
40
/// 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
+ /// };
44
45
/// use noisy_float::types::{N64, n64};
45
46
///
46
47
/// // 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> {
84
85
85
86
impl < A : Ord > Grid < A > {
86
87
/// 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
+ /// ```
87
100
pub fn ndim ( & self ) -> usize {
88
101
self . projections . len ( )
89
102
}
90
103
91
104
/// 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
+ /// ```
92
119
pub fn shape ( & self ) -> Vec < usize > {
93
120
self . projections . iter ( ) . map ( |e| e. len ( ) ) . collect ( )
94
121
}
@@ -103,7 +130,49 @@ impl<A: Ord> Grid<A> {
103
130
///
104
131
/// Returns `None` if the point is outside the grid.
105
132
///
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
+ /// ```
107
176
pub fn index_of < S > ( & self , point : & ArrayBase < S , Ix1 > ) -> Option < Vec < usize > >
108
177
where
109
178
S : Data < Elem = A > ,
@@ -129,8 +198,47 @@ impl<A: Ord + Clone> Grid<A> {
129
198
/// `I_{i_0}x...xI_{i_{n-1}}`, an `n`-dimensional bin, where `I_{i_j}` is
130
199
/// the `i_j`-th interval on the `j`-th projection of the grid on the coordinate axes.
131
200
///
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
133
204
/// 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
+ /// ```
134
242
pub fn index ( & self , index : & [ usize ] ) -> Vec < Range < A > > {
135
243
assert_eq ! (
136
244
index. len( ) ,
@@ -146,8 +254,8 @@ impl<A: Ord + Clone> Grid<A> {
146
254
}
147
255
}
148
256
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.
151
259
///
152
260
/// [`Grid`]: struct.Grid.html
153
261
/// [`histogram`]: trait.HistogramExt.html
@@ -165,11 +273,14 @@ where
165
273
/// it returns a `GridBuilder` instance that has learned the required parameter
166
274
/// to build a [`Grid`] according to the specified [`strategy`].
167
275
///
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
169
279
/// the observed data according to the chosen [`strategy`].
170
280
///
171
281
/// [`Grid`]: struct.Grid.html
172
282
/// [`strategy`]: strategies/index.html
283
+ /// [`BinsBuildError`]: errors/enum.BinsBuildError.html
173
284
pub fn from_array < S > ( array : & ArrayBase < S , Ix2 > ) -> Result < Self , BinsBuildError >
174
285
where
175
286
S : Data < Elem = A > ,
0 commit comments