Skip to content

Commit 8f72716

Browse files
Merge branch 'development' into ridge
2 parents cc26555 + b86c553 commit 8f72716

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+793
-499
lines changed

.circleci/config.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
version: 2.1
22

3+
workflows:
4+
version: 2.1
5+
build:
6+
jobs:
7+
- build
8+
- clippy
39
jobs:
410
build:
511
docker:
@@ -24,3 +30,14 @@ jobs:
2430
paths:
2531
- "~/.cargo"
2632
- "./target"
33+
clippy:
34+
docker:
35+
- image: circleci/rust:latest
36+
steps:
37+
- checkout
38+
- run:
39+
name: Install cargo clippy
40+
command: rustup component add clippy
41+
- run:
42+
name: Run cargo clippy
43+
command: cargo clippy --all-features -- -Drust-2018-idioms -Dwarnings

src/algorithm/neighbour/bbd_tree.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ impl<T: RealNumber> BBDTree<T> {
5050
}
5151

5252
let mut tree = BBDTree {
53-
nodes: nodes,
54-
index: index,
53+
nodes,
54+
index,
5555
root: 0,
5656
};
5757

@@ -113,7 +113,7 @@ impl<T: RealNumber> BBDTree<T> {
113113
}
114114
}
115115

116-
if !self.nodes[node].lower.is_none() {
116+
if self.nodes[node].lower.is_some() {
117117
let mut new_candidates = vec![0; k];
118118
let mut newk = 0;
119119

@@ -134,15 +134,15 @@ impl<T: RealNumber> BBDTree<T> {
134134
return self.filter(
135135
self.nodes[node].lower.unwrap(),
136136
centroids,
137-
&mut new_candidates,
137+
&new_candidates,
138138
newk,
139139
sums,
140140
counts,
141141
membership,
142142
) + self.filter(
143143
self.nodes[node].upper.unwrap(),
144144
centroids,
145-
&mut new_candidates,
145+
&new_candidates,
146146
newk,
147147
sums,
148148
counts,
@@ -152,7 +152,7 @@ impl<T: RealNumber> BBDTree<T> {
152152
}
153153

154154
for i in 0..d {
155-
sums[closest][i] = sums[closest][i] + self.nodes[node].sum[i];
155+
sums[closest][i] += self.nodes[node].sum[i];
156156
}
157157

158158
counts[closest] += self.nodes[node].count;
@@ -184,11 +184,11 @@ impl<T: RealNumber> BBDTree<T> {
184184
let mut rhs = T::zero();
185185
for i in 0..d {
186186
let diff = test[i] - best[i];
187-
lhs = lhs + diff * diff;
187+
lhs += diff * diff;
188188
if diff > T::zero() {
189-
rhs = rhs + (center[i] + radius[i] - best[i]) * diff;
189+
rhs += (center[i] + radius[i] - best[i]) * diff;
190190
} else {
191-
rhs = rhs + (center[i] - radius[i] - best[i]) * diff;
191+
rhs += (center[i] - radius[i] - best[i]) * diff;
192192
}
193193
}
194194

@@ -244,7 +244,7 @@ impl<T: RealNumber> BBDTree<T> {
244244
if end > begin + 1 {
245245
let len = end - begin;
246246
for i in 0..d {
247-
node.sum[i] = node.sum[i] * T::from(len).unwrap();
247+
node.sum[i] *= T::from(len).unwrap();
248248
}
249249
}
250250

@@ -261,9 +261,7 @@ impl<T: RealNumber> BBDTree<T> {
261261
let mut i2_good = data.get(self.index[i2], split_index) >= split_cutoff;
262262

263263
if !i1_good && !i2_good {
264-
let temp = self.index[i1];
265-
self.index[i1] = self.index[i2];
266-
self.index[i2] = temp;
264+
self.index.swap(i1, i2);
267265
i1_good = true;
268266
i2_good = true;
269267
}
@@ -302,7 +300,7 @@ impl<T: RealNumber> BBDTree<T> {
302300
let mut scatter = T::zero();
303301
for i in 0..d {
304302
let x = (node.sum[i] / T::from(node.count).unwrap()) - center[i];
305-
scatter = scatter + x * x;
303+
scatter += x * x;
306304
}
307305
node.cost + T::from(node.count).unwrap() * scatter
308306
}

src/algorithm/neighbour/cover_tree.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<T, F: RealNumber, D: Distance<T, F>> PartialEq for CoverTree<T, F, D> {
5151
return false;
5252
}
5353
}
54-
return true;
54+
true
5555
}
5656
}
5757

@@ -84,11 +84,11 @@ impl<T: Debug + PartialEq, F: RealNumber, D: Distance<T, F>> CoverTree<T, F, D>
8484
scale: 0,
8585
};
8686
let mut tree = CoverTree {
87-
base: base,
87+
base,
8888
inv_log_base: F::one() / base.ln(),
89-
distance: distance,
90-
root: root,
91-
data: data,
89+
distance,
90+
root,
91+
data,
9292
identical_excluded: false,
9393
};
9494

@@ -101,7 +101,7 @@ impl<T: Debug + PartialEq, F: RealNumber, D: Distance<T, F>> CoverTree<T, F, D>
101101
/// * `p` - look for k nearest points to `p`
102102
/// * `k` - the number of nearest neighbors to return
103103
pub fn find(&self, p: &T, k: usize) -> Result<Vec<(usize, F, &T)>, Failed> {
104-
if k <= 0 {
104+
if k == 0 {
105105
return Err(Failed::because(FailedError::FindFailed, "k should be > 0"));
106106
}
107107

@@ -147,10 +147,11 @@ impl<T: Debug + PartialEq, F: RealNumber, D: Distance<T, F>> CoverTree<T, F, D>
147147
*heap.peek()
148148
};
149149
if d <= (upper_bound + child.max_dist) {
150-
if c > 0 && d < upper_bound {
151-
if !self.identical_excluded || self.get_data_value(child.idx) != p {
152-
heap.add(d);
153-
}
150+
if c > 0
151+
&& d < upper_bound
152+
&& (!self.identical_excluded || self.get_data_value(child.idx) != p)
153+
{
154+
heap.add(d);
154155
}
155156

156157
if !child.children.is_empty() {
@@ -234,7 +235,7 @@ impl<T: Debug + PartialEq, F: RealNumber, D: Distance<T, F>> CoverTree<T, F, D>
234235

235236
fn new_leaf(&self, idx: usize) -> Node<F> {
236237
Node {
237-
idx: idx,
238+
idx,
238239
max_dist: F::zero(),
239240
parent_dist: F::zero(),
240241
children: Vec::new(),
@@ -298,7 +299,7 @@ impl<T: Debug + PartialEq, F: RealNumber, D: Distance<T, F>> CoverTree<T, F, D>
298299
idx: p,
299300
max_dist: F::zero(),
300301
parent_dist: F::zero(),
301-
children: children,
302+
children,
302303
scale: 100,
303304
}
304305
} else {
@@ -368,7 +369,7 @@ impl<T: Debug + PartialEq, F: RealNumber, D: Distance<T, F>> CoverTree<T, F, D>
368369
idx: p,
369370
max_dist: self.max(consumed_set),
370371
parent_dist: F::zero(),
371-
children: children,
372+
children,
372373
scale: (top_scale - max_scale),
373374
}
374375
}
@@ -442,7 +443,7 @@ impl<T: Debug + PartialEq, F: RealNumber, D: Distance<T, F>> CoverTree<T, F, D>
442443
max = n.dist[n.dist.len() - 1];
443444
}
444445
}
445-
return max;
446+
max
446447
}
447448
}
448449

src/algorithm/neighbour/linear_search.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ impl<T, F: RealNumber, D: Distance<T, F>> LinearKNNSearch<T, F, D> {
4444
/// * `distance` - distance metric to use for searching. This function should extend [`Distance`](../../../math/distance/index.html) interface.
4545
pub fn new(data: Vec<T>, distance: D) -> Result<LinearKNNSearch<T, F, D>, Failed> {
4646
Ok(LinearKNNSearch {
47-
data: data,
48-
distance: distance,
47+
data,
48+
distance,
4949
f: PhantomData,
5050
})
5151
}
@@ -157,7 +157,7 @@ mod tests {
157157
.iter()
158158
.map(|v| v.0)
159159
.collect();
160-
found_idxs1.sort();
160+
found_idxs1.sort_unstable();
161161

162162
assert_eq!(vec!(0, 1, 2), found_idxs1);
163163

@@ -167,7 +167,7 @@ mod tests {
167167
.iter()
168168
.map(|v| *v.2)
169169
.collect();
170-
found_idxs1.sort();
170+
found_idxs1.sort_unstable();
171171

172172
assert_eq!(vec!(2, 3, 4, 5, 6, 7, 8), found_idxs1);
173173

@@ -187,7 +187,7 @@ mod tests {
187187
.iter()
188188
.map(|v| v.0)
189189
.collect();
190-
found_idxs2.sort();
190+
found_idxs2.sort_unstable();
191191

192192
assert_eq!(vec!(1, 2, 3), found_idxs2);
193193
}

src/algorithm/neighbour/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ impl KNNAlgorithmName {
6666
) -> Result<KNNAlgorithm<T, D>, Failed> {
6767
match *self {
6868
KNNAlgorithmName::LinearSearch => {
69-
LinearKNNSearch::new(data, distance).map(|a| KNNAlgorithm::LinearSearch(a))
69+
LinearKNNSearch::new(data, distance).map(KNNAlgorithm::LinearSearch)
7070
}
7171
KNNAlgorithmName::CoverTree => {
72-
CoverTree::new(data, distance).map(|a| KNNAlgorithm::CoverTree(a))
72+
CoverTree::new(data, distance).map(KNNAlgorithm::CoverTree)
7373
}
7474
}
7575
}

src/algorithm/sort/heap_select.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct HeapSelection<T: PartialOrd + Debug> {
1515
impl<'a, T: PartialOrd + Debug> HeapSelection<T> {
1616
pub fn with_capacity(k: usize) -> HeapSelection<T> {
1717
HeapSelection {
18-
k: k,
18+
k,
1919
n: 0,
2020
sorted: false,
2121
heap: Vec::new(),
@@ -51,7 +51,7 @@ impl<'a, T: PartialOrd + Debug> HeapSelection<T> {
5151

5252
pub fn peek(&self) -> &T {
5353
if self.sorted {
54-
return &self.heap[0];
54+
&self.heap[0]
5555
} else {
5656
&self
5757
.heap
@@ -62,11 +62,11 @@ impl<'a, T: PartialOrd + Debug> HeapSelection<T> {
6262
}
6363

6464
pub fn peek_mut(&mut self) -> &mut T {
65-
return &mut self.heap[0];
65+
&mut self.heap[0]
6666
}
6767

6868
pub fn get(self) -> Vec<T> {
69-
return self.heap;
69+
self.heap
7070
}
7171

7272
fn sift_down(&mut self, k: usize, n: usize) {

src/cluster/dbscan.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
//! * ["A Density-Based Algorithm for Discovering Clusters in Large Spatial Databases with Noise", Ester M., Kriegel HP., Sander J., Xu X.](http://faculty.marshall.usc.edu/gareth-james/ISL/)
3030
//! * ["Density-Based Clustering in Spatial Databases: The Algorithm GDBSCAN and its Applications", Sander J., Ester M., Kriegel HP., Xu X.](https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.63.1629&rep=rep1&type=pdf)
3131
32-
extern crate rand;
33-
3432
use std::fmt::Debug;
3533
use std::iter::Sum;
3634

@@ -93,11 +91,11 @@ impl<T: RealNumber + Sum, D: Distance<Vec<T>, T>> DBSCAN<T, D> {
9391
parameters: DBSCANParameters<T>,
9492
) -> Result<DBSCAN<T, D>, Failed> {
9593
if parameters.min_samples < 1 {
96-
return Err(Failed::fit(&format!("Invalid minPts")));
94+
return Err(Failed::fit(&"Invalid minPts".to_string()));
9795
}
9896

9997
if parameters.eps <= T::zero() {
100-
return Err(Failed::fit(&format!("Invalid radius: ")));
98+
return Err(Failed::fit(&"Invalid radius: ".to_string()));
10199
}
102100

103101
let mut k = 0;

src/cluster/kmeans.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
//! * ["An Introduction to Statistical Learning", James G., Witten D., Hastie T., Tibshirani R., 10.3.1 K-Means Clustering](http://faculty.marshall.usc.edu/gareth-james/ISL/)
5353
//! * ["k-means++: The Advantages of Careful Seeding", Arthur D., Vassilvitskii S.](http://ilpubs.stanford.edu:8090/778/1/2006-13.pdf)
5454
55-
extern crate rand;
56-
5755
use rand::Rng;
5856
use std::fmt::Debug;
5957
use std::iter::Sum;
@@ -129,7 +127,7 @@ impl<T: RealNumber + Sum> KMeans<T> {
129127
return Err(Failed::fit(&format!("invalid number of clusters: {}", k)));
130128
}
131129

132-
if parameters.max_iter <= 0 {
130+
if parameters.max_iter == 0 {
133131
return Err(Failed::fit(&format!(
134132
"invalid maximum number of iterations: {}",
135133
parameters.max_iter
@@ -149,13 +147,13 @@ impl<T: RealNumber + Sum> KMeans<T> {
149147

150148
for i in 0..n {
151149
for j in 0..d {
152-
centroids[y[i]][j] = centroids[y[i]][j] + data.get(i, j);
150+
centroids[y[i]][j] += data.get(i, j);
153151
}
154152
}
155153

156154
for i in 0..k {
157155
for j in 0..d {
158-
centroids[i][j] = centroids[i][j] / T::from(size[i]).unwrap();
156+
centroids[i][j] /= T::from(size[i]).unwrap();
159157
}
160158
}
161159

@@ -178,11 +176,11 @@ impl<T: RealNumber + Sum> KMeans<T> {
178176
}
179177

180178
Ok(KMeans {
181-
k: k,
182-
y: y,
183-
size: size,
184-
distortion: distortion,
185-
centroids: centroids,
179+
k,
180+
y,
181+
size,
182+
distortion,
183+
centroids,
186184
})
187185
}
188186

@@ -235,13 +233,13 @@ impl<T: RealNumber + Sum> KMeans<T> {
235233

236234
let mut sum: T = T::zero();
237235
for i in d.iter() {
238-
sum = sum + *i;
236+
sum += *i;
239237
}
240238
let cutoff = T::from(rng.gen::<f64>()).unwrap() * sum;
241239
let mut cost = T::zero();
242240
let mut index = 0;
243241
while index < n {
244-
cost = cost + d[index];
242+
cost += d[index];
245243
if cost >= cutoff {
246244
break;
247245
}

src/dataset/boston.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ pub fn load_dataset() -> Dataset<f32, f32> {
3838
Dataset {
3939
data: x,
4040
target: y,
41-
num_samples: num_samples,
42-
num_features: num_features,
41+
num_samples,
42+
num_features,
4343
feature_names: vec![
4444
"CRIM", "ZN", "INDUS", "CHAS", "NOX", "RM", "AGE", "DIS", "RAD", "TAX", "PTRATIO", "B",
4545
"LSTAT",

src/dataset/breast_cancer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ pub fn load_dataset() -> Dataset<f32, f32> {
4040
Dataset {
4141
data: x,
4242
target: y,
43-
num_samples: num_samples,
44-
num_features: num_features,
43+
num_samples,
44+
num_features,
4545
feature_names: vec![
4646
"mean radius", "mean texture", "mean perimeter", "mean area",
4747
"mean smoothness", "mean compactness", "mean concavity",

0 commit comments

Comments
 (0)