Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-whitehead committed Sep 11, 2024
1 parent 9f972ab commit e9e5346
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
16 changes: 12 additions & 4 deletions src/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub enum DistanceMetric {
/// Also known as L-infinity norm.
Chebyshev,
/// The distance between points in a cylindrical coordinate system where the input data is
/// three-dimensional in the form (ρ , φ , z), or (radial distance, angular coordinate, height).
/// Degrees should be in radians and distances percents. If you're using HSV or HSL colour
/// three-dimensional in the form (ρ , φ , z), or (radial distance, angular coordinate, height).
/// Degrees should be in radians and distances percents. If you're using HSV or HSL colour
/// systems, the coordinates will need to be re-ordered to SHV or SHL, respectively.
Cylindrical,
/// The length of the line between two points. Also known as L2 norm.
Expand Down Expand Up @@ -95,7 +95,11 @@ pub(crate) fn chebyshev_distance<T: Float>(a: &[T], b: &[T]) -> T {

pub(crate) fn cylindrical_distance<T: Float>(a: &[T], b: &[T]) -> T {
assert_inputs(a, b);
assert_eq!(a.len(), 3, "Cylindrical coordinates must have three dimensions (ρ, φ, z)");
assert_eq!(
a.len(),
3,
"Cylindrical coordinates must have three dimensions (ρ, φ, z)"
);

let (r1, theta1, z1) = (a[0], a[1], a[2]);
let (r2, theta2, z2) = (b[0], b[1], b[2]);
Expand All @@ -108,6 +112,10 @@ pub(crate) fn cylindrical_distance<T: Float>(a: &[T], b: &[T]) -> T {
}

fn assert_inputs<T: Float>(a: &[T], b: &[T]) {
assert_eq!(a.len(), b.len(), "Two vectors need to have the same dimensions");
assert_eq!(
a.len(),
b.len(),
"Two vectors need to have the same dimensions"
);
assert!(!a.is_empty(), "The vectors cannot be empty");
}
38 changes: 19 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,23 +286,23 @@ impl<'a, T: Float> Hdbscan<'a, T> {
if n_dim != 3 {
return Err(HdbscanError::WrongDimension(format!(
"Cylindrical coordinates should have three dimensions (ρ, φ, z), not {n_dim}"
)))
)));
}
for datapoint in self.data {
let (dim1, dim2, dim3) = (datapoint[0], datapoint[1], datapoint[2]);
if dim1 < T::zero() || dim1 > T::one() {
return Err(HdbscanError::WrongDimension(String::from(
"Dimension 1 of cylindrical coordinates should be a percent in range 0 to 1"
"Dimension 1 of cylindrical coordinates should be a percent in range 0 to 1",
)));
}
if dim2 < T::zero() || dim2 > T::from(PI * 2.0).unwrap() {
return Err(HdbscanError::WrongDimension(String::from(
"Dimension 2 of cylindrical coordinates should be a radian in range 0 to 2π"
"Dimension 2 of cylindrical coordinates should be a radian in range 0 to 2π",
)));
}
if dim3 < T::zero() || dim3 > T::one() {
return Err(HdbscanError::WrongDimension(String::from(
"Dimension 3 of cylindrical coordinates should be a percent in range 0 to 1"
"Dimension 3 of cylindrical coordinates should be a percent in range 0 to 1",
)));
}
}
Expand All @@ -314,20 +314,20 @@ impl<'a, T: Float> Hdbscan<'a, T> {
if n_dim != 2 {
return Err(HdbscanError::WrongDimension(format!(
"Geographical coordinates should have two dimensions (lat, lon), not {n_dim}"
)))
)));
}
for datapoint in self.data {
let (lat, lon) = (datapoint[0], datapoint[1]);
if lat < T::from(-90.0).unwrap() || lat > T::from(90.0).unwrap() {
return Err(HdbscanError::WrongDimension(String::from(
"Dimension 1 of geographical coordinates used in with Haversine distance \
should be a latitude in range -90 to 90"
should be a latitude in range -90 to 90",
)));
}
if lon < T::from(-180.0).unwrap() || lon > T::from(180.0).unwrap() {
return Err(HdbscanError::WrongDimension(String::from(
"Dimension 2 of geographical coordinates used in with Haversine distance \
should be a longitude in range -180 to 180"
should be a longitude in range -180 to 180",
)));
}
}
Expand Down Expand Up @@ -1036,18 +1036,18 @@ mod tests {
#[test]
fn test_cylindrical_hsv_colours() {
// HSV colours re-ordered to SHV
let data = vec![
// Blues
vec![0.91, 3.80482, 0.62],
vec![0.96, 4.13643, 0.86],
vec![0.95, 3.56047, 0.85],
// Greens
vec![0.74, 1.91986, 0.39],
vec![0.90, 1.69297, 0.82],
vec![0.84, 2.14675, 0.72],
// Red
vec![0.60, 6.2657, 0.00],
];
let data = vec![
// Blues
vec![0.91, 3.80482, 0.62],
vec![0.96, 4.13643, 0.86],
vec![0.95, 3.56047, 0.85],
// Greens
vec![0.74, 1.91986, 0.39],
vec![0.90, 1.69297, 0.82],
vec![0.84, 2.14675, 0.72],
// Red
vec![0.60, 6.2657, 0.00],
];

let hyper_params = HdbscanHyperParams::builder()
.dist_metric(DistanceMetric::Cylindrical)
Expand Down

0 comments on commit e9e5346

Please sign in to comment.