Skip to content

Commit 07110df

Browse files
authored
Merge pull request #31 from LukeMathWalker/formatter
Add rustfmt to the CI pipeline
2 parents 4e01b15 + 7e9402c commit 07110df

File tree

13 files changed

+196
-202
lines changed

13 files changed

+196
-202
lines changed

.travis.yml

+10-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ before_cache: |
1818
if [[ "$TRAVIS_RUST_VERSION" == nightly ]]; then
1919
RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install cargo-tarpaulin
2020
fi
21-
script:
22-
- cargo clean
23-
- cargo build
24-
- cargo test
21+
before_script:
22+
- rustup component add rustfmt
23+
# As a result of https://github.com/travis-ci/travis-ci/issues/1066, we run
24+
# everything in one large command instead of multiple commands.
25+
# In this way, the build stops immediately if one of the commands fails.
26+
script: |
27+
cargo clean &&
28+
cargo fmt --all -- --check &&
29+
cargo build &&
30+
cargo test
2531
after_success: |
2632
if [[ "$TRAVIS_RUST_VERSION" == nightly ]]; then
2733
cargo tarpaulin --out Xml

src/correlation.rs

+41-55
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,15 @@ where
131131
{
132132
let observation_axis = Axis(1);
133133
let n_observations = A::from_usize(self.len_of(observation_axis)).unwrap();
134-
let dof =
135-
if ddof >= n_observations {
136-
panic!("`ddof` needs to be strictly smaller than the \
137-
number of observations provided for each \
138-
random variable!")
139-
} else {
140-
n_observations - ddof
141-
};
134+
let dof = if ddof >= n_observations {
135+
panic!(
136+
"`ddof` needs to be strictly smaller than the \
137+
number of observations provided for each \
138+
random variable!"
139+
)
140+
} else {
141+
n_observations - ddof
142+
};
142143
let mean = self.mean_axis(observation_axis);
143144
let denoised = self - &mean.insert_axis(observation_axis);
144145
let covariance = denoised.dot(&denoised.t());
@@ -156,7 +157,9 @@ where
156157
// observation per random variable (or no observations at all)
157158
let ddof = -A::one();
158159
let cov = self.cov(ddof);
159-
let std = self.std_axis(observation_axis, ddof).insert_axis(observation_axis);
160+
let std = self
161+
.std_axis(observation_axis, ddof)
162+
.insert_axis(observation_axis);
160163
let std_matrix = std.dot(&std.t());
161164
// element-wise division
162165
cov / std_matrix
@@ -167,10 +170,10 @@ where
167170
mod cov_tests {
168171
use super::*;
169172
use ndarray::array;
173+
use ndarray_rand::RandomExt;
170174
use quickcheck::quickcheck;
171175
use rand;
172176
use rand::distributions::Uniform;
173-
use ndarray_rand::RandomExt;
174177

175178
quickcheck! {
176179
fn constant_random_variables_have_zero_covariance_matrix(value: f64) -> bool {
@@ -200,10 +203,7 @@ mod cov_tests {
200203
fn test_invalid_ddof() {
201204
let n_random_variables = 3;
202205
let n_observations = 4;
203-
let a = Array::random(
204-
(n_random_variables, n_observations),
205-
Uniform::new(0., 10.)
206-
);
206+
let a = Array::random((n_random_variables, n_observations), Uniform::new(0., 10.));
207207
let invalid_ddof = (n_observations as f64) + rand::random::<f64>().abs();
208208
a.cov(invalid_ddof);
209209
}
@@ -235,55 +235,46 @@ mod cov_tests {
235235
#[test]
236236
fn test_covariance_for_random_array() {
237237
let a = array![
238-
[ 0.72009497, 0.12568055, 0.55705966, 0.5959984 , 0.69471457],
239-
[ 0.56717131, 0.47619486, 0.21526298, 0.88915366, 0.91971245],
240-
[ 0.59044195, 0.10720363, 0.76573717, 0.54693675, 0.95923036],
241-
[ 0.24102952, 0.131347, 0.11118028, 0.21451351, 0.30515539],
242-
[ 0.26952473, 0.93079841, 0.8080893 , 0.42814155, 0.24642258]
238+
[0.72009497, 0.12568055, 0.55705966, 0.5959984, 0.69471457],
239+
[0.56717131, 0.47619486, 0.21526298, 0.88915366, 0.91971245],
240+
[0.59044195, 0.10720363, 0.76573717, 0.54693675, 0.95923036],
241+
[0.24102952, 0.131347, 0.11118028, 0.21451351, 0.30515539],
242+
[0.26952473, 0.93079841, 0.8080893, 0.42814155, 0.24642258]
243243
];
244244
let numpy_covariance = array![
245-
[ 0.05786248, 0.02614063, 0.06446215, 0.01285105, -0.06443992],
246-
[ 0.02614063, 0.08733569, 0.02436933, 0.01977437, -0.06715555],
247-
[ 0.06446215, 0.02436933, 0.10052129, 0.01393589, -0.06129912],
248-
[ 0.01285105, 0.01977437, 0.01393589, 0.00638795, -0.02355557],
249-
[-0.06443992, -0.06715555, -0.06129912, -0.02355557, 0.09909855]
245+
[0.05786248, 0.02614063, 0.06446215, 0.01285105, -0.06443992],
246+
[0.02614063, 0.08733569, 0.02436933, 0.01977437, -0.06715555],
247+
[0.06446215, 0.02436933, 0.10052129, 0.01393589, -0.06129912],
248+
[0.01285105, 0.01977437, 0.01393589, 0.00638795, -0.02355557],
249+
[
250+
-0.06443992,
251+
-0.06715555,
252+
-0.06129912,
253+
-0.02355557,
254+
0.09909855
255+
]
250256
];
251257
assert_eq!(a.ndim(), 2);
252-
assert!(
253-
a.cov(1.).all_close(
254-
&numpy_covariance,
255-
1e-8
256-
)
257-
);
258+
assert!(a.cov(1.).all_close(&numpy_covariance, 1e-8));
258259
}
259260

260261
#[test]
261262
#[should_panic]
262263
// We lose precision, hence the failing assert
263264
fn test_covariance_for_badly_conditioned_array() {
264-
let a: Array2<f64> = array![
265-
[ 1e12 + 1., 1e12 - 1.],
266-
[ 1e-6 + 1e-12, 1e-6 - 1e-12],
267-
];
268-
let expected_covariance = array![
269-
[2., 2e-12], [2e-12, 2e-24]
270-
];
271-
assert!(
272-
a.cov(1.).all_close(
273-
&expected_covariance,
274-
1e-24
275-
)
276-
);
265+
let a: Array2<f64> = array![[1e12 + 1., 1e12 - 1.], [1e-6 + 1e-12, 1e-6 - 1e-12],];
266+
let expected_covariance = array![[2., 2e-12], [2e-12, 2e-24]];
267+
assert!(a.cov(1.).all_close(&expected_covariance, 1e-24));
277268
}
278269
}
279270

280271
#[cfg(test)]
281272
mod pearson_correlation_tests {
282273
use super::*;
283274
use ndarray::array;
275+
use ndarray_rand::RandomExt;
284276
use quickcheck::quickcheck;
285277
use rand::distributions::Uniform;
286-
use ndarray_rand::RandomExt;
287278

288279
quickcheck! {
289280
fn output_matrix_is_symmetric(bound: f64) -> bool {
@@ -337,19 +328,14 @@ mod pearson_correlation_tests {
337328
[0.26979716, 0.20887228, 0.95454999, 0.96290785]
338329
];
339330
let numpy_corrcoeff = array![
340-
[ 1. , 0.38089376, 0.08122504, -0.59931623, 0.1365648 ],
341-
[ 0.38089376, 1. , 0.80918429, -0.52615195, 0.38954398],
342-
[ 0.08122504, 0.80918429, 1. , 0.07134906, -0.17324776],
343-
[-0.59931623, -0.52615195, 0.07134906, 1. , -0.8743213 ],
344-
[ 0.1365648 , 0.38954398, -0.17324776, -0.8743213 , 1. ]
331+
[1., 0.38089376, 0.08122504, -0.59931623, 0.1365648],
332+
[0.38089376, 1., 0.80918429, -0.52615195, 0.38954398],
333+
[0.08122504, 0.80918429, 1., 0.07134906, -0.17324776],
334+
[-0.59931623, -0.52615195, 0.07134906, 1., -0.8743213],
335+
[0.1365648, 0.38954398, -0.17324776, -0.8743213, 1.]
345336
];
346337
assert_eq!(a.ndim(), 2);
347-
assert!(
348-
a.pearson_correlation().all_close(
349-
&numpy_corrcoeff,
350-
1e-7
351-
)
352-
);
338+
assert!(a.pearson_correlation().all_close(&numpy_corrcoeff, 1e-7));
353339
}
354340

355341
}

src/histogram/bins.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ pub struct Edges<A: Ord> {
3333
}
3434

3535
impl<A: Ord> From<Vec<A>> for Edges<A> {
36-
3736
/// Get an `Edges` instance from a `Vec<A>`:
3837
/// the vector will be sorted in increasing order
3938
/// using an unstable sorting algorithm and duplicates
@@ -89,7 +88,7 @@ impl<A: Ord + Clone> From<Array1<A>> for Edges<A> {
8988
}
9089
}
9190

92-
impl<A: Ord> Index<usize> for Edges<A>{
91+
impl<A: Ord> Index<usize> for Edges<A> {
9392
type Output = A;
9493

9594
/// Get the `i`-th edge.
@@ -182,13 +181,11 @@ impl<A: Ord> Edges<A> {
182181
match self.edges.binary_search(value) {
183182
Ok(i) if i == n_edges - 1 => None,
184183
Ok(i) => Some((i, i + 1)),
185-
Err(i) => {
186-
match i {
187-
0 => None,
188-
j if j == n_edges => None,
189-
j => Some((j - 1, j)),
190-
}
191-
}
184+
Err(i) => match i {
185+
0 => None,
186+
j if j == n_edges => None,
187+
j => Some((j - 1, j)),
188+
},
192189
}
193190
}
194191

@@ -309,18 +306,14 @@ impl<A: Ord> Bins<A> {
309306
/// );
310307
/// ```
311308
pub fn range_of(&self, value: &A) -> Option<Range<A>>
312-
where
313-
A: Clone,
309+
where
310+
A: Clone,
314311
{
315312
let edges_indexes = self.edges.indices_of(value);
316-
edges_indexes.map(
317-
|(left, right)| {
318-
Range {
319-
start: self.edges[left].clone(),
320-
end: self.edges[right].clone(),
321-
}
322-
}
323-
)
313+
edges_indexes.map(|(left, right)| Range {
314+
start: self.edges[left].clone(),
315+
end: self.edges[right].clone(),
316+
})
324317
}
325318

326319
/// Get the `i`-th bin.
@@ -341,7 +334,7 @@ impl<A: Ord> Bins<A> {
341334
/// );
342335
/// ```
343336
pub fn index(&self, index: usize) -> Range<A>
344-
where
337+
where
345338
A: Clone,
346339
{
347340
// It was not possible to implement this functionality
@@ -350,7 +343,7 @@ impl<A: Ord> Bins<A> {
350343
// Index, in fact, forces you to return a reference.
351344
Range {
352345
start: self.edges[index].clone(),
353-
end: self.edges[index+1].clone(),
346+
end: self.edges[index + 1].clone(),
354347
}
355348
}
356349
}

src/histogram/grid.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::bins::Bins;
22
use super::strategies::BinsBuildingStrategy;
3-
use std::ops::Range;
43
use itertools::izip;
5-
use ndarray::{ArrayBase, Data, Ix1, Ix2, Axis};
4+
use ndarray::{ArrayBase, Axis, Data, Ix1, Ix2};
5+
use std::ops::Range;
66

77
/// A `Grid` is a partition of a rectangular region of an *n*-dimensional
88
/// space—e.g. [*a*<sub>0</sub>, *b*<sub>0</sub>) × ⋯ × [*a*<sub>*n*−1</sub>,
@@ -72,7 +72,6 @@ pub struct Grid<A: Ord> {
7272
}
7373

7474
impl<A: Ord> From<Vec<Bins<A>>> for Grid<A> {
75-
7675
/// Get a `Grid` instance from a `Vec<Bins<A>>`.
7776
///
7877
/// The `i`-th element in `Vec<Bins<A>>` represents the 1-dimensional
@@ -113,9 +112,14 @@ impl<A: Ord> Grid<A> {
113112
where
114113
S: Data<Elem = A>,
115114
{
116-
assert_eq!(point.len(), self.ndim(),
117-
"Dimension mismatch: the point has {:?} dimensions, the grid \
118-
expected {:?} dimensions.", point.len(), self.ndim());
115+
assert_eq!(
116+
point.len(),
117+
self.ndim(),
118+
"Dimension mismatch: the point has {:?} dimensions, the grid \
119+
expected {:?} dimensions.",
120+
point.len(),
121+
self.ndim()
122+
);
119123
point
120124
.iter()
121125
.zip(self.projections.iter())
@@ -132,9 +136,14 @@ impl<A: Ord + Clone> Grid<A> {
132136
/// **Panics** if at least one among `(i_0, ..., i_{n-1})` is out of bounds on the respective
133137
/// coordinate axis - i.e. if there exists `j` such that `i_j >= self.projections[j].len()`.
134138
pub fn index(&self, index: &[usize]) -> Vec<Range<A>> {
135-
assert_eq!(index.len(), self.ndim(),
136-
"Dimension mismatch: the index has {0:?} dimensions, the grid \
137-
expected {1:?} dimensions.", index.len(), self.ndim());
139+
assert_eq!(
140+
index.len(),
141+
self.ndim(),
142+
"Dimension mismatch: the index has {0:?} dimensions, the grid \
143+
expected {1:?} dimensions.",
144+
index.len(),
145+
self.ndim()
146+
);
138147
izip!(&self.projections, index)
139148
.map(|(bins, &i)| bins.index(i))
140149
.collect()
@@ -164,7 +173,7 @@ where
164173
/// [`strategy`]: strategies/index.html
165174
pub fn from_array<S>(array: &ArrayBase<S, Ix2>) -> Self
166175
where
167-
S: Data<Elem=A>,
176+
S: Data<Elem = A>,
168177
{
169178
let bin_builders = array
170179
.axis_iter(Axis(1))

src/histogram/histograms.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use super::errors::BinNotFound;
2+
use super::grid::Grid;
13
use ndarray::prelude::*;
24
use ndarray::Data;
3-
use super::grid::Grid;
4-
use super::errors::BinNotFound;
55

66
/// Histogram data structure.
77
pub struct Histogram<A: Ord> {
@@ -58,8 +58,8 @@ impl<A: Ord> Histogram<A> {
5858
Some(bin_index) => {
5959
self.counts[&*bin_index] += 1;
6060
Ok(())
61-
},
62-
None => Err(BinNotFound)
61+
}
62+
None => Err(BinNotFound),
6363
}
6464
}
6565

@@ -82,8 +82,8 @@ impl<A: Ord> Histogram<A> {
8282

8383
/// Extension trait for `ArrayBase` providing methods to compute histograms.
8484
pub trait HistogramExt<A, S>
85-
where
86-
S: Data<Elem = A>,
85+
where
86+
S: Data<Elem = A>,
8787
{
8888
/// Returns the [histogram](https://en.wikipedia.org/wiki/Histogram)
8989
/// for a 2-dimensional array of points `M`.
@@ -145,17 +145,16 @@ pub trait HistogramExt<A, S>
145145
/// # }
146146
/// ```
147147
fn histogram(&self, grid: Grid<A>) -> Histogram<A>
148-
where
149-
A: Ord;
148+
where
149+
A: Ord;
150150
}
151151

152152
impl<A, S> HistogramExt<A, S> for ArrayBase<S, Ix2>
153-
where
154-
S: Data<Elem = A>,
155-
A: Ord,
153+
where
154+
S: Data<Elem = A>,
155+
A: Ord,
156156
{
157-
fn histogram(&self, grid: Grid<A>) -> Histogram<A>
158-
{
157+
fn histogram(&self, grid: Grid<A>) -> Histogram<A> {
159158
let mut histogram = Histogram::new(grid);
160159
for point in self.axis_iter(Axis(0)) {
161160
let _ = histogram.add_observation(&point);

src/histogram/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Histogram functionalities.
2-
pub use self::histograms::{Histogram, HistogramExt};
3-
pub use self::bins::{Edges, Bins};
2+
pub use self::bins::{Bins, Edges};
43
pub use self::grid::{Grid, GridBuilder};
4+
pub use self::histograms::{Histogram, HistogramExt};
55

6-
mod histograms;
76
mod bins;
8-
pub mod strategies;
9-
mod grid;
107
pub mod errors;
8+
mod grid;
9+
mod histograms;
10+
pub mod strategies;

0 commit comments

Comments
 (0)