Skip to content

Commit 4921ae7

Browse files
Volodymyr OrlovVolodymyr Orlov
authored andcommitted
fix: formatting
1 parent 9b7a2df commit 4921ae7

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

src/cluster/kmeans.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ use std::iter::Sum;
6060

6161
use serde::{Deserialize, Serialize};
6262

63-
use crate::error::{FitFailedError, PredictFailedError};
6463
use crate::algorithm::neighbour::bbd_tree::BBDTree;
64+
use crate::error::{FitFailedError, PredictFailedError};
6565
use crate::linalg::Matrix;
6666
use crate::math::distance::euclidian::*;
6767
use crate::math::num::RealNumber;
@@ -118,15 +118,23 @@ impl<T: RealNumber + Sum> KMeans<T> {
118118
/// * `data` - training instances to cluster
119119
/// * `k` - number of clusters
120120
/// * `parameters` - cluster parameters
121-
pub fn fit<M: Matrix<T>>(data: &M, k: usize, parameters: KMeansParameters) -> Result<KMeans<T>, FitFailedError> {
121+
pub fn fit<M: Matrix<T>>(
122+
data: &M,
123+
k: usize,
124+
parameters: KMeansParameters,
125+
) -> Result<KMeans<T>, FitFailedError> {
122126
let bbd = BBDTree::new(data);
123127

124128
if k < 2 {
125-
return Err(FitFailedError::new(&format!("Invalid number of clusters: {}", k)));
129+
return Err(FitFailedError::new(&format!(
130+
"Invalid number of clusters: {}",
131+
k
132+
)));
126133
}
127134

128135
if parameters.max_iter <= 0 {
129-
return Err(FitFailedError::new(&format!("Invalid maximum number of iterations: {}",
136+
return Err(FitFailedError::new(&format!(
137+
"Invalid maximum number of iterations: {}",
130138
parameters.max_iter
131139
)));
132140
}
@@ -264,16 +272,17 @@ mod tests {
264272

265273
#[test]
266274
fn invalid_k() {
267-
let x = DenseMatrix::from_2d_array(&[
268-
&[1., 2., 3.],
269-
&[4., 5., 6.],
270-
]);
275+
let x = DenseMatrix::from_2d_array(&[&[1., 2., 3.], &[4., 5., 6.]]);
271276

272277
println!("{:?}", KMeans::fit(&x, 0, Default::default()));
273278

274-
assert!(KMeans::fit(&x, 0, Default::default()).is_err());
275-
assert_eq!("Invalid number of clusters: 1", KMeans::fit(&x, 1, Default::default()).unwrap_err().to_string());
276-
279+
assert!(KMeans::fit(&x, 0, Default::default()).is_err());
280+
assert_eq!(
281+
"Invalid number of clusters: 1",
282+
KMeans::fit(&x, 1, Default::default())
283+
.unwrap_err()
284+
.to_string()
285+
);
277286
}
278287

279288
#[test]

src/error/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@ use std::fmt;
55
/// Error to be raised when model does not fits data.
66
#[derive(Debug)]
77
pub struct FitFailedError {
8-
details: String
8+
details: String,
99
}
1010

1111
/// Error to be raised when model prediction cannot be calculated.
1212
#[derive(Debug)]
1313
pub struct PredictFailedError {
14-
details: String
14+
details: String,
1515
}
1616

1717
impl FitFailedError {
1818
/// Creates new instance of `FitFailedError`
1919
/// * `msg` - description of the error
2020
pub fn new(msg: &str) -> FitFailedError {
21-
FitFailedError{details: msg.to_string()}
21+
FitFailedError {
22+
details: msg.to_string(),
23+
}
2224
}
2325
}
2426

2527
impl fmt::Display for FitFailedError {
2628
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27-
write!(f,"{}",self.details)
29+
write!(f, "{}", self.details)
2830
}
2931
}
3032

@@ -38,13 +40,15 @@ impl PredictFailedError {
3840
/// Creates new instance of `PredictFailedError`
3941
/// * `msg` - description of the error
4042
pub fn new(msg: &str) -> PredictFailedError {
41-
PredictFailedError{details: msg.to_string()}
43+
PredictFailedError {
44+
details: msg.to_string(),
45+
}
4246
}
4347
}
4448

4549
impl fmt::Display for PredictFailedError {
4650
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47-
write!(f,"{}",self.details)
51+
write!(f, "{}", self.details)
4852
}
4953
}
5054

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub mod dataset;
7575
pub mod decomposition;
7676
/// Ensemble methods, including Random Forest classifier and regressor
7777
pub mod ensemble;
78+
pub mod error;
7879
/// Diverse collection of linear algebra abstractions and methods that power SmartCore algorithms
7980
pub mod linalg;
8081
/// Supervised classification and regression models that assume linear relationship between dependent and explanatory variables.
@@ -89,4 +90,3 @@ pub mod neighbors;
8990
pub(crate) mod optimization;
9091
/// Supervised tree-based learning methods
9192
pub mod tree;
92-
pub mod error;

0 commit comments

Comments
 (0)