diff --git a/.travis.yml b/.travis.yml index 01ee30e3..6e1a620a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ addons: - libssl-dev cache: cargo rust: - - 1.31.0 + - 1.34.0 - stable - beta - nightly diff --git a/Cargo.toml b/Cargo.toml index 505419d4..c3729d5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "ndarray-stats" version = "0.1.0" authors = ["Jim Turner ", "LukeMathWalker "] +edition = "2018" license = "MIT/Apache-2.0" diff --git a/benches/sort.rs b/benches/sort.rs index 2995e9ff..03bce688 100644 --- a/benches/sort.rs +++ b/benches/sort.rs @@ -1,8 +1,3 @@ -extern crate criterion; -extern crate ndarray; -extern crate ndarray_stats; -extern crate rand; - use criterion::{ black_box, criterion_group, criterion_main, AxisScale, BatchSize, Criterion, ParameterizedBenchmark, PlotConfiguration, diff --git a/src/correlation.rs b/src/correlation.rs index 254d7b9d..9985ad87 100644 --- a/src/correlation.rs +++ b/src/correlation.rs @@ -49,8 +49,6 @@ where /// # Example /// /// ``` - /// extern crate ndarray; - /// extern crate ndarray_stats; /// use ndarray::{aview2, arr2}; /// use ndarray_stats::CorrelationExt; /// @@ -98,8 +96,6 @@ where /// /// variables is zero and division by zero panics for type A. /// ``` - /// extern crate ndarray; - /// extern crate ndarray_stats; /// use ndarray::arr2; /// use ndarray_stats::CorrelationExt; /// @@ -175,31 +171,31 @@ mod cov_tests { use super::*; use ndarray::array; use ndarray_rand::RandomExt; - use quickcheck::quickcheck; + use quickcheck_macros::quickcheck; use rand; use rand::distributions::Uniform; - quickcheck! { - fn constant_random_variables_have_zero_covariance_matrix(value: f64) -> bool { - let n_random_variables = 3; - let n_observations = 4; - let a = Array::from_elem((n_random_variables, n_observations), value); - a.cov(1.).all_close( - &Array::zeros((n_random_variables, n_random_variables)), - 1e-8 - ) - } + #[quickcheck] + fn constant_random_variables_have_zero_covariance_matrix(value: f64) -> bool { + let n_random_variables = 3; + let n_observations = 4; + let a = Array::from_elem((n_random_variables, n_observations), value); + a.cov(1.).all_close( + &Array::zeros((n_random_variables, n_random_variables)), + 1e-8, + ) + } - fn covariance_matrix_is_symmetric(bound: f64) -> bool { - let n_random_variables = 3; - let n_observations = 4; - let a = Array::random( - (n_random_variables, n_observations), - Uniform::new(-bound.abs(), bound.abs()) - ); - let covariance = a.cov(1.); - covariance.all_close(&covariance.t(), 1e-8) - } + #[quickcheck] + fn covariance_matrix_is_symmetric(bound: f64) -> bool { + let n_random_variables = 3; + let n_observations = 4; + let a = Array::random( + (n_random_variables, n_observations), + Uniform::new(-bound.abs(), bound.abs()), + ); + let covariance = a.cov(1.); + covariance.all_close(&covariance.t(), 1e-8) } #[test] @@ -277,28 +273,31 @@ mod pearson_correlation_tests { use super::*; use ndarray::array; use ndarray_rand::RandomExt; - use quickcheck::quickcheck; + use quickcheck_macros::quickcheck; use rand::distributions::Uniform; - quickcheck! { - fn output_matrix_is_symmetric(bound: f64) -> bool { - let n_random_variables = 3; - let n_observations = 4; - let a = Array::random( - (n_random_variables, n_observations), - Uniform::new(-bound.abs(), bound.abs()) - ); - let pearson_correlation = a.pearson_correlation(); - pearson_correlation.all_close(&pearson_correlation.t(), 1e-8) - } + #[quickcheck] + fn output_matrix_is_symmetric(bound: f64) -> bool { + let n_random_variables = 3; + let n_observations = 4; + let a = Array::random( + (n_random_variables, n_observations), + Uniform::new(-bound.abs(), bound.abs()), + ); + let pearson_correlation = a.pearson_correlation(); + pearson_correlation.all_close(&pearson_correlation.t(), 1e-8) + } - fn constant_random_variables_have_nan_correlation(value: f64) -> bool { - let n_random_variables = 3; - let n_observations = 4; - let a = Array::from_elem((n_random_variables, n_observations), value); - let pearson_correlation = a.pearson_correlation(); - pearson_correlation.iter().map(|x| x.is_nan()).fold(true, |acc, flag| acc & flag) - } + #[quickcheck] + fn constant_random_variables_have_nan_correlation(value: f64) -> bool { + let n_random_variables = 3; + let n_observations = 4; + let a = Array::from_elem((n_random_variables, n_observations), value); + let pearson_correlation = a.pearson_correlation(); + pearson_correlation + .iter() + .map(|x| x.is_nan()) + .fold(true, |acc, flag| acc & flag) } #[test] diff --git a/src/entropy.rs b/src/entropy.rs index e32d3d43..77e9d78f 100644 --- a/src/entropy.rs +++ b/src/entropy.rs @@ -221,8 +221,8 @@ where #[cfg(test)] mod tests { use super::EntropyExt; + use crate::errors::{EmptyInput, MultiInputError}; use approx::assert_abs_diff_eq; - use errors::{EmptyInput, MultiInputError}; use ndarray::{array, Array1}; use noisy_float::types::n64; use std::f64; diff --git a/src/errors.rs b/src/errors.rs index e2ee6965..2386a301 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -8,7 +8,7 @@ use std::fmt; pub struct EmptyInput; impl fmt::Display for EmptyInput { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Empty input.") } } @@ -25,7 +25,7 @@ pub enum MinMaxError { } impl fmt::Display for MinMaxError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { MinMaxError::EmptyInput => write!(f, "Empty input."), MinMaxError::UndefinedOrder => { @@ -53,7 +53,7 @@ pub struct ShapeMismatch { } impl fmt::Display for ShapeMismatch { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "Array shapes do not match: {:?} and {:?}.", @@ -92,7 +92,7 @@ impl MultiInputError { } impl fmt::Display for MultiInputError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { MultiInputError::EmptyInput => write!(f, "Empty input."), MultiInputError::ShapeMismatch(e) => write!(f, "Shape mismatch: {}", e), @@ -124,7 +124,7 @@ pub enum QuantileError { } impl fmt::Display for QuantileError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { QuantileError::EmptyInput => write!(f, "Empty input."), QuantileError::InvalidQuantile(q) => { diff --git a/src/histogram/bins.rs b/src/histogram/bins.rs index 3a83eae9..887f3e04 100644 --- a/src/histogram/bins.rs +++ b/src/histogram/bins.rs @@ -9,8 +9,6 @@ use std::ops::{Index, Range}; /// # Example: /// /// ``` -/// extern crate ndarray_stats; -/// extern crate noisy_float; /// use ndarray_stats::histogram::{Edges, Bins}; /// use noisy_float::types::n64; /// @@ -41,19 +39,15 @@ impl From> for Edges { /// # Example: /// /// ``` - /// extern crate ndarray_stats; - /// #[macro_use(array)] - /// extern crate ndarray; + /// use ndarray::array; /// use ndarray_stats::histogram::Edges; /// - /// # fn main() { /// let edges = Edges::from(array![1, 15, 10, 10, 20]); /// // The array gets sorted! /// assert_eq!( /// edges[2], /// 15 /// ); - /// # } /// ``` fn from(mut edges: Vec) -> Self { // sort the array in-place @@ -72,7 +66,6 @@ impl From> for Edges { /// # Example: /// /// ``` - /// extern crate ndarray_stats; /// use ndarray_stats::histogram::Edges; /// /// let edges = Edges::from(vec![1, 15, 10, 20]); @@ -98,7 +91,6 @@ impl Index for Edges { /// # Example: /// /// ``` - /// extern crate ndarray_stats; /// use ndarray_stats::histogram::Edges; /// /// let edges = Edges::from(vec![1, 5, 10, 20]); @@ -118,8 +110,6 @@ impl Edges { /// # Example: /// /// ``` - /// extern crate ndarray_stats; - /// extern crate noisy_float; /// use ndarray_stats::histogram::Edges; /// use noisy_float::types::n64; /// @@ -139,8 +129,6 @@ impl Edges { /// # Example: /// /// ``` - /// extern crate ndarray; - /// extern crate ndarray_stats; /// use ndarray::array; /// use ndarray_stats::histogram::Edges; /// @@ -150,7 +138,7 @@ impl Edges { /// array![0, 3, 5].view() /// ); /// ``` - pub fn as_array_view(&self) -> ArrayView1 { + pub fn as_array_view(&self) -> ArrayView1<'_, A> { ArrayView1::from(&self.edges) } @@ -162,7 +150,6 @@ impl Edges { /// # Example: /// /// ``` - /// extern crate ndarray_stats; /// use ndarray_stats::histogram::Edges; /// /// let edges = Edges::from(vec![0, 2, 3]); @@ -202,8 +189,6 @@ impl Edges { /// # Example: /// /// ``` -/// extern crate ndarray_stats; -/// extern crate noisy_float; /// use ndarray_stats::histogram::{Edges, Bins}; /// use noisy_float::types::n64; /// @@ -238,8 +223,6 @@ impl Bins { /// # Example: /// /// ``` - /// extern crate ndarray_stats; - /// extern crate noisy_float; /// use ndarray_stats::histogram::{Edges, Bins}; /// use noisy_float::types::n64; /// @@ -264,7 +247,6 @@ impl Bins { /// # Example: /// /// ``` - /// extern crate ndarray_stats; /// use ndarray_stats::histogram::{Edges, Bins}; /// /// let edges = Edges::from(vec![0, 2, 4, 6]); @@ -291,7 +273,6 @@ impl Bins { /// # Example: /// /// ``` - /// extern crate ndarray_stats; /// use ndarray_stats::histogram::{Edges, Bins}; /// /// let edges = Edges::from(vec![0, 2, 4, 6]); @@ -323,7 +304,6 @@ impl Bins { /// # Example: /// /// ``` - /// extern crate ndarray_stats; /// use ndarray_stats::histogram::{Edges, Bins}; /// /// let edges = Edges::from(vec![1, 5, 10, 20]); @@ -351,68 +331,71 @@ impl Bins { #[cfg(test)] mod edges_tests { use super::*; - use quickcheck::quickcheck; + use quickcheck_macros::quickcheck; use std::collections::BTreeSet; use std::iter::FromIterator; - quickcheck! { - fn check_sorted_from_vec(v: Vec) -> bool { - let edges = Edges::from(v); - let n = edges.len(); - for i in 1..n { - if edges[i-1] > edges[i] { - return false; - } + #[quickcheck] + fn check_sorted_from_vec(v: Vec) -> bool { + let edges = Edges::from(v); + let n = edges.len(); + for i in 1..n { + if edges[i - 1] > edges[i] { + return false; } - true } + true + } - fn check_sorted_from_array(v: Vec) -> bool { - let a = Array1::from_vec(v); - let edges = Edges::from(a); - let n = edges.len(); - for i in 1..n { - if edges[i-1] > edges[i] { - return false; - } + #[quickcheck] + fn check_sorted_from_array(v: Vec) -> bool { + let a = Array1::from_vec(v); + let edges = Edges::from(a); + let n = edges.len(); + for i in 1..n { + if edges[i - 1] > edges[i] { + return false; } - true } + true + } - fn edges_are_right_exclusive(v: Vec) -> bool { - let edges = Edges::from(v); - let view = edges.as_array_view(); - if view.len() == 0 { - true - } else { - let last = view[view.len()-1]; - edges.indices_of(&last).is_none() - } + #[quickcheck] + fn edges_are_right_exclusive(v: Vec) -> bool { + let edges = Edges::from(v); + let view = edges.as_array_view(); + if view.len() == 0 { + true + } else { + let last = view[view.len() - 1]; + edges.indices_of(&last).is_none() } + } - fn edges_are_left_inclusive(v: Vec) -> bool { - let edges = Edges::from(v); - match edges.len() { - 1 => true, - _ => { - let view = edges.as_array_view(); - if view.len() == 0 { - true - } else { - let first = view[0]; - edges.indices_of(&first).is_some() - } + #[quickcheck] + fn edges_are_left_inclusive(v: Vec) -> bool { + let edges = Edges::from(v); + match edges.len() { + 1 => true, + _ => { + let view = edges.as_array_view(); + if view.len() == 0 { + true + } else { + let first = view[0]; + edges.indices_of(&first).is_some() } } } + } - fn edges_are_deduped(v: Vec) -> bool { - let unique_elements = BTreeSet::from_iter(v.iter()); - let edges = Edges::from(v.clone()); - let view = edges.as_array_view(); - let unique_edges = BTreeSet::from_iter(view.iter()); - unique_edges == unique_elements - } + #[quickcheck] + fn edges_are_deduped(v: Vec) -> bool { + let unique_elements = BTreeSet::from_iter(v.iter()); + let edges = Edges::from(v.clone()); + let view = edges.as_array_view(); + let unique_edges = BTreeSet::from_iter(view.iter()); + unique_edges == unique_elements } } diff --git a/src/histogram/errors.rs b/src/histogram/errors.rs index 8deb6218..144b7233 100644 --- a/src/histogram/errors.rs +++ b/src/histogram/errors.rs @@ -7,7 +7,7 @@ use std::fmt; pub struct BinNotFound; impl fmt::Display for BinNotFound { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "No bin has been found.") } } @@ -48,7 +48,7 @@ impl BinsBuildError { } impl fmt::Display for BinsBuildError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "The strategy failed to determine a non-zero bin width.") } } diff --git a/src/histogram/grid.rs b/src/histogram/grid.rs index 91caab03..66d8e9a9 100644 --- a/src/histogram/grid.rs +++ b/src/histogram/grid.rs @@ -37,16 +37,12 @@ use std::ops::Range; /// # Example: /// /// ``` -/// extern crate ndarray_stats; -/// extern crate ndarray; -/// extern crate noisy_float; /// use ndarray::{Array, array}; /// use ndarray_stats::{HistogramExt, /// histogram::{Histogram, Grid, GridBuilder, /// Edges, Bins, strategies::Auto}}; /// use noisy_float::types::{N64, n64}; /// -/// # fn main() { /// // 1-dimensional observations, as a (n_observations, 1) 2-d matrix /// let observations = Array::from_shape_vec( /// (12, 1), @@ -65,7 +61,6 @@ use std::ops::Range; /// // Bins are left inclusive, right exclusive! /// let expected = array![4, 3, 3, 1, 0, 1]; /// assert_eq!(histogram_matrix, expected.into_dyn()); -/// # } /// ``` #[derive(Clone, Debug, Eq, PartialEq)] pub struct Grid { diff --git a/src/histogram/histograms.rs b/src/histogram/histograms.rs index 2cd367ce..603a5019 100644 --- a/src/histogram/histograms.rs +++ b/src/histogram/histograms.rs @@ -24,14 +24,10 @@ impl Histogram { /// /// # Example: /// ``` - /// extern crate ndarray_stats; - /// extern crate ndarray; - /// extern crate noisy_float; /// use ndarray::array; /// use ndarray_stats::histogram::{Edges, Bins, Histogram, Grid}; /// use noisy_float::types::n64; /// - /// # fn main() -> Result<(), Box> { /// let edges = Edges::from(vec![n64(-1.), n64(0.), n64(1.)]); /// let bins = Bins::new(edges); /// let square_grid = Grid::from(vec![bins.clone(), bins.clone()]); @@ -47,8 +43,7 @@ impl Histogram { /// [0, 1], /// ]; /// assert_eq!(histogram_matrix, expected.into_dyn()); - /// # Ok(()) - /// # } + /// # Ok::<(), Box>(()) /// ``` pub fn add_observation(&mut self, observation: &ArrayBase) -> Result<(), BinNotFound> where @@ -70,7 +65,7 @@ impl Histogram { } /// Borrows a view on the histogram counts matrix. - pub fn counts(&self) -> ArrayViewD { + pub fn counts(&self) -> ArrayViewD<'_, usize> { self.counts.view() } @@ -103,10 +98,7 @@ where /// # Example: /// /// ``` - /// extern crate ndarray_stats; - /// #[macro_use(array)] - /// extern crate ndarray; - /// extern crate noisy_float; + /// use ndarray::array; /// use ndarray_stats::{ /// HistogramExt, /// histogram::{ @@ -116,7 +108,6 @@ where /// }; /// use noisy_float::types::{N64, n64}; /// - /// # fn main() { /// let observations = array![ /// [n64(1.), n64(0.5)], /// [n64(-0.5), n64(1.)], @@ -142,7 +133,6 @@ where /// [0, 1, 0], /// ]; /// assert_eq!(histogram_matrix, expected.into_dyn()); - /// # } /// ``` fn histogram(&self, grid: Grid) -> Histogram where diff --git a/src/lib.rs b/src/lib.rs index 09ade038..c41dc1ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,28 +25,13 @@ //! [`NumPy`]: https://docs.scipy.org/doc/numpy-1.14.1/reference/routines.statistics.html //! [`StatsBase.jl`]: https://juliastats.github.io/StatsBase.jl/latest/ -extern crate indexmap; -extern crate itertools; -extern crate ndarray; -extern crate noisy_float; -extern crate num_integer; -extern crate num_traits; -extern crate rand; - -#[cfg(test)] -extern crate approx; -#[cfg(test)] -extern crate ndarray_rand; -#[cfg(test)] -extern crate quickcheck; - -pub use correlation::CorrelationExt; -pub use entropy::EntropyExt; -pub use histogram::HistogramExt; -pub use maybe_nan::{MaybeNan, MaybeNanExt}; -pub use quantile::{interpolate, Quantile1dExt, QuantileExt}; -pub use sort::Sort1dExt; -pub use summary_statistics::SummaryStatisticsExt; +pub use crate::correlation::CorrelationExt; +pub use crate::entropy::EntropyExt; +pub use crate::histogram::HistogramExt; +pub use crate::maybe_nan::{MaybeNan, MaybeNanExt}; +pub use crate::quantile::{interpolate, Quantile1dExt, QuantileExt}; +pub use crate::sort::Sort1dExt; +pub use crate::summary_statistics::SummaryStatisticsExt; #[macro_use] mod private { diff --git a/src/maybe_nan/impl_not_none.rs b/src/maybe_nan/impl_not_none.rs index 658aea40..e8c2755b 100644 --- a/src/maybe_nan/impl_not_none.rs +++ b/src/maybe_nan/impl_not_none.rs @@ -24,7 +24,7 @@ impl DerefMut for NotNone { } impl fmt::Display for NotNone { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { self.deref().fmt(f) } } diff --git a/src/maybe_nan/mod.rs b/src/maybe_nan/mod.rs index eba3b2f5..8b42b73a 100644 --- a/src/maybe_nan/mod.rs +++ b/src/maybe_nan/mod.rs @@ -18,17 +18,17 @@ pub trait MaybeNan: Sized { /// Converts the value. /// /// If the value is `None`, a NaN value is returned. - fn from_not_nan(Self::NotNan) -> Self; + fn from_not_nan(_: Self::NotNan) -> Self; /// Converts the value. /// /// If the value is `None`, a NaN value is returned. - fn from_not_nan_opt(Option) -> Self; + fn from_not_nan_opt(_: Option) -> Self; /// Converts the value. /// /// If the value is `None`, a NaN value is returned. - fn from_not_nan_ref_opt(Option<&Self::NotNan>) -> &Self; + fn from_not_nan_ref_opt(_: Option<&Self::NotNan>) -> &Self; /// Returns a view with the NaN values removed. /// @@ -36,13 +36,13 @@ pub trait MaybeNan: Sized { /// order of the elements is unspecified. However, this method is /// idempotent, and given the same input data, the result is always ordered /// the same way. - fn remove_nan_mut(ArrayViewMut1) -> ArrayViewMut1; + fn remove_nan_mut(_: ArrayViewMut1<'_, Self>) -> ArrayViewMut1<'_, Self::NotNan>; } /// Returns a view with the NaN values removed. /// /// This modifies the input view by moving elements as necessary. -fn remove_nan_mut(mut view: ArrayViewMut1) -> ArrayViewMut1 { +fn remove_nan_mut(mut view: ArrayViewMut1<'_, A>) -> ArrayViewMut1<'_, A> { if view.len() == 0 { return view.slice_move(s![..0]); } @@ -100,7 +100,7 @@ macro_rules! impl_maybenan_for_fxx { } } - fn remove_nan_mut(view: ArrayViewMut1<$fxx>) -> ArrayViewMut1<$Nxx> { + fn remove_nan_mut(view: ArrayViewMut1<'_, $fxx>) -> ArrayViewMut1<'_, $Nxx> { let not_nan = remove_nan_mut(view); // This is safe because `remove_nan_mut` has removed the NaN // values, and `$Nxx` is a thin wrapper around `$fxx`. @@ -150,7 +150,7 @@ macro_rules! impl_maybenan_for_opt_never_nan { } } - fn remove_nan_mut(view: ArrayViewMut1) -> ArrayViewMut1 { + fn remove_nan_mut(view: ArrayViewMut1<'_, Self>) -> ArrayViewMut1<'_, Self::NotNan> { let not_nan = remove_nan_mut(view); // This is safe because `remove_nan_mut` has removed the `None` // values, and `NotNone<$ty>` is a thin wrapper around `Option<$ty>`. @@ -374,37 +374,38 @@ where #[cfg(test)] mod tests { use super::*; - use quickcheck::quickcheck; - - quickcheck! { - fn remove_nan_mut_idempotent(is_nan: Vec) -> bool { - let mut values: Vec<_> = is_nan - .into_iter() - .map(|is_nan| if is_nan { None } else { Some(1) }) - .collect(); - let view = ArrayViewMut1::from_shape(values.len(), &mut values).unwrap(); - let removed = remove_nan_mut(view); - removed == remove_nan_mut(removed.to_owned().view_mut()) - } + use quickcheck_macros::quickcheck; + + #[quickcheck] + fn remove_nan_mut_idempotent(is_nan: Vec) -> bool { + let mut values: Vec<_> = is_nan + .into_iter() + .map(|is_nan| if is_nan { None } else { Some(1) }) + .collect(); + let view = ArrayViewMut1::from_shape(values.len(), &mut values).unwrap(); + let removed = remove_nan_mut(view); + removed == remove_nan_mut(removed.to_owned().view_mut()) + } - fn remove_nan_mut_only_nan_remaining(is_nan: Vec) -> bool { - let mut values: Vec<_> = is_nan - .into_iter() - .map(|is_nan| if is_nan { None } else { Some(1) }) - .collect(); - let view = ArrayViewMut1::from_shape(values.len(), &mut values).unwrap(); - remove_nan_mut(view).iter().all(|elem| !elem.is_nan()) - } + #[quickcheck] + fn remove_nan_mut_only_nan_remaining(is_nan: Vec) -> bool { + let mut values: Vec<_> = is_nan + .into_iter() + .map(|is_nan| if is_nan { None } else { Some(1) }) + .collect(); + let view = ArrayViewMut1::from_shape(values.len(), &mut values).unwrap(); + remove_nan_mut(view).iter().all(|elem| !elem.is_nan()) + } - fn remove_nan_mut_keep_all_non_nan(is_nan: Vec) -> bool { - let non_nan_count = is_nan.iter().filter(|&&is_nan| !is_nan).count(); - let mut values: Vec<_> = is_nan - .into_iter() - .map(|is_nan| if is_nan { None } else { Some(1) }) - .collect(); - let view = ArrayViewMut1::from_shape(values.len(), &mut values).unwrap(); - remove_nan_mut(view).len() == non_nan_count - } + #[quickcheck] + fn remove_nan_mut_keep_all_non_nan(is_nan: Vec) -> bool { + let non_nan_count = is_nan.iter().filter(|&&is_nan| !is_nan).count(); + let mut values: Vec<_> = is_nan + .into_iter() + .map(|is_nan| if is_nan { None } else { Some(1) }) + .collect(); + let view = ArrayViewMut1::from_shape(values.len(), &mut values).unwrap(); + remove_nan_mut(view).len() == non_nan_count } } diff --git a/src/quantile/mod.rs b/src/quantile/mod.rs index e264dac5..ba3f5356 100644 --- a/src/quantile/mod.rs +++ b/src/quantile/mod.rs @@ -1,12 +1,12 @@ use self::interpolate::{higher_index, lower_index, Interpolate}; use super::sort::get_many_from_sorted_mut_unchecked; +use crate::errors::QuantileError; use crate::errors::{EmptyInput, MinMaxError, MinMaxError::UndefinedOrder}; -use errors::QuantileError; +use crate::{MaybeNan, MaybeNanExt}; use ndarray::prelude::*; use ndarray::{Data, DataMut, RemoveAxis, Zip}; use noisy_float::types::N64; use std::cmp; -use {MaybeNan, MaybeNanExt}; /// Quantile methods for `ArrayBase`. pub trait QuantileExt @@ -29,9 +29,6 @@ where /// # Example /// /// ``` - /// extern crate ndarray; - /// extern crate ndarray_stats; - /// /// use ndarray::array; /// use ndarray_stats::QuantileExt; /// @@ -55,9 +52,6 @@ where /// # Example /// /// ``` - /// extern crate ndarray; - /// extern crate ndarray_stats; - /// /// use ndarray::array; /// use ndarray_stats::QuantileExt; /// @@ -114,9 +108,6 @@ where /// # Example /// /// ``` - /// extern crate ndarray; - /// extern crate ndarray_stats; - /// /// use ndarray::array; /// use ndarray_stats::QuantileExt; /// @@ -140,9 +131,6 @@ where /// # Example /// /// ``` - /// extern crate ndarray; - /// extern crate ndarray_stats; - /// /// use ndarray::array; /// use ndarray_stats::QuantileExt; /// @@ -249,15 +237,10 @@ where /// # Example /// /// ```rust - /// # extern crate ndarray; - /// # extern crate ndarray_stats; - /// # extern crate noisy_float; - /// # /// use ndarray::{array, aview1, Axis}; /// use ndarray_stats::{QuantileExt, interpolate::Nearest}; /// use noisy_float::types::n64; /// - /// # fn main() { /// let mut data = array![[3, 4, 5], [6, 7, 8]]; /// let axis = Axis(1); /// let qs = &[n64(0.3), n64(0.7)]; @@ -265,7 +248,6 @@ where /// for (&q, quantile) in qs.iter().zip(quantiles.axis_iter(axis)) { /// assert_eq!(quantile, data.quantile_axis_mut(axis, q, &Nearest).unwrap()); /// } - /// # } /// ``` fn quantiles_axis_mut( &mut self, @@ -453,9 +435,9 @@ where { // Minimize number of type parameters to avoid monomorphization bloat. fn quantiles_axis_mut( - mut data: ArrayViewMut, + mut data: ArrayViewMut<'_, A, D>, axis: Axis, - qs: ArrayView1, + qs: ArrayView1<'_, N64>, _interpolate: &I, ) -> Result, QuantileError> where diff --git a/src/sort.rs b/src/sort.rs index 4f4bfc41..56db3715 100644 --- a/src/sort.rs +++ b/src/sort.rs @@ -74,13 +74,9 @@ where /// # Example /// /// ``` - /// extern crate ndarray; - /// extern crate ndarray_stats; - /// /// use ndarray::array; /// use ndarray_stats::Sort1dExt; /// - /// # fn main() { /// let mut data = array![3, 1, 4, 5, 2]; /// let pivot_index = 2; /// let pivot_value = data[pivot_index]; @@ -97,7 +93,6 @@ where /// for i in (new_index + 1)..data.len() { /// assert!(data[i] >= pivot_value); /// } - /// # } /// ``` fn partition_mut(&mut self, pivot_index: usize) -> usize where @@ -230,7 +225,7 @@ where /// `values` is a pre-allocated slice to use for writing the output. Its /// initial element values are ignored. fn _get_many_from_sorted_mut_unchecked( - mut array: ArrayViewMut1, + mut array: ArrayViewMut1<'_, A>, indexes: &mut [usize], values: &mut [A], ) where diff --git a/tests/quantile.rs b/tests/quantile.rs index 7fefb277..4b312a07 100644 --- a/tests/quantile.rs +++ b/tests/quantile.rs @@ -1,11 +1,3 @@ -extern crate itertools; -extern crate ndarray; -extern crate ndarray_stats; -extern crate noisy_float; -#[macro_use] -extern crate quickcheck; -extern crate quickcheck_macros; - use itertools::izip; use ndarray::array; use ndarray::prelude::*; @@ -32,11 +24,10 @@ fn test_argmin() { assert_eq!(a.argmin(), Err(MinMaxError::EmptyInput)); } -quickcheck! { - fn argmin_matches_min(data: Vec) -> bool { - let a = Array1::from(data); - a.argmin().map(|i| &a[i]) == a.min() - } +#[quickcheck] +fn argmin_matches_min(data: Vec) -> bool { + let a = Array1::from(data); + a.argmin().map(|i| &a[i]) == a.min() } #[test] @@ -57,16 +48,15 @@ fn test_argmin_skipnan() { assert_eq!(a.argmin_skipnan(), Err(EmptyInput)); } -quickcheck! { - fn argmin_skipnan_matches_min_skipnan(data: Vec>) -> bool { - let a = Array1::from(data); - let min = a.min_skipnan(); - let argmin = a.argmin_skipnan(); - if min.is_none() { - argmin == Err(EmptyInput) - } else { - a[argmin.unwrap()] == *min - } +#[quickcheck] +fn argmin_skipnan_matches_min_skipnan(data: Vec>) -> bool { + let a = Array1::from(data); + let min = a.min_skipnan(); + let argmin = a.argmin_skipnan(); + if min.is_none() { + argmin == Err(EmptyInput) + } else { + a[argmin.unwrap()] == *min } } @@ -112,11 +102,10 @@ fn test_argmax() { assert_eq!(a.argmax(), Err(MinMaxError::EmptyInput)); } -quickcheck! { - fn argmax_matches_max(data: Vec) -> bool { - let a = Array1::from(data); - a.argmax().map(|i| &a[i]) == a.max() - } +#[quickcheck] +fn argmax_matches_max(data: Vec) -> bool { + let a = Array1::from(data); + a.argmax().map(|i| &a[i]) == a.max() } #[test] @@ -140,16 +129,15 @@ fn test_argmax_skipnan() { assert_eq!(a.argmax_skipnan(), Err(EmptyInput)); } -quickcheck! { - fn argmax_skipnan_matches_max_skipnan(data: Vec>) -> bool { - let a = Array1::from(data); - let max = a.max_skipnan(); - let argmax = a.argmax_skipnan(); - if max.is_none() { - argmax == Err(EmptyInput) - } else { - a[argmax.unwrap()] == *max - } +#[quickcheck] +fn argmax_skipnan_matches_max_skipnan(data: Vec>) -> bool { + let a = Array1::from(data); + let max = a.max_skipnan(); + let argmax = a.argmax_skipnan(); + if max.is_none() { + argmax == Err(EmptyInput) + } else { + a[argmax.unwrap()] == *max } } @@ -336,7 +324,7 @@ fn test_quantiles_mut(xs: Vec) -> bool { fn check_one_interpolation_method_for_quantiles_mut( mut v: Array1, - quantile_indexes: ArrayView1, + quantile_indexes: ArrayView1<'_, N64>, interpolate: &impl Interpolate, ) -> bool { let bulk_quantiles = v.clone().quantiles_mut(&quantile_indexes, interpolate); @@ -408,7 +396,7 @@ fn test_quantiles_axis_mut(mut xs: Vec) -> bool { fn check_one_interpolation_method_for_quantiles_axis_mut( mut v: Array2, - quantile_indexes: ArrayView1, + quantile_indexes: ArrayView1<'_, N64>, axis: Axis, interpolate: &impl Interpolate, ) -> bool { diff --git a/tests/sort.rs b/tests/sort.rs index 2d1df06c..af2717c4 100644 --- a/tests/sort.rs +++ b/tests/sort.rs @@ -1,8 +1,3 @@ -extern crate ndarray; -extern crate ndarray_stats; -extern crate quickcheck; -extern crate quickcheck_macros; - use ndarray::prelude::*; use ndarray_stats::Sort1dExt; use quickcheck_macros::quickcheck;