From 820201e92079aa70f71ee780f504cc72817c6b60 Mon Sep 17 00:00:00 2001 From: morenol <22335041+morenol@users.noreply.github.com> Date: Thu, 5 May 2022 10:39:18 -0400 Subject: [PATCH] Solve conflic with num-traits (#130) * Solve conflic with num-traits * Fix clippy warnings Co-authored-by: Luis Moreno --- src/algorithm/neighbour/cover_tree.rs | 10 +++++----- src/cluster/dbscan.rs | 4 ++-- src/cluster/kmeans.rs | 10 +++++----- src/dataset/mod.rs | 4 ++-- src/ensemble/random_forest_classifier.rs | 4 ++-- src/ensemble/random_forest_regressor.rs | 4 ++-- src/linalg/cholesky.rs | 7 +++---- src/linalg/evd.rs | 19 +++++++------------ src/linalg/lu.rs | 6 +++--- src/linalg/mod.rs | 9 ++++----- src/linalg/naive/dense_matrix.rs | 1 - src/linalg/svd.rs | 12 ++++++------ src/linear/linear_regression.rs | 6 +++--- src/linear/logistic_regression.rs | 2 +- src/linear/ridge_regression.rs | 4 ++-- src/naive_bayes/categorical.rs | 2 +- src/optimization/first_order/lbfgs.rs | 1 + src/optimization/mod.rs | 1 + src/preprocessing/categorical.rs | 3 +-- src/svm/svc.rs | 2 +- src/svm/svr.rs | 2 +- src/tree/decision_tree_classifier.rs | 6 +++--- src/tree/decision_tree_regressor.rs | 4 ++-- 23 files changed, 58 insertions(+), 65 deletions(-) diff --git a/src/algorithm/neighbour/cover_tree.rs b/src/algorithm/neighbour/cover_tree.rs index 8fb8b7d..e329220 100644 --- a/src/algorithm/neighbour/cover_tree.rs +++ b/src/algorithm/neighbour/cover_tree.rs @@ -65,7 +65,7 @@ struct Node { max_dist: F, parent_dist: F, children: Vec>, - scale: i64, + _scale: i64, } #[derive(Debug)] @@ -85,7 +85,7 @@ impl> CoverTree max_dist: F::zero(), parent_dist: F::zero(), children: Vec::new(), - scale: 0, + _scale: 0, }; let mut tree = CoverTree { base, @@ -243,7 +243,7 @@ impl> CoverTree max_dist: F::zero(), parent_dist: F::zero(), children: Vec::new(), - scale: 100, + _scale: 100, } } @@ -304,7 +304,7 @@ impl> CoverTree max_dist: F::zero(), parent_dist: F::zero(), children, - scale: 100, + _scale: 100, } } else { let mut far: Vec> = Vec::new(); @@ -373,7 +373,7 @@ impl> CoverTree max_dist: self.max(consumed_set), parent_dist: F::zero(), children, - scale: (top_scale - max_scale), + _scale: (top_scale - max_scale), } } } diff --git a/src/cluster/dbscan.rs b/src/cluster/dbscan.rs index b1231c3..7f2baef 100644 --- a/src/cluster/dbscan.rs +++ b/src/cluster/dbscan.rs @@ -155,11 +155,11 @@ impl, T>> DBSCAN { parameters: DBSCANParameters, ) -> Result, Failed> { if parameters.min_samples < 1 { - return Err(Failed::fit(&"Invalid minPts".to_string())); + return Err(Failed::fit("Invalid minPts")); } if parameters.eps <= T::zero() { - return Err(Failed::fit(&"Invalid radius: ".to_string())); + return Err(Failed::fit("Invalid radius: ")); } let mut k = 0; diff --git a/src/cluster/kmeans.rs b/src/cluster/kmeans.rs index fd43a14..05af680 100644 --- a/src/cluster/kmeans.rs +++ b/src/cluster/kmeans.rs @@ -71,9 +71,9 @@ use crate::math::num::RealNumber; #[derive(Debug)] pub struct KMeans { k: usize, - y: Vec, + _y: Vec, size: Vec, - distortion: T, + _distortion: T, centroids: Vec>, } @@ -208,9 +208,9 @@ impl KMeans { Ok(KMeans { k: parameters.k, - y, + _y: y, size, - distortion, + _distortion: distortion, centroids, }) } @@ -344,7 +344,7 @@ mod tests { let y = kmeans.predict(&x).unwrap(); for i in 0..y.len() { - assert_eq!(y[i] as usize, kmeans.y[i]); + assert_eq!(y[i] as usize, kmeans._y[i]); } } diff --git a/src/dataset/mod.rs b/src/dataset/mod.rs index 5fe4c45..acd7641 100644 --- a/src/dataset/mod.rs +++ b/src/dataset/mod.rs @@ -67,14 +67,14 @@ pub(crate) fn serialize_data( .data .iter() .copied() - .flat_map(|f| f.to_f32_bits().to_le_bytes().to_vec().into_iter()) + .flat_map(|f| f.to_f32_bits().to_le_bytes().to_vec()) .collect(); file.write_all(&x)?; let y: Vec = dataset .target .iter() .copied() - .flat_map(|f| f.to_f32_bits().to_le_bytes().to_vec().into_iter()) + .flat_map(|f| f.to_f32_bits().to_le_bytes().to_vec()) .collect(); file.write_all(&y)?; } diff --git a/src/ensemble/random_forest_classifier.rs b/src/ensemble/random_forest_classifier.rs index 5cebced..247b502 100644 --- a/src/ensemble/random_forest_classifier.rs +++ b/src/ensemble/random_forest_classifier.rs @@ -88,7 +88,7 @@ pub struct RandomForestClassifierParameters { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug)] pub struct RandomForestClassifier { - parameters: RandomForestClassifierParameters, + _parameters: RandomForestClassifierParameters, trees: Vec>, classes: Vec, samples: Option>>, @@ -249,7 +249,7 @@ impl RandomForestClassifier { } Ok(RandomForestClassifier { - parameters, + _parameters: parameters, trees, classes, samples: maybe_all_samples, diff --git a/src/ensemble/random_forest_regressor.rs b/src/ensemble/random_forest_regressor.rs index c923cd8..08a7dcc 100644 --- a/src/ensemble/random_forest_regressor.rs +++ b/src/ensemble/random_forest_regressor.rs @@ -84,7 +84,7 @@ pub struct RandomForestRegressorParameters { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug)] pub struct RandomForestRegressor { - parameters: RandomForestRegressorParameters, + _parameters: RandomForestRegressorParameters, trees: Vec>, samples: Option>>, } @@ -215,7 +215,7 @@ impl RandomForestRegressor { } Ok(RandomForestRegressor { - parameters, + _parameters: parameters, trees, samples: maybe_all_samples, }) diff --git a/src/linalg/cholesky.rs b/src/linalg/cholesky.rs index 053cbfa..9b5b9cc 100644 --- a/src/linalg/cholesky.rs +++ b/src/linalg/cholesky.rs @@ -87,8 +87,7 @@ impl> Cholesky { if bn != rn { return Err(Failed::because( FailedError::SolutionFailed, - &"Can\'t solve Ax = b for x. Number of rows in b != number of rows in R." - .to_string(), + "Can\'t solve Ax = b for x. Number of rows in b != number of rows in R.", )); } @@ -128,7 +127,7 @@ pub trait CholeskyDecomposableMatrix: BaseMatrix { if m != n { return Err(Failed::because( FailedError::DecompositionFailed, - &"Can\'t do Cholesky decomposition on a non-square matrix".to_string(), + "Can\'t do Cholesky decomposition on a non-square matrix", )); } @@ -148,7 +147,7 @@ pub trait CholeskyDecomposableMatrix: BaseMatrix { if d < T::zero() { return Err(Failed::because( FailedError::DecompositionFailed, - &"The matrix is not positive definite.".to_string(), + "The matrix is not positive definite.", )); } diff --git a/src/linalg/evd.rs b/src/linalg/evd.rs index 81e2315..bf195a0 100644 --- a/src/linalg/evd.rs +++ b/src/linalg/evd.rs @@ -97,7 +97,7 @@ pub trait EVDDecomposableMatrix: BaseMatrix { } } -fn tred2>(V: &mut M, d: &mut Vec, e: &mut Vec) { +fn tred2>(V: &mut M, d: &mut [T], e: &mut [T]) { let (n, _) = V.shape(); for (i, d_i) in d.iter_mut().enumerate().take(n) { *d_i = V.get(n - 1, i); @@ -195,7 +195,7 @@ fn tred2>(V: &mut M, d: &mut Vec, e: &mut Vec e[0] = T::zero(); } -fn tql2>(V: &mut M, d: &mut Vec, e: &mut Vec) { +fn tql2>(V: &mut M, d: &mut [T], e: &mut [T]) { let (n, _) = V.shape(); for i in 1..n { e[i - 1] = e[i]; @@ -419,7 +419,7 @@ fn eltran>(A: &M, V: &mut M, perm: &[usize]) { } } -fn hqr2>(A: &mut M, V: &mut M, d: &mut Vec, e: &mut Vec) { +fn hqr2>(A: &mut M, V: &mut M, d: &mut [T], e: &mut [T]) { let (n, _) = A.shape(); let mut z = T::zero(); let mut s = T::zero(); @@ -471,7 +471,7 @@ fn hqr2>(A: &mut M, V: &mut M, d: &mut Vec, e A.set(nn, nn, x); A.set(nn - 1, nn - 1, y + t); if q >= T::zero() { - z = p + z.copysign(p); + z = p + RealNumber::copysign(z, p); d[nn - 1] = x + z; d[nn] = x + z; if z != T::zero() { @@ -570,7 +570,7 @@ fn hqr2>(A: &mut M, V: &mut M, d: &mut Vec, e r /= x; } } - let s = (p * p + q * q + r * r).sqrt().copysign(p); + let s = RealNumber::copysign((p * p + q * q + r * r).sqrt(), p); if s != T::zero() { if k == m { if l != m { @@ -594,12 +594,7 @@ fn hqr2>(A: &mut M, V: &mut M, d: &mut Vec, e A.sub_element_mut(k + 1, j, p * y); A.sub_element_mut(k, j, p * x); } - let mmin; - if nn < k + 3 { - mmin = nn; - } else { - mmin = k + 3; - } + let mmin = if nn < k + 3 { nn } else { k + 3 }; for i in 0..mmin + 1 { p = x * A.get(i, k) + y * A.get(i, k + 1); if k + 1 != nn { @@ -783,7 +778,7 @@ fn balbak>(V: &mut M, scale: &[T]) { } } -fn sort>(d: &mut Vec, e: &mut Vec, V: &mut M) { +fn sort>(d: &mut [T], e: &mut [T], V: &mut M) { let n = d.len(); let mut temp = vec![T::zero(); n]; for j in 1..n { diff --git a/src/linalg/lu.rs b/src/linalg/lu.rs index 72d6079..cb001af 100644 --- a/src/linalg/lu.rs +++ b/src/linalg/lu.rs @@ -46,13 +46,13 @@ use crate::math::num::RealNumber; pub struct LU> { LU: M, pivot: Vec, - pivot_sign: i8, + _pivot_sign: i8, singular: bool, phantom: PhantomData, } impl> LU { - pub(crate) fn new(LU: M, pivot: Vec, pivot_sign: i8) -> LU { + pub(crate) fn new(LU: M, pivot: Vec, _pivot_sign: i8) -> LU { let (_, n) = LU.shape(); let mut singular = false; @@ -66,7 +66,7 @@ impl> LU { LU { LU, pivot, - pivot_sign, + _pivot_sign, singular, phantom: PhantomData, } diff --git a/src/linalg/mod.rs b/src/linalg/mod.rs index d2d2212..59b6089 100644 --- a/src/linalg/mod.rs +++ b/src/linalg/mod.rs @@ -689,12 +689,11 @@ impl<'a, T: RealNumber, M: BaseMatrix> Iterator for RowIter<'a, T, M> { type Item = Vec; fn next(&mut self) -> Option> { - let res; - if self.pos < self.max_pos { - res = Some(self.m.get_row_as_vec(self.pos)) + let res = if self.pos < self.max_pos { + Some(self.m.get_row_as_vec(self.pos)) } else { - res = None - } + None + }; self.pos += 1; res } diff --git a/src/linalg/naive/dense_matrix.rs b/src/linalg/naive/dense_matrix.rs index 9866618..1af926c 100644 --- a/src/linalg/naive/dense_matrix.rs +++ b/src/linalg/naive/dense_matrix.rs @@ -523,7 +523,6 @@ impl PartialEq for DenseMatrix { true } } - impl From> for Vec { fn from(dense_matrix: DenseMatrix) -> Vec { dense_matrix.values diff --git a/src/linalg/svd.rs b/src/linalg/svd.rs index 3746071..97d85ca 100644 --- a/src/linalg/svd.rs +++ b/src/linalg/svd.rs @@ -47,7 +47,7 @@ pub struct SVD> { pub V: M, /// Singular values of the original matrix pub s: Vec, - full: bool, + _full: bool, m: usize, n: usize, tol: T, @@ -116,7 +116,7 @@ pub trait SVDDecomposableMatrix: BaseMatrix { } let mut f = U.get(i, i); - g = -s.sqrt().copysign(f); + g = -RealNumber::copysign(s.sqrt(), f); let h = f * g - s; U.set(i, i, f - g); for j in l - 1..n { @@ -152,7 +152,7 @@ pub trait SVDDecomposableMatrix: BaseMatrix { } let f = U.get(i, l - 1); - g = -s.sqrt().copysign(f); + g = -RealNumber::copysign(s.sqrt(), f); let h = f * g - s; U.set(i, l - 1, f - g); @@ -299,7 +299,7 @@ pub trait SVDDecomposableMatrix: BaseMatrix { let mut h = rv1[k]; let mut f = ((y - z) * (y + z) + (g - h) * (g + h)) / (T::two() * h * y); g = f.hypot(T::one()); - f = ((x - z) * (x + z) + h * ((y / (f + g.copysign(f))) - h)) / x; + f = ((x - z) * (x + z) + h * ((y / (f + RealNumber::copysign(g, f))) - h)) / x; let mut c = T::one(); let mut s = T::one(); @@ -428,13 +428,13 @@ impl> SVD { pub(crate) fn new(U: M, V: M, s: Vec) -> SVD { let m = U.shape().0; let n = V.shape().0; - let full = s.len() == m.min(n); + let _full = s.len() == m.min(n); let tol = T::half() * (T::from(m + n).unwrap() + T::one()).sqrt() * s[0] * T::epsilon(); SVD { U, V, s, - full, + _full, m, n, tol, diff --git a/src/linear/linear_regression.rs b/src/linear/linear_regression.rs index a10b5ac..b1f7c51 100644 --- a/src/linear/linear_regression.rs +++ b/src/linear/linear_regression.rs @@ -94,7 +94,7 @@ pub struct LinearRegressionParameters { pub struct LinearRegression> { coefficients: M, intercept: T, - solver: LinearRegressionSolverName, + _solver: LinearRegressionSolverName, } impl LinearRegressionParameters { @@ -155,7 +155,7 @@ impl> LinearRegression { if x_nrows != y_nrows { return Err(Failed::fit( - &"Number of rows of X doesn\'t match number of rows of Y".to_string(), + "Number of rows of X doesn\'t match number of rows of Y", )); } @@ -171,7 +171,7 @@ impl> LinearRegression { Ok(LinearRegression { intercept: w.get(num_attributes, 0), coefficients: wights, - solver: parameters.solver, + _solver: parameters.solver, }) } diff --git a/src/linear/logistic_regression.rs b/src/linear/logistic_regression.rs index ad2cdb3..1a20077 100644 --- a/src/linear/logistic_regression.rs +++ b/src/linear/logistic_regression.rs @@ -321,7 +321,7 @@ impl> LogisticRegression { if x_nrows != y_nrows { return Err(Failed::fit( - &"Number of rows of X doesn\'t match number of rows of Y".to_string(), + "Number of rows of X doesn\'t match number of rows of Y", )); } diff --git a/src/linear/ridge_regression.rs b/src/linear/ridge_regression.rs index 94ac700..ecad250 100644 --- a/src/linear/ridge_regression.rs +++ b/src/linear/ridge_regression.rs @@ -96,7 +96,7 @@ pub struct RidgeRegressionParameters { pub struct RidgeRegression> { coefficients: M, intercept: T, - solver: RidgeRegressionSolverName, + _solver: RidgeRegressionSolverName, } impl RidgeRegressionParameters { @@ -226,7 +226,7 @@ impl> RidgeRegression { Ok(RidgeRegression { intercept: b, coefficients: w, - solver: parameters.solver, + _solver: parameters.solver, }) } diff --git a/src/naive_bayes/categorical.rs b/src/naive_bayes/categorical.rs index 44e5dd9..8706702 100644 --- a/src/naive_bayes/categorical.rs +++ b/src/naive_bayes/categorical.rs @@ -161,7 +161,7 @@ impl CategoricalNBDistribution { let y_max = y .iter() .max() - .ok_or_else(|| Failed::fit(&"Failed to get the labels of y.".to_string()))?; + .ok_or_else(|| Failed::fit("Failed to get the labels of y."))?; let class_labels: Vec = (0..*y_max + 1) .map(|label| T::from(label).unwrap()) diff --git a/src/optimization/first_order/lbfgs.rs b/src/optimization/first_order/lbfgs.rs index 6c0b89b..1b3bfde 100644 --- a/src/optimization/first_order/lbfgs.rs +++ b/src/optimization/first_order/lbfgs.rs @@ -8,6 +8,7 @@ use crate::optimization::first_order::{FirstOrderOptimizer, OptimizerResult}; use crate::optimization::line_search::LineSearchMethod; use crate::optimization::{DF, F}; +#[allow(clippy::upper_case_acronyms)] pub struct LBFGS { pub max_iter: usize, pub g_rtol: T, diff --git a/src/optimization/mod.rs b/src/optimization/mod.rs index e5e58d1..b0be9d6 100644 --- a/src/optimization/mod.rs +++ b/src/optimization/mod.rs @@ -4,6 +4,7 @@ pub mod line_search; pub type F<'a, T, X> = dyn for<'b> Fn(&'b X) -> T + 'a; pub type DF<'a, X> = dyn for<'b> Fn(&'b mut X, &'b X) + 'a; +#[allow(clippy::upper_case_acronyms)] #[derive(Debug, PartialEq)] pub enum FunctionOrder { SECOND, diff --git a/src/preprocessing/categorical.rs b/src/preprocessing/categorical.rs index adc85a6..478e706 100644 --- a/src/preprocessing/categorical.rs +++ b/src/preprocessing/categorical.rs @@ -78,8 +78,7 @@ fn find_new_idxs(num_params: usize, cat_sizes: &[usize], cat_idxs: &[usize]) -> .zip( repeats .zip(offset) - .map(|(r, o)| iter::repeat(o).take(r)) - .flatten(), + .flat_map(|(r, o)| iter::repeat(o).take(r)), ) .map(|(idx, ofst)| idx + ofst) .collect(); diff --git a/src/svm/svc.rs b/src/svm/svc.rs index b12e558..7432b9c 100644 --- a/src/svm/svc.rs +++ b/src/svm/svc.rs @@ -222,7 +222,7 @@ impl, K: Kernel> SVC { if n != y.len() { return Err(Failed::fit( - &"Number of rows of X doesn\'t match number of rows of Y".to_string(), + "Number of rows of X doesn\'t match number of rows of Y", )); } diff --git a/src/svm/svr.rs b/src/svm/svr.rs index 455e51f..3257111 100644 --- a/src/svm/svr.rs +++ b/src/svm/svr.rs @@ -212,7 +212,7 @@ impl, K: Kernel> SVR { if n != y.len() { return Err(Failed::fit( - &"Number of rows of X doesn\'t match number of rows of Y".to_string(), + "Number of rows of X doesn\'t match number of rows of Y", )); } diff --git a/src/tree/decision_tree_classifier.rs b/src/tree/decision_tree_classifier.rs index 751d5d1..d86f59a 100644 --- a/src/tree/decision_tree_classifier.rs +++ b/src/tree/decision_tree_classifier.rs @@ -118,7 +118,7 @@ pub enum SplitCriterion { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug)] struct Node { - index: usize, + _index: usize, output: usize, split_feature: usize, split_value: Option, @@ -204,7 +204,7 @@ impl Default for DecisionTreeClassifierParameters { impl Node { fn new(index: usize, output: usize) -> Self { Node { - index, + _index: index, output, split_feature: 0, split_value: Option::None, @@ -514,7 +514,7 @@ impl DecisionTreeClassifier { visitor: &mut NodeVisitor<'_, T, M>, n: usize, count: &[usize], - false_count: &mut Vec, + false_count: &mut [usize], parent_impurity: T, j: usize, ) { diff --git a/src/tree/decision_tree_regressor.rs b/src/tree/decision_tree_regressor.rs index 34f58a9..94fa0f8 100644 --- a/src/tree/decision_tree_regressor.rs +++ b/src/tree/decision_tree_regressor.rs @@ -97,7 +97,7 @@ pub struct DecisionTreeRegressor { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug)] struct Node { - index: usize, + _index: usize, output: T, split_feature: usize, split_value: Option, @@ -137,7 +137,7 @@ impl Default for DecisionTreeRegressorParameters { impl Node { fn new(index: usize, output: T) -> Self { Node { - index, + _index: index, output, split_feature: 0, split_value: Option::None,